1 //===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ThreadGDBRemote.h"
12 #include "lldb/Breakpoint/Watchpoint.h"
13 #include "lldb/Core/ArchSpec.h"
14 #include "lldb/Core/State.h"
15 #include "lldb/Target/Platform.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/RegisterContext.h"
18 #include "lldb/Target/StopInfo.h"
19 #include "lldb/Target/SystemRuntime.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/UnixSignals.h"
22 #include "lldb/Target/Unwind.h"
23 #include "lldb/Utility/DataExtractor.h"
24 #include "lldb/Utility/StreamString.h"
26 #include "ProcessGDBRemote.h"
27 #include "ProcessGDBRemoteLog.h"
28 #include "Utility/StringExtractorGDBRemote.h"
31 using namespace lldb_private;
32 using namespace lldb_private::process_gdb_remote;
34 //----------------------------------------------------------------------
36 //----------------------------------------------------------------------
38 ThreadGDBRemote::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 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
45 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
49 ThreadGDBRemote::~ThreadGDBRemote() {
50 ProcessSP process_sp(GetProcess());
51 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());
57 const char *ThreadGDBRemote::GetName() {
58 if (m_thread_name.empty())
60 return m_thread_name.c_str();
63 void 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;
71 void 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;
82 const 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
87 // that the info is valid in m_dispatch_queue_name without refetching it
88 if (CachedQueueInfoIsValid()) {
89 if (m_dispatch_queue_name.empty())
92 return m_dispatch_queue_name.c_str();
94 // Always re-fetch the dispatch queue name since it can change
96 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
99 if (m_thread_dispatch_qaddr != 0 &&
100 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
101 ProcessSP process_sp(GetProcess());
103 SystemRuntime *runtime = process_sp->GetSystemRuntime();
105 m_dispatch_queue_name =
106 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
108 m_dispatch_queue_name.clear();
110 if (!m_dispatch_queue_name.empty())
111 return m_dispatch_queue_name.c_str();
117 QueueKind 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
122 // that the info is valid in m_dispatch_queue_name without refetching it
123 if (CachedQueueInfoIsValid()) {
127 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
128 return eQueueKindUnknown;
130 if (m_thread_dispatch_qaddr != 0 &&
131 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
132 ProcessSP process_sp(GetProcess());
134 SystemRuntime *runtime = process_sp->GetSystemRuntime();
136 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
140 return eQueueKindUnknown;
143 queue_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
148 // that the info is valid in m_dispatch_queue_name without refetching it
149 if (CachedQueueInfoIsValid())
150 return m_queue_serial_number;
152 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
153 return LLDB_INVALID_QUEUE_ID;
155 if (m_thread_dispatch_qaddr != 0 &&
156 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
157 ProcessSP process_sp(GetProcess());
159 SystemRuntime *runtime = process_sp->GetSystemRuntime();
161 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
165 return LLDB_INVALID_QUEUE_ID;
168 QueueSP ThreadGDBRemote::GetQueue() {
169 queue_id_t queue_id = GetQueueID();
171 if (queue_id != LLDB_INVALID_QUEUE_ID) {
172 ProcessSP process_sp(GetProcess());
174 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
180 addr_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());
186 SystemRuntime *runtime = process_sp->GetSystemRuntime();
189 runtime->GetLibdispatchQueueAddressFromThreadQAddress(
190 m_thread_dispatch_qaddr);
195 return m_dispatch_queue_t;
198 void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
199 lldb::addr_t dispatch_queue_t) {
200 m_dispatch_queue_t = dispatch_queue_t;
203 bool 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) {
213 LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
214 return m_associated_with_libdispatch_queue;
217 void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
218 LazyBool associated_with_libdispatch_queue) {
219 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
222 StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
223 StructuredData::ObjectSP object_sp;
224 const lldb::user_id_t tid = GetProtocolID();
225 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
227 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
228 ProcessSP process_sp(GetProcess());
230 ProcessGDBRemote *gdb_process =
231 static_cast<ProcessGDBRemote *>(process_sp.get());
232 object_sp = gdb_process->GetExtendedInfoForThread(tid);
237 void ThreadGDBRemote::WillResume(StateType resume_state) {
238 int signo = GetResumeSignal();
239 const lldb::user_id_t tid = GetProtocolID();
240 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
242 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
243 StateAsCString(resume_state));
245 ProcessSP process_sp(GetProcess());
247 ProcessGDBRemote *gdb_process =
248 static_cast<ProcessGDBRemote *>(process_sp.get());
249 switch (resume_state) {
250 case eStateSuspended:
252 // Don't append anything for threads that should stay stopped.
256 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
257 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
259 gdb_process->m_continue_c_tids.push_back(tid);
263 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
264 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
266 gdb_process->m_continue_s_tids.push_back(tid);
275 void 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
284 const bool force = false;
285 GetRegisterContext()->InvalidateIfNeeded(force);
288 bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
292 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
294 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
295 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
296 if (m_reg_context_sp.get() == NULL)
297 m_reg_context_sp = CreateRegisterContextForFrame(NULL);
298 return m_reg_context_sp;
301 lldb::RegisterContextSP
302 ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
303 lldb::RegisterContextSP reg_ctx_sp;
304 uint32_t concrete_frame_idx = 0;
307 concrete_frame_idx = frame->GetConcreteFrameIndex();
309 if (concrete_frame_idx == 0) {
310 ProcessSP process_sp(GetProcess());
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));
322 Unwind *unwinder = GetUnwinder();
324 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
329 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
330 llvm::ArrayRef<uint8_t> data) {
331 GDBRemoteRegisterContext *gdb_reg_ctx =
332 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
334 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
337 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
338 GDBRemoteRegisterContext *gdb_reg_ctx =
339 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
341 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
344 bool ThreadGDBRemote::CalculateStopInfo() {
345 ProcessSP process_sp(GetProcess());
347 return static_cast<ProcessGDBRemote *>(process_sp.get())
348 ->CalculateThreadStopInfo(this);