blob: 635c00c743890873eca05737aaad770aaba2dd94 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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/Core/ArchSpec.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/StreamString.h"
Jim Inghamf9f40c22011-02-08 05:20:59 +000016#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000019#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Target.h"
21#include "lldb/Target/Unwind.h"
22#include "lldb/Breakpoint/WatchpointLocation.h"
23
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "ProcessGDBRemote.h"
25#include "ProcessGDBRemoteLog.h"
Greg Claytonf29a08f2011-02-09 17:41:27 +000026#include "Plugins/Process/Utility/UnwindLLDB.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000027#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
Greg Claytonc3c46612011-02-15 00:19:15 +000029#if defined(__APPLE__)
Greg Clayton8d110ff2011-02-05 06:35:06 +000030#include "UnwindMacOSXFrameBackchain.h"
31#endif
32
Chris Lattner24943d22010-06-08 16:52:24 +000033using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// Thread Registers
38//----------------------------------------------------------------------
39
40ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
41 Thread(process, tid),
Chris Lattner24943d22010-06-08 16:52:24 +000042 m_thread_name (),
43 m_dispatch_queue_name (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000044 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +000045{
46// ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD | GDBR_LOG_VERBOSE, "ThreadGDBRemote::ThreadGDBRemote ( pid = %i, tid = 0x%4.4x, )", m_process.GetID(), GetID());
47 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
48}
49
50ThreadGDBRemote::~ThreadGDBRemote ()
51{
52 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +000053 DestroyThread();
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
56
57const char *
58ThreadGDBRemote::GetInfo ()
59{
60 return NULL;
61}
62
63
64const char *
65ThreadGDBRemote::GetName ()
66{
67 if (m_thread_name.empty())
68 return NULL;
69 return m_thread_name.c_str();
70}
71
72
73const char *
74ThreadGDBRemote::GetQueueName ()
75{
76 // Always re-fetch the dispatch queue name since it can change
77 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
78 return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
79 return NULL;
80}
81
82bool
83ThreadGDBRemote::WillResume (StateType resume_state)
84{
Chris Lattner24943d22010-06-08 16:52:24 +000085 ClearStackFrames();
Greg Clayton8f6be2a2010-10-09 01:40:57 +000086 // Call the Thread::WillResume first. If we stop at a signal, the stop info
87 // class for signal will set the resume signal that we need below. The signal
88 // stuff obeys the Process::UnixSignal defaults.
89 Thread::WillResume(resume_state);
90
Chris Lattner24943d22010-06-08 16:52:24 +000091 int signo = GetResumeSignal();
Jim Inghamf9f40c22011-02-08 05:20:59 +000092 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
93 if (log)
94 log->Printf ("Resuming thread: %4.4x with state: %s.", GetID(), StateAsCString(resume_state));
Greg Clayton643ee732010-08-04 01:40:35 +000095
Greg Claytonc1f45872011-02-12 06:28:37 +000096 ProcessGDBRemote &process = GetGDBProcess();
Chris Lattner24943d22010-06-08 16:52:24 +000097 switch (resume_state)
98 {
99 case eStateSuspended:
100 case eStateStopped:
Greg Claytonc3c46612011-02-15 00:19:15 +0000101 // Don't append anything for threads that should stay stopped.
Chris Lattner24943d22010-06-08 16:52:24 +0000102 break;
103
104 case eStateRunning:
105 if (m_process.GetUnixSignals().SignalIsValid (signo))
Greg Claytonc1f45872011-02-12 06:28:37 +0000106 process.m_continue_C_tids.push_back(std::make_pair(GetID(), signo));
Chris Lattner24943d22010-06-08 16:52:24 +0000107 else
Greg Claytonc1f45872011-02-12 06:28:37 +0000108 process.m_continue_c_tids.push_back(GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000109 break;
110
111 case eStateStepping:
112 if (m_process.GetUnixSignals().SignalIsValid (signo))
Greg Claytonc1f45872011-02-12 06:28:37 +0000113 process.m_continue_S_tids.push_back(std::make_pair(GetID(), signo));
Chris Lattner24943d22010-06-08 16:52:24 +0000114 else
Greg Claytonc1f45872011-02-12 06:28:37 +0000115 process.m_continue_s_tids.push_back(GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000116 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000117
118 default:
119 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000120 }
Chris Lattner24943d22010-06-08 16:52:24 +0000121 return true;
122}
123
124void
125ThreadGDBRemote::RefreshStateAfterStop()
126{
Greg Claytona875b642011-01-09 21:07:35 +0000127 // Invalidate all registers in our register context. We don't set "force" to
128 // true because the stop reply packet might have had some register values
129 // that were expedited and these will already be copied into the register
130 // context by the time this function gets called. The GDBRemoteRegisterContext
131 // class has been made smart enough to detect when it needs to invalidate
132 // which registers are valid by putting hooks in the register read and
133 // register supply functions where they check the process stop ID and do
134 // the right thing.
135 const bool force = false;
136 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner24943d22010-06-08 16:52:24 +0000137}
138
139Unwind *
140ThreadGDBRemote::GetUnwinder ()
141{
142 if (m_unwinder_ap.get() == NULL)
143 {
144 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
145 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
146 {
Jason Molenda8132f2d2010-11-04 09:51:29 +0000147 m_unwinder_ap.reset (new UnwindLLDB (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000148 }
Greg Claytonc3c46612011-02-15 00:19:15 +0000149#if defined(__APPLE__)
Chris Lattner24943d22010-06-08 16:52:24 +0000150 else
151 {
152 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
153 }
Greg Clayton8d110ff2011-02-05 06:35:06 +0000154#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000155 }
156 return m_unwinder_ap.get();
157}
158
Chris Lattner24943d22010-06-08 16:52:24 +0000159void
160ThreadGDBRemote::ClearStackFrames ()
161{
162 Unwind *unwinder = GetUnwinder ();
163 if (unwinder)
164 unwinder->Clear();
165 Thread::ClearStackFrames();
166}
167
168
169bool
170ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
171{
172 return thread != 0;
173}
174
175void
176ThreadGDBRemote::Dump(Log *log, uint32_t index)
177{
178}
179
180
181bool
182ThreadGDBRemote::ShouldStop (bool &step_more)
183{
184 return true;
185}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000186lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000187ThreadGDBRemote::GetRegisterContext ()
188{
189 if (m_reg_context_sp.get() == NULL)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000190 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
191 return m_reg_context_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000192}
193
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000194lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000195ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
196{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000197 lldb::RegisterContextSP reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000198 const bool read_all_registers_at_once = false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000199 uint32_t concrete_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000200
201 if (frame)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000202 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000203
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000204 if (concrete_frame_idx == 0)
205 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, GetGDBProcess().m_register_info, read_all_registers_at_once));
Greg Clayton33ed1702010-08-24 00:45:41 +0000206 else if (m_unwinder_ap.get())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000207 reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
208 return reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000209}
210
Greg Claytonc3c46612011-02-15 00:19:15 +0000211bool
Greg Claytona875b642011-01-09 21:07:35 +0000212ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
213{
214 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
215 assert (gdb_reg_ctx);
Greg Claytonc3c46612011-02-15 00:19:15 +0000216 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Claytona875b642011-01-09 21:07:35 +0000217}
218
Chris Lattner24943d22010-06-08 16:52:24 +0000219bool
220ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
221{
222 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
223 if (frame_sp)
224 {
225 checkpoint.SetStackID(frame_sp->GetStackID());
226 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
227 }
228 return false;
229}
230
231bool
232ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
233{
234 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
235 if (frame_sp)
236 {
237 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
Greg Claytona875b642011-01-09 21:07:35 +0000238 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000239 ClearStackFrames();
240 return ret;
241 }
242 return false;
243}
244
Greg Clayton643ee732010-08-04 01:40:35 +0000245lldb::StopInfoSP
246ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000247{
Greg Claytona875b642011-01-09 21:07:35 +0000248 const uint32_t process_stop_id = GetProcess().GetStopID();
249 if (m_thread_stop_reason_stop_id != process_stop_id ||
250 (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
Chris Lattner24943d22010-06-08 16:52:24 +0000251 {
Greg Claytona875b642011-01-09 21:07:35 +0000252 // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
253 // for this thread, then m_actual_stop_info_sp will not ever contain
254 // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
255 // check will never be able to tell us if we have the correct stop info
256 // for this thread and we will continually send qThreadStopInfo packets
257 // down to the remote GDB server, so we need to keep our own notion
258 // of the stop ID that m_actual_stop_info_sp is valid for (even if it
259 // contains nothing). We use m_thread_stop_reason_stop_id for this below.
260 m_thread_stop_reason_stop_id = process_stop_id;
Greg Clayton643ee732010-08-04 01:40:35 +0000261 m_actual_stop_info_sp.reset();
262
Chris Lattner24943d22010-06-08 16:52:24 +0000263 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000264 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000265 StringExtractorGDBRemote stop_packet;
266 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
267 {
Chris Lattner24943d22010-06-08 16:52:24 +0000268 GetGDBProcess().SetThreadStopInfo (stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +0000269 }
270 }
Greg Clayton643ee732010-08-04 01:40:35 +0000271 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000272}
273
274