ThreadGDBRemote.cpp revision 321369
1254721Semaste//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "ThreadGDBRemote.h" 11254721Semaste 12258054Semaste#include "lldb/Breakpoint/Watchpoint.h" 13254721Semaste#include "lldb/Core/ArchSpec.h" 14258054Semaste#include "lldb/Core/State.h" 15258054Semaste#include "lldb/Target/Platform.h" 16254721Semaste#include "lldb/Target/Process.h" 17254721Semaste#include "lldb/Target/RegisterContext.h" 18254721Semaste#include "lldb/Target/StopInfo.h" 19262528Semaste#include "lldb/Target/SystemRuntime.h" 20254721Semaste#include "lldb/Target/Target.h" 21288943Sdim#include "lldb/Target/UnixSignals.h" 22254721Semaste#include "lldb/Target/Unwind.h" 23321369Sdim#include "lldb/Utility/DataExtractor.h" 24321369Sdim#include "lldb/Utility/StreamString.h" 25254721Semaste 26254721Semaste#include "ProcessGDBRemote.h" 27254721Semaste#include "ProcessGDBRemoteLog.h" 28254721Semaste#include "Utility/StringExtractorGDBRemote.h" 29254721Semaste 30254721Semasteusing namespace lldb; 31254721Semasteusing namespace lldb_private; 32288943Sdimusing namespace lldb_private::process_gdb_remote; 33254721Semaste 34254721Semaste//---------------------------------------------------------------------- 35254721Semaste// Thread Registers 36254721Semaste//---------------------------------------------------------------------- 37254721Semaste 38314564SdimThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid) 39314564Sdim : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(), 40314564Sdim m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS), 41314564Sdim m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown), 42314564Sdim m_queue_serial_number(LLDB_INVALID_QUEUE_ID), 43314564Sdim m_associated_with_libdispatch_queue(eLazyBoolCalculate) { 44321369Sdim Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 45321369Sdim LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(), 46321369Sdim GetID()); 47254721Semaste} 48254721Semaste 49314564SdimThreadGDBRemote::~ThreadGDBRemote() { 50314564Sdim ProcessSP process_sp(GetProcess()); 51321369Sdim Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 52321369Sdim LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, 53321369Sdim process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID()); 54314564Sdim DestroyThread(); 55254721Semaste} 56254721Semaste 57314564Sdimconst char *ThreadGDBRemote::GetName() { 58314564Sdim if (m_thread_name.empty()) 59314564Sdim return NULL; 60314564Sdim return m_thread_name.c_str(); 61254721Semaste} 62254721Semaste 63314564Sdimvoid ThreadGDBRemote::ClearQueueInfo() { 64314564Sdim m_dispatch_queue_name.clear(); 65314564Sdim m_queue_kind = eQueueKindUnknown; 66314564Sdim m_queue_serial_number = 0; 67314564Sdim m_dispatch_queue_t = LLDB_INVALID_ADDRESS; 68314564Sdim m_associated_with_libdispatch_queue = eLazyBoolCalculate; 69288943Sdim} 70254721Semaste 71314564Sdimvoid ThreadGDBRemote::SetQueueInfo(std::string &&queue_name, 72314564Sdim QueueKind queue_kind, uint64_t queue_serial, 73314564Sdim addr_t dispatch_queue_t, 74314564Sdim LazyBool associated_with_libdispatch_queue) { 75314564Sdim m_dispatch_queue_name = queue_name; 76314564Sdim m_queue_kind = queue_kind; 77314564Sdim m_queue_serial_number = queue_serial; 78314564Sdim m_dispatch_queue_t = dispatch_queue_t; 79314564Sdim m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 80288943Sdim} 81288943Sdim 82314564Sdimconst char *ThreadGDBRemote::GetQueueName() { 83314564Sdim // If our cached queue info is valid, then someone called 84314564Sdim // ThreadGDBRemote::SetQueueInfo(...) 85314564Sdim // with valid information that was gleaned from the stop reply packet. In this 86314564Sdim // case we trust 87314564Sdim // that the info is valid in m_dispatch_queue_name without refetching it 88314564Sdim if (CachedQueueInfoIsValid()) { 89314564Sdim if (m_dispatch_queue_name.empty()) 90314564Sdim return nullptr; 91314564Sdim else 92314564Sdim return m_dispatch_queue_name.c_str(); 93314564Sdim } 94314564Sdim // Always re-fetch the dispatch queue name since it can change 95288943Sdim 96314564Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 97314564Sdim return nullptr; 98254721Semaste 99314564Sdim if (m_thread_dispatch_qaddr != 0 && 100314564Sdim m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 101314564Sdim ProcessSP process_sp(GetProcess()); 102314564Sdim if (process_sp) { 103314564Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime(); 104314564Sdim if (runtime) 105314564Sdim m_dispatch_queue_name = 106314564Sdim runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr); 107314564Sdim else 108314564Sdim m_dispatch_queue_name.clear(); 109296417Sdim 110314564Sdim if (!m_dispatch_queue_name.empty()) 111314564Sdim return m_dispatch_queue_name.c_str(); 112254721Semaste } 113314564Sdim } 114314564Sdim return NULL; 115254721Semaste} 116254721Semaste 117314564SdimQueueKind ThreadGDBRemote::GetQueueKind() { 118314564Sdim // If our cached queue info is valid, then someone called 119314564Sdim // ThreadGDBRemote::SetQueueInfo(...) 120314564Sdim // with valid information that was gleaned from the stop reply packet. In this 121314564Sdim // case we trust 122314564Sdim // that the info is valid in m_dispatch_queue_name without refetching it 123314564Sdim if (CachedQueueInfoIsValid()) { 124314564Sdim return m_queue_kind; 125314564Sdim } 126296417Sdim 127314564Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 128314564Sdim return eQueueKindUnknown; 129296417Sdim 130314564Sdim if (m_thread_dispatch_qaddr != 0 && 131314564Sdim m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 132314564Sdim ProcessSP process_sp(GetProcess()); 133314564Sdim if (process_sp) { 134314564Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime(); 135314564Sdim if (runtime) 136314564Sdim m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr); 137314564Sdim return m_queue_kind; 138296417Sdim } 139314564Sdim } 140314564Sdim return eQueueKindUnknown; 141296417Sdim} 142296417Sdim 143314564Sdimqueue_id_t ThreadGDBRemote::GetQueueID() { 144314564Sdim // If our cached queue info is valid, then someone called 145314564Sdim // ThreadGDBRemote::SetQueueInfo(...) 146314564Sdim // with valid information that was gleaned from the stop reply packet. In this 147314564Sdim // case we trust 148314564Sdim // that the info is valid in m_dispatch_queue_name without refetching it 149314564Sdim if (CachedQueueInfoIsValid()) 150314564Sdim return m_queue_serial_number; 151296417Sdim 152314564Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 153314564Sdim return LLDB_INVALID_QUEUE_ID; 154288943Sdim 155314564Sdim if (m_thread_dispatch_qaddr != 0 && 156314564Sdim m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 157314564Sdim ProcessSP process_sp(GetProcess()); 158314564Sdim if (process_sp) { 159314564Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime(); 160314564Sdim if (runtime) { 161314564Sdim return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr); 162314564Sdim } 163258054Semaste } 164314564Sdim } 165314564Sdim return LLDB_INVALID_QUEUE_ID; 166258054Semaste} 167258054Semaste 168314564SdimQueueSP ThreadGDBRemote::GetQueue() { 169314564Sdim queue_id_t queue_id = GetQueueID(); 170314564Sdim QueueSP queue; 171314564Sdim if (queue_id != LLDB_INVALID_QUEUE_ID) { 172314564Sdim ProcessSP process_sp(GetProcess()); 173314564Sdim if (process_sp) { 174314564Sdim queue = process_sp->GetQueueList().FindQueueByID(queue_id); 175276479Sdim } 176314564Sdim } 177314564Sdim return queue; 178276479Sdim} 179276479Sdim 180314564Sdimaddr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() { 181314564Sdim if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) { 182314564Sdim if (m_thread_dispatch_qaddr != 0 && 183314564Sdim m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 184314564Sdim ProcessSP process_sp(GetProcess()); 185314564Sdim if (process_sp) { 186314564Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime(); 187314564Sdim if (runtime) { 188314564Sdim m_dispatch_queue_t = 189314564Sdim runtime->GetLibdispatchQueueAddressFromThreadQAddress( 190314564Sdim m_thread_dispatch_qaddr); 191276479Sdim } 192314564Sdim } 193276479Sdim } 194314564Sdim } 195314564Sdim return m_dispatch_queue_t; 196276479Sdim} 197276479Sdim 198314564Sdimvoid ThreadGDBRemote::SetQueueLibdispatchQueueAddress( 199314564Sdim lldb::addr_t dispatch_queue_t) { 200314564Sdim m_dispatch_queue_t = dispatch_queue_t; 201296417Sdim} 202296417Sdim 203314564Sdimbool ThreadGDBRemote::ThreadHasQueueInformation() const { 204314564Sdim if (m_thread_dispatch_qaddr != 0 && 205314564Sdim m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS && 206314564Sdim m_dispatch_queue_t != LLDB_INVALID_ADDRESS && 207314564Sdim m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) { 208314564Sdim return true; 209314564Sdim } 210314564Sdim return false; 211296417Sdim} 212296417Sdim 213314564SdimLazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() { 214314564Sdim return m_associated_with_libdispatch_queue; 215296417Sdim} 216296417Sdim 217314564Sdimvoid ThreadGDBRemote::SetAssociatedWithLibdispatchQueue( 218314564Sdim LazyBool associated_with_libdispatch_queue) { 219314564Sdim m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 220296417Sdim} 221296417Sdim 222314564SdimStructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() { 223314564Sdim StructuredData::ObjectSP object_sp; 224314564Sdim const lldb::user_id_t tid = GetProtocolID(); 225314564Sdim Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 226314564Sdim if (log) 227314564Sdim log->Printf("Fetching extended information for thread %4.4" PRIx64, tid); 228314564Sdim ProcessSP process_sp(GetProcess()); 229314564Sdim if (process_sp) { 230314564Sdim ProcessGDBRemote *gdb_process = 231314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 232314564Sdim object_sp = gdb_process->GetExtendedInfoForThread(tid); 233314564Sdim } 234314564Sdim return object_sp; 235276479Sdim} 236276479Sdim 237314564Sdimvoid ThreadGDBRemote::WillResume(StateType resume_state) { 238314564Sdim int signo = GetResumeSignal(); 239314564Sdim const lldb::user_id_t tid = GetProtocolID(); 240314564Sdim Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 241314564Sdim if (log) 242314564Sdim log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, 243314564Sdim StateAsCString(resume_state)); 244254721Semaste 245314564Sdim ProcessSP process_sp(GetProcess()); 246314564Sdim if (process_sp) { 247314564Sdim ProcessGDBRemote *gdb_process = 248314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 249314564Sdim switch (resume_state) { 250314564Sdim case eStateSuspended: 251314564Sdim case eStateStopped: 252314564Sdim // Don't append anything for threads that should stay stopped. 253314564Sdim break; 254254721Semaste 255314564Sdim case eStateRunning: 256314564Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 257314564Sdim gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 258314564Sdim else 259314564Sdim gdb_process->m_continue_c_tids.push_back(tid); 260314564Sdim break; 261254721Semaste 262314564Sdim case eStateStepping: 263314564Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 264314564Sdim gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 265314564Sdim else 266314564Sdim gdb_process->m_continue_s_tids.push_back(tid); 267314564Sdim break; 268254721Semaste 269314564Sdim default: 270314564Sdim break; 271254721Semaste } 272314564Sdim } 273254721Semaste} 274254721Semaste 275314564Sdimvoid ThreadGDBRemote::RefreshStateAfterStop() { 276314564Sdim // Invalidate all registers in our register context. We don't set "force" to 277314564Sdim // true because the stop reply packet might have had some register values 278314564Sdim // that were expedited and these will already be copied into the register 279314564Sdim // context by the time this function gets called. The GDBRemoteRegisterContext 280314564Sdim // class has been made smart enough to detect when it needs to invalidate 281314564Sdim // which registers are valid by putting hooks in the register read and 282314564Sdim // register supply functions where they check the process stop ID and do 283314564Sdim // the right thing. 284314564Sdim const bool force = false; 285314564Sdim GetRegisterContext()->InvalidateIfNeeded(force); 286254721Semaste} 287254721Semaste 288314564Sdimbool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) { 289314564Sdim return thread != 0; 290254721Semaste} 291254721Semaste 292314564Sdimvoid ThreadGDBRemote::Dump(Log *log, uint32_t index) {} 293254721Semaste 294314564Sdimbool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; } 295314564Sdimlldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() { 296314564Sdim if (m_reg_context_sp.get() == NULL) 297314564Sdim m_reg_context_sp = CreateRegisterContextForFrame(NULL); 298314564Sdim return m_reg_context_sp; 299254721Semaste} 300254721Semaste 301254721Semastelldb::RegisterContextSP 302314564SdimThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) { 303314564Sdim lldb::RegisterContextSP reg_ctx_sp; 304314564Sdim uint32_t concrete_frame_idx = 0; 305254721Semaste 306314564Sdim if (frame) 307314564Sdim concrete_frame_idx = frame->GetConcreteFrameIndex(); 308314564Sdim 309314564Sdim if (concrete_frame_idx == 0) { 310314564Sdim ProcessSP process_sp(GetProcess()); 311314564Sdim if (process_sp) { 312314564Sdim ProcessGDBRemote *gdb_process = 313314564Sdim static_cast<ProcessGDBRemote *>(process_sp.get()); 314314564Sdim // read_all_registers_at_once will be true if 'p' packet is not supported. 315314564Sdim bool read_all_registers_at_once = 316314564Sdim !gdb_process->GetGDBRemote().GetpPacketSupported(GetID()); 317314564Sdim reg_ctx_sp.reset(new GDBRemoteRegisterContext( 318314564Sdim *this, concrete_frame_idx, gdb_process->m_register_info, 319314564Sdim read_all_registers_at_once)); 320254721Semaste } 321314564Sdim } else { 322314564Sdim Unwind *unwinder = GetUnwinder(); 323314564Sdim if (unwinder) 324314564Sdim reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); 325314564Sdim } 326314564Sdim return reg_ctx_sp; 327254721Semaste} 328254721Semaste 329314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, 330314564Sdim llvm::ArrayRef<uint8_t> data) { 331314564Sdim GDBRemoteRegisterContext *gdb_reg_ctx = 332314564Sdim static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 333314564Sdim assert(gdb_reg_ctx); 334314564Sdim return gdb_reg_ctx->PrivateSetRegisterValue(reg, data); 335254721Semaste} 336254721Semaste 337314564Sdimbool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) { 338314564Sdim GDBRemoteRegisterContext *gdb_reg_ctx = 339314564Sdim static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 340314564Sdim assert(gdb_reg_ctx); 341314564Sdim return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval); 342296417Sdim} 343296417Sdim 344314564Sdimbool ThreadGDBRemote::CalculateStopInfo() { 345314564Sdim ProcessSP process_sp(GetProcess()); 346314564Sdim if (process_sp) 347314564Sdim return static_cast<ProcessGDBRemote *>(process_sp.get()) 348314564Sdim ->CalculateThreadStopInfo(this); 349314564Sdim return false; 350254721Semaste} 351