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)); 218360784Sdim LLDB_LOGF(log, "Fetching extended information for thread %4.4" PRIx64, tid); 219314564Sdim ProcessSP process_sp(GetProcess()); 220314564Sdim if (process_sp) { 221314564Sdim ProcessGDBRemote *gdb_process = 222314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 223314564Sdim object_sp = gdb_process->GetExtendedInfoForThread(tid); 224314564Sdim } 225314564Sdim return object_sp; 226276479Sdim} 227276479Sdim 228314564Sdimvoid ThreadGDBRemote::WillResume(StateType resume_state) { 229314564Sdim int signo = GetResumeSignal(); 230314564Sdim const lldb::user_id_t tid = GetProtocolID(); 231314564Sdim Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 232360784Sdim LLDB_LOGF(log, "Resuming thread: %4.4" PRIx64 " with state: %s.", tid, 233360784Sdim StateAsCString(resume_state)); 234254721Semaste 235314564Sdim ProcessSP process_sp(GetProcess()); 236314564Sdim if (process_sp) { 237314564Sdim ProcessGDBRemote *gdb_process = 238314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 239314564Sdim switch (resume_state) { 240314564Sdim case eStateSuspended: 241314564Sdim case eStateStopped: 242314564Sdim // Don't append anything for threads that should stay stopped. 243314564Sdim break; 244254721Semaste 245314564Sdim case eStateRunning: 246314564Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 247314564Sdim gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 248314564Sdim else 249314564Sdim gdb_process->m_continue_c_tids.push_back(tid); 250314564Sdim break; 251254721Semaste 252314564Sdim case eStateStepping: 253314564Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 254314564Sdim gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 255314564Sdim else 256314564Sdim gdb_process->m_continue_s_tids.push_back(tid); 257314564Sdim break; 258254721Semaste 259314564Sdim default: 260314564Sdim break; 261254721Semaste } 262314564Sdim } 263254721Semaste} 264254721Semaste 265314564Sdimvoid ThreadGDBRemote::RefreshStateAfterStop() { 266314564Sdim // Invalidate all registers in our register context. We don't set "force" to 267314564Sdim // true because the stop reply packet might have had some register values 268314564Sdim // that were expedited and these will already be copied into the register 269341825Sdim // context by the time this function gets called. The 270341825Sdim // GDBRemoteRegisterContext class has been made smart enough to detect when 271341825Sdim // it needs to invalidate which registers are valid by putting hooks in the 272341825Sdim // register read and register supply functions where they check the process 273341825Sdim // stop ID and do the right thing. 274314564Sdim const bool force = false; 275314564Sdim GetRegisterContext()->InvalidateIfNeeded(force); 276254721Semaste} 277254721Semaste 278314564Sdimbool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) { 279314564Sdim return thread != 0; 280254721Semaste} 281254721Semaste 282314564Sdimvoid ThreadGDBRemote::Dump(Log *log, uint32_t index) {} 283254721Semaste 284314564Sdimbool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; } 285314564Sdimlldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() { 286341825Sdim if (!m_reg_context_sp) 287341825Sdim m_reg_context_sp = CreateRegisterContextForFrame(nullptr); 288314564Sdim return m_reg_context_sp; 289254721Semaste} 290254721Semaste 291254721Semastelldb::RegisterContextSP 292314564SdimThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) { 293314564Sdim lldb::RegisterContextSP reg_ctx_sp; 294314564Sdim uint32_t concrete_frame_idx = 0; 295254721Semaste 296314564Sdim if (frame) 297314564Sdim concrete_frame_idx = frame->GetConcreteFrameIndex(); 298314564Sdim 299314564Sdim if (concrete_frame_idx == 0) { 300314564Sdim ProcessSP process_sp(GetProcess()); 301314564Sdim if (process_sp) { 302314564Sdim ProcessGDBRemote *gdb_process = 303314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 304360784Sdim bool pSupported = 305360784Sdim gdb_process->GetGDBRemote().GetpPacketSupported(GetID()); 306314564Sdim bool read_all_registers_at_once = 307360784Sdim !pSupported || gdb_process->m_use_g_packet_for_reading; 308360784Sdim bool write_all_registers_at_once = !pSupported; 309353358Sdim reg_ctx_sp = std::make_shared<GDBRemoteRegisterContext>( 310314564Sdim *this, concrete_frame_idx, gdb_process->m_register_info, 311360784Sdim read_all_registers_at_once, write_all_registers_at_once); 312254721Semaste } 313314564Sdim } else { 314314564Sdim Unwind *unwinder = GetUnwinder(); 315341825Sdim if (unwinder != nullptr) 316314564Sdim reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); 317314564Sdim } 318314564Sdim return reg_ctx_sp; 319254721Semaste} 320254721Semaste 321314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, 322314564Sdim llvm::ArrayRef<uint8_t> data) { 323314564Sdim GDBRemoteRegisterContext *gdb_reg_ctx = 324314564Sdim static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 325314564Sdim assert(gdb_reg_ctx); 326314564Sdim return gdb_reg_ctx->PrivateSetRegisterValue(reg, data); 327254721Semaste} 328254721Semaste 329314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) { 330314564Sdim GDBRemoteRegisterContext *gdb_reg_ctx = 331314564Sdim static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 332314564Sdim assert(gdb_reg_ctx); 333314564Sdim return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval); 334296417Sdim} 335296417Sdim 336314564Sdimbool ThreadGDBRemote::CalculateStopInfo() { 337314564Sdim ProcessSP process_sp(GetProcess()); 338314564Sdim if (process_sp) 339314564Sdim return static_cast<ProcessGDBRemote *>(process_sp.get()) 340314564Sdim ->CalculateThreadStopInfo(this); 341314564Sdim return false; 342254721Semaste} 343