blob: 9467801500ee5b5b4e126747b4652152e775fce9 [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"
16#include "lldb/Target/Process.h"
17#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000018#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/Target.h"
20#include "lldb/Target/Unwind.h"
21#include "lldb/Breakpoint/WatchpointLocation.h"
22
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "ProcessGDBRemote.h"
24#include "ProcessGDBRemoteLog.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000025#include "Utility/StringExtractorGDBRemote.h"
Jason Molenda8132f2d2010-11-04 09:51:29 +000026#include "UnwindLLDB.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
Greg Clayton8d110ff2011-02-05 06:35:06 +000028#ifdef __APPLE__
29#include "UnwindMacOSXFrameBackchain.h"
30#endif
31
Chris Lattner24943d22010-06-08 16:52:24 +000032using namespace lldb;
33using namespace lldb_private;
34
35//----------------------------------------------------------------------
36// Thread Registers
37//----------------------------------------------------------------------
38
39ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
40 Thread(process, tid),
Chris Lattner24943d22010-06-08 16:52:24 +000041 m_thread_name (),
42 m_dispatch_queue_name (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000043 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +000044{
45// ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD | GDBR_LOG_VERBOSE, "ThreadGDBRemote::ThreadGDBRemote ( pid = %i, tid = 0x%4.4x, )", m_process.GetID(), GetID());
46 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
47}
48
49ThreadGDBRemote::~ThreadGDBRemote ()
50{
51 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 +000052 DestroyThread();
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
55
56const char *
57ThreadGDBRemote::GetInfo ()
58{
59 return NULL;
60}
61
62
63const char *
64ThreadGDBRemote::GetName ()
65{
66 if (m_thread_name.empty())
67 return NULL;
68 return m_thread_name.c_str();
69}
70
71
72const char *
73ThreadGDBRemote::GetQueueName ()
74{
75 // Always re-fetch the dispatch queue name since it can change
76 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
77 return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
78 return NULL;
79}
80
81bool
82ThreadGDBRemote::WillResume (StateType resume_state)
83{
Chris Lattner24943d22010-06-08 16:52:24 +000084 ClearStackFrames();
Greg Clayton8f6be2a2010-10-09 01:40:57 +000085 // Call the Thread::WillResume first. If we stop at a signal, the stop info
86 // class for signal will set the resume signal that we need below. The signal
87 // stuff obeys the Process::UnixSignal defaults.
88 Thread::WillResume(resume_state);
89
Chris Lattner24943d22010-06-08 16:52:24 +000090 int signo = GetResumeSignal();
Greg Clayton643ee732010-08-04 01:40:35 +000091
Chris Lattner24943d22010-06-08 16:52:24 +000092 switch (resume_state)
93 {
94 case eStateSuspended:
95 case eStateStopped:
96 // Don't append anything for threads that should stay stopped.
97 break;
98
99 case eStateRunning:
100 if (m_process.GetUnixSignals().SignalIsValid (signo))
101 GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
102 else
103 GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
104 break;
105
106 case eStateStepping:
107 if (m_process.GetUnixSignals().SignalIsValid (signo))
108 GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
109 else
110 GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
111 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000112
113 default:
114 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000115 }
Chris Lattner24943d22010-06-08 16:52:24 +0000116 return true;
117}
118
119void
120ThreadGDBRemote::RefreshStateAfterStop()
121{
Greg Claytona875b642011-01-09 21:07:35 +0000122 // Invalidate all registers in our register context. We don't set "force" to
123 // true because the stop reply packet might have had some register values
124 // that were expedited and these will already be copied into the register
125 // context by the time this function gets called. The GDBRemoteRegisterContext
126 // class has been made smart enough to detect when it needs to invalidate
127 // which registers are valid by putting hooks in the register read and
128 // register supply functions where they check the process stop ID and do
129 // the right thing.
130 const bool force = false;
131 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner24943d22010-06-08 16:52:24 +0000132}
133
134Unwind *
135ThreadGDBRemote::GetUnwinder ()
136{
137 if (m_unwinder_ap.get() == NULL)
138 {
139 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
140 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
141 {
Jason Molenda8132f2d2010-11-04 09:51:29 +0000142 m_unwinder_ap.reset (new UnwindLLDB (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000143 }
Greg Clayton8d110ff2011-02-05 06:35:06 +0000144#ifdef __APPLE__
Chris Lattner24943d22010-06-08 16:52:24 +0000145 else
146 {
147 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
148 }
Greg Clayton8d110ff2011-02-05 06:35:06 +0000149#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000150 }
151 return m_unwinder_ap.get();
152}
153
Chris Lattner24943d22010-06-08 16:52:24 +0000154void
155ThreadGDBRemote::ClearStackFrames ()
156{
157 Unwind *unwinder = GetUnwinder ();
158 if (unwinder)
159 unwinder->Clear();
160 Thread::ClearStackFrames();
161}
162
163
164bool
165ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
166{
167 return thread != 0;
168}
169
170void
171ThreadGDBRemote::Dump(Log *log, uint32_t index)
172{
173}
174
175
176bool
177ThreadGDBRemote::ShouldStop (bool &step_more)
178{
179 return true;
180}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000181lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000182ThreadGDBRemote::GetRegisterContext ()
183{
184 if (m_reg_context_sp.get() == NULL)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000185 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
186 return m_reg_context_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000187}
188
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000189lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000190ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
191{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000192 lldb::RegisterContextSP reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000193 const bool read_all_registers_at_once = false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000194 uint32_t concrete_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000195
196 if (frame)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000197 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000198
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000199 if (concrete_frame_idx == 0)
200 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 +0000201 else if (m_unwinder_ap.get())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000202 reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
203 return reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000204}
205
Greg Claytona875b642011-01-09 21:07:35 +0000206void
207ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
208{
209 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
210 assert (gdb_reg_ctx);
211 gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
212}
213
Chris Lattner24943d22010-06-08 16:52:24 +0000214bool
215ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
216{
217 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
218 if (frame_sp)
219 {
220 checkpoint.SetStackID(frame_sp->GetStackID());
221 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
222 }
223 return false;
224}
225
226bool
227ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
228{
229 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
230 if (frame_sp)
231 {
232 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
Greg Claytona875b642011-01-09 21:07:35 +0000233 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000234 ClearStackFrames();
235 return ret;
236 }
237 return false;
238}
239
Greg Clayton643ee732010-08-04 01:40:35 +0000240lldb::StopInfoSP
241ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000242{
Greg Claytona875b642011-01-09 21:07:35 +0000243 const uint32_t process_stop_id = GetProcess().GetStopID();
244 if (m_thread_stop_reason_stop_id != process_stop_id ||
245 (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
Chris Lattner24943d22010-06-08 16:52:24 +0000246 {
Greg Claytona875b642011-01-09 21:07:35 +0000247 // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
248 // for this thread, then m_actual_stop_info_sp will not ever contain
249 // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
250 // check will never be able to tell us if we have the correct stop info
251 // for this thread and we will continually send qThreadStopInfo packets
252 // down to the remote GDB server, so we need to keep our own notion
253 // of the stop ID that m_actual_stop_info_sp is valid for (even if it
254 // contains nothing). We use m_thread_stop_reason_stop_id for this below.
255 m_thread_stop_reason_stop_id = process_stop_id;
Greg Clayton643ee732010-08-04 01:40:35 +0000256 m_actual_stop_info_sp.reset();
257
Chris Lattner24943d22010-06-08 16:52:24 +0000258 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000259 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000260 StringExtractorGDBRemote stop_packet;
261 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
262 {
Chris Lattner24943d22010-06-08 16:52:24 +0000263 GetGDBProcess().SetThreadStopInfo (stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +0000264 }
265 }
Greg Clayton643ee732010-08-04 01:40:35 +0000266 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000267}
268
269