1 //===-- ThreadMachCore.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 
11 #include "ThreadMachCore.h"
12 
13 #include "llvm/Support/MachO.h"
14 
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataExtractor.h"
17 #include "lldb/Core/StreamString.h"
18 #include "lldb/Core/State.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Target/StopInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Unwind.h"
24 #include "lldb/Breakpoint/Watchpoint.h"
25 
26 #include "ProcessMachCore.h"
27 //#include "RegisterContextKDP_arm.h"
28 //#include "RegisterContextKDP_i386.h"
29 //#include "RegisterContextKDP_x86_64.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 //----------------------------------------------------------------------
35 // Thread Registers
36 //----------------------------------------------------------------------
37 
38 ThreadMachCore::ThreadMachCore (const lldb::ProcessSP &process_sp, lldb::tid_t tid) :
39     Thread(process_sp, tid),
40     m_thread_name (),
41     m_dispatch_queue_name (),
42     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
43     m_thread_reg_ctx_sp ()
44 {
45 }
46 
47 ThreadMachCore::~ThreadMachCore ()
48 {
49     DestroyThread();
50 }
51 
52 const char *
53 ThreadMachCore::GetName ()
54 {
55     if (m_thread_name.empty())
56         return NULL;
57     return m_thread_name.c_str();
58 }
59 
60 void
61 ThreadMachCore::RefreshStateAfterStop()
62 {
63     // Invalidate all registers in our register context. We don't set "force" to
64     // true because the stop reply packet might have had some register values
65     // that were expedited and these will already be copied into the register
66     // context by the time this function gets called. The KDPRegisterContext
67     // class has been made smart enough to detect when it needs to invalidate
68     // which registers are valid by putting hooks in the register read and
69     // register supply functions where they check the process stop ID and do
70     // the right thing.
71     const bool force = false;
72     GetRegisterContext()->InvalidateIfNeeded (force);
73 }
74 
75 void
76 ThreadMachCore::ClearStackFrames ()
77 {
78     Unwind *unwinder = GetUnwinder ();
79     if (unwinder)
80         unwinder->Clear();
81     Thread::ClearStackFrames();
82 }
83 
84 
85 bool
86 ThreadMachCore::ThreadIDIsValid (lldb::tid_t thread)
87 {
88     return thread != 0;
89 }
90 
91 lldb::RegisterContextSP
92 ThreadMachCore::GetRegisterContext ()
93 {
94     if (m_reg_context_sp.get() == NULL)
95         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
96     return m_reg_context_sp;
97 }
98 
99 lldb::RegisterContextSP
100 ThreadMachCore::CreateRegisterContextForFrame (StackFrame *frame)
101 {
102     lldb::RegisterContextSP reg_ctx_sp;
103     uint32_t concrete_frame_idx = 0;
104 
105     if (frame)
106         concrete_frame_idx = frame->GetConcreteFrameIndex ();
107 
108     if (concrete_frame_idx == 0)
109     {
110         if (!m_thread_reg_ctx_sp)
111         {
112             ProcessSP process_sp (GetProcess());
113 
114             ObjectFile *core_objfile = static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile ();
115             if (core_objfile)
116                 m_thread_reg_ctx_sp = core_objfile->GetThreadContextAtIndex (GetID(), *this);
117         }
118         reg_ctx_sp = m_thread_reg_ctx_sp;
119     }
120     else if (m_unwinder_ap.get())
121     {
122         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
123     }
124     return reg_ctx_sp;
125 }
126 
127 lldb::StopInfoSP
128 ThreadMachCore::GetPrivateStopReason ()
129 {
130     ProcessSP process_sp (GetProcess());
131 
132     if (process_sp)
133     {
134         const uint32_t process_stop_id = process_sp->GetStopID();
135         if (m_thread_stop_reason_stop_id != process_stop_id ||
136             (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
137         {
138             // TODO: can we query the initial state of the thread here?
139             // For now I am just going to pretend that a SIGSTOP happened.
140 
141             SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
142 
143             // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason
144             // for this thread, then m_actual_stop_info_sp will not ever contain
145             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
146             // check will never be able to tell us if we have the correct stop info
147             // for this thread and we will continually send qThreadStopInfo packets
148             // down to the remote KDP server, so we need to keep our own notion
149             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
150             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
151     //        m_thread_stop_reason_stop_id = process_stop_id;
152     //        m_actual_stop_info_sp.reset();
153 
154         }
155     }
156     return m_actual_stop_info_sp;
157 }
158 
159 
160