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 
12 #include "lldb/Target/Platform.h"
13 #include "lldb/Target/Process.h"
15 #include "lldb/Target/StopInfo.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 
30 using namespace lldb;
31 using namespace lldb_private;
32 using namespace lldb_private::process_gdb_remote;
33 
34 // Thread Registers
35 
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) {
43  LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
44  GetID());
45 }
46 
48  ProcessSP process_sp(GetProcess());
50  LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
51  process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
52  DestroyThread();
53 }
54 
55 const char *ThreadGDBRemote::GetName() {
56  if (m_thread_name.empty())
57  return nullptr;
58  return m_thread_name.c_str();
59 }
60 
62  m_dispatch_queue_name.clear();
67 }
68 
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;
78 }
79 
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())
87  return nullptr;
88  else
89  return m_dispatch_queue_name.c_str();
90  }
91  // Always re-fetch the dispatch queue name since it can change
92 
94  return nullptr;
95 
96  if (m_thread_dispatch_qaddr != 0 &&
98  ProcessSP process_sp(GetProcess());
99  if (process_sp) {
100  SystemRuntime *runtime = process_sp->GetSystemRuntime();
101  if (runtime)
104  else
105  m_dispatch_queue_name.clear();
106 
107  if (!m_dispatch_queue_name.empty())
108  return m_dispatch_queue_name.c_str();
109  }
110  }
111  return nullptr;
112 }
113 
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()) {
120  return m_queue_kind;
121  }
122 
124  return eQueueKindUnknown;
125 
126  if (m_thread_dispatch_qaddr != 0 &&
128  ProcessSP process_sp(GetProcess());
129  if (process_sp) {
130  SystemRuntime *runtime = process_sp->GetSystemRuntime();
131  if (runtime)
133  return m_queue_kind;
134  }
135  }
136  return eQueueKindUnknown;
137 }
138 
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
145  return m_queue_serial_number;
146 
148  return LLDB_INVALID_QUEUE_ID;
149 
150  if (m_thread_dispatch_qaddr != 0 &&
152  ProcessSP process_sp(GetProcess());
153  if (process_sp) {
154  SystemRuntime *runtime = process_sp->GetSystemRuntime();
155  if (runtime) {
157  }
158  }
159  }
160  return LLDB_INVALID_QUEUE_ID;
161 }
162 
164  queue_id_t queue_id = GetQueueID();
165  QueueSP queue;
166  if (queue_id != LLDB_INVALID_QUEUE_ID) {
167  ProcessSP process_sp(GetProcess());
168  if (process_sp) {
169  queue = process_sp->GetQueueList().FindQueueByID(queue_id);
170  }
171  }
172  return queue;
173 }
174 
177  if (m_thread_dispatch_qaddr != 0 &&
179  ProcessSP process_sp(GetProcess());
180  if (process_sp) {
181  SystemRuntime *runtime = process_sp->GetSystemRuntime();
182  if (runtime) {
186  }
187  }
188  }
189  }
190  return m_dispatch_queue_t;
191 }
192 
194  lldb::addr_t dispatch_queue_t) {
195  m_dispatch_queue_t = dispatch_queue_t;
196 }
197 
199  return m_thread_dispatch_qaddr != 0 &&
203 }
204 
207 }
208 
210  LazyBool associated_with_libdispatch_queue) {
211  m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
212 }
213 
215  StructuredData::ObjectSP object_sp;
216  const lldb::user_id_t tid = GetProtocolID();
218  LLDB_LOGF(log, "Fetching extended information for thread %4.4" PRIx64, tid);
219  ProcessSP process_sp(GetProcess());
220  if (process_sp) {
221  ProcessGDBRemote *gdb_process =
222  static_cast<ProcessGDBRemote *>(process_sp.get());
223  object_sp = gdb_process->GetExtendedInfoForThread(tid);
224  }
225  return object_sp;
226 }
227 
229  int signo = GetResumeSignal();
230  const lldb::user_id_t tid = GetProtocolID();
232  LLDB_LOGF(log, "Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
233  StateAsCString(resume_state));
234 
235  ProcessSP process_sp(GetProcess());
236  if (process_sp) {
237  ProcessGDBRemote *gdb_process =
238  static_cast<ProcessGDBRemote *>(process_sp.get());
239  switch (resume_state) {
240  case eStateSuspended:
241  case eStateStopped:
242  // Don't append anything for threads that should stay stopped.
243  break;
244 
245  case eStateRunning:
246  if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
247  gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
248  else
249  gdb_process->m_continue_c_tids.push_back(tid);
250  break;
251 
252  case eStateStepping:
253  if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
254  gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
255  else
256  gdb_process->m_continue_s_tids.push_back(tid);
257  break;
258 
259  default:
260  break;
261  }
262  }
263 }
264 
266  // Invalidate all registers in our register context. We don't set "force" to
267  // true because the stop reply packet might have had some register values
268  // that were expedited and these will already be copied into the register
269  // context by the time this function gets called. The
270  // GDBRemoteRegisterContext class has been made smart enough to detect when
271  // it needs to invalidate which registers are valid by putting hooks in the
272  // register read and register supply functions where they check the process
273  // stop ID and do the right thing.
274  const bool force = false;
275  GetRegisterContext()->InvalidateIfNeeded(force);
276 }
277 
279  return thread != 0;
280 }
281 
282 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
283 
284 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
285 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
286  if (!m_reg_context_sp)
288  return m_reg_context_sp;
289 }
290 
291 lldb::RegisterContextSP
293  lldb::RegisterContextSP reg_ctx_sp;
294  uint32_t concrete_frame_idx = 0;
295 
296  if (frame)
297  concrete_frame_idx = frame->GetConcreteFrameIndex();
298 
299  if (concrete_frame_idx == 0) {
300  ProcessSP process_sp(GetProcess());
301  if (process_sp) {
302  ProcessGDBRemote *gdb_process =
303  static_cast<ProcessGDBRemote *>(process_sp.get());
304  bool pSupported =
305  gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
306  bool read_all_registers_at_once =
307  !pSupported || gdb_process->m_use_g_packet_for_reading;
308  bool write_all_registers_at_once = !pSupported;
309  reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>(
310  *this, concrete_frame_idx, gdb_process->m_register_info,
311  read_all_registers_at_once, write_all_registers_at_once);
312  }
313  } else {
314  reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
315  }
316  return reg_ctx_sp;
317 }
318 
320  llvm::ArrayRef<uint8_t> data) {
321  GDBRemoteRegisterContext *gdb_reg_ctx =
322  static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
323  assert(gdb_reg_ctx);
324  return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
325 }
326 
328  GDBRemoteRegisterContext *gdb_reg_ctx =
329  static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
330  assert(gdb_reg_ctx);
331  return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
332 }
333 
335  ProcessSP process_sp(GetProcess());
336  if (process_sp)
337  return static_cast<ProcessGDBRemote *>(process_sp.get())
338  ->CalculateThreadStopInfo(this);
339  return false;
340 }
virtual lldb::queue_id_t GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the QueueID for the libdispatch queue given the thread&#39;s dispatch_qaddr.
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
A class that represents a running process on the host machine.
virtual std::string GetQueueNameFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the queue name for a thread given a thread&#39;s dispatch_qaddr.
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:92
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47
#define GDBR_LOG_THREAD
#define LLDB_INVALID_QUEUE_ID
Definition: lldb-defines.h:98
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1860
virtual lldb::QueueKind GetQueueKind(lldb::addr_t dispatch_qaddr)
Retrieve the Queue kind for the queue at a thread&#39;s dispatch_qaddr.
void SetAssociatedWithLibdispatchQueue(lldb_private::LazyBool associated_with_libdispatch_queue) override
Process or thread is stopped and can be examined.
QueueKind
Queue type.
lldb::QueueKind GetQueueKind() override
Retrieve the Queue kind for the queue currently using this Thread.
virtual lldb::user_id_t GetProtocolID() const
Definition: Thread.h:1058
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread&#39;s current register state.
Definition: Thread.h:1229
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
lldb::RegisterContextSP GetRegisterContext() override
StructuredData::ObjectSP FetchThreadExtendedInfo() override
StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid)
const lldb::UnixSignalsSP & GetUnixSignals()
Definition: Process.cpp:3422
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:56
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::queue_id_t GetQueueID() override
Retrieve the Queue ID for the queue currently using this Thread.
uint64_t user_id_t
Definition: lldb-types.h:84
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)
StateType
Process and Thread States.
uint64_t tid_t
Definition: lldb-types.h:86
A plug-in interface definition class for debugging a process.
Definition: Process.h:362
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame) override
virtual void DestroyThread()
Definition: Thread.cpp:254
Process or thread is in the process of stepping and can not be examined.
lldb_private::LazyBool GetAssociatedWithLibdispatchQueue() override
Whether this thread can be associated with a libdispatch queue.
lldb::ProcessSP GetProcess() const
Definition: Thread.h:153
uint64_t queue_id_t
Definition: lldb-types.h:91
int GetResumeSignal() const
Definition: Thread.h:155
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb::QueueSP GetQueue() override
Retrieve the Queue for this thread, if any.
uint64_t addr_t
Definition: lldb-types.h:83
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:62
bool PrivateSetRegisterValue(uint32_t reg, llvm::ArrayRef< uint8_t > data)
Definition: SBAddress.h:15
const char * GetQueueName() override
Retrieve the Queue name for the queue currently using this Thread.
std::shared_ptr< Object > ObjectSP
virtual lldb::addr_t GetLibdispatchQueueAddressFromThreadQAddress(lldb::addr_t dispatch_qaddr)
Get the libdispatch_queue_t address for the queue given the thread&#39;s dispatch_qaddr.
lldb::addr_t GetQueueLibdispatchQueueAddress() override
Retrieve the address of the libdispatch_queue_t struct for queue currently using this Thread...
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread&#39;s StackFrameList, not counting inlined frame...
Definition: StackFrame.h:389
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.
This base class provides an interface to stack frames.
Definition: StackFrame.h:40
void WillResume(lldb::StateType resume_state) override
A plug-in interface definition class for system runtimes.
Definition: SystemRuntime.h:42
Process or thread is running and can&#39;t be examined.