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