1 //===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ThreadGDBRemote.h"
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/Unwind.h"
26 #include "ProcessGDBRemote.h"
27 #include "ProcessGDBRemoteLog.h"
28 #include "Utility/StringExtractorGDBRemote.h"
31 using namespace lldb_private;
33 //----------------------------------------------------------------------
35 //----------------------------------------------------------------------
37 ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
40 m_dispatch_queue_name (),
41 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
43 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
49 ThreadGDBRemote::~ThreadGDBRemote ()
51 ProcessSP process_sp(GetProcess());
52 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
54 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
60 ThreadGDBRemote::GetName ()
62 if (m_thread_name.empty())
64 return m_thread_name.c_str();
69 ThreadGDBRemote::GetQueueName ()
71 // Always re-fetch the dispatch queue name since it can change
73 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
75 ProcessSP process_sp (GetProcess());
78 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
81 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
83 if (m_dispatch_queue_name.length() > 0)
85 return m_dispatch_queue_name.c_str();
93 ThreadGDBRemote::GetQueueID ()
95 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
97 ProcessSP process_sp (GetProcess());
100 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
103 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
107 return LLDB_INVALID_QUEUE_ID;
111 ThreadGDBRemote::GetQueue ()
113 queue_id_t queue_id = GetQueueID();
115 if (queue_id != LLDB_INVALID_QUEUE_ID)
117 ProcessSP process_sp (GetProcess());
120 queue = process_sp->GetQueueList().FindQueueByID (queue_id);
127 ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
129 addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS;
130 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
132 ProcessSP process_sp (GetProcess());
135 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
138 dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
142 return dispatch_queue_t_addr;
145 StructuredData::ObjectSP
146 ThreadGDBRemote::FetchThreadExtendedInfo ()
148 StructuredData::ObjectSP object_sp;
149 const lldb::user_id_t tid = GetProtocolID();
150 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
152 log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid);
153 ProcessSP process_sp (GetProcess());
156 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
157 object_sp = gdb_process->GetExtendedInfoForThread (tid);
163 ThreadGDBRemote::WillResume (StateType resume_state)
165 int signo = GetResumeSignal();
166 const lldb::user_id_t tid = GetProtocolID();
167 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
169 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
171 ProcessSP process_sp (GetProcess());
174 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
175 switch (resume_state)
177 case eStateSuspended:
179 // Don't append anything for threads that should stay stopped.
183 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
184 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
186 gdb_process->m_continue_c_tids.push_back(tid);
190 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
191 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
193 gdb_process->m_continue_s_tids.push_back(tid);
203 ThreadGDBRemote::RefreshStateAfterStop()
205 // Invalidate all registers in our register context. We don't set "force" to
206 // true because the stop reply packet might have had some register values
207 // that were expedited and these will already be copied into the register
208 // context by the time this function gets called. The GDBRemoteRegisterContext
209 // class has been made smart enough to detect when it needs to invalidate
210 // which registers are valid by putting hooks in the register read and
211 // register supply functions where they check the process stop ID and do
213 const bool force = false;
214 GetRegisterContext()->InvalidateIfNeeded (force);
218 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
224 ThreadGDBRemote::Dump(Log *log, uint32_t index)
230 ThreadGDBRemote::ShouldStop (bool &step_more)
234 lldb::RegisterContextSP
235 ThreadGDBRemote::GetRegisterContext ()
237 if (m_reg_context_sp.get() == NULL)
238 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
239 return m_reg_context_sp;
242 lldb::RegisterContextSP
243 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
245 lldb::RegisterContextSP reg_ctx_sp;
246 uint32_t concrete_frame_idx = 0;
249 concrete_frame_idx = frame->GetConcreteFrameIndex ();
252 if (concrete_frame_idx == 0)
254 ProcessSP process_sp (GetProcess());
257 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
258 // read_all_registers_at_once will be true if 'p' packet is not supported.
259 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
260 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
265 Unwind *unwinder = GetUnwinder ();
267 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
273 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
275 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
276 assert (gdb_reg_ctx);
277 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
281 ThreadGDBRemote::CalculateStopInfo ()
283 ProcessSP process_sp (GetProcess());
286 StringExtractorGDBRemote stop_packet;
287 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
288 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
289 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;