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