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/Core/ArchSpec.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/StreamString.h"
16 #include "lldb/Core/State.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/RegisterContext.h"
19 #include "lldb/Target/StopInfo.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Target/Unwind.h"
22 #include "lldb/Breakpoint/Watchpoint.h"
24 #include "ProcessGDBRemote.h"
25 #include "ProcessGDBRemoteLog.h"
26 #include "Utility/StringExtractorGDBRemote.h"
29 using namespace lldb_private;
31 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
35 ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
38 m_dispatch_queue_name (),
39 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
41 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
47 ThreadGDBRemote::~ThreadGDBRemote ()
49 ProcessSP process_sp(GetProcess());
50 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
52 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
58 ThreadGDBRemote::GetName ()
60 if (m_thread_name.empty())
62 return m_thread_name.c_str();
67 ThreadGDBRemote::GetQueueName ()
69 // Always re-fetch the dispatch queue name since it can change
71 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
73 ProcessSP process_sp (GetProcess());
76 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
77 return gdb_process->GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
84 ThreadGDBRemote::WillResume (StateType resume_state)
86 int signo = GetResumeSignal();
87 const lldb::user_id_t tid = GetProtocolID();
88 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
90 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
92 ProcessSP process_sp (GetProcess());
95 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
100 // Don't append anything for threads that should stay stopped.
104 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
105 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
107 gdb_process->m_continue_c_tids.push_back(tid);
111 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
112 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
114 gdb_process->m_continue_s_tids.push_back(tid);
124 ThreadGDBRemote::RefreshStateAfterStop()
126 // Invalidate all registers in our register context. We don't set "force" to
127 // true because the stop reply packet might have had some register values
128 // that were expedited and these will already be copied into the register
129 // context by the time this function gets called. The GDBRemoteRegisterContext
130 // class has been made smart enough to detect when it needs to invalidate
131 // which registers are valid by putting hooks in the register read and
132 // register supply functions where they check the process stop ID and do
134 const bool force = false;
135 GetRegisterContext()->InvalidateIfNeeded (force);
139 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
145 ThreadGDBRemote::Dump(Log *log, uint32_t index)
151 ThreadGDBRemote::ShouldStop (bool &step_more)
155 lldb::RegisterContextSP
156 ThreadGDBRemote::GetRegisterContext ()
158 if (m_reg_context_sp.get() == NULL)
159 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
160 return m_reg_context_sp;
163 lldb::RegisterContextSP
164 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
166 lldb::RegisterContextSP reg_ctx_sp;
167 const bool read_all_registers_at_once = false;
168 uint32_t concrete_frame_idx = 0;
171 concrete_frame_idx = frame->GetConcreteFrameIndex ();
174 if (concrete_frame_idx == 0)
176 ProcessSP process_sp (GetProcess());
179 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
180 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
185 Unwind *unwinder = GetUnwinder ();
187 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
193 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
195 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
196 assert (gdb_reg_ctx);
197 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
201 ThreadGDBRemote::CalculateStopInfo ()
203 ProcessSP process_sp (GetProcess());
206 StringExtractorGDBRemote stop_packet;
207 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
208 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
209 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;