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 11 #include "ThreadGDBRemote.h" 12 13 #include "lldb/Breakpoint/Watchpoint.h" 14 #include "lldb/Core/ArchSpec.h" 15 #include "lldb/Core/DataExtractor.h" 16 #include "lldb/Core/State.h" 17 #include "lldb/Core/StreamString.h" 18 #include "lldb/Target/Platform.h" 19 #include "lldb/Target/Process.h" 20 #include "lldb/Target/RegisterContext.h" 21 #include "lldb/Target/StopInfo.h" 22 #include "lldb/Target/SystemRuntime.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Target/UnixSignals.h" 25 #include "lldb/Target/Unwind.h" 26 27 #include "ProcessGDBRemote.h" 28 #include "ProcessGDBRemoteLog.h" 29 #include "Utility/StringExtractorGDBRemote.h" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 34 //---------------------------------------------------------------------- 35 // Thread Registers 36 //---------------------------------------------------------------------- 37 38 ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) : 39 Thread(process, tid), 40 m_thread_name (), 41 m_dispatch_queue_name (), 42 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) 43 { 44 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 45 this, 46 process.GetID(), 47 GetID()); 48 } 49 50 ThreadGDBRemote::~ThreadGDBRemote () 51 { 52 ProcessSP process_sp(GetProcess()); 53 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 54 this, 55 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, 56 GetID()); 57 DestroyThread(); 58 } 59 60 const char * 61 ThreadGDBRemote::GetName () 62 { 63 if (m_thread_name.empty()) 64 return NULL; 65 return m_thread_name.c_str(); 66 } 67 68 69 const char * 70 ThreadGDBRemote::GetQueueName () 71 { 72 // Always re-fetch the dispatch queue name since it can change 73 74 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 75 { 76 ProcessSP process_sp (GetProcess()); 77 if (process_sp) 78 { 79 SystemRuntime *runtime = process_sp->GetSystemRuntime (); 80 if (runtime) 81 { 82 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr); 83 } 84 if (m_dispatch_queue_name.length() > 0) 85 { 86 return m_dispatch_queue_name.c_str(); 87 } 88 } 89 } 90 return NULL; 91 } 92 93 queue_id_t 94 ThreadGDBRemote::GetQueueID () 95 { 96 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 97 { 98 ProcessSP process_sp (GetProcess()); 99 if (process_sp) 100 { 101 SystemRuntime *runtime = process_sp->GetSystemRuntime (); 102 if (runtime) 103 { 104 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr); 105 } 106 } 107 } 108 return LLDB_INVALID_QUEUE_ID; 109 } 110 111 QueueSP 112 ThreadGDBRemote::GetQueue () 113 { 114 queue_id_t queue_id = GetQueueID(); 115 QueueSP queue; 116 if (queue_id != LLDB_INVALID_QUEUE_ID) 117 { 118 ProcessSP process_sp (GetProcess()); 119 if (process_sp) 120 { 121 queue = process_sp->GetQueueList().FindQueueByID (queue_id); 122 } 123 } 124 return queue; 125 } 126 127 addr_t 128 ThreadGDBRemote::GetQueueLibdispatchQueueAddress () 129 { 130 addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS; 131 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 132 { 133 ProcessSP process_sp (GetProcess()); 134 if (process_sp) 135 { 136 SystemRuntime *runtime = process_sp->GetSystemRuntime (); 137 if (runtime) 138 { 139 dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr); 140 } 141 } 142 } 143 return dispatch_queue_t_addr; 144 } 145 146 StructuredData::ObjectSP 147 ThreadGDBRemote::FetchThreadExtendedInfo () 148 { 149 StructuredData::ObjectSP object_sp; 150 const lldb::user_id_t tid = GetProtocolID(); 151 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); 152 if (log) 153 log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid); 154 ProcessSP process_sp (GetProcess()); 155 if (process_sp) 156 { 157 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 158 object_sp = gdb_process->GetExtendedInfoForThread (tid); 159 } 160 return object_sp; 161 } 162 163 void 164 ThreadGDBRemote::WillResume (StateType resume_state) 165 { 166 int signo = GetResumeSignal(); 167 const lldb::user_id_t tid = GetProtocolID(); 168 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); 169 if (log) 170 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state)); 171 172 ProcessSP process_sp (GetProcess()); 173 if (process_sp) 174 { 175 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 176 switch (resume_state) 177 { 178 case eStateSuspended: 179 case eStateStopped: 180 // Don't append anything for threads that should stay stopped. 181 break; 182 183 case eStateRunning: 184 if (gdb_process->GetUnixSignals().SignalIsValid (signo)) 185 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 186 else 187 gdb_process->m_continue_c_tids.push_back(tid); 188 break; 189 190 case eStateStepping: 191 if (gdb_process->GetUnixSignals().SignalIsValid (signo)) 192 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 193 else 194 gdb_process->m_continue_s_tids.push_back(tid); 195 break; 196 197 default: 198 break; 199 } 200 } 201 } 202 203 void 204 ThreadGDBRemote::RefreshStateAfterStop() 205 { 206 // Invalidate all registers in our register context. We don't set "force" to 207 // true because the stop reply packet might have had some register values 208 // that were expedited and these will already be copied into the register 209 // context by the time this function gets called. The GDBRemoteRegisterContext 210 // class has been made smart enough to detect when it needs to invalidate 211 // which registers are valid by putting hooks in the register read and 212 // register supply functions where they check the process stop ID and do 213 // the right thing. 214 const bool force = false; 215 GetRegisterContext()->InvalidateIfNeeded (force); 216 } 217 218 bool 219 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) 220 { 221 return thread != 0; 222 } 223 224 void 225 ThreadGDBRemote::Dump(Log *log, uint32_t index) 226 { 227 } 228 229 230 bool 231 ThreadGDBRemote::ShouldStop (bool &step_more) 232 { 233 return true; 234 } 235 lldb::RegisterContextSP 236 ThreadGDBRemote::GetRegisterContext () 237 { 238 if (m_reg_context_sp.get() == NULL) 239 m_reg_context_sp = CreateRegisterContextForFrame (NULL); 240 return m_reg_context_sp; 241 } 242 243 lldb::RegisterContextSP 244 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) 245 { 246 lldb::RegisterContextSP reg_ctx_sp; 247 uint32_t concrete_frame_idx = 0; 248 249 if (frame) 250 concrete_frame_idx = frame->GetConcreteFrameIndex (); 251 252 253 if (concrete_frame_idx == 0) 254 { 255 ProcessSP process_sp (GetProcess()); 256 if (process_sp) 257 { 258 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 259 // read_all_registers_at_once will be true if 'p' packet is not supported. 260 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID()); 261 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once)); 262 } 263 } 264 else 265 { 266 Unwind *unwinder = GetUnwinder (); 267 if (unwinder) 268 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); 269 } 270 return reg_ctx_sp; 271 } 272 273 bool 274 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) 275 { 276 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); 277 assert (gdb_reg_ctx); 278 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); 279 } 280 281 bool 282 ThreadGDBRemote::CalculateStopInfo () 283 { 284 ProcessSP process_sp (GetProcess()); 285 if (process_sp) 286 { 287 StringExtractorGDBRemote stop_packet; 288 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 289 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet)) 290 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped; 291 } 292 return false; 293 } 294 295 296