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/Target/Platform.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/StopInfo.h"
17 #include "lldb/Target/SystemRuntime.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/UnixSignals.h"
20 #include "lldb/Target/Unwind.h"
21 #include "lldb/Utility/DataExtractor.h"
22 #include "lldb/Utility/State.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 return 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;
206 LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
207 return m_associated_with_libdispatch_queue;
210 void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
211 LazyBool associated_with_libdispatch_queue) {
212 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
215 StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
216 StructuredData::ObjectSP object_sp;
217 const lldb::user_id_t tid = GetProtocolID();
218 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
220 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
221 ProcessSP process_sp(GetProcess());
223 ProcessGDBRemote *gdb_process =
224 static_cast<ProcessGDBRemote *>(process_sp.get());
225 object_sp = gdb_process->GetExtendedInfoForThread(tid);
230 void ThreadGDBRemote::WillResume(StateType resume_state) {
231 int signo = GetResumeSignal();
232 const lldb::user_id_t tid = GetProtocolID();
233 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
235 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
236 StateAsCString(resume_state));
238 ProcessSP process_sp(GetProcess());
240 ProcessGDBRemote *gdb_process =
241 static_cast<ProcessGDBRemote *>(process_sp.get());
242 switch (resume_state) {
243 case eStateSuspended:
245 // Don't append anything for threads that should stay stopped.
249 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
250 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
252 gdb_process->m_continue_c_tids.push_back(tid);
256 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
257 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
259 gdb_process->m_continue_s_tids.push_back(tid);
268 void ThreadGDBRemote::RefreshStateAfterStop() {
269 // Invalidate all registers in our register context. We don't set "force" to
270 // true because the stop reply packet might have had some register values
271 // that were expedited and these will already be copied into the register
272 // context by the time this function gets called. The
273 // GDBRemoteRegisterContext class has been made smart enough to detect when
274 // it needs to invalidate which registers are valid by putting hooks in the
275 // register read and register supply functions where they check the process
276 // stop ID and do the right thing.
277 const bool force = false;
278 GetRegisterContext()->InvalidateIfNeeded(force);
281 bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
285 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
287 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
288 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
289 if (!m_reg_context_sp)
290 m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
291 return m_reg_context_sp;
294 lldb::RegisterContextSP
295 ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
296 lldb::RegisterContextSP reg_ctx_sp;
297 uint32_t concrete_frame_idx = 0;
300 concrete_frame_idx = frame->GetConcreteFrameIndex();
302 if (concrete_frame_idx == 0) {
303 ProcessSP process_sp(GetProcess());
305 ProcessGDBRemote *gdb_process =
306 static_cast<ProcessGDBRemote *>(process_sp.get());
307 // read_all_registers_at_once will be true if 'p' packet is not
309 bool read_all_registers_at_once =
310 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
311 reg_ctx_sp.reset(new GDBRemoteRegisterContext(
312 *this, concrete_frame_idx, gdb_process->m_register_info,
313 read_all_registers_at_once));
316 Unwind *unwinder = GetUnwinder();
317 if (unwinder != nullptr)
318 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
323 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
324 llvm::ArrayRef<uint8_t> data) {
325 GDBRemoteRegisterContext *gdb_reg_ctx =
326 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
328 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
331 bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
332 GDBRemoteRegisterContext *gdb_reg_ctx =
333 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
335 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
338 bool ThreadGDBRemote::CalculateStopInfo() {
339 ProcessSP process_sp(GetProcess());
341 return static_cast<ProcessGDBRemote *>(process_sp.get())
342 ->CalculateThreadStopInfo(this);