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