LLDB mainline
ThreadGDBRemote.cpp
Go to the documentation of this file.
1//===-- ThreadGDBRemote.cpp -----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "ThreadGDBRemote.h"
10
13#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
19#include "lldb/Target/Unwind.h"
21#include "lldb/Utility/State.h"
24
25#include "ProcessGDBRemote.h"
26#include "ProcessGDBRemoteLog.h"
27
28#include <memory>
29
30using namespace lldb;
31using namespace lldb_private;
33
34// Thread Registers
35
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) {
43 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
44 GetID());
45 // At this point we can clone reg_info for architectures supporting
46 // run-time update to register sizes and offsets..
47 auto &gdb_process = static_cast<ProcessGDBRemote &>(process);
48 if (!gdb_process.m_register_info_sp->IsReconfigurable())
49 m_reg_info_sp = gdb_process.m_register_info_sp;
50 else
51 m_reg_info_sp = std::make_shared<GDBRemoteDynamicRegisterInfo>(
52 *gdb_process.m_register_info_sp);
53}
54
56 ProcessSP process_sp(GetProcess());
58 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
59 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
61}
62
64 if (m_thread_name.empty())
65 return nullptr;
66 return m_thread_name.c_str();
67}
68
75}
76
77void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
78 QueueKind queue_kind, uint64_t queue_serial,
79 addr_t dispatch_queue_t,
80 LazyBool associated_with_libdispatch_queue) {
81 m_dispatch_queue_name = queue_name;
82 m_queue_kind = queue_kind;
83 m_queue_serial_number = queue_serial;
84 m_dispatch_queue_t = dispatch_queue_t;
85 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
86}
87
89 // If our cached queue info is valid, then someone called
90 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
91 // from the stop reply packet. In this case we trust that the info is valid
92 // in m_dispatch_queue_name without refetching it
94 if (m_dispatch_queue_name.empty())
95 return nullptr;
96 else
97 return m_dispatch_queue_name.c_str();
98 }
99 // Always re-fetch the dispatch queue name since it can change
100
102 return nullptr;
103
104 if (m_thread_dispatch_qaddr != 0 &&
106 ProcessSP process_sp(GetProcess());
107 if (process_sp) {
108 SystemRuntime *runtime = process_sp->GetSystemRuntime();
109 if (runtime)
112 else
113 m_dispatch_queue_name.clear();
114
115 if (!m_dispatch_queue_name.empty())
116 return m_dispatch_queue_name.c_str();
117 }
118 }
119 return nullptr;
120}
121
123 // If our cached queue info is valid, then someone called
124 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
125 // from the stop reply packet. In this case we trust that the info is valid
126 // in m_dispatch_queue_name without refetching it
128 return m_queue_kind;
129 }
130
132 return eQueueKindUnknown;
133
134 if (m_thread_dispatch_qaddr != 0 &&
136 ProcessSP process_sp(GetProcess());
137 if (process_sp) {
138 SystemRuntime *runtime = process_sp->GetSystemRuntime();
139 if (runtime)
141 return m_queue_kind;
142 }
143 }
144 return eQueueKindUnknown;
145}
146
148 // If our cached queue info is valid, then someone called
149 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
150 // from the stop reply packet. In this case we trust that the info is valid
151 // in m_dispatch_queue_name without refetching it
154
157
158 if (m_thread_dispatch_qaddr != 0 &&
160 ProcessSP process_sp(GetProcess());
161 if (process_sp) {
162 SystemRuntime *runtime = process_sp->GetSystemRuntime();
163 if (runtime) {
165 }
166 }
167 }
169}
170
172 queue_id_t queue_id = GetQueueID();
173 QueueSP queue;
174 if (queue_id != LLDB_INVALID_QUEUE_ID) {
175 ProcessSP process_sp(GetProcess());
176 if (process_sp) {
177 queue = process_sp->GetQueueList().FindQueueByID(queue_id);
178 }
179 }
180 return queue;
181}
182
185 if (m_thread_dispatch_qaddr != 0 &&
187 ProcessSP process_sp(GetProcess());
188 if (process_sp) {
189 SystemRuntime *runtime = process_sp->GetSystemRuntime();
190 if (runtime) {
194 }
195 }
196 }
197 }
198 return m_dispatch_queue_t;
199}
200
202 lldb::addr_t dispatch_queue_t) {
203 m_dispatch_queue_t = dispatch_queue_t;
204}
205
207 return m_thread_dispatch_qaddr != 0 &&
211}
212
215}
216
218 LazyBool associated_with_libdispatch_queue) {
219 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
220}
221
223 StructuredData::ObjectSP object_sp;
224 const lldb::user_id_t tid = GetProtocolID();
225 Log *log = GetLog(GDBRLog::Thread);
226 LLDB_LOGF(log, "Fetching extended information for thread %4.4" PRIx64, tid);
227 ProcessSP process_sp(GetProcess());
228 if (process_sp) {
229 ProcessGDBRemote *gdb_process =
230 static_cast<ProcessGDBRemote *>(process_sp.get());
231 object_sp = gdb_process->GetExtendedInfoForThread(tid);
232 }
233 return object_sp;
234}
235
237 int signo = GetResumeSignal();
238 const lldb::user_id_t tid = GetProtocolID();
239 Log *log = GetLog(GDBRLog::Thread);
240 LLDB_LOGF(log, "Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
241 StateAsCString(resume_state));
242
243 ProcessSP process_sp(GetProcess());
244 if (process_sp) {
245 ProcessGDBRemote *gdb_process =
246 static_cast<ProcessGDBRemote *>(process_sp.get());
247 switch (resume_state) {
248 case eStateSuspended:
249 case eStateStopped:
250 // Don't append anything for threads that should stay stopped.
251 break;
252
253 case eStateRunning:
254 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
255 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
256 else
257 gdb_process->m_continue_c_tids.push_back(tid);
258 break;
259
260 case eStateStepping:
261 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
262 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
263 else
264 gdb_process->m_continue_s_tids.push_back(tid);
265 break;
266
267 default:
268 break;
269 }
270 }
271}
272
274 // Invalidate all registers in our register context. We don't set "force" to
275 // true because the stop reply packet might have had some register values
276 // that were expedited and these will already be copied into the register
277 // context by the time this function gets called. The
278 // GDBRemoteRegisterContext class has been made smart enough to detect when
279 // it needs to invalidate which registers are valid by putting hooks in the
280 // register read and register supply functions where they check the process
281 // stop ID and do the right thing.
282 const bool force = false;
283 GetRegisterContext()->InvalidateIfNeeded(force);
284}
285
287 return thread != 0;
288}
289
290void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
291
292bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
294 if (!m_reg_context_sp)
296 return m_reg_context_sp;
297}
298
301 lldb::RegisterContextSP reg_ctx_sp;
302 uint32_t concrete_frame_idx = 0;
303
304 if (frame)
305 concrete_frame_idx = frame->GetConcreteFrameIndex();
306
307 if (concrete_frame_idx == 0) {
308 ProcessSP process_sp(GetProcess());
309 if (process_sp) {
310 ProcessGDBRemote *gdb_process =
311 static_cast<ProcessGDBRemote *>(process_sp.get());
312 bool pSupported =
313 gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
314 bool read_all_registers_at_once =
315 !pSupported || gdb_process->m_use_g_packet_for_reading;
316 bool write_all_registers_at_once = !pSupported;
317 reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>(
318 *this, concrete_frame_idx, m_reg_info_sp, read_all_registers_at_once,
319 write_all_registers_at_once);
320 }
321 } else {
322 reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
323 }
324 return reg_ctx_sp;
325}
326
328 llvm::ArrayRef<uint8_t> data) {
329 GDBRemoteRegisterContext *gdb_reg_ctx =
330 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
331 assert(gdb_reg_ctx);
332 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
333}
334
335bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
336 GDBRemoteRegisterContext *gdb_reg_ctx =
337 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
338 assert(gdb_reg_ctx);
339 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
340}
341
343 ProcessSP process_sp(GetProcess());
344 if (process_sp)
345 return static_cast<ProcessGDBRemote *>(process_sp.get())
346 ->CalculateThreadStopInfo(this);
347 return false;
348}
349
350llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
351ThreadGDBRemote::GetSiginfo(size_t max_size) const {
352 ProcessSP process_sp(GetProcess());
353 if (!process_sp)
354 return llvm::createStringError(llvm::inconvertibleErrorCode(),
355 "no process");
356 ProcessGDBRemote *gdb_process =
357 static_cast<ProcessGDBRemote *>(process_sp.get());
358 if (!gdb_process->m_gdb_comm.GetQXferSigInfoReadSupported())
359 return llvm::createStringError(llvm::inconvertibleErrorCode(),
360 "qXfer:siginfo:read not supported");
361
362 llvm::Expected<std::string> response =
363 gdb_process->m_gdb_comm.ReadExtFeature("siginfo", "");
364 if (!response)
365 return response.takeError();
366
367 return llvm::MemoryBuffer::getMemBufferCopy(response.get());
368}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:369
#define LLDB_LOGF(log,...)
Definition: Log.h:376
A plug-in interface definition class for debugging a process.
Definition: Process.h:343
lldb::pid_t GetID() const
Returns the pid of the process or LLDB_INVALID_PROCESS_ID if there is no known pid.
Definition: Process.h:542
const lldb::UnixSignalsSP & GetUnixSignals()
Definition: Process.cpp:3606
This base class provides an interface to stack frames.
Definition: StackFrame.h:44
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition: StackFrame.h:448
std::shared_ptr< Object > ObjectSP
A plug-in interface definition class for system runtimes.
Definition: SystemRuntime.h:43
virtual std::string GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the queue name for a thread given a thread's dispatch_qaddr.
virtual lldb::addr_t GetLibdispatchQueueAddressFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the libdispatch_queue_t address for the queue given the thread's dispatch_qaddr.
virtual lldb::queue_id_t GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the QueueID for the libdispatch queue given the thread's dispatch_qaddr.
virtual lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_qaddr)
Retrieve the Queue kind for the queue at a thread's dispatch_qaddr.
virtual lldb::user_id_t GetProtocolID() const
Definition: Thread.h:1113
virtual void DestroyThread()
Definition: Thread.cpp:251
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1915
int GetResumeSignal() const
Definition: Thread.h:159
lldb::ProcessSP GetProcess() const
Definition: Thread.h:157
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread's current register state.
Definition: Thread.h:1319
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
llvm::Expected< std::string > ReadExtFeature(llvm::StringRef object, llvm::StringRef annex)
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid)
StructuredData::ObjectSP FetchThreadExtendedInfo() override
void SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t) override
bool ThreadHasQueueInformation() const override
Whether this Thread already has all the Queue information cached or not.
lldb::QueueSP GetQueue() override
Retrieve the Queue for this thread, if any.
void SetQueueInfo(std::string &&queue_name, lldb::QueueKind queue_kind, uint64_t queue_serial, lldb::addr_t dispatch_queue_t, lldb_private::LazyBool associated_with_libdispatch_queue)
llvm::Expected< std::unique_ptr< llvm::MemoryBuffer > > GetSiginfo(size_t max_size) const override
lldb_private::LazyBool GetAssociatedWithLibdispatchQueue() override
Whether this thread can be associated with a libdispatch queue.
lldb::QueueKind GetQueueKind() override
Retrieve the Queue kind for the queue currently using this Thread.
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame) override
lldb::queue_id_t GetQueueID() override
Retrieve the Queue ID for the queue currently using this Thread.
const char * GetQueueName() override
Retrieve the Queue name for the queue currently using this Thread.
bool CalculateStopInfo() override
Ask the thread subclass to set its stop info.
void WillResume(lldb::StateType resume_state) override
lldb::RegisterContextSP GetRegisterContext() override
ThreadGDBRemote(Process &process, lldb::tid_t tid)
lldb::addr_t GetQueueLibdispatchQueueAddress() override
Retrieve the address of the libdispatch_queue_t struct for queue currently using this Thread.
void SetAssociatedWithLibdispatchQueue(lldb_private::LazyBool associated_with_libdispatch_queue) override
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
#define LLDB_INVALID_QUEUE_ID
Definition: lldb-defines.h:96
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:89
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Queue > QueueSP
Definition: lldb-forward.h:398
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
QueueKind
Queue type.
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:394
uint64_t tid_t
Definition: lldb-types.h:84
uint64_t queue_id_t
Definition: lldb-types.h:90
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47