1 //===-- ThreadKDP.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 "ThreadKDP.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/WatchpointLocation.h"
25 
26 #include "ProcessKDP.h"
27 #include "ProcessKDPLog.h"
28 #include "RegisterContextKDP_arm.h"
29 #include "RegisterContextKDP_i386.h"
30 #include "RegisterContextKDP_x86_64.h"
31 #include "Plugins/Process/Utility/UnwindLLDB.h"
32 
33 #if defined(__APPLE__)
34 #include "UnwindMacOSXFrameBackchain.h"
35 #endif
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 
40 //----------------------------------------------------------------------
41 // Thread Registers
42 //----------------------------------------------------------------------
43 
44 ThreadKDP::ThreadKDP (ProcessKDP &process, lldb::tid_t tid) :
45     Thread(process, tid),
46     m_thread_name (),
47     m_dispatch_queue_name (),
48     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
49 {
50     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
51 }
52 
53 ThreadKDP::~ThreadKDP ()
54 {
55     ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
56     DestroyThread();
57 }
58 
59 
60 const char *
61 ThreadKDP::GetInfo ()
62 {
63     return NULL;
64 }
65 
66 
67 const char *
68 ThreadKDP::GetName ()
69 {
70     if (m_thread_name.empty())
71         return NULL;
72     return m_thread_name.c_str();
73 }
74 
75 const char *
76 ThreadKDP::GetQueueName ()
77 {
78     return NULL;
79 }
80 
81 bool
82 ThreadKDP::WillResume (StateType resume_state)
83 {
84     ClearStackFrames();
85     // Call the Thread::WillResume first. If we stop at a signal, the stop info
86     // class for signal will set the resume signal that we need below. The signal
87     // stuff obeys the Process::UnixSignal defaults.
88     Thread::WillResume(resume_state);
89 
90     lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
91     if (log)
92         log->Printf ("Resuming thread: %4.4x with state: %s.", GetID(), StateAsCString(resume_state));
93 
94 //    ProcessKDP &process = GetKDPProcess();
95 //    switch (resume_state)
96 //    {
97 //    case eStateSuspended:
98 //    case eStateStopped:
99 //        // Don't append anything for threads that should stay stopped.
100 //        break;
101 //
102 //    case eStateRunning:
103 //    case eStateStepping:
104 //        break;
105 //
106 //    default:
107 //        break;
108 //    }
109     return true;
110 }
111 
112 void
113 ThreadKDP::RefreshStateAfterStop()
114 {
115     // Invalidate all registers in our register context. We don't set "force" to
116     // true because the stop reply packet might have had some register values
117     // that were expedited and these will already be copied into the register
118     // context by the time this function gets called. The KDPRegisterContext
119     // class has been made smart enough to detect when it needs to invalidate
120     // which registers are valid by putting hooks in the register read and
121     // register supply functions where they check the process stop ID and do
122     // the right thing.
123     const bool force = false;
124     GetRegisterContext()->InvalidateIfNeeded (force);
125 }
126 
127 Unwind *
128 ThreadKDP::GetUnwinder ()
129 {
130     if (m_unwinder_ap.get() == NULL)
131     {
132         const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
133         const llvm::Triple::ArchType machine = target_arch.GetMachine();
134         switch (machine)
135         {
136             case llvm::Triple::x86_64:
137             case llvm::Triple::x86:
138             case llvm::Triple::arm:
139             case llvm::Triple::thumb:
140                 m_unwinder_ap.reset (new UnwindLLDB (*this));
141                 break;
142 
143             default:
144 #if defined(__APPLE__)
145                 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
146 #endif
147                 break;
148         }
149     }
150     return m_unwinder_ap.get();
151 }
152 
153 void
154 ThreadKDP::ClearStackFrames ()
155 {
156     Unwind *unwinder = GetUnwinder ();
157     if (unwinder)
158         unwinder->Clear();
159     Thread::ClearStackFrames();
160 }
161 
162 
163 bool
164 ThreadKDP::ThreadIDIsValid (lldb::tid_t thread)
165 {
166     return thread != 0;
167 }
168 
169 void
170 ThreadKDP::Dump(Log *log, uint32_t index)
171 {
172 }
173 
174 
175 bool
176 ThreadKDP::ShouldStop (bool &step_more)
177 {
178     return true;
179 }
180 lldb::RegisterContextSP
181 ThreadKDP::GetRegisterContext ()
182 {
183     if (m_reg_context_sp.get() == NULL)
184         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
185     return m_reg_context_sp;
186 }
187 
188 lldb::RegisterContextSP
189 ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame)
190 {
191     lldb::RegisterContextSP reg_ctx_sp;
192     uint32_t concrete_frame_idx = 0;
193 
194     if (frame)
195         concrete_frame_idx = frame->GetConcreteFrameIndex ();
196 
197     if (concrete_frame_idx == 0)
198     {
199         switch (GetKDPProcess().GetCommunication().GetCPUType())
200         {
201             case llvm::MachO::CPUTypeARM:
202                 reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx));
203                 break;
204             case llvm::MachO::CPUTypeI386:
205                 reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx));
206                 break;
207             case llvm::MachO::CPUTypeX86_64:
208                 reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx));
209                 break;
210             default:
211                 assert (!"Add CPU type support in KDP");
212                 break;
213         }
214     }
215     else if (m_unwinder_ap.get())
216         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
217     return reg_ctx_sp;
218 }
219 
220 lldb::StopInfoSP
221 ThreadKDP::GetPrivateStopReason ()
222 {
223     const uint32_t process_stop_id = GetProcess().GetStopID();
224     if (m_thread_stop_reason_stop_id != process_stop_id ||
225         (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
226     {
227         // TODO: can we query the initial state of the thread here?
228         // For now I am just going to pretend that a SIGSTOP happened.
229 
230         SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
231 
232         // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason
233         // for this thread, then m_actual_stop_info_sp will not ever contain
234         // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
235         // check will never be able to tell us if we have the correct stop info
236         // for this thread and we will continually send qThreadStopInfo packets
237         // down to the remote KDP server, so we need to keep our own notion
238         // of the stop ID that m_actual_stop_info_sp is valid for (even if it
239         // contains nothing). We use m_thread_stop_reason_stop_id for this below.
240 //        m_thread_stop_reason_stop_id = process_stop_id;
241 //        m_actual_stop_info_sp.reset();
242 
243     }
244     return m_actual_stop_info_sp;
245 }
246 
247 
248