ThreadGDBRemote.cpp revision 296417
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 11254721Semaste#include "ThreadGDBRemote.h" 12254721Semaste 13258054Semaste#include "lldb/Breakpoint/Watchpoint.h" 14254721Semaste#include "lldb/Core/ArchSpec.h" 15254721Semaste#include "lldb/Core/DataExtractor.h" 16258054Semaste#include "lldb/Core/State.h" 17254721Semaste#include "lldb/Core/StreamString.h" 18258054Semaste#include "lldb/Target/Platform.h" 19254721Semaste#include "lldb/Target/Process.h" 20254721Semaste#include "lldb/Target/RegisterContext.h" 21254721Semaste#include "lldb/Target/StopInfo.h" 22262528Semaste#include "lldb/Target/SystemRuntime.h" 23254721Semaste#include "lldb/Target/Target.h" 24288943Sdim#include "lldb/Target/UnixSignals.h" 25254721Semaste#include "lldb/Target/Unwind.h" 26254721Semaste 27254721Semaste#include "ProcessGDBRemote.h" 28254721Semaste#include "ProcessGDBRemoteLog.h" 29254721Semaste#include "Utility/StringExtractorGDBRemote.h" 30254721Semaste 31254721Semasteusing namespace lldb; 32254721Semasteusing namespace lldb_private; 33288943Sdimusing namespace lldb_private::process_gdb_remote; 34254721Semaste 35254721Semaste//---------------------------------------------------------------------- 36254721Semaste// Thread Registers 37254721Semaste//---------------------------------------------------------------------- 38254721Semaste 39254721SemasteThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) : 40254721Semaste Thread(process, tid), 41254721Semaste m_thread_name (), 42254721Semaste m_dispatch_queue_name (), 43288943Sdim m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS), 44296417Sdim m_dispatch_queue_t (LLDB_INVALID_ADDRESS), 45296417Sdim m_queue_kind (eQueueKindUnknown), 46296417Sdim m_queue_serial_number (LLDB_INVALID_QUEUE_ID), 47296417Sdim m_associated_with_libdispatch_queue (eLazyBoolCalculate) 48254721Semaste{ 49288943Sdim ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 50254721Semaste this, 51254721Semaste process.GetID(), 52254721Semaste GetID()); 53254721Semaste} 54254721Semaste 55254721SemasteThreadGDBRemote::~ThreadGDBRemote () 56254721Semaste{ 57254721Semaste ProcessSP process_sp(GetProcess()); 58254721Semaste ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 59254721Semaste this, 60254721Semaste process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, 61254721Semaste GetID()); 62254721Semaste DestroyThread(); 63254721Semaste} 64254721Semaste 65254721Semasteconst char * 66254721SemasteThreadGDBRemote::GetName () 67254721Semaste{ 68254721Semaste if (m_thread_name.empty()) 69254721Semaste return NULL; 70254721Semaste return m_thread_name.c_str(); 71254721Semaste} 72254721Semaste 73288943Sdimvoid 74288943SdimThreadGDBRemote::ClearQueueInfo () 75288943Sdim{ 76288943Sdim m_dispatch_queue_name.clear(); 77288943Sdim m_queue_kind = eQueueKindUnknown; 78296417Sdim m_queue_serial_number = 0; 79296417Sdim m_dispatch_queue_t = LLDB_INVALID_ADDRESS; 80296417Sdim m_associated_with_libdispatch_queue = eLazyBoolCalculate; 81288943Sdim} 82254721Semaste 83288943Sdimvoid 84296417SdimThreadGDBRemote::SetQueueInfo (std::string &&queue_name, QueueKind queue_kind, uint64_t queue_serial, addr_t dispatch_queue_t, LazyBool associated_with_libdispatch_queue) 85288943Sdim{ 86288943Sdim m_dispatch_queue_name = queue_name; 87288943Sdim m_queue_kind = queue_kind; 88296417Sdim m_queue_serial_number = queue_serial; 89296417Sdim m_dispatch_queue_t = dispatch_queue_t; 90296417Sdim m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 91288943Sdim} 92288943Sdim 93288943Sdim 94254721Semasteconst char * 95254721SemasteThreadGDBRemote::GetQueueName () 96254721Semaste{ 97288943Sdim // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...) 98288943Sdim // with valid information that was gleaned from the stop reply packet. In this case we trust 99288943Sdim // that the info is valid in m_dispatch_queue_name without refetching it 100288943Sdim if (CachedQueueInfoIsValid()) 101288943Sdim { 102288943Sdim if (m_dispatch_queue_name.empty()) 103288943Sdim return nullptr; 104288943Sdim else 105288943Sdim return m_dispatch_queue_name.c_str(); 106288943Sdim } 107254721Semaste // Always re-fetch the dispatch queue name since it can change 108254721Semaste 109296417Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 110296417Sdim return nullptr; 111296417Sdim 112296417Sdim if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 113254721Semaste { 114254721Semaste ProcessSP process_sp (GetProcess()); 115254721Semaste if (process_sp) 116254721Semaste { 117262528Semaste SystemRuntime *runtime = process_sp->GetSystemRuntime (); 118262528Semaste if (runtime) 119262528Semaste m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr); 120288943Sdim else 121288943Sdim m_dispatch_queue_name.clear(); 122288943Sdim 123288943Sdim if (!m_dispatch_queue_name.empty()) 124258054Semaste return m_dispatch_queue_name.c_str(); 125254721Semaste } 126254721Semaste } 127254721Semaste return NULL; 128254721Semaste} 129254721Semaste 130296417SdimQueueKind 131296417SdimThreadGDBRemote::GetQueueKind () 132296417Sdim{ 133296417Sdim // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...) 134296417Sdim // with valid information that was gleaned from the stop reply packet. In this case we trust 135296417Sdim // that the info is valid in m_dispatch_queue_name without refetching it 136296417Sdim if (CachedQueueInfoIsValid()) 137296417Sdim { 138296417Sdim return m_queue_kind; 139296417Sdim } 140296417Sdim 141296417Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 142296417Sdim return eQueueKindUnknown; 143296417Sdim 144296417Sdim if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 145296417Sdim { 146296417Sdim ProcessSP process_sp (GetProcess()); 147296417Sdim if (process_sp) 148296417Sdim { 149296417Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime (); 150296417Sdim if (runtime) 151296417Sdim m_queue_kind = runtime->GetQueueKind (m_thread_dispatch_qaddr); 152296417Sdim return m_queue_kind; 153296417Sdim } 154296417Sdim } 155296417Sdim return eQueueKindUnknown; 156296417Sdim} 157296417Sdim 158296417Sdim 159258054Semastequeue_id_t 160258054SemasteThreadGDBRemote::GetQueueID () 161258054Semaste{ 162288943Sdim // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...) 163288943Sdim // with valid information that was gleaned from the stop reply packet. In this case we trust 164288943Sdim // that the info is valid in m_dispatch_queue_name without refetching it 165288943Sdim if (CachedQueueInfoIsValid()) 166296417Sdim return m_queue_serial_number; 167288943Sdim 168296417Sdim if (m_associated_with_libdispatch_queue == eLazyBoolNo) 169296417Sdim return LLDB_INVALID_QUEUE_ID; 170296417Sdim 171296417Sdim if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 172258054Semaste { 173258054Semaste ProcessSP process_sp (GetProcess()); 174258054Semaste if (process_sp) 175258054Semaste { 176262528Semaste SystemRuntime *runtime = process_sp->GetSystemRuntime (); 177262528Semaste if (runtime) 178258054Semaste { 179262528Semaste return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr); 180258054Semaste } 181258054Semaste } 182258054Semaste } 183258054Semaste return LLDB_INVALID_QUEUE_ID; 184258054Semaste} 185258054Semaste 186276479SdimQueueSP 187276479SdimThreadGDBRemote::GetQueue () 188276479Sdim{ 189276479Sdim queue_id_t queue_id = GetQueueID(); 190276479Sdim QueueSP queue; 191276479Sdim if (queue_id != LLDB_INVALID_QUEUE_ID) 192276479Sdim { 193276479Sdim ProcessSP process_sp (GetProcess()); 194276479Sdim if (process_sp) 195276479Sdim { 196276479Sdim queue = process_sp->GetQueueList().FindQueueByID (queue_id); 197276479Sdim } 198276479Sdim } 199276479Sdim return queue; 200276479Sdim} 201276479Sdim 202276479Sdimaddr_t 203276479SdimThreadGDBRemote::GetQueueLibdispatchQueueAddress () 204276479Sdim{ 205296417Sdim if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) 206276479Sdim { 207296417Sdim if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 208276479Sdim { 209296417Sdim ProcessSP process_sp (GetProcess()); 210296417Sdim if (process_sp) 211276479Sdim { 212296417Sdim SystemRuntime *runtime = process_sp->GetSystemRuntime (); 213296417Sdim if (runtime) 214296417Sdim { 215296417Sdim m_dispatch_queue_t = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr); 216296417Sdim } 217276479Sdim } 218276479Sdim } 219276479Sdim } 220296417Sdim return m_dispatch_queue_t; 221276479Sdim} 222276479Sdim 223296417Sdimvoid 224296417SdimThreadGDBRemote::SetQueueLibdispatchQueueAddress (lldb::addr_t dispatch_queue_t) 225296417Sdim{ 226296417Sdim m_dispatch_queue_t = dispatch_queue_t; 227296417Sdim} 228296417Sdim 229296417Sdimbool 230296417SdimThreadGDBRemote::ThreadHasQueueInformation () const 231296417Sdim{ 232296417Sdim if (m_thread_dispatch_qaddr != 0 233296417Sdim && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS 234296417Sdim && m_dispatch_queue_t != LLDB_INVALID_ADDRESS 235296417Sdim && m_queue_kind != eQueueKindUnknown 236296417Sdim && m_queue_serial_number != 0) 237296417Sdim { 238296417Sdim return true; 239296417Sdim } 240296417Sdim return false; 241296417Sdim} 242296417Sdim 243296417SdimLazyBool 244296417SdimThreadGDBRemote::GetAssociatedWithLibdispatchQueue () 245296417Sdim{ 246296417Sdim return m_associated_with_libdispatch_queue; 247296417Sdim} 248296417Sdim 249296417Sdimvoid 250296417SdimThreadGDBRemote::SetAssociatedWithLibdispatchQueue (LazyBool associated_with_libdispatch_queue) 251296417Sdim{ 252296417Sdim m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 253296417Sdim} 254296417Sdim 255276479SdimStructuredData::ObjectSP 256276479SdimThreadGDBRemote::FetchThreadExtendedInfo () 257276479Sdim{ 258276479Sdim StructuredData::ObjectSP object_sp; 259276479Sdim const lldb::user_id_t tid = GetProtocolID(); 260288943Sdim Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); 261276479Sdim if (log) 262276479Sdim log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid); 263276479Sdim ProcessSP process_sp (GetProcess()); 264276479Sdim if (process_sp) 265276479Sdim { 266276479Sdim ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 267276479Sdim object_sp = gdb_process->GetExtendedInfoForThread (tid); 268276479Sdim } 269276479Sdim return object_sp; 270276479Sdim} 271276479Sdim 272254721Semastevoid 273254721SemasteThreadGDBRemote::WillResume (StateType resume_state) 274254721Semaste{ 275254721Semaste int signo = GetResumeSignal(); 276254721Semaste const lldb::user_id_t tid = GetProtocolID(); 277288943Sdim Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); 278254721Semaste if (log) 279254721Semaste log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state)); 280254721Semaste 281254721Semaste ProcessSP process_sp (GetProcess()); 282254721Semaste if (process_sp) 283254721Semaste { 284254721Semaste ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 285254721Semaste switch (resume_state) 286254721Semaste { 287254721Semaste case eStateSuspended: 288254721Semaste case eStateStopped: 289254721Semaste // Don't append anything for threads that should stay stopped. 290254721Semaste break; 291254721Semaste 292254721Semaste case eStateRunning: 293288943Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 294254721Semaste gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 295254721Semaste else 296254721Semaste gdb_process->m_continue_c_tids.push_back(tid); 297254721Semaste break; 298254721Semaste 299254721Semaste case eStateStepping: 300288943Sdim if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 301254721Semaste gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 302254721Semaste else 303254721Semaste gdb_process->m_continue_s_tids.push_back(tid); 304254721Semaste break; 305254721Semaste 306254721Semaste default: 307254721Semaste break; 308254721Semaste } 309254721Semaste } 310254721Semaste} 311254721Semaste 312254721Semastevoid 313254721SemasteThreadGDBRemote::RefreshStateAfterStop() 314254721Semaste{ 315254721Semaste // Invalidate all registers in our register context. We don't set "force" to 316254721Semaste // true because the stop reply packet might have had some register values 317254721Semaste // that were expedited and these will already be copied into the register 318254721Semaste // context by the time this function gets called. The GDBRemoteRegisterContext 319254721Semaste // class has been made smart enough to detect when it needs to invalidate 320254721Semaste // which registers are valid by putting hooks in the register read and 321254721Semaste // register supply functions where they check the process stop ID and do 322254721Semaste // the right thing. 323254721Semaste const bool force = false; 324254721Semaste GetRegisterContext()->InvalidateIfNeeded (force); 325254721Semaste} 326254721Semaste 327254721Semastebool 328254721SemasteThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) 329254721Semaste{ 330254721Semaste return thread != 0; 331254721Semaste} 332254721Semaste 333254721Semastevoid 334254721SemasteThreadGDBRemote::Dump(Log *log, uint32_t index) 335254721Semaste{ 336254721Semaste} 337254721Semaste 338254721Semaste 339254721Semastebool 340254721SemasteThreadGDBRemote::ShouldStop (bool &step_more) 341254721Semaste{ 342254721Semaste return true; 343254721Semaste} 344254721Semastelldb::RegisterContextSP 345254721SemasteThreadGDBRemote::GetRegisterContext () 346254721Semaste{ 347254721Semaste if (m_reg_context_sp.get() == NULL) 348254721Semaste m_reg_context_sp = CreateRegisterContextForFrame (NULL); 349254721Semaste return m_reg_context_sp; 350254721Semaste} 351254721Semaste 352254721Semastelldb::RegisterContextSP 353254721SemasteThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) 354254721Semaste{ 355254721Semaste lldb::RegisterContextSP reg_ctx_sp; 356254721Semaste uint32_t concrete_frame_idx = 0; 357254721Semaste 358254721Semaste if (frame) 359254721Semaste concrete_frame_idx = frame->GetConcreteFrameIndex (); 360254721Semaste 361254721Semaste 362254721Semaste if (concrete_frame_idx == 0) 363254721Semaste { 364254721Semaste ProcessSP process_sp (GetProcess()); 365254721Semaste if (process_sp) 366254721Semaste { 367254721Semaste ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 368258054Semaste // read_all_registers_at_once will be true if 'p' packet is not supported. 369258054Semaste bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID()); 370254721Semaste reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once)); 371254721Semaste } 372254721Semaste } 373254721Semaste else 374254721Semaste { 375254721Semaste Unwind *unwinder = GetUnwinder (); 376254721Semaste if (unwinder) 377254721Semaste reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); 378254721Semaste } 379254721Semaste return reg_ctx_sp; 380254721Semaste} 381254721Semaste 382254721Semastebool 383254721SemasteThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) 384254721Semaste{ 385254721Semaste GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); 386254721Semaste assert (gdb_reg_ctx); 387254721Semaste return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); 388254721Semaste} 389254721Semaste 390254721Semastebool 391296417SdimThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, uint64_t regval) 392296417Sdim{ 393296417Sdim GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); 394296417Sdim assert (gdb_reg_ctx); 395296417Sdim return gdb_reg_ctx->PrivateSetRegisterValue (reg, regval); 396296417Sdim} 397296417Sdim 398296417Sdimbool 399254721SemasteThreadGDBRemote::CalculateStopInfo () 400254721Semaste{ 401254721Semaste ProcessSP process_sp (GetProcess()); 402254721Semaste if (process_sp) 403288943Sdim return static_cast<ProcessGDBRemote *>(process_sp.get())->CalculateThreadStopInfo(this); 404254721Semaste return false; 405254721Semaste} 406254721Semaste 407254721Semaste 408