blob: 27dd03bfc7bc5ab98ab8a5210e9ea09726e4b656 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "ThreadGDBRemote.h"
11
Jason Molenda3dc4f442013-10-18 05:55:24 +000012#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham0f16e732011-02-08 05:20:59 +000013#include "lldb/Core/State.h"
Jason Molenda3dc4f442013-10-18 05:55:24 +000014#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/Process.h"
16#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Target/StopInfo.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000018#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/Target.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000020#include "lldb/Target/UnixSignals.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/Unwind.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000022#include "lldb/Utility/DataExtractor.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000023#include "lldb/Utility/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "ProcessGDBRemote.h"
26#include "ProcessGDBRemoteLog.h"
Greg Claytonc982c762010-07-09 20:39:50 +000027#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000031using namespace lldb_private::process_gdb_remote;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33//----------------------------------------------------------------------
34// Thread Registers
35//----------------------------------------------------------------------
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037ThreadGDBRemote::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) {
Pavel Labath39addef2017-02-17 16:09:06 +000043 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
44 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
45 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048ThreadGDBRemote::~ThreadGDBRemote() {
49 ProcessSP process_sp(GetProcess());
Pavel Labath39addef2017-02-17 16:09:06 +000050 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());
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056const char *ThreadGDBRemote::GetName() {
57 if (m_thread_name.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 return m_thread_name.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062void 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;
Jason Molenda77f89352016-01-12 07:09:16 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070void 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;
Jason Molenda3dc4f442013-10-18 05:55:24 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081const char *ThreadGDBRemote::GetQueueName() {
82 // If our cached queue info is valid, then someone called
83 // ThreadGDBRemote::SetQueueInfo(...)
84 // with valid information that was gleaned from the stop reply packet. In this
85 // case we trust
86 // that the info is valid in m_dispatch_queue_name without refetching it
87 if (CachedQueueInfoIsValid()) {
88 if (m_dispatch_queue_name.empty())
89 return nullptr;
Greg Claytonb3ae8762013-04-12 20:07:46 +000090 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 return m_dispatch_queue_name.c_str();
92 }
93 // Always re-fetch the dispatch queue name since it can change
94
95 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
96 return nullptr;
97
98 if (m_thread_dispatch_qaddr != 0 &&
99 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
100 ProcessSP process_sp(GetProcess());
101 if (process_sp) {
102 SystemRuntime *runtime = process_sp->GetSystemRuntime();
103 if (runtime)
104 m_dispatch_queue_name =
105 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
106 else
107 m_dispatch_queue_name.clear();
108
109 if (!m_dispatch_queue_name.empty())
110 return m_dispatch_queue_name.c_str();
Greg Claytonb3ae8762013-04-12 20:07:46 +0000111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 }
113 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116QueueKind ThreadGDBRemote::GetQueueKind() {
117 // If our cached queue info is valid, then someone called
118 // ThreadGDBRemote::SetQueueInfo(...)
119 // with valid information that was gleaned from the stop reply packet. In this
120 // case we trust
121 // that the info is valid in m_dispatch_queue_name without refetching it
122 if (CachedQueueInfoIsValid()) {
123 return m_queue_kind;
124 }
125
126 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
127 return eQueueKindUnknown;
128
129 if (m_thread_dispatch_qaddr != 0 &&
130 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
131 ProcessSP process_sp(GetProcess());
132 if (process_sp) {
133 SystemRuntime *runtime = process_sp->GetSystemRuntime();
134 if (runtime)
135 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
136 return m_queue_kind;
137 }
138 }
139 return eQueueKindUnknown;
Greg Clayton3e06bd92011-01-09 21:07:35 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142queue_id_t ThreadGDBRemote::GetQueueID() {
143 // If our cached queue info is valid, then someone called
144 // ThreadGDBRemote::SetQueueInfo(...)
145 // with valid information that was gleaned from the stop reply packet. In this
146 // case we trust
147 // that the info is valid in m_dispatch_queue_name without refetching it
148 if (CachedQueueInfoIsValid())
149 return m_queue_serial_number;
150
151 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
152 return LLDB_INVALID_QUEUE_ID;
153
154 if (m_thread_dispatch_qaddr != 0 &&
155 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
156 ProcessSP process_sp(GetProcess());
157 if (process_sp) {
158 SystemRuntime *runtime = process_sp->GetSystemRuntime();
159 if (runtime) {
160 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
161 }
162 }
163 }
164 return LLDB_INVALID_QUEUE_ID;
Jason Molenda545304d2015-12-18 00:45:35 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167QueueSP ThreadGDBRemote::GetQueue() {
168 queue_id_t queue_id = GetQueueID();
169 QueueSP queue;
170 if (queue_id != LLDB_INVALID_QUEUE_ID) {
171 ProcessSP process_sp(GetProcess());
172 if (process_sp) {
173 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
174 }
175 }
176 return queue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
180 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
181 if (m_thread_dispatch_qaddr != 0 &&
182 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
183 ProcessSP process_sp(GetProcess());
184 if (process_sp) {
185 SystemRuntime *runtime = process_sp->GetSystemRuntime();
186 if (runtime) {
187 m_dispatch_queue_t =
188 runtime->GetLibdispatchQueueAddressFromThreadQAddress(
189 m_thread_dispatch_qaddr);
190 }
191 }
192 }
193 }
194 return m_dispatch_queue_t;
195}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
198 lldb::addr_t dispatch_queue_t) {
199 m_dispatch_queue_t = dispatch_queue_t;
200}
201
202bool ThreadGDBRemote::ThreadHasQueueInformation() const {
203 if (m_thread_dispatch_qaddr != 0 &&
204 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
205 m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
206 m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) {
207 return true;
208 }
209 return false;
210}
211
212LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
213 return m_associated_with_libdispatch_queue;
214}
215
216void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
217 LazyBool associated_with_libdispatch_queue) {
218 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
219}
220
221StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
222 StructuredData::ObjectSP object_sp;
223 const lldb::user_id_t tid = GetProtocolID();
224 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
225 if (log)
226 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
227 ProcessSP process_sp(GetProcess());
228 if (process_sp) {
229 ProcessGDBRemote *gdb_process =
230 static_cast<ProcessGDBRemote *>(process_sp.get());
231 object_sp = gdb_process->GetExtendedInfoForThread(tid);
232 }
233 return object_sp;
234}
235
236void ThreadGDBRemote::WillResume(StateType resume_state) {
237 int signo = GetResumeSignal();
238 const lldb::user_id_t tid = GetProtocolID();
239 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
240 if (log)
241 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
242 StateAsCString(resume_state));
243
244 ProcessSP process_sp(GetProcess());
245 if (process_sp) {
246 ProcessGDBRemote *gdb_process =
247 static_cast<ProcessGDBRemote *>(process_sp.get());
248 switch (resume_state) {
249 case eStateSuspended:
250 case eStateStopped:
251 // Don't append anything for threads that should stay stopped.
252 break;
253
254 case eStateRunning:
255 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
256 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
257 else
258 gdb_process->m_continue_c_tids.push_back(tid);
259 break;
260
261 case eStateStepping:
262 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
263 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
264 else
265 gdb_process->m_continue_s_tids.push_back(tid);
266 break;
267
268 default:
269 break;
270 }
271 }
272}
273
274void ThreadGDBRemote::RefreshStateAfterStop() {
275 // Invalidate all registers in our register context. We don't set "force" to
276 // true because the stop reply packet might have had some register values
277 // that were expedited and these will already be copied into the register
278 // context by the time this function gets called. The GDBRemoteRegisterContext
279 // class has been made smart enough to detect when it needs to invalidate
280 // which registers are valid by putting hooks in the register read and
281 // register supply functions where they check the process stop ID and do
282 // the right thing.
283 const bool force = false;
284 GetRegisterContext()->InvalidateIfNeeded(force);
285}
286
287bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
288 return thread != 0;
289}
290
291void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
292
293bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
294lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
295 if (m_reg_context_sp.get() == NULL)
296 m_reg_context_sp = CreateRegisterContextForFrame(NULL);
297 return m_reg_context_sp;
298}
299
300lldb::RegisterContextSP
301ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
302 lldb::RegisterContextSP reg_ctx_sp;
303 uint32_t concrete_frame_idx = 0;
304
305 if (frame)
306 concrete_frame_idx = frame->GetConcreteFrameIndex();
307
308 if (concrete_frame_idx == 0) {
309 ProcessSP process_sp(GetProcess());
310 if (process_sp) {
311 ProcessGDBRemote *gdb_process =
312 static_cast<ProcessGDBRemote *>(process_sp.get());
313 // read_all_registers_at_once will be true if 'p' packet is not supported.
314 bool read_all_registers_at_once =
315 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
316 reg_ctx_sp.reset(new GDBRemoteRegisterContext(
317 *this, concrete_frame_idx, gdb_process->m_register_info,
318 read_all_registers_at_once));
319 }
320 } else {
321 Unwind *unwinder = GetUnwinder();
322 if (unwinder)
323 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
324 }
325 return reg_ctx_sp;
326}
327
328bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
329 llvm::ArrayRef<uint8_t> data) {
330 GDBRemoteRegisterContext *gdb_reg_ctx =
331 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
332 assert(gdb_reg_ctx);
333 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
334}
335
336bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
337 GDBRemoteRegisterContext *gdb_reg_ctx =
338 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
339 assert(gdb_reg_ctx);
340 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
341}
342
343bool ThreadGDBRemote::CalculateStopInfo() {
344 ProcessSP process_sp(GetProcess());
345 if (process_sp)
346 return static_cast<ProcessGDBRemote *>(process_sp.get())
347 ->CalculateThreadStopInfo(this);
348 return false;
349}