ThreadGDBRemote.cpp revision 314564
1//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ThreadGDBRemote.h"
11
12#include "lldb/Breakpoint/Watchpoint.h"
13#include "lldb/Core/ArchSpec.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/State.h"
16#include "lldb/Core/StreamString.h"
17#include "lldb/Target/Platform.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/RegisterContext.h"
20#include "lldb/Target/StopInfo.h"
21#include "lldb/Target/SystemRuntime.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/UnixSignals.h"
24#include "lldb/Target/Unwind.h"
25
26#include "ProcessGDBRemote.h"
27#include "ProcessGDBRemoteLog.h"
28#include "Utility/StringExtractorGDBRemote.h"
29
30using namespace lldb;
31using namespace lldb_private;
32using namespace lldb_private::process_gdb_remote;
33
34//----------------------------------------------------------------------
35// Thread Registers
36//----------------------------------------------------------------------
37
38ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid)
39    : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
40      m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS),
41      m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown),
42      m_queue_serial_number(LLDB_INVALID_QUEUE_ID),
43      m_associated_with_libdispatch_queue(eLazyBoolCalculate) {
44  ProcessGDBRemoteLog::LogIf(
45      GDBR_LOG_THREAD,
46      "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this,
47      process.GetID(), GetID());
48}
49
50ThreadGDBRemote::~ThreadGDBRemote() {
51  ProcessSP process_sp(GetProcess());
52  ProcessGDBRemoteLog::LogIf(
53      GDBR_LOG_THREAD,
54      "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this,
55      process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
56  DestroyThread();
57}
58
59const char *ThreadGDBRemote::GetName() {
60  if (m_thread_name.empty())
61    return NULL;
62  return m_thread_name.c_str();
63}
64
65void ThreadGDBRemote::ClearQueueInfo() {
66  m_dispatch_queue_name.clear();
67  m_queue_kind = eQueueKindUnknown;
68  m_queue_serial_number = 0;
69  m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
70  m_associated_with_libdispatch_queue = eLazyBoolCalculate;
71}
72
73void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
74                                   QueueKind queue_kind, uint64_t queue_serial,
75                                   addr_t dispatch_queue_t,
76                                   LazyBool associated_with_libdispatch_queue) {
77  m_dispatch_queue_name = queue_name;
78  m_queue_kind = queue_kind;
79  m_queue_serial_number = queue_serial;
80  m_dispatch_queue_t = dispatch_queue_t;
81  m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
82}
83
84const char *ThreadGDBRemote::GetQueueName() {
85  // If our cached queue info is valid, then someone called
86  // ThreadGDBRemote::SetQueueInfo(...)
87  // with valid information that was gleaned from the stop reply packet. In this
88  // case we trust
89  // that the info is valid in m_dispatch_queue_name without refetching it
90  if (CachedQueueInfoIsValid()) {
91    if (m_dispatch_queue_name.empty())
92      return nullptr;
93    else
94      return m_dispatch_queue_name.c_str();
95  }
96  // Always re-fetch the dispatch queue name since it can change
97
98  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
99    return nullptr;
100
101  if (m_thread_dispatch_qaddr != 0 &&
102      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
103    ProcessSP process_sp(GetProcess());
104    if (process_sp) {
105      SystemRuntime *runtime = process_sp->GetSystemRuntime();
106      if (runtime)
107        m_dispatch_queue_name =
108            runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
109      else
110        m_dispatch_queue_name.clear();
111
112      if (!m_dispatch_queue_name.empty())
113        return m_dispatch_queue_name.c_str();
114    }
115  }
116  return NULL;
117}
118
119QueueKind ThreadGDBRemote::GetQueueKind() {
120  // If our cached queue info is valid, then someone called
121  // ThreadGDBRemote::SetQueueInfo(...)
122  // with valid information that was gleaned from the stop reply packet. In this
123  // case we trust
124  // that the info is valid in m_dispatch_queue_name without refetching it
125  if (CachedQueueInfoIsValid()) {
126    return m_queue_kind;
127  }
128
129  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
130    return eQueueKindUnknown;
131
132  if (m_thread_dispatch_qaddr != 0 &&
133      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
134    ProcessSP process_sp(GetProcess());
135    if (process_sp) {
136      SystemRuntime *runtime = process_sp->GetSystemRuntime();
137      if (runtime)
138        m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
139      return m_queue_kind;
140    }
141  }
142  return eQueueKindUnknown;
143}
144
145queue_id_t ThreadGDBRemote::GetQueueID() {
146  // If our cached queue info is valid, then someone called
147  // ThreadGDBRemote::SetQueueInfo(...)
148  // with valid information that was gleaned from the stop reply packet. In this
149  // case we trust
150  // that the info is valid in m_dispatch_queue_name without refetching it
151  if (CachedQueueInfoIsValid())
152    return m_queue_serial_number;
153
154  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
155    return LLDB_INVALID_QUEUE_ID;
156
157  if (m_thread_dispatch_qaddr != 0 &&
158      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
159    ProcessSP process_sp(GetProcess());
160    if (process_sp) {
161      SystemRuntime *runtime = process_sp->GetSystemRuntime();
162      if (runtime) {
163        return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
164      }
165    }
166  }
167  return LLDB_INVALID_QUEUE_ID;
168}
169
170QueueSP ThreadGDBRemote::GetQueue() {
171  queue_id_t queue_id = GetQueueID();
172  QueueSP queue;
173  if (queue_id != LLDB_INVALID_QUEUE_ID) {
174    ProcessSP process_sp(GetProcess());
175    if (process_sp) {
176      queue = process_sp->GetQueueList().FindQueueByID(queue_id);
177    }
178  }
179  return queue;
180}
181
182addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
183  if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
184    if (m_thread_dispatch_qaddr != 0 &&
185        m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
186      ProcessSP process_sp(GetProcess());
187      if (process_sp) {
188        SystemRuntime *runtime = process_sp->GetSystemRuntime();
189        if (runtime) {
190          m_dispatch_queue_t =
191              runtime->GetLibdispatchQueueAddressFromThreadQAddress(
192                  m_thread_dispatch_qaddr);
193        }
194      }
195    }
196  }
197  return m_dispatch_queue_t;
198}
199
200void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
201    lldb::addr_t dispatch_queue_t) {
202  m_dispatch_queue_t = dispatch_queue_t;
203}
204
205bool ThreadGDBRemote::ThreadHasQueueInformation() const {
206  if (m_thread_dispatch_qaddr != 0 &&
207      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
208      m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
209      m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) {
210    return true;
211  }
212  return false;
213}
214
215LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
216  return m_associated_with_libdispatch_queue;
217}
218
219void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
220    LazyBool associated_with_libdispatch_queue) {
221  m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
222}
223
224StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
225  StructuredData::ObjectSP object_sp;
226  const lldb::user_id_t tid = GetProtocolID();
227  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
228  if (log)
229    log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
230  ProcessSP process_sp(GetProcess());
231  if (process_sp) {
232    ProcessGDBRemote *gdb_process =
233        static_cast<ProcessGDBRemote *>(process_sp.get());
234    object_sp = gdb_process->GetExtendedInfoForThread(tid);
235  }
236  return object_sp;
237}
238
239void ThreadGDBRemote::WillResume(StateType resume_state) {
240  int signo = GetResumeSignal();
241  const lldb::user_id_t tid = GetProtocolID();
242  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
243  if (log)
244    log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
245                StateAsCString(resume_state));
246
247  ProcessSP process_sp(GetProcess());
248  if (process_sp) {
249    ProcessGDBRemote *gdb_process =
250        static_cast<ProcessGDBRemote *>(process_sp.get());
251    switch (resume_state) {
252    case eStateSuspended:
253    case eStateStopped:
254      // Don't append anything for threads that should stay stopped.
255      break;
256
257    case eStateRunning:
258      if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
259        gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
260      else
261        gdb_process->m_continue_c_tids.push_back(tid);
262      break;
263
264    case eStateStepping:
265      if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
266        gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
267      else
268        gdb_process->m_continue_s_tids.push_back(tid);
269      break;
270
271    default:
272      break;
273    }
274  }
275}
276
277void ThreadGDBRemote::RefreshStateAfterStop() {
278  // Invalidate all registers in our register context. We don't set "force" to
279  // true because the stop reply packet might have had some register values
280  // that were expedited and these will already be copied into the register
281  // context by the time this function gets called. The GDBRemoteRegisterContext
282  // class has been made smart enough to detect when it needs to invalidate
283  // which registers are valid by putting hooks in the register read and
284  // register supply functions where they check the process stop ID and do
285  // the right thing.
286  const bool force = false;
287  GetRegisterContext()->InvalidateIfNeeded(force);
288}
289
290bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
291  return thread != 0;
292}
293
294void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
295
296bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
297lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
298  if (m_reg_context_sp.get() == NULL)
299    m_reg_context_sp = CreateRegisterContextForFrame(NULL);
300  return m_reg_context_sp;
301}
302
303lldb::RegisterContextSP
304ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
305  lldb::RegisterContextSP reg_ctx_sp;
306  uint32_t concrete_frame_idx = 0;
307
308  if (frame)
309    concrete_frame_idx = frame->GetConcreteFrameIndex();
310
311  if (concrete_frame_idx == 0) {
312    ProcessSP process_sp(GetProcess());
313    if (process_sp) {
314      ProcessGDBRemote *gdb_process =
315          static_cast<ProcessGDBRemote *>(process_sp.get());
316      // read_all_registers_at_once will be true if 'p' packet is not supported.
317      bool read_all_registers_at_once =
318          !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
319      reg_ctx_sp.reset(new GDBRemoteRegisterContext(
320          *this, concrete_frame_idx, gdb_process->m_register_info,
321          read_all_registers_at_once));
322    }
323  } else {
324    Unwind *unwinder = GetUnwinder();
325    if (unwinder)
326      reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
327  }
328  return reg_ctx_sp;
329}
330
331bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
332                                              llvm::ArrayRef<uint8_t> data) {
333  GDBRemoteRegisterContext *gdb_reg_ctx =
334      static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
335  assert(gdb_reg_ctx);
336  return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
337}
338
339bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
340  GDBRemoteRegisterContext *gdb_reg_ctx =
341      static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
342  assert(gdb_reg_ctx);
343  return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
344}
345
346bool ThreadGDBRemote::CalculateStopInfo() {
347  ProcessSP process_sp(GetProcess());
348  if (process_sp)
349    return static_cast<ProcessGDBRemote *>(process_sp.get())
350        ->CalculateThreadStopInfo(this);
351  return false;
352}
353