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/State.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/StopInfo.h"
18 #include "lldb/Target/SystemRuntime.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/UnixSignals.h"
21 #include "lldb/Target/Unwind.h"
22 #include "lldb/Utility/DataExtractor.h"
23 #include "lldb/Utility/StreamString.h"
25 #include "ProcessGDBRemote.h"
26 #include "ProcessGDBRemoteLog.h"
27 #include "lldb/Utility/StringExtractorGDBRemote.h"
30 using namespace lldb_private;
31 using namespace lldb_private::process_gdb_remote;
33 //----------------------------------------------------------------------
35 //----------------------------------------------------------------------
37 ThreadGDBRemote::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) {
43 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
44 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
48 ThreadGDBRemote::~ThreadGDBRemote() {
49 ProcessSP process_sp(GetProcess());
50 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());
56 const char *ThreadGDBRemote::GetName() {
57 if (m_thread_name.empty())
59 return m_thread_name.c_str();
62 void 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;
70 void 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;
81 const char *ThreadGDBRemote::GetQueueName() {
82 // If our cached queue info is valid, then someone called
83 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
84 // from the stop reply packet. In this case we trust that the info is valid
85 // in m_dispatch_queue_name without refetching it
86 if (CachedQueueInfoIsValid()) {
87 if (m_dispatch_queue_name.empty())
90 return m_dispatch_queue_name.c_str();
92 // Always re-fetch the dispatch queue name since it can change
94 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
97 if (m_thread_dispatch_qaddr != 0 &&
98 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
99 ProcessSP process_sp(GetProcess());
101 SystemRuntime *runtime = process_sp->GetSystemRuntime();
103 m_dispatch_queue_name =
104 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
106 m_dispatch_queue_name.clear();
108 if (!m_dispatch_queue_name.empty())
109 return m_dispatch_queue_name.c_str();
115 QueueKind ThreadGDBRemote::GetQueueKind() {
116 // If our cached queue info is valid, then someone called
117 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
118 // from the stop reply packet. In this case we trust that the info is valid
119 // in m_dispatch_queue_name without refetching it
120 if (CachedQueueInfoIsValid()) {
124 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
125 return eQueueKindUnknown;
127 if (m_thread_dispatch_qaddr != 0 &&
128 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
129 ProcessSP process_sp(GetProcess());
131 SystemRuntime *runtime = process_sp->GetSystemRuntime();
133 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
137 return eQueueKindUnknown;
140 queue_id_t ThreadGDBRemote::GetQueueID() {
141 // If our cached queue info is valid, then someone called
142 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
143 // from the stop reply packet. In this case we trust that the info is valid
144 // in m_dispatch_queue_name without refetching it
145 if (CachedQueueInfoIsValid())
146 return m_queue_serial_number;
148 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
149 return LLDB_INVALID_QUEUE_ID;
151 if (m_thread_dispatch_qaddr != 0 &&
152 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
153 ProcessSP process_sp(GetProcess());
155 SystemRuntime *runtime = process_sp->GetSystemRuntime();
157 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
161 return LLDB_INVALID_QUEUE_ID;
164 QueueSP ThreadGDBRemote::GetQueue() {
165 queue_id_t queue_id = GetQueueID();
167 if (queue_id != LLDB_INVALID_QUEUE_ID) {
168 ProcessSP process_sp(GetProcess());
170 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
176 addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
177 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
178 if (m_thread_dispatch_qaddr != 0 &&
179 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
180 ProcessSP process_sp(GetProcess());
182 SystemRuntime *runtime = process_sp->GetSystemRuntime();
185 runtime->GetLibdispatchQueueAddressFromThreadQAddress(
186 m_thread_dispatch_qaddr);
191 return m_dispatch_queue_t;
194 void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
195 lldb::addr_t dispatch_queue_t) {
196 m_dispatch_queue_t = dispatch_queue_t;
199 bool ThreadGDBRemote::ThreadHasQueueInformation() const {
200 if (m_thread_dispatch_qaddr != 0 &&
201 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
202 m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
203 m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) {
209 LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
210 return m_associated_with_libdispatch_queue;
213 void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
214 LazyBool associated_with_libdispatch_queue) {
215 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
218 StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
219 StructuredData::ObjectSP object_sp;
220 const lldb::user_id_t tid = GetProtocolID();
221 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
223 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
224 ProcessSP process_sp(GetProcess());
226 ProcessGDBRemote *gdb_process =
227 static_cast<ProcessGDBRemote *>(process_sp.get());
228 object_sp = gdb_process->GetExtendedInfoForThread(tid);
233 void ThreadGDBRemote::WillResume(StateType resume_state) {
234 int signo = GetResumeSignal();
235 const lldb::user_id_t tid = GetProtocolID();
236 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
238 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
239 StateAsCString(resume_state));
241 ProcessSP process_sp(GetProcess());
243 ProcessGDBRemote *gdb_process =
244 static_cast<ProcessGDBRemote *>(process_sp.get());
245 switch (resume_state) {
246 case eStateSuspended:
248 // Don't append anything for threads that should stay stopped.
252 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
253 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
255 gdb_process->m_continue_c_tids.push_back(tid);
259 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
260 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
262 gdb_process->m_continue_s_tids.push_back(tid);
271 void ThreadGDBRemote::RefreshStateAfterStop() {
272 // Invalidate all registers in our register context. We don't set "force" to
273 // true because the stop reply packet might have had some register values
274 // that were expedited and these will already be copied into the register
275 // context by the time this function gets called. The
276 // GDBRemoteRegisterContext class has been made smart enough to detect when
277 // it needs to invalidate which registers are valid by putting hooks in the
278 // register read and register supply functions where they check the process
279 // stop ID and do the right thing.
280 const bool force = false;
281 GetRegisterContext()->InvalidateIfNeeded(force);
284 bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
288 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
290 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
291 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
292 if (!m_reg_context_sp)
293 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
294 return m_reg_context_sp;
297 lldb::RegisterContextSP
298 ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
299 lldb::RegisterContextSP reg_ctx_sp;
300 uint32_t concrete_frame_idx = 0;
303 concrete_frame_idx = frame->GetConcreteFrameIndex();
305 if (concrete_frame_idx == 0) {
306 ProcessSP process_sp(GetProcess());
308 ProcessGDBRemote *gdb_process =
309 static_cast<ProcessGDBRemote *>(process_sp.get());
310 // read_all_registers_at_once will be true if 'p' packet is not
312 bool read_all_registers_at_once =
313 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
314 reg_ctx_sp.reset(new GDBRemoteRegisterContext(
315 *this, concrete_frame_idx, gdb_process->m_register_info,
316 read_all_registers_at_once));
319 Unwind *unwinder = GetUnwinder();
320 if (unwinder != nullptr)
321 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
326 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
327 llvm::ArrayRef<uint8_t> data) {
328 GDBRemoteRegisterContext *gdb_reg_ctx =
329 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
331 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
334 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
335 GDBRemoteRegisterContext *gdb_reg_ctx =
336 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
338 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
341 bool ThreadGDBRemote::CalculateStopInfo() {
342 ProcessSP process_sp(GetProcess());
344 return static_cast<ProcessGDBRemote *>(process_sp.get())
345 ->CalculateThreadStopInfo(this);