1dda28197Spatrick //===-- ThreadGDBRemote.cpp -----------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "ThreadGDBRemote.h"
10061da546Spatrick
11061da546Spatrick #include "lldb/Breakpoint/Watchpoint.h"
12061da546Spatrick #include "lldb/Target/Platform.h"
13061da546Spatrick #include "lldb/Target/Process.h"
14061da546Spatrick #include "lldb/Target/RegisterContext.h"
15061da546Spatrick #include "lldb/Target/StopInfo.h"
16061da546Spatrick #include "lldb/Target/SystemRuntime.h"
17061da546Spatrick #include "lldb/Target/Target.h"
18061da546Spatrick #include "lldb/Target/UnixSignals.h"
19061da546Spatrick #include "lldb/Target/Unwind.h"
20061da546Spatrick #include "lldb/Utility/DataExtractor.h"
21061da546Spatrick #include "lldb/Utility/State.h"
22061da546Spatrick #include "lldb/Utility/StreamString.h"
23061da546Spatrick #include "lldb/Utility/StringExtractorGDBRemote.h"
24061da546Spatrick
25061da546Spatrick #include "ProcessGDBRemote.h"
26061da546Spatrick #include "ProcessGDBRemoteLog.h"
27061da546Spatrick
28061da546Spatrick #include <memory>
29061da546Spatrick
30061da546Spatrick using namespace lldb;
31061da546Spatrick using namespace lldb_private;
32061da546Spatrick using namespace lldb_private::process_gdb_remote;
33061da546Spatrick
34061da546Spatrick // Thread Registers
35061da546Spatrick
ThreadGDBRemote(Process & process,lldb::tid_t tid)36061da546Spatrick ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid)
37061da546Spatrick : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
38061da546Spatrick m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS),
39061da546Spatrick m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown),
40061da546Spatrick m_queue_serial_number(LLDB_INVALID_QUEUE_ID),
41061da546Spatrick m_associated_with_libdispatch_queue(eLazyBoolCalculate) {
42*f6aab3d8Srobert Log *log = GetLog(GDBRLog::Thread);
43061da546Spatrick LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(),
44061da546Spatrick GetID());
45be691f3bSpatrick // At this point we can clone reg_info for architectures supporting
46be691f3bSpatrick // run-time update to register sizes and offsets..
47be691f3bSpatrick auto &gdb_process = static_cast<ProcessGDBRemote &>(process);
48be691f3bSpatrick if (!gdb_process.m_register_info_sp->IsReconfigurable())
49be691f3bSpatrick m_reg_info_sp = gdb_process.m_register_info_sp;
50be691f3bSpatrick else
51be691f3bSpatrick m_reg_info_sp = std::make_shared<GDBRemoteDynamicRegisterInfo>(
52be691f3bSpatrick *gdb_process.m_register_info_sp);
53061da546Spatrick }
54061da546Spatrick
~ThreadGDBRemote()55061da546Spatrick ThreadGDBRemote::~ThreadGDBRemote() {
56061da546Spatrick ProcessSP process_sp(GetProcess());
57*f6aab3d8Srobert Log *log = GetLog(GDBRLog::Thread);
58061da546Spatrick LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this,
59061da546Spatrick process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID());
60061da546Spatrick DestroyThread();
61061da546Spatrick }
62061da546Spatrick
GetName()63061da546Spatrick const char *ThreadGDBRemote::GetName() {
64061da546Spatrick if (m_thread_name.empty())
65061da546Spatrick return nullptr;
66061da546Spatrick return m_thread_name.c_str();
67061da546Spatrick }
68061da546Spatrick
ClearQueueInfo()69061da546Spatrick void ThreadGDBRemote::ClearQueueInfo() {
70061da546Spatrick m_dispatch_queue_name.clear();
71061da546Spatrick m_queue_kind = eQueueKindUnknown;
72061da546Spatrick m_queue_serial_number = 0;
73061da546Spatrick m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
74061da546Spatrick m_associated_with_libdispatch_queue = eLazyBoolCalculate;
75061da546Spatrick }
76061da546Spatrick
SetQueueInfo(std::string && queue_name,QueueKind queue_kind,uint64_t queue_serial,addr_t dispatch_queue_t,LazyBool associated_with_libdispatch_queue)77061da546Spatrick void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name,
78061da546Spatrick QueueKind queue_kind, uint64_t queue_serial,
79061da546Spatrick addr_t dispatch_queue_t,
80061da546Spatrick LazyBool associated_with_libdispatch_queue) {
81061da546Spatrick m_dispatch_queue_name = queue_name;
82061da546Spatrick m_queue_kind = queue_kind;
83061da546Spatrick m_queue_serial_number = queue_serial;
84061da546Spatrick m_dispatch_queue_t = dispatch_queue_t;
85061da546Spatrick m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
86061da546Spatrick }
87061da546Spatrick
GetQueueName()88061da546Spatrick const char *ThreadGDBRemote::GetQueueName() {
89061da546Spatrick // If our cached queue info is valid, then someone called
90061da546Spatrick // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
91061da546Spatrick // from the stop reply packet. In this case we trust that the info is valid
92061da546Spatrick // in m_dispatch_queue_name without refetching it
93061da546Spatrick if (CachedQueueInfoIsValid()) {
94061da546Spatrick if (m_dispatch_queue_name.empty())
95061da546Spatrick return nullptr;
96061da546Spatrick else
97061da546Spatrick return m_dispatch_queue_name.c_str();
98061da546Spatrick }
99061da546Spatrick // Always re-fetch the dispatch queue name since it can change
100061da546Spatrick
101061da546Spatrick if (m_associated_with_libdispatch_queue == eLazyBoolNo)
102061da546Spatrick return nullptr;
103061da546Spatrick
104061da546Spatrick if (m_thread_dispatch_qaddr != 0 &&
105061da546Spatrick m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
106061da546Spatrick ProcessSP process_sp(GetProcess());
107061da546Spatrick if (process_sp) {
108061da546Spatrick SystemRuntime *runtime = process_sp->GetSystemRuntime();
109061da546Spatrick if (runtime)
110061da546Spatrick m_dispatch_queue_name =
111061da546Spatrick runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr);
112061da546Spatrick else
113061da546Spatrick m_dispatch_queue_name.clear();
114061da546Spatrick
115061da546Spatrick if (!m_dispatch_queue_name.empty())
116061da546Spatrick return m_dispatch_queue_name.c_str();
117061da546Spatrick }
118061da546Spatrick }
119061da546Spatrick return nullptr;
120061da546Spatrick }
121061da546Spatrick
GetQueueKind()122061da546Spatrick QueueKind ThreadGDBRemote::GetQueueKind() {
123061da546Spatrick // If our cached queue info is valid, then someone called
124061da546Spatrick // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
125061da546Spatrick // from the stop reply packet. In this case we trust that the info is valid
126061da546Spatrick // in m_dispatch_queue_name without refetching it
127061da546Spatrick if (CachedQueueInfoIsValid()) {
128061da546Spatrick return m_queue_kind;
129061da546Spatrick }
130061da546Spatrick
131061da546Spatrick if (m_associated_with_libdispatch_queue == eLazyBoolNo)
132061da546Spatrick return eQueueKindUnknown;
133061da546Spatrick
134061da546Spatrick if (m_thread_dispatch_qaddr != 0 &&
135061da546Spatrick m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
136061da546Spatrick ProcessSP process_sp(GetProcess());
137061da546Spatrick if (process_sp) {
138061da546Spatrick SystemRuntime *runtime = process_sp->GetSystemRuntime();
139061da546Spatrick if (runtime)
140061da546Spatrick m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr);
141061da546Spatrick return m_queue_kind;
142061da546Spatrick }
143061da546Spatrick }
144061da546Spatrick return eQueueKindUnknown;
145061da546Spatrick }
146061da546Spatrick
GetQueueID()147061da546Spatrick queue_id_t ThreadGDBRemote::GetQueueID() {
148061da546Spatrick // If our cached queue info is valid, then someone called
149061da546Spatrick // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned
150061da546Spatrick // from the stop reply packet. In this case we trust that the info is valid
151061da546Spatrick // in m_dispatch_queue_name without refetching it
152061da546Spatrick if (CachedQueueInfoIsValid())
153061da546Spatrick return m_queue_serial_number;
154061da546Spatrick
155061da546Spatrick if (m_associated_with_libdispatch_queue == eLazyBoolNo)
156061da546Spatrick return LLDB_INVALID_QUEUE_ID;
157061da546Spatrick
158061da546Spatrick if (m_thread_dispatch_qaddr != 0 &&
159061da546Spatrick m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
160061da546Spatrick ProcessSP process_sp(GetProcess());
161061da546Spatrick if (process_sp) {
162061da546Spatrick SystemRuntime *runtime = process_sp->GetSystemRuntime();
163061da546Spatrick if (runtime) {
164061da546Spatrick return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr);
165061da546Spatrick }
166061da546Spatrick }
167061da546Spatrick }
168061da546Spatrick return LLDB_INVALID_QUEUE_ID;
169061da546Spatrick }
170061da546Spatrick
GetQueue()171061da546Spatrick QueueSP ThreadGDBRemote::GetQueue() {
172061da546Spatrick queue_id_t queue_id = GetQueueID();
173061da546Spatrick QueueSP queue;
174061da546Spatrick if (queue_id != LLDB_INVALID_QUEUE_ID) {
175061da546Spatrick ProcessSP process_sp(GetProcess());
176061da546Spatrick if (process_sp) {
177061da546Spatrick queue = process_sp->GetQueueList().FindQueueByID(queue_id);
178061da546Spatrick }
179061da546Spatrick }
180061da546Spatrick return queue;
181061da546Spatrick }
182061da546Spatrick
GetQueueLibdispatchQueueAddress()183061da546Spatrick addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() {
184061da546Spatrick if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) {
185061da546Spatrick if (m_thread_dispatch_qaddr != 0 &&
186061da546Spatrick m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) {
187061da546Spatrick ProcessSP process_sp(GetProcess());
188061da546Spatrick if (process_sp) {
189061da546Spatrick SystemRuntime *runtime = process_sp->GetSystemRuntime();
190061da546Spatrick if (runtime) {
191061da546Spatrick m_dispatch_queue_t =
192061da546Spatrick runtime->GetLibdispatchQueueAddressFromThreadQAddress(
193061da546Spatrick m_thread_dispatch_qaddr);
194061da546Spatrick }
195061da546Spatrick }
196061da546Spatrick }
197061da546Spatrick }
198061da546Spatrick return m_dispatch_queue_t;
199061da546Spatrick }
200061da546Spatrick
SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t)201061da546Spatrick void ThreadGDBRemote::SetQueueLibdispatchQueueAddress(
202061da546Spatrick lldb::addr_t dispatch_queue_t) {
203061da546Spatrick m_dispatch_queue_t = dispatch_queue_t;
204061da546Spatrick }
205061da546Spatrick
ThreadHasQueueInformation() const206061da546Spatrick bool ThreadGDBRemote::ThreadHasQueueInformation() const {
207061da546Spatrick return m_thread_dispatch_qaddr != 0 &&
208061da546Spatrick m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS &&
209061da546Spatrick m_dispatch_queue_t != LLDB_INVALID_ADDRESS &&
210061da546Spatrick m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0;
211061da546Spatrick }
212061da546Spatrick
GetAssociatedWithLibdispatchQueue()213061da546Spatrick LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() {
214061da546Spatrick return m_associated_with_libdispatch_queue;
215061da546Spatrick }
216061da546Spatrick
SetAssociatedWithLibdispatchQueue(LazyBool associated_with_libdispatch_queue)217061da546Spatrick void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue(
218061da546Spatrick LazyBool associated_with_libdispatch_queue) {
219061da546Spatrick m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
220061da546Spatrick }
221061da546Spatrick
FetchThreadExtendedInfo()222061da546Spatrick StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() {
223061da546Spatrick StructuredData::ObjectSP object_sp;
224061da546Spatrick const lldb::user_id_t tid = GetProtocolID();
225*f6aab3d8Srobert Log *log = GetLog(GDBRLog::Thread);
226061da546Spatrick LLDB_LOGF(log, "Fetching extended information for thread %4.4" PRIx64, tid);
227061da546Spatrick ProcessSP process_sp(GetProcess());
228061da546Spatrick if (process_sp) {
229061da546Spatrick ProcessGDBRemote *gdb_process =
230061da546Spatrick static_cast<ProcessGDBRemote *>(process_sp.get());
231061da546Spatrick object_sp = gdb_process->GetExtendedInfoForThread(tid);
232061da546Spatrick }
233061da546Spatrick return object_sp;
234061da546Spatrick }
235061da546Spatrick
WillResume(StateType resume_state)236061da546Spatrick void ThreadGDBRemote::WillResume(StateType resume_state) {
237061da546Spatrick int signo = GetResumeSignal();
238061da546Spatrick const lldb::user_id_t tid = GetProtocolID();
239*f6aab3d8Srobert Log *log = GetLog(GDBRLog::Thread);
240061da546Spatrick LLDB_LOGF(log, "Resuming thread: %4.4" PRIx64 " with state: %s.", tid,
241061da546Spatrick StateAsCString(resume_state));
242061da546Spatrick
243061da546Spatrick ProcessSP process_sp(GetProcess());
244061da546Spatrick if (process_sp) {
245061da546Spatrick ProcessGDBRemote *gdb_process =
246061da546Spatrick static_cast<ProcessGDBRemote *>(process_sp.get());
247061da546Spatrick switch (resume_state) {
248061da546Spatrick case eStateSuspended:
249061da546Spatrick case eStateStopped:
250061da546Spatrick // Don't append anything for threads that should stay stopped.
251061da546Spatrick break;
252061da546Spatrick
253061da546Spatrick case eStateRunning:
254061da546Spatrick if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
255061da546Spatrick gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
256061da546Spatrick else
257061da546Spatrick gdb_process->m_continue_c_tids.push_back(tid);
258061da546Spatrick break;
259061da546Spatrick
260061da546Spatrick case eStateStepping:
261061da546Spatrick if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
262061da546Spatrick gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
263061da546Spatrick else
264061da546Spatrick gdb_process->m_continue_s_tids.push_back(tid);
265061da546Spatrick break;
266061da546Spatrick
267061da546Spatrick default:
268061da546Spatrick break;
269061da546Spatrick }
270061da546Spatrick }
271061da546Spatrick }
272061da546Spatrick
RefreshStateAfterStop()273061da546Spatrick void ThreadGDBRemote::RefreshStateAfterStop() {
274061da546Spatrick // Invalidate all registers in our register context. We don't set "force" to
275061da546Spatrick // true because the stop reply packet might have had some register values
276061da546Spatrick // that were expedited and these will already be copied into the register
277061da546Spatrick // context by the time this function gets called. The
278061da546Spatrick // GDBRemoteRegisterContext class has been made smart enough to detect when
279061da546Spatrick // it needs to invalidate which registers are valid by putting hooks in the
280061da546Spatrick // register read and register supply functions where they check the process
281061da546Spatrick // stop ID and do the right thing.
282061da546Spatrick const bool force = false;
283061da546Spatrick GetRegisterContext()->InvalidateIfNeeded(force);
284061da546Spatrick }
285061da546Spatrick
ThreadIDIsValid(lldb::tid_t thread)286061da546Spatrick bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) {
287061da546Spatrick return thread != 0;
288061da546Spatrick }
289061da546Spatrick
Dump(Log * log,uint32_t index)290061da546Spatrick void ThreadGDBRemote::Dump(Log *log, uint32_t index) {}
291061da546Spatrick
ShouldStop(bool & step_more)292061da546Spatrick bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; }
GetRegisterContext()293061da546Spatrick lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() {
294061da546Spatrick if (!m_reg_context_sp)
295061da546Spatrick m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
296061da546Spatrick return m_reg_context_sp;
297061da546Spatrick }
298061da546Spatrick
299061da546Spatrick lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)300061da546Spatrick ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) {
301061da546Spatrick lldb::RegisterContextSP reg_ctx_sp;
302061da546Spatrick uint32_t concrete_frame_idx = 0;
303061da546Spatrick
304061da546Spatrick if (frame)
305061da546Spatrick concrete_frame_idx = frame->GetConcreteFrameIndex();
306061da546Spatrick
307061da546Spatrick if (concrete_frame_idx == 0) {
308061da546Spatrick ProcessSP process_sp(GetProcess());
309061da546Spatrick if (process_sp) {
310061da546Spatrick ProcessGDBRemote *gdb_process =
311061da546Spatrick static_cast<ProcessGDBRemote *>(process_sp.get());
312061da546Spatrick bool pSupported =
313061da546Spatrick gdb_process->GetGDBRemote().GetpPacketSupported(GetID());
314061da546Spatrick bool read_all_registers_at_once =
315061da546Spatrick !pSupported || gdb_process->m_use_g_packet_for_reading;
316061da546Spatrick bool write_all_registers_at_once = !pSupported;
317061da546Spatrick reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>(
318be691f3bSpatrick *this, concrete_frame_idx, m_reg_info_sp, read_all_registers_at_once,
319be691f3bSpatrick write_all_registers_at_once);
320061da546Spatrick }
321061da546Spatrick } else {
322dda28197Spatrick reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
323061da546Spatrick }
324061da546Spatrick return reg_ctx_sp;
325061da546Spatrick }
326061da546Spatrick
PrivateSetRegisterValue(uint32_t reg,llvm::ArrayRef<uint8_t> data)327061da546Spatrick bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg,
328061da546Spatrick llvm::ArrayRef<uint8_t> data) {
329061da546Spatrick GDBRemoteRegisterContext *gdb_reg_ctx =
330061da546Spatrick static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
331061da546Spatrick assert(gdb_reg_ctx);
332061da546Spatrick return gdb_reg_ctx->PrivateSetRegisterValue(reg, data);
333061da546Spatrick }
334061da546Spatrick
PrivateSetRegisterValue(uint32_t reg,uint64_t regval)335061da546Spatrick bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) {
336061da546Spatrick GDBRemoteRegisterContext *gdb_reg_ctx =
337061da546Spatrick static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get());
338061da546Spatrick assert(gdb_reg_ctx);
339061da546Spatrick return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval);
340061da546Spatrick }
341061da546Spatrick
CalculateStopInfo()342061da546Spatrick bool ThreadGDBRemote::CalculateStopInfo() {
343061da546Spatrick ProcessSP process_sp(GetProcess());
344061da546Spatrick if (process_sp)
345061da546Spatrick return static_cast<ProcessGDBRemote *>(process_sp.get())
346061da546Spatrick ->CalculateThreadStopInfo(this);
347061da546Spatrick return false;
348061da546Spatrick }
349*f6aab3d8Srobert
350*f6aab3d8Srobert llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
GetSiginfo(size_t max_size) const351*f6aab3d8Srobert ThreadGDBRemote::GetSiginfo(size_t max_size) const {
352*f6aab3d8Srobert ProcessSP process_sp(GetProcess());
353*f6aab3d8Srobert if (!process_sp)
354*f6aab3d8Srobert return llvm::createStringError(llvm::inconvertibleErrorCode(),
355*f6aab3d8Srobert "no process");
356*f6aab3d8Srobert ProcessGDBRemote *gdb_process =
357*f6aab3d8Srobert static_cast<ProcessGDBRemote *>(process_sp.get());
358*f6aab3d8Srobert if (!gdb_process->m_gdb_comm.GetQXferSigInfoReadSupported())
359*f6aab3d8Srobert return llvm::createStringError(llvm::inconvertibleErrorCode(),
360*f6aab3d8Srobert "qXfer:siginfo:read not supported");
361*f6aab3d8Srobert
362*f6aab3d8Srobert llvm::Expected<std::string> response =
363*f6aab3d8Srobert gdb_process->m_gdb_comm.ReadExtFeature("siginfo", "");
364*f6aab3d8Srobert if (!response)
365*f6aab3d8Srobert return response.takeError();
366*f6aab3d8Srobert
367*f6aab3d8Srobert return llvm::MemoryBuffer::getMemBufferCopy(response.get());
368*f6aab3d8Srobert }
369