LLDB  mainline
ThreadGDBRemote.cpp
Go to the documentation of this file.
1 //===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
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  if (log)
219  log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
220  ProcessSP process_sp(GetProcess());
221  if (process_sp) {
222  ProcessGDBRemote *gdb_process =
223  static_cast<ProcessGDBRemote *>(process_sp.get());
224  object_sp = gdb_process->GetExtendedInfoForThread(tid);
225  }
226  return object_sp;
227 }
228 
230  int signo = GetResumeSignal();
231  const lldb::user_id_t tid = GetProtocolID();
233  if (log)
234  log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
235  StateAsCString(resume_state));
236 
237  ProcessSP process_sp(GetProcess());
238  if (process_sp) {
239  ProcessGDBRemote *gdb_process =
240  static_cast<ProcessGDBRemote *>(process_sp.get());
241  switch (resume_state) {
242  case eStateSuspended:
243  case eStateStopped:
244  // Don't append anything for threads that should stay stopped.
245  break;
246 
247  case eStateRunning:
248  if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
249  gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
250  else
251  gdb_process->m_continue_c_tids.push_back(tid);
252  break;
253 
254  case eStateStepping:
255  if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
256  gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
257  else
258  gdb_process->m_continue_s_tids.push_back(tid);
259  break;
260 
261  default:
262  break;
263  }
264  }
265 }
266 
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);
278 }
279 
281  return thread != 0;
282 }
283 
284 void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
285 
286 bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
287 lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
288  if (!m_reg_context_sp)
290  return m_reg_context_sp;
291 }
292 
293 lldb::RegisterContextSP
295  lldb::RegisterContextSP reg_ctx_sp;
296  uint32_t concrete_frame_idx = 0;
297 
298  if (frame)
299  concrete_frame_idx = frame->GetConcreteFrameIndex();
300 
301  if (concrete_frame_idx == 0) {
302  ProcessSP process_sp(GetProcess());
303  if (process_sp) {
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
307  // supported.
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);
313  }
314  } else {
315  Unwind *unwinder = GetUnwinder();
316  if (unwinder != nullptr)
317  reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
318  }
319  return reg_ctx_sp;
320 }
321 
323  llvm::ArrayRef<uint8_t> data) {
324  GDBRemoteRegisterContext *gdb_reg_ctx =
325  static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
326  assert(gdb_reg_ctx);
327  return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
328 }
329 
331  GDBRemoteRegisterContext *gdb_reg_ctx =
332  static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
333  assert(gdb_reg_ctx);
334  return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
335 }
336 
338  ProcessSP process_sp(GetProcess());
339  if (process_sp)
340  return static_cast<ProcessGDBRemote *>(process_sp.get())
341  ->CalculateThreadStopInfo(this);
342  return false;
343 }
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
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
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:49
#define GDBR_LOG_THREAD
#define LLDB_INVALID_QUEUE_ID
Definition: lldb-defines.h:98
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.
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:1060
lldb::RegisterContextSP m_reg_context_sp
The register context for this thread&#39;s current register state.
Definition: Thread.h:1220
#define LLDB_LOG(log,...)
Definition: Log.h:209
lldb::RegisterContextSP GetRegisterContext() override
StructuredData::ObjectSP FetchThreadExtendedInfo() override
StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid)
const lldb::UnixSignalsSP & GetUnixSignals()
Definition: Process.cpp:3361
virtual lldb_private::Unwind * GetUnwinder()
Definition: Thread.cpp:2056
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame)
Definition: Unwind.h:55
#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)
uint64_t tid_t
Definition: lldb-types.h:86
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame) override
virtual void DestroyThread()
Definition: Thread.cpp:278
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:154
uint64_t queue_id_t
Definition: lldb-types.h:91
int GetResumeSignal() const
Definition: Thread.h:156
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
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:61
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 Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
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.