ThreadGDBRemote.cpp revision 341825
1//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "ThreadGDBRemote.h" 11 12#include "lldb/Breakpoint/Watchpoint.h" 13#include "lldb/Core/State.h" 14#include "lldb/Target/Platform.h" 15#include "lldb/Target/Process.h" 16#include "lldb/Target/RegisterContext.h" 17#include "lldb/Target/StopInfo.h" 18#include "lldb/Target/SystemRuntime.h" 19#include "lldb/Target/Target.h" 20#include "lldb/Target/UnixSignals.h" 21#include "lldb/Target/Unwind.h" 22#include "lldb/Utility/DataExtractor.h" 23#include "lldb/Utility/StreamString.h" 24 25#include "ProcessGDBRemote.h" 26#include "ProcessGDBRemoteLog.h" 27#include "lldb/Utility/StringExtractorGDBRemote.h" 28 29using namespace lldb; 30using namespace lldb_private; 31using namespace lldb_private::process_gdb_remote; 32 33//---------------------------------------------------------------------- 34// Thread Registers 35//---------------------------------------------------------------------- 36 37ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid) 38 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(), 39 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS), 40 m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown), 41 m_queue_serial_number(LLDB_INVALID_QUEUE_ID), 42 m_associated_with_libdispatch_queue(eLazyBoolCalculate) { 43 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 44 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, process.GetID(), 45 GetID()); 46} 47 48ThreadGDBRemote::~ThreadGDBRemote() { 49 ProcessSP process_sp(GetProcess()); 50 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 51 LLDB_LOG(log, "this = {0}, pid = {1}, tid = {2}", this, 52 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID()); 53 DestroyThread(); 54} 55 56const char *ThreadGDBRemote::GetName() { 57 if (m_thread_name.empty()) 58 return nullptr; 59 return m_thread_name.c_str(); 60} 61 62void ThreadGDBRemote::ClearQueueInfo() { 63 m_dispatch_queue_name.clear(); 64 m_queue_kind = eQueueKindUnknown; 65 m_queue_serial_number = 0; 66 m_dispatch_queue_t = LLDB_INVALID_ADDRESS; 67 m_associated_with_libdispatch_queue = eLazyBoolCalculate; 68} 69 70void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name, 71 QueueKind queue_kind, uint64_t queue_serial, 72 addr_t dispatch_queue_t, 73 LazyBool associated_with_libdispatch_queue) { 74 m_dispatch_queue_name = queue_name; 75 m_queue_kind = queue_kind; 76 m_queue_serial_number = queue_serial; 77 m_dispatch_queue_t = dispatch_queue_t; 78 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 79} 80 81const char *ThreadGDBRemote::GetQueueName() { 82 // If our cached queue info is valid, then someone called 83 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned 84 // from the stop reply packet. In this case we trust that the info is valid 85 // in m_dispatch_queue_name without refetching it 86 if (CachedQueueInfoIsValid()) { 87 if (m_dispatch_queue_name.empty()) 88 return nullptr; 89 else 90 return m_dispatch_queue_name.c_str(); 91 } 92 // Always re-fetch the dispatch queue name since it can change 93 94 if (m_associated_with_libdispatch_queue == eLazyBoolNo) 95 return nullptr; 96 97 if (m_thread_dispatch_qaddr != 0 && 98 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 99 ProcessSP process_sp(GetProcess()); 100 if (process_sp) { 101 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 102 if (runtime) 103 m_dispatch_queue_name = 104 runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr); 105 else 106 m_dispatch_queue_name.clear(); 107 108 if (!m_dispatch_queue_name.empty()) 109 return m_dispatch_queue_name.c_str(); 110 } 111 } 112 return nullptr; 113} 114 115QueueKind ThreadGDBRemote::GetQueueKind() { 116 // If our cached queue info is valid, then someone called 117 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned 118 // from the stop reply packet. In this case we trust that the info is valid 119 // in m_dispatch_queue_name without refetching it 120 if (CachedQueueInfoIsValid()) { 121 return m_queue_kind; 122 } 123 124 if (m_associated_with_libdispatch_queue == eLazyBoolNo) 125 return eQueueKindUnknown; 126 127 if (m_thread_dispatch_qaddr != 0 && 128 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 129 ProcessSP process_sp(GetProcess()); 130 if (process_sp) { 131 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 132 if (runtime) 133 m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr); 134 return m_queue_kind; 135 } 136 } 137 return eQueueKindUnknown; 138} 139 140queue_id_t ThreadGDBRemote::GetQueueID() { 141 // If our cached queue info is valid, then someone called 142 // ThreadGDBRemote::SetQueueInfo(...) with valid information that was gleaned 143 // from the stop reply packet. In this case we trust that the info is valid 144 // in m_dispatch_queue_name without refetching it 145 if (CachedQueueInfoIsValid()) 146 return m_queue_serial_number; 147 148 if (m_associated_with_libdispatch_queue == eLazyBoolNo) 149 return LLDB_INVALID_QUEUE_ID; 150 151 if (m_thread_dispatch_qaddr != 0 && 152 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 153 ProcessSP process_sp(GetProcess()); 154 if (process_sp) { 155 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 156 if (runtime) { 157 return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr); 158 } 159 } 160 } 161 return LLDB_INVALID_QUEUE_ID; 162} 163 164QueueSP ThreadGDBRemote::GetQueue() { 165 queue_id_t queue_id = GetQueueID(); 166 QueueSP queue; 167 if (queue_id != LLDB_INVALID_QUEUE_ID) { 168 ProcessSP process_sp(GetProcess()); 169 if (process_sp) { 170 queue = process_sp->GetQueueList().FindQueueByID(queue_id); 171 } 172 } 173 return queue; 174} 175 176addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() { 177 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) { 178 if (m_thread_dispatch_qaddr != 0 && 179 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { 180 ProcessSP process_sp(GetProcess()); 181 if (process_sp) { 182 SystemRuntime *runtime = process_sp->GetSystemRuntime(); 183 if (runtime) { 184 m_dispatch_queue_t = 185 runtime->GetLibdispatchQueueAddressFromThreadQAddress( 186 m_thread_dispatch_qaddr); 187 } 188 } 189 } 190 } 191 return m_dispatch_queue_t; 192} 193 194void ThreadGDBRemote::SetQueueLibdispatchQueueAddress( 195 lldb::addr_t dispatch_queue_t) { 196 m_dispatch_queue_t = dispatch_queue_t; 197} 198 199bool ThreadGDBRemote::ThreadHasQueueInformation() const { 200 if (m_thread_dispatch_qaddr != 0 && 201 m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS && 202 m_dispatch_queue_t != LLDB_INVALID_ADDRESS && 203 m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) { 204 return true; 205 } 206 return false; 207} 208 209LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() { 210 return m_associated_with_libdispatch_queue; 211} 212 213void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue( 214 LazyBool associated_with_libdispatch_queue) { 215 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; 216} 217 218StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() { 219 StructuredData::ObjectSP object_sp; 220 const lldb::user_id_t tid = GetProtocolID(); 221 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 222 if (log) 223 log->Printf("Fetching extended information for thread %4.4" PRIx64, tid); 224 ProcessSP process_sp(GetProcess()); 225 if (process_sp) { 226 ProcessGDBRemote *gdb_process = 227 static_cast<ProcessGDBRemote *>(process_sp.get()); 228 object_sp = gdb_process->GetExtendedInfoForThread(tid); 229 } 230 return object_sp; 231} 232 233void ThreadGDBRemote::WillResume(StateType resume_state) { 234 int signo = GetResumeSignal(); 235 const lldb::user_id_t tid = GetProtocolID(); 236 Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); 237 if (log) 238 log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, 239 StateAsCString(resume_state)); 240 241 ProcessSP process_sp(GetProcess()); 242 if (process_sp) { 243 ProcessGDBRemote *gdb_process = 244 static_cast<ProcessGDBRemote *>(process_sp.get()); 245 switch (resume_state) { 246 case eStateSuspended: 247 case eStateStopped: 248 // Don't append anything for threads that should stay stopped. 249 break; 250 251 case eStateRunning: 252 if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 253 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 254 else 255 gdb_process->m_continue_c_tids.push_back(tid); 256 break; 257 258 case eStateStepping: 259 if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) 260 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 261 else 262 gdb_process->m_continue_s_tids.push_back(tid); 263 break; 264 265 default: 266 break; 267 } 268 } 269} 270 271void ThreadGDBRemote::RefreshStateAfterStop() { 272 // Invalidate all registers in our register context. We don't set "force" to 273 // true because the stop reply packet might have had some register values 274 // that were expedited and these will already be copied into the register 275 // context by the time this function gets called. The 276 // GDBRemoteRegisterContext class has been made smart enough to detect when 277 // it needs to invalidate which registers are valid by putting hooks in the 278 // register read and register supply functions where they check the process 279 // stop ID and do the right thing. 280 const bool force = false; 281 GetRegisterContext()->InvalidateIfNeeded(force); 282} 283 284bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) { 285 return thread != 0; 286} 287 288void ThreadGDBRemote::Dump(Log *log, uint32_t index) {} 289 290bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; } 291lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() { 292 if (!m_reg_context_sp) 293 m_reg_context_sp = CreateRegisterContextForFrame(nullptr); 294 return m_reg_context_sp; 295} 296 297lldb::RegisterContextSP 298ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) { 299 lldb::RegisterContextSP reg_ctx_sp; 300 uint32_t concrete_frame_idx = 0; 301 302 if (frame) 303 concrete_frame_idx = frame->GetConcreteFrameIndex(); 304 305 if (concrete_frame_idx == 0) { 306 ProcessSP process_sp(GetProcess()); 307 if (process_sp) { 308 ProcessGDBRemote *gdb_process = 309 static_cast<ProcessGDBRemote *>(process_sp.get()); 310 // read_all_registers_at_once will be true if 'p' packet is not 311 // supported. 312 bool read_all_registers_at_once = 313 !gdb_process->GetGDBRemote().GetpPacketSupported(GetID()); 314 reg_ctx_sp.reset(new GDBRemoteRegisterContext( 315 *this, concrete_frame_idx, gdb_process->m_register_info, 316 read_all_registers_at_once)); 317 } 318 } else { 319 Unwind *unwinder = GetUnwinder(); 320 if (unwinder != nullptr) 321 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); 322 } 323 return reg_ctx_sp; 324} 325 326bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, 327 llvm::ArrayRef<uint8_t> data) { 328 GDBRemoteRegisterContext *gdb_reg_ctx = 329 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 330 assert(gdb_reg_ctx); 331 return gdb_reg_ctx->PrivateSetRegisterValue(reg, data); 332} 333 334bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) { 335 GDBRemoteRegisterContext *gdb_reg_ctx = 336 static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); 337 assert(gdb_reg_ctx); 338 return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval); 339} 340 341bool ThreadGDBRemote::CalculateStopInfo() { 342 ProcessSP process_sp(GetProcess()); 343 if (process_sp) 344 return static_cast<ProcessGDBRemote *>(process_sp.get()) 345 ->CalculateThreadStopInfo(this); 346 return false; 347} 348