1*0b57cec5SDimitry Andric //===-- HistoryThread.cpp -------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "lldb/lldb-private.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/HistoryThread.h"
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/HistoryUnwind.h"
14*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextHistory.h"
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
17*0b57cec5SDimitry Andric #include "lldb/Target/StackFrameList.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric #include <memory>
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric using namespace lldb;
23*0b57cec5SDimitry Andric using namespace lldb_private;
24*0b57cec5SDimitry Andric
25*0b57cec5SDimitry Andric // Constructor
26*0b57cec5SDimitry Andric
HistoryThread(lldb_private::Process & process,lldb::tid_t tid,std::vector<lldb::addr_t> pcs,bool pcs_are_call_addresses)27*0b57cec5SDimitry Andric HistoryThread::HistoryThread(lldb_private::Process &process, lldb::tid_t tid,
28*0b57cec5SDimitry Andric std::vector<lldb::addr_t> pcs,
29*0b57cec5SDimitry Andric bool pcs_are_call_addresses)
30*0b57cec5SDimitry Andric : Thread(process, tid, true), m_framelist_mutex(), m_framelist(),
31*0b57cec5SDimitry Andric m_pcs(pcs), m_extended_unwind_token(LLDB_INVALID_ADDRESS), m_queue_name(),
32*0b57cec5SDimitry Andric m_thread_name(), m_originating_unique_thread_id(tid),
33*0b57cec5SDimitry Andric m_queue_id(LLDB_INVALID_QUEUE_ID) {
34*0b57cec5SDimitry Andric m_unwinder_up =
35*0b57cec5SDimitry Andric std::make_unique<HistoryUnwind>(*this, pcs, pcs_are_call_addresses);
36*0b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
37*0b57cec5SDimitry Andric LLDB_LOGF(log, "%p HistoryThread::HistoryThread", static_cast<void *>(this));
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric
40*0b57cec5SDimitry Andric // Destructor
41*0b57cec5SDimitry Andric
~HistoryThread()42*0b57cec5SDimitry Andric HistoryThread::~HistoryThread() {
43*0b57cec5SDimitry Andric Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
44*0b57cec5SDimitry Andric LLDB_LOGF(log, "%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")",
45*0b57cec5SDimitry Andric static_cast<void *>(this), GetID());
46*0b57cec5SDimitry Andric DestroyThread();
47*0b57cec5SDimitry Andric }
48*0b57cec5SDimitry Andric
GetRegisterContext()49*0b57cec5SDimitry Andric lldb::RegisterContextSP HistoryThread::GetRegisterContext() {
50*0b57cec5SDimitry Andric RegisterContextSP rctx;
51*0b57cec5SDimitry Andric if (m_pcs.size() > 0) {
52*0b57cec5SDimitry Andric rctx = std::make_shared<RegisterContextHistory>(
53*0b57cec5SDimitry Andric *this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]);
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric return rctx;
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric
58*0b57cec5SDimitry Andric lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)59*0b57cec5SDimitry Andric HistoryThread::CreateRegisterContextForFrame(StackFrame *frame) {
60*0b57cec5SDimitry Andric return m_unwinder_up->CreateRegisterContextForFrame(frame);
61*0b57cec5SDimitry Andric }
62*0b57cec5SDimitry Andric
GetStackFrameList()63*0b57cec5SDimitry Andric lldb::StackFrameListSP HistoryThread::GetStackFrameList() {
64*0b57cec5SDimitry Andric // FIXME do not throw away the lock after we acquire it..
65*0b57cec5SDimitry Andric std::unique_lock<std::mutex> lock(m_framelist_mutex);
66*0b57cec5SDimitry Andric lock.unlock();
67*0b57cec5SDimitry Andric if (m_framelist.get() == nullptr) {
68*0b57cec5SDimitry Andric m_framelist =
69*0b57cec5SDimitry Andric std::make_shared<StackFrameList>(*this, StackFrameListSP(), true);
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric
72*0b57cec5SDimitry Andric return m_framelist;
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric
GetExtendedBacktraceOriginatingIndexID()75*0b57cec5SDimitry Andric uint32_t HistoryThread::GetExtendedBacktraceOriginatingIndexID() {
76*0b57cec5SDimitry Andric if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID) {
77*0b57cec5SDimitry Andric if (GetProcess()->HasAssignedIndexIDToThread(
78*0b57cec5SDimitry Andric m_originating_unique_thread_id)) {
79*0b57cec5SDimitry Andric return GetProcess()->AssignIndexIDToThread(
80*0b57cec5SDimitry Andric m_originating_unique_thread_id);
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric }
83*0b57cec5SDimitry Andric return LLDB_INVALID_THREAD_ID;
84*0b57cec5SDimitry Andric }
85