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