1 //===-- ThreadGDBRemote.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 "ThreadGDBRemote.h"
11
12 #include "lldb/Breakpoint/Watchpoint.h"
13 #include "lldb/Target/Platform.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/StopInfo.h"
17 #include "lldb/Target/SystemRuntime.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/UnixSignals.h"
20 #include "lldb/Target/Unwind.h"
21 #include "lldb/Utility/DataExtractor.h"
22 #include "lldb/Utility/State.h"
23 #include "lldb/Utility/StreamString.h"
24
25 #include "ProcessGDBRemote.h"
26 #include "ProcessGDBRemoteLog.h"
27 #include "lldb/Utility/StringExtractorGDBRemote.h"
28
29 using namespace lldb;
30 using namespace lldb_private;
31 using namespace lldb_private::process_gdb_remote;
32
33 //----------------------------------------------------------------------
34 // Thread Registers
35 //----------------------------------------------------------------------
36
ThreadGDBRemote(Process & process,lldb::tid_t tid)37 ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid)
38 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
39 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS),
40 m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown),
41 m_queue_serial_number(LLDB_INVALID_QUEUE_ID),
42 m_associated_with_libdispatch_queue(eLazyBoolCalculate) {
43 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
44 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
45 GetID());
46 }
47
~ThreadGDBRemote()48 ThreadGDBRemote::~ThreadGDBRemote() {
49 ProcessSP process_sp(GetProcess());
50 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
51 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
52 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
53 DestroyThread();
54 }
55
GetName()56 const char *ThreadGDBRemote::GetName() {
57 if (m_thread_name.empty())
58 return nullptr;
59 return m_thread_name.c_str();
60 }
61
ClearQueueInfo()62 void ThreadGDBRemote::ClearQueueInfo() {
63 m_dispatch_queue_name.clear();
64 m_queue_kind = eQueueKindUnknown;
65 m_queue_serial_number = 0;
66 m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
67 m_associated_with_libdispatch_queue = eLazyBoolCalculate;
68 }
69
SetQueueInfo(std::string && queue_name,QueueKind queue_kind,uint64_t queue_serial,addr_t dispatch_queue_t,LazyBool associated_with_libdispatch_queue)70 void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
71 QueueKind queue_kind, uint64_t queue_serial,
72 addr_t dispatch_queue_t,
73 LazyBool associated_with_libdispatch_queue) {
74 m_dispatch_queue_name = queue_name;
75 m_queue_kind = queue_kind;
76 m_queue_serial_number = queue_serial;
77 m_dispatch_queue_t = dispatch_queue_t;
78 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
79 }
80
GetQueueName()81 const char *ThreadGDBRemote::GetQueueName() {
82 // If our cached queue info is valid, then someone called
83 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
84 // from the stop reply packet. In this case we trust that the info is valid
85 // in m_dispatch_queue_name without refetching it
86 if (CachedQueueInfoIsValid()) {
87 if (m_dispatch_queue_name.empty())
88 return nullptr;
89 else
90 return m_dispatch_queue_name.c_str();
91 }
92 // Always re-fetch the dispatch queue name since it can change
93
94 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
95 return nullptr;
96
97 if (m_thread_dispatch_qaddr != 0 &&
98 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
99 ProcessSP process_sp(GetProcess());
100 if (process_sp) {
101 SystemRuntime *runtime = process_sp->GetSystemRuntime();
102 if (runtime)
103 m_dispatch_queue_name =
104 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
105 else
106 m_dispatch_queue_name.clear();
107
108 if (!m_dispatch_queue_name.empty())
109 return m_dispatch_queue_name.c_str();
110 }
111 }
112 return nullptr;
113 }
114
GetQueueKind()115 QueueKind ThreadGDBRemote::GetQueueKind() {
116 // If our cached queue info is valid, then someone called
117 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
118 // from the stop reply packet. In this case we trust that the info is valid
119 // in m_dispatch_queue_name without refetching it
120 if (CachedQueueInfoIsValid()) {
121 return m_queue_kind;
122 }
123
124 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
125 return eQueueKindUnknown;
126
127 if (m_thread_dispatch_qaddr != 0 &&
128 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
129 ProcessSP process_sp(GetProcess());
130 if (process_sp) {
131 SystemRuntime *runtime = process_sp->GetSystemRuntime();
132 if (runtime)
133 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
134 return m_queue_kind;
135 }
136 }
137 return eQueueKindUnknown;
138 }
139
GetQueueID()140 queue_id_t ThreadGDBRemote::GetQueueID() {
141 // If our cached queue info is valid, then someone called
142 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
143 // from the stop reply packet. In this case we trust that the info is valid
144 // in m_dispatch_queue_name without refetching it
145 if (CachedQueueInfoIsValid())
146 return m_queue_serial_number;
147
148 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
149 return LLDB_INVALID_QUEUE_ID;
150
151 if (m_thread_dispatch_qaddr != 0 &&
152 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
153 ProcessSP process_sp(GetProcess());
154 if (process_sp) {
155 SystemRuntime *runtime = process_sp->GetSystemRuntime();
156 if (runtime) {
157 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
158 }
159 }
160 }
161 return LLDB_INVALID_QUEUE_ID;
162 }
163
GetQueue()164 QueueSP ThreadGDBRemote::GetQueue() {
165 queue_id_t queue_id = GetQueueID();
166 QueueSP queue;
167 if (queue_id != LLDB_INVALID_QUEUE_ID) {
168 ProcessSP process_sp(GetProcess());
169 if (process_sp) {
170 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
171 }
172 }
173 return queue;
174 }
175
GetQueueLibdispatchQueueAddress()176 addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
177 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
178 if (m_thread_dispatch_qaddr != 0 &&
179 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
180 ProcessSP process_sp(GetProcess());
181 if (process_sp) {
182 SystemRuntime *runtime = process_sp->GetSystemRuntime();
183 if (runtime) {
184 m_dispatch_queue_t =
185 runtime->GetLibdispatchQueueAddressFromThreadQAddress(
186 m_thread_dispatch_qaddr);
187 }
188 }
189 }
190 }
191 return m_dispatch_queue_t;
192 }
193
SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t)194 void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
195 lldb::addr_t dispatch_queue_t) {
196 m_dispatch_queue_t = dispatch_queue_t;
197 }
198
ThreadHasQueueInformation() const199 bool ThreadGDBRemote::ThreadHasQueueInformation() const {
200 return m_thread_dispatch_qaddr != 0 &&
201 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
202 m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
203 m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0;
204 }
205
GetAssociatedWithLibdispatchQueue()206 LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
207 return m_associated_with_libdispatch_queue;
208 }
209
SetAssociatedWithLibdispatchQueue(LazyBool associated_with_libdispatch_queue)210 void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
211 LazyBool associated_with_libdispatch_queue) {
212 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
213 }
214
FetchThreadExtendedInfo()215 StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
216 StructuredData::ObjectSP object_sp;
217 const lldb::user_id_t tid = GetProtocolID();
218 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
219 if (log)
220 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
221 ProcessSP process_sp(GetProcess());
222 if (process_sp) {
223 ProcessGDBRemote *gdb_process =
224 static_cast<ProcessGDBRemote *>(process_sp.get());
225 object_sp = gdb_process->GetExtendedInfoForThread(tid);
226 }
227 return object_sp;
228 }
229
WillResume(StateType resume_state)230 void ThreadGDBRemote::WillResume(StateType resume_state) {
231 int signo = GetResumeSignal();
232 const lldb::user_id_t tid = GetProtocolID();
233 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
234 if (log)
235 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
236 StateAsCString(resume_state));
237
238 ProcessSP process_sp(GetProcess());
239 if (process_sp) {
240 ProcessGDBRemote *gdb_process =
241 static_cast<ProcessGDBRemote *>(process_sp.get());
242 switch (resume_state) {
243 case eStateSuspended:
244 case eStateStopped:
245 // Don't append anything for threads that should stay stopped.
246 break;
247
248 case eStateRunning:
249 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
250 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
251 else
252 gdb_process->m_continue_c_tids.push_back(tid);
253 break;
254
255 case eStateStepping:
256 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
257 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
258 else
259 gdb_process->m_continue_s_tids.push_back(tid);
260 break;
261
262 default:
263 break;
264 }
265 }
266 }
267
RefreshStateAfterStop()268 void ThreadGDBRemote::RefreshStateAfterStop() {
269 // Invalidate all registers in our register context. We don't set "force" to
270 // true because the stop reply packet might have had some register values
271 // that were expedited and these will already be copied into the register
272 // context by the time this function gets called. The
273 // GDBRemoteRegisterContext class has been made smart enough to detect when
274 // it needs to invalidate which registers are valid by putting hooks in the
275 // register read and register supply functions where they check the process
276 // stop ID and do the right thing.
277 const bool force = false;
278 GetRegisterContext()->InvalidateIfNeeded(force);
279 }
280
ThreadIDIsValid(lldb::tid_t thread)281 bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
282 return thread != 0;
283 }
284
Dump(Log * log,uint32_t index)285 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
286
ShouldStop(bool & step_more)287 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
GetRegisterContext()288 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
289 if (!m_reg_context_sp)
290 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
291 return m_reg_context_sp;
292 }
293
294 lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)295 ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
296 lldb::RegisterContextSP reg_ctx_sp;
297 uint32_t concrete_frame_idx = 0;
298
299 if (frame)
300 concrete_frame_idx = frame->GetConcreteFrameIndex();
301
302 if (concrete_frame_idx == 0) {
303 ProcessSP process_sp(GetProcess());
304 if (process_sp) {
305 ProcessGDBRemote *gdb_process =
306 static_cast<ProcessGDBRemote *>(process_sp.get());
307 // read_all_registers_at_once will be true if 'p' packet is not
308 // supported.
309 bool read_all_registers_at_once =
310 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
311 reg_ctx_sp.reset(new GDBRemoteRegisterContext(
312 *this, concrete_frame_idx, gdb_process->m_register_info,
313 read_all_registers_at_once));
314 }
315 } else {
316 Unwind *unwinder = GetUnwinder();
317 if (unwinder != nullptr)
318 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
319 }
320 return reg_ctx_sp;
321 }
322
PrivateSetRegisterValue(uint32_t reg,llvm::ArrayRef<uint8_t> data)323 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
324 llvm::ArrayRef<uint8_t> data) {
325 GDBRemoteRegisterContext *gdb_reg_ctx =
326 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
327 assert(gdb_reg_ctx);
328 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
329 }
330
PrivateSetRegisterValue(uint32_t reg,uint64_t regval)331 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
332 GDBRemoteRegisterContext *gdb_reg_ctx =
333 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
334 assert(gdb_reg_ctx);
335 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
336 }
337
CalculateStopInfo()338 bool ThreadGDBRemote::CalculateStopInfo() {
339 ProcessSP process_sp(GetProcess());
340 if (process_sp)
341 return static_cast<ProcessGDBRemote *>(process_sp.get())
342 ->CalculateThreadStopInfo(this);
343 return false;
344 }
345