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/Target.h" 23 #include "lldb/Target/Unwind.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 32 //---------------------------------------------------------------------- 33 // Thread Registers 34 //---------------------------------------------------------------------- 35 36 ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) : 37 Thread(process, tid), 38 m_thread_name (), 39 m_dispatch_queue_name (), 40 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) 41 { 42 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 43 this, 44 process.GetID(), 45 GetID()); 46 } 47 48 ThreadGDBRemote::~ThreadGDBRemote () 49 { 50 ProcessSP process_sp(GetProcess()); 51 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", 52 this, 53 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, 54 GetID()); 55 DestroyThread(); 56 } 57 58 const char * 59 ThreadGDBRemote::GetName () 60 { 61 if (m_thread_name.empty()) 62 return NULL; 63 return m_thread_name.c_str(); 64 } 65 66 67 const char * 68 ThreadGDBRemote::GetQueueName () 69 { 70 // Always re-fetch the dispatch queue name since it can change 71 72 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 73 { 74 ProcessSP process_sp (GetProcess()); 75 if (process_sp) 76 { 77 PlatformSP platform_sp (process_sp->GetTarget().GetPlatform()); 78 if (platform_sp) 79 { 80 m_dispatch_queue_name = platform_sp->GetQueueNameForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr); 81 } 82 if (m_dispatch_queue_name.length() > 0) 83 { 84 return m_dispatch_queue_name.c_str(); 85 } 86 } 87 } 88 return NULL; 89 } 90 91 queue_id_t 92 ThreadGDBRemote::GetQueueID () 93 { 94 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) 95 { 96 ProcessSP process_sp (GetProcess()); 97 if (process_sp) 98 { 99 PlatformSP platform_sp (process_sp->GetTarget().GetPlatform()); 100 if (platform_sp) 101 { 102 return platform_sp->GetQueueIDForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr); 103 } 104 } 105 } 106 return LLDB_INVALID_QUEUE_ID; 107 } 108 109 void 110 ThreadGDBRemote::WillResume (StateType resume_state) 111 { 112 int signo = GetResumeSignal(); 113 const lldb::user_id_t tid = GetProtocolID(); 114 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); 115 if (log) 116 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state)); 117 118 ProcessSP process_sp (GetProcess()); 119 if (process_sp) 120 { 121 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 122 switch (resume_state) 123 { 124 case eStateSuspended: 125 case eStateStopped: 126 // Don't append anything for threads that should stay stopped. 127 break; 128 129 case eStateRunning: 130 if (gdb_process->GetUnixSignals().SignalIsValid (signo)) 131 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); 132 else 133 gdb_process->m_continue_c_tids.push_back(tid); 134 break; 135 136 case eStateStepping: 137 if (gdb_process->GetUnixSignals().SignalIsValid (signo)) 138 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); 139 else 140 gdb_process->m_continue_s_tids.push_back(tid); 141 break; 142 143 default: 144 break; 145 } 146 } 147 } 148 149 void 150 ThreadGDBRemote::RefreshStateAfterStop() 151 { 152 // Invalidate all registers in our register context. We don't set "force" to 153 // true because the stop reply packet might have had some register values 154 // that were expedited and these will already be copied into the register 155 // context by the time this function gets called. The GDBRemoteRegisterContext 156 // class has been made smart enough to detect when it needs to invalidate 157 // which registers are valid by putting hooks in the register read and 158 // register supply functions where they check the process stop ID and do 159 // the right thing. 160 const bool force = false; 161 GetRegisterContext()->InvalidateIfNeeded (force); 162 } 163 164 bool 165 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) 166 { 167 return thread != 0; 168 } 169 170 void 171 ThreadGDBRemote::Dump(Log *log, uint32_t index) 172 { 173 } 174 175 176 bool 177 ThreadGDBRemote::ShouldStop (bool &step_more) 178 { 179 return true; 180 } 181 lldb::RegisterContextSP 182 ThreadGDBRemote::GetRegisterContext () 183 { 184 if (m_reg_context_sp.get() == NULL) 185 m_reg_context_sp = CreateRegisterContextForFrame (NULL); 186 return m_reg_context_sp; 187 } 188 189 lldb::RegisterContextSP 190 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) 191 { 192 lldb::RegisterContextSP reg_ctx_sp; 193 uint32_t concrete_frame_idx = 0; 194 195 if (frame) 196 concrete_frame_idx = frame->GetConcreteFrameIndex (); 197 198 199 if (concrete_frame_idx == 0) 200 { 201 ProcessSP process_sp (GetProcess()); 202 if (process_sp) 203 { 204 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 205 // read_all_registers_at_once will be true if 'p' packet is not supported. 206 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID()); 207 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once)); 208 } 209 } 210 else 211 { 212 Unwind *unwinder = GetUnwinder (); 213 if (unwinder) 214 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); 215 } 216 return reg_ctx_sp; 217 } 218 219 bool 220 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) 221 { 222 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); 223 assert (gdb_reg_ctx); 224 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); 225 } 226 227 bool 228 ThreadGDBRemote::CalculateStopInfo () 229 { 230 ProcessSP process_sp (GetProcess()); 231 if (process_sp) 232 { 233 StringExtractorGDBRemote stop_packet; 234 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); 235 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet)) 236 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped; 237 } 238 return false; 239 } 240 241 242