1 //===-- HistoryThread.cpp ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-private.h"
11 
12 #include "Plugins/Process/Utility/HistoryUnwind.h"
13 #include "Plugins/Process/Utility/HistoryThread.h"
14 #include "Plugins/Process/Utility/RegisterContextHistory.h"
15 
16 #include "lldb/Core/Log.h"
17 #include "lldb/Target/StackFrameList.h"
18 #include "lldb/Target/Process.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 //  Constructor
24 
25 HistoryThread::HistoryThread (lldb_private::Process &process,
26                               lldb::tid_t tid,
27                               std::vector<lldb::addr_t> pcs,
28                               uint32_t stop_id,
29                               bool stop_id_is_valid) :
30         Thread (process, tid, true),
31         m_framelist_mutex(),
32         m_framelist(),
33         m_pcs (pcs),
34         m_stop_id (stop_id),
35         m_stop_id_is_valid (stop_id_is_valid),
36         m_extended_unwind_token (LLDB_INVALID_ADDRESS),
37         m_queue_name (),
38         m_thread_name (),
39         m_originating_unique_thread_id (tid),
40         m_queue_id (LLDB_INVALID_QUEUE_ID)
41 {
42     m_unwinder_ap.reset (new HistoryUnwind (*this, pcs, stop_id, stop_id_is_valid));
43     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
44     if (log)
45         log->Printf ("%p HistoryThread::HistoryThread", this);
46 }
47 
48 //  Destructor
49 
50 HistoryThread::~HistoryThread ()
51 {
52     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
53     if (log)
54         log->Printf ("%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")", this, GetID());
55     DestroyThread();
56 }
57 
58 lldb::RegisterContextSP
59 HistoryThread::GetRegisterContext ()
60 {
61     RegisterContextSP rctx ;
62     if (m_pcs.size() > 0)
63     {
64         rctx.reset (new RegisterContextHistory (*this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]));
65     }
66     return rctx;
67 
68 }
69 
70 lldb::RegisterContextSP
71 HistoryThread::CreateRegisterContextForFrame (StackFrame *frame)
72 {
73     return m_unwinder_ap->CreateRegisterContextForFrame (frame);
74 }
75 
76 lldb::StackFrameListSP
77 HistoryThread::GetStackFrameList ()
78 {
79     Mutex::Locker (m_framelist_mutex);
80     if (m_framelist.get() == NULL)
81     {
82         m_framelist.reset (new StackFrameList (*this, StackFrameListSP(), true));
83     }
84 
85     return m_framelist;
86 }
87 
88 uint32_t
89 HistoryThread::GetExtendedBacktraceOriginatingIndexID ()
90 {
91     if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID)
92     {
93         if (GetProcess()->HasAssignedIndexIDToThread (m_originating_unique_thread_id))
94         {
95             return GetProcess()->AssignIndexIDToThread (m_originating_unique_thread_id);
96         }
97     }
98     return LLDB_INVALID_THREAD_ID;
99 }
100