ThreadGDBRemote.cpp revision 353358
1254721Semaste//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#include "ThreadGDBRemote.h"
10254721Semaste
11258054Semaste#include "lldb/Breakpoint/Watchpoint.h"
12258054Semaste#include "lldb/Target/Platform.h"
13254721Semaste#include "lldb/Target/Process.h"
14254721Semaste#include "lldb/Target/RegisterContext.h"
15254721Semaste#include "lldb/Target/StopInfo.h"
16262528Semaste#include "lldb/Target/SystemRuntime.h"
17254721Semaste#include "lldb/Target/Target.h"
18288943Sdim#include "lldb/Target/UnixSignals.h"
19254721Semaste#include "lldb/Target/Unwind.h"
20321369Sdim#include "lldb/Utility/DataExtractor.h"
21344779Sdim#include "lldb/Utility/State.h"
22321369Sdim#include "lldb/Utility/StreamString.h"
23353358Sdim#include "lldb/Utility/StringExtractorGDBRemote.h"
24254721Semaste
25254721Semaste#include "ProcessGDBRemote.h"
26254721Semaste#include "ProcessGDBRemoteLog.h"
27254721Semaste
28353358Sdim#include <memory>
29353358Sdim
30254721Semasteusing namespace lldb;
31254721Semasteusing namespace lldb_private;
32288943Sdimusing namespace lldb_private::process_gdb_remote;
33254721Semaste
34254721Semaste// Thread Registers
35254721Semaste
36314564SdimThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid)
37314564Sdim    : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
38314564Sdim      m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS),
39314564Sdim      m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown),
40314564Sdim      m_queue_serial_number(LLDB_INVALID_QUEUE_ID),
41314564Sdim      m_associated_with_libdispatch_queue(eLazyBoolCalculate) {
42321369Sdim  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
43321369Sdim  LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
44321369Sdim           GetID());
45254721Semaste}
46254721Semaste
47314564SdimThreadGDBRemote::~ThreadGDBRemote() {
48314564Sdim  ProcessSP process_sp(GetProcess());
49321369Sdim  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
50321369Sdim  LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
51321369Sdim           process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
52314564Sdim  DestroyThread();
53254721Semaste}
54254721Semaste
55314564Sdimconst char *ThreadGDBRemote::GetName() {
56314564Sdim  if (m_thread_name.empty())
57341825Sdim    return nullptr;
58314564Sdim  return m_thread_name.c_str();
59254721Semaste}
60254721Semaste
61314564Sdimvoid ThreadGDBRemote::ClearQueueInfo() {
62314564Sdim  m_dispatch_queue_name.clear();
63314564Sdim  m_queue_kind = eQueueKindUnknown;
64314564Sdim  m_queue_serial_number = 0;
65314564Sdim  m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
66314564Sdim  m_associated_with_libdispatch_queue = eLazyBoolCalculate;
67288943Sdim}
68254721Semaste
69314564Sdimvoid ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
70314564Sdim                                   QueueKind queue_kind, uint64_t queue_serial,
71314564Sdim                                   addr_t dispatch_queue_t,
72314564Sdim                                   LazyBool associated_with_libdispatch_queue) {
73314564Sdim  m_dispatch_queue_name = queue_name;
74314564Sdim  m_queue_kind = queue_kind;
75314564Sdim  m_queue_serial_number = queue_serial;
76314564Sdim  m_dispatch_queue_t = dispatch_queue_t;
77314564Sdim  m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
78288943Sdim}
79288943Sdim
80314564Sdimconst char *ThreadGDBRemote::GetQueueName() {
81314564Sdim  // If our cached queue info is valid, then someone called
82341825Sdim  // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
83341825Sdim  // from the stop reply packet. In this case we trust that the info is valid
84341825Sdim  // in m_dispatch_queue_name without refetching it
85314564Sdim  if (CachedQueueInfoIsValid()) {
86314564Sdim    if (m_dispatch_queue_name.empty())
87314564Sdim      return nullptr;
88314564Sdim    else
89314564Sdim      return m_dispatch_queue_name.c_str();
90314564Sdim  }
91314564Sdim  // Always re-fetch the dispatch queue name since it can change
92288943Sdim
93314564Sdim  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
94314564Sdim    return nullptr;
95254721Semaste
96314564Sdim  if (m_thread_dispatch_qaddr != 0 &&
97314564Sdim      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
98314564Sdim    ProcessSP process_sp(GetProcess());
99314564Sdim    if (process_sp) {
100314564Sdim      SystemRuntime *runtime = process_sp->GetSystemRuntime();
101314564Sdim      if (runtime)
102314564Sdim        m_dispatch_queue_name =
103314564Sdim            runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
104314564Sdim      else
105314564Sdim        m_dispatch_queue_name.clear();
106296417Sdim
107314564Sdim      if (!m_dispatch_queue_name.empty())
108314564Sdim        return m_dispatch_queue_name.c_str();
109254721Semaste    }
110314564Sdim  }
111341825Sdim  return nullptr;
112254721Semaste}
113254721Semaste
114314564SdimQueueKind ThreadGDBRemote::GetQueueKind() {
115314564Sdim  // If our cached queue info is valid, then someone called
116341825Sdim  // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
117341825Sdim  // from the stop reply packet. In this case we trust that the info is valid
118341825Sdim  // in m_dispatch_queue_name without refetching it
119314564Sdim  if (CachedQueueInfoIsValid()) {
120314564Sdim    return m_queue_kind;
121314564Sdim  }
122296417Sdim
123314564Sdim  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
124314564Sdim    return eQueueKindUnknown;
125296417Sdim
126314564Sdim  if (m_thread_dispatch_qaddr != 0 &&
127314564Sdim      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
128314564Sdim    ProcessSP process_sp(GetProcess());
129314564Sdim    if (process_sp) {
130314564Sdim      SystemRuntime *runtime = process_sp->GetSystemRuntime();
131314564Sdim      if (runtime)
132314564Sdim        m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
133314564Sdim      return m_queue_kind;
134296417Sdim    }
135314564Sdim  }
136314564Sdim  return eQueueKindUnknown;
137296417Sdim}
138296417Sdim
139314564Sdimqueue_id_t ThreadGDBRemote::GetQueueID() {
140314564Sdim  // If our cached queue info is valid, then someone called
141341825Sdim  // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
142341825Sdim  // from the stop reply packet. In this case we trust that the info is valid
143341825Sdim  // in m_dispatch_queue_name without refetching it
144314564Sdim  if (CachedQueueInfoIsValid())
145314564Sdim    return m_queue_serial_number;
146296417Sdim
147314564Sdim  if (m_associated_with_libdispatch_queue == eLazyBoolNo)
148314564Sdim    return LLDB_INVALID_QUEUE_ID;
149288943Sdim
150314564Sdim  if (m_thread_dispatch_qaddr != 0 &&
151314564Sdim      m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
152314564Sdim    ProcessSP process_sp(GetProcess());
153314564Sdim    if (process_sp) {
154314564Sdim      SystemRuntime *runtime = process_sp->GetSystemRuntime();
155314564Sdim      if (runtime) {
156314564Sdim        return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
157314564Sdim      }
158258054Semaste    }
159314564Sdim  }
160314564Sdim  return LLDB_INVALID_QUEUE_ID;
161258054Semaste}
162258054Semaste
163314564SdimQueueSP ThreadGDBRemote::GetQueue() {
164314564Sdim  queue_id_t queue_id = GetQueueID();
165314564Sdim  QueueSP queue;
166314564Sdim  if (queue_id != LLDB_INVALID_QUEUE_ID) {
167314564Sdim    ProcessSP process_sp(GetProcess());
168314564Sdim    if (process_sp) {
169314564Sdim      queue = process_sp->GetQueueList().FindQueueByID(queue_id);
170276479Sdim    }
171314564Sdim  }
172314564Sdim  return queue;
173276479Sdim}
174276479Sdim
175314564Sdimaddr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
176314564Sdim  if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
177314564Sdim    if (m_thread_dispatch_qaddr != 0 &&
178314564Sdim        m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
179314564Sdim      ProcessSP process_sp(GetProcess());
180314564Sdim      if (process_sp) {
181314564Sdim        SystemRuntime *runtime = process_sp->GetSystemRuntime();
182314564Sdim        if (runtime) {
183314564Sdim          m_dispatch_queue_t =
184314564Sdim              runtime->GetLibdispatchQueueAddressFromThreadQAddress(
185314564Sdim                  m_thread_dispatch_qaddr);
186276479Sdim        }
187314564Sdim      }
188276479Sdim    }
189314564Sdim  }
190314564Sdim  return m_dispatch_queue_t;
191276479Sdim}
192276479Sdim
193314564Sdimvoid ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
194314564Sdim    lldb::addr_t dispatch_queue_t) {
195314564Sdim  m_dispatch_queue_t = dispatch_queue_t;
196296417Sdim}
197296417Sdim
198314564Sdimbool ThreadGDBRemote::ThreadHasQueueInformation() const {
199344779Sdim  return m_thread_dispatch_qaddr != 0 &&
200344779Sdim         m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
201344779Sdim         m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
202344779Sdim         m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0;
203296417Sdim}
204296417Sdim
205314564SdimLazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
206314564Sdim  return m_associated_with_libdispatch_queue;
207296417Sdim}
208296417Sdim
209314564Sdimvoid ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
210314564Sdim    LazyBool associated_with_libdispatch_queue) {
211314564Sdim  m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
212296417Sdim}
213296417Sdim
214314564SdimStructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
215314564Sdim  StructuredData::ObjectSP object_sp;
216314564Sdim  const lldb::user_id_t tid = GetProtocolID();
217314564Sdim  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
218314564Sdim  if (log)
219314564Sdim    log->Printf("Fetching extended information for thread %4.4" PRIx64, tid);
220314564Sdim  ProcessSP process_sp(GetProcess());
221314564Sdim  if (process_sp) {
222314564Sdim    ProcessGDBRemote *gdb_process =
223314564Sdim        static_cast<ProcessGDBRemote *>(process_sp.get());
224314564Sdim    object_sp = gdb_process->GetExtendedInfoForThread(tid);
225314564Sdim  }
226314564Sdim  return object_sp;
227276479Sdim}
228276479Sdim
229314564Sdimvoid ThreadGDBRemote::WillResume(StateType resume_state) {
230314564Sdim  int signo = GetResumeSignal();
231314564Sdim  const lldb::user_id_t tid = GetProtocolID();
232314564Sdim  Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD));
233314564Sdim  if (log)
234314564Sdim    log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
235314564Sdim                StateAsCString(resume_state));
236254721Semaste
237314564Sdim  ProcessSP process_sp(GetProcess());
238314564Sdim  if (process_sp) {
239314564Sdim    ProcessGDBRemote *gdb_process =
240314564Sdim        static_cast<ProcessGDBRemote *>(process_sp.get());
241314564Sdim    switch (resume_state) {
242314564Sdim    case eStateSuspended:
243314564Sdim    case eStateStopped:
244314564Sdim      // Don't append anything for threads that should stay stopped.
245314564Sdim      break;
246254721Semaste
247314564Sdim    case eStateRunning:
248314564Sdim      if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
249314564Sdim        gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
250314564Sdim      else
251314564Sdim        gdb_process->m_continue_c_tids.push_back(tid);
252314564Sdim      break;
253254721Semaste
254314564Sdim    case eStateStepping:
255314564Sdim      if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
256314564Sdim        gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
257314564Sdim      else
258314564Sdim        gdb_process->m_continue_s_tids.push_back(tid);
259314564Sdim      break;
260254721Semaste
261314564Sdim    default:
262314564Sdim      break;
263254721Semaste    }
264314564Sdim  }
265254721Semaste}
266254721Semaste
267314564Sdimvoid ThreadGDBRemote::RefreshStateAfterStop() {
268314564Sdim  // Invalidate all registers in our register context. We don't set "force" to
269314564Sdim  // true because the stop reply packet might have had some register values
270314564Sdim  // that were expedited and these will already be copied into the register
271341825Sdim  // context by the time this function gets called. The
272341825Sdim  // GDBRemoteRegisterContext class has been made smart enough to detect when
273341825Sdim  // it needs to invalidate which registers are valid by putting hooks in the
274341825Sdim  // register read and register supply functions where they check the process
275341825Sdim  // stop ID and do the right thing.
276314564Sdim  const bool force = false;
277314564Sdim  GetRegisterContext()->InvalidateIfNeeded(force);
278254721Semaste}
279254721Semaste
280314564Sdimbool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
281314564Sdim  return thread != 0;
282254721Semaste}
283254721Semaste
284314564Sdimvoid ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
285254721Semaste
286314564Sdimbool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
287314564Sdimlldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
288341825Sdim  if (!m_reg_context_sp)
289341825Sdim    m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
290314564Sdim  return m_reg_context_sp;
291254721Semaste}
292254721Semaste
293254721Semastelldb::RegisterContextSP
294314564SdimThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
295314564Sdim  lldb::RegisterContextSP reg_ctx_sp;
296314564Sdim  uint32_t concrete_frame_idx = 0;
297254721Semaste
298314564Sdim  if (frame)
299314564Sdim    concrete_frame_idx = frame->GetConcreteFrameIndex();
300314564Sdim
301314564Sdim  if (concrete_frame_idx == 0) {
302314564Sdim    ProcessSP process_sp(GetProcess());
303314564Sdim    if (process_sp) {
304314564Sdim      ProcessGDBRemote *gdb_process =
305314564Sdim          static_cast<ProcessGDBRemote *>(process_sp.get());
306341825Sdim      // read_all_registers_at_once will be true if 'p' packet is not
307341825Sdim      // supported.
308314564Sdim      bool read_all_registers_at_once =
309314564Sdim          !gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
310353358Sdim      reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>(
311314564Sdim          *this, concrete_frame_idx, gdb_process->m_register_info,
312353358Sdim          read_all_registers_at_once);
313254721Semaste    }
314314564Sdim  } else {
315314564Sdim    Unwind *unwinder = GetUnwinder();
316341825Sdim    if (unwinder != nullptr)
317314564Sdim      reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
318314564Sdim  }
319314564Sdim  return reg_ctx_sp;
320254721Semaste}
321254721Semaste
322314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
323314564Sdim                                              llvm::ArrayRef<uint8_t> data) {
324314564Sdim  GDBRemoteRegisterContext *gdb_reg_ctx =
325314564Sdim      static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
326314564Sdim  assert(gdb_reg_ctx);
327314564Sdim  return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
328254721Semaste}
329254721Semaste
330314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
331314564Sdim  GDBRemoteRegisterContext *gdb_reg_ctx =
332314564Sdim      static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
333314564Sdim  assert(gdb_reg_ctx);
334314564Sdim  return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
335296417Sdim}
336296417Sdim
337314564Sdimbool ThreadGDBRemote::CalculateStopInfo() {
338314564Sdim  ProcessSP process_sp(GetProcess());
339314564Sdim  if (process_sp)
340314564Sdim    return static_cast<ProcessGDBRemote *>(process_sp.get())
341314564Sdim        ->CalculateThreadStopInfo(this);
342314564Sdim  return false;
343254721Semaste}
344