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