1 //===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ThreadGDBRemote.h"
11 #include "lldb/Breakpoint/Watchpoint.h"
12 #include "lldb/Target/Platform.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/RegisterContext.h"
15 #include "lldb/Target/StopInfo.h"
16 #include "lldb/Target/SystemRuntime.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/UnixSignals.h"
19 #include "lldb/Target/Unwind.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/State.h"
22 #include "lldb/Utility/StreamString.h"
23 #include "lldb/Utility/StringExtractorGDBRemote.h"
25 #include "ProcessGDBRemote.h"
26 #include "ProcessGDBRemoteLog.h"
31 using namespace lldb_private;
32 using namespace lldb_private::process_gdb_remote;
36 ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid)
37 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
38 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS),
39 m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown),
40 m_queue_serial_number(LLDB_INVALID_QUEUE_ID),
41 m_associated_with_libdispatch_queue(eLazyBoolCalculate) {
42 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
43 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
47 ThreadGDBRemote::~ThreadGDBRemote() {
48 ProcessSP process_sp(GetProcess());
49 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
50 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
51 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
55 const char *ThreadGDBRemote::GetName() {
56 if (m_thread_name.empty())
58 return m_thread_name.c_str();
61 void ThreadGDBRemote::ClearQueueInfo() {
62 m_dispatch_queue_name.clear();
63 m_queue_kind = eQueueKindUnknown;
64 m_queue_serial_number = 0;
65 m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
66 m_associated_with_libdispatch_queue = eLazyBoolCalculate;
69 void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
70 QueueKind queue_kind, uint64_t queue_serial,
71 addr_t dispatch_queue_t,
72 LazyBool associated_with_libdispatch_queue) {
73 m_dispatch_queue_name = queue_name;
74 m_queue_kind = queue_kind;
75 m_queue_serial_number = queue_serial;
76 m_dispatch_queue_t = dispatch_queue_t;
77 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
80 const char *ThreadGDBRemote::GetQueueName() {
81 // If our cached queue info is valid, then someone called
82 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
83 // from the stop reply packet. In this case we trust that the info is valid
84 // in m_dispatch_queue_name without refetching it
85 if (CachedQueueInfoIsValid()) {
86 if (m_dispatch_queue_name.empty())
89 return m_dispatch_queue_name.c_str();
91 // Always re-fetch the dispatch queue name since it can change
93 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
96 if (m_thread_dispatch_qaddr != 0 &&
97 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
98 ProcessSP process_sp(GetProcess());
100 SystemRuntime *runtime = process_sp->GetSystemRuntime();
102 m_dispatch_queue_name =
103 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
105 m_dispatch_queue_name.clear();
107 if (!m_dispatch_queue_name.empty())
108 return m_dispatch_queue_name.c_str();
114 QueueKind ThreadGDBRemote::GetQueueKind() {
115 // If our cached queue info is valid, then someone called
116 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
117 // from the stop reply packet. In this case we trust that the info is valid
118 // in m_dispatch_queue_name without refetching it
119 if (CachedQueueInfoIsValid()) {
123 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
124 return eQueueKindUnknown;
126 if (m_thread_dispatch_qaddr != 0 &&
127 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
128 ProcessSP process_sp(GetProcess());
130 SystemRuntime *runtime = process_sp->GetSystemRuntime();
132 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
136 return eQueueKindUnknown;
139 queue_id_t ThreadGDBRemote::GetQueueID() {
140 // If our cached queue info is valid, then someone called
141 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
142 // from the stop reply packet. In this case we trust that the info is valid
143 // in m_dispatch_queue_name without refetching it
144 if (CachedQueueInfoIsValid())
145 return m_queue_serial_number;
147 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
148 return LLDB_INVALID_QUEUE_ID;
150 if (m_thread_dispatch_qaddr != 0 &&
151 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
152 ProcessSP process_sp(GetProcess());
154 SystemRuntime *runtime = process_sp->GetSystemRuntime();
156 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
160 return LLDB_INVALID_QUEUE_ID;
163 QueueSP ThreadGDBRemote::GetQueue() {
164 queue_id_t queue_id = GetQueueID();
166 if (queue_id != LLDB_INVALID_QUEUE_ID) {
167 ProcessSP process_sp(GetProcess());
169 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
175 addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
176 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
177 if (m_thread_dispatch_qaddr != 0 &&
178 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
179 ProcessSP process_sp(GetProcess());
181 SystemRuntime *runtime = process_sp->GetSystemRuntime();
184 runtime->GetLibdispatchQueueAddressFromThreadQAddress(
185 m_thread_dispatch_qaddr);
190 return m_dispatch_queue_t;
193 void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
194 lldb::addr_t dispatch_queue_t) {
195 m_dispatch_queue_t = dispatch_queue_t;
198 bool ThreadGDBRemote::ThreadHasQueueInformation() const {
199 return m_thread_dispatch_qaddr != 0 &&
200 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
201 m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
202 m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0;
205 LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
206 return m_associated_with_libdispatch_queue;
209 void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
210 LazyBool associated_with_libdispatch_queue) {
211 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
214 StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
215 StructuredData::ObjectSP object_sp;
216 const lldb::user_id_t tid = GetProtocolID();
217 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
219 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
220 ProcessSP process_sp(GetProcess());
222 ProcessGDBRemote *gdb_process =
223 static_cast<ProcessGDBRemote *>(process_sp.get());
224 object_sp = gdb_process->GetExtendedInfoForThread(tid);
229 void ThreadGDBRemote::WillResume(StateType resume_state) {
230 int signo = GetResumeSignal();
231 const lldb::user_id_t tid = GetProtocolID();
232 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
234 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
235 StateAsCString(resume_state));
237 ProcessSP process_sp(GetProcess());
239 ProcessGDBRemote *gdb_process =
240 static_cast<ProcessGDBRemote *>(process_sp.get());
241 switch (resume_state) {
242 case eStateSuspended:
244 // Don't append anything for threads that should stay stopped.
248 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
249 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
251 gdb_process->m_continue_c_tids.push_back(tid);
255 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
256 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
258 gdb_process->m_continue_s_tids.push_back(tid);
267 void ThreadGDBRemote::RefreshStateAfterStop() {
268 // Invalidate all registers in our register context. We don't set "force" to
269 // true because the stop reply packet might have had some register values
270 // that were expedited and these will already be copied into the register
271 // context by the time this function gets called. The
272 // GDBRemoteRegisterContext class has been made smart enough to detect when
273 // it needs to invalidate which registers are valid by putting hooks in the
274 // register read and register supply functions where they check the process
275 // stop ID and do the right thing.
276 const bool force = false;
277 GetRegisterContext()->InvalidateIfNeeded(force);
280 bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
284 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
286 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
287 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
288 if (!m_reg_context_sp)
289 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
290 return m_reg_context_sp;
293 lldb::RegisterContextSP
294 ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
295 lldb::RegisterContextSP reg_ctx_sp;
296 uint32_t concrete_frame_idx = 0;
299 concrete_frame_idx = frame->GetConcreteFrameIndex();
301 if (concrete_frame_idx == 0) {
302 ProcessSP process_sp(GetProcess());
304 ProcessGDBRemote *gdb_process =
305 static_cast<ProcessGDBRemote *>(process_sp.get());
306 // read_all_registers_at_once will be true if 'p' packet is not
308 bool read_all_registers_at_once =
309 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
310 reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>(
311 *this, concrete_frame_idx, gdb_process->m_register_info,
312 read_all_registers_at_once);
315 Unwind *unwinder = GetUnwinder();
316 if (unwinder != nullptr)
317 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
322 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
323 llvm::ArrayRef<uint8_t> data) {
324 GDBRemoteRegisterContext *gdb_reg_ctx =
325 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
327 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
330 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
331 GDBRemoteRegisterContext *gdb_reg_ctx =
332 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
334 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
337 bool ThreadGDBRemote::CalculateStopInfo() {
338 ProcessSP process_sp(GetProcess());
340 return static_cast<ProcessGDBRemote *>(process_sp.get())
341 ->CalculateThreadStopInfo(this);