blob: 37485edf4e5d0355d6392623b4bd8ce865cdb7b1 [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"
18#include "lldb/Target/Target.h"
19#include "lldb/Target/Unwind.h"
20#include "lldb/Breakpoint/WatchpointLocation.h"
21
22#include "LibUnwindRegisterContext.h"
23#include "ProcessGDBRemote.h"
24#include "ProcessGDBRemoteLog.h"
25#include "StringExtractorGDBRemote.h"
26#include "UnwindLibUnwind.h"
27#include "UnwindMacOSXFrameBackchain.h"
28
29using namespace lldb;
30using namespace lldb_private;
31
32//----------------------------------------------------------------------
33// Thread Registers
34//----------------------------------------------------------------------
35
36ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
37 Thread(process, tid),
38 m_stop_info_stop_id (0),
39 m_stop_info (this),
40 m_thread_name (),
41 m_dispatch_queue_name (),
42 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
43 m_unwinder_ap ()
44{
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());
52}
53
54
55const char *
56ThreadGDBRemote::GetInfo ()
57{
58 return NULL;
59}
60
61
62const char *
63ThreadGDBRemote::GetName ()
64{
65 if (m_thread_name.empty())
66 return NULL;
67 return m_thread_name.c_str();
68}
69
70
71const char *
72ThreadGDBRemote::GetQueueName ()
73{
74 // Always re-fetch the dispatch queue name since it can change
75 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
76 return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
77 return NULL;
78}
79
80bool
81ThreadGDBRemote::WillResume (StateType resume_state)
82{
83 // TODO: cache for next time in case we can match things up??
84 ClearStackFrames();
85 int signo = GetResumeSignal();
86 m_stop_info.Clear();
87 switch (resume_state)
88 {
89 case eStateSuspended:
90 case eStateStopped:
91 // Don't append anything for threads that should stay stopped.
92 break;
93
94 case eStateRunning:
95 if (m_process.GetUnixSignals().SignalIsValid (signo))
96 GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
97 else
98 GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
99 break;
100
101 case eStateStepping:
102 if (m_process.GetUnixSignals().SignalIsValid (signo))
103 GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
104 else
105 GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
106 break;
107 }
108 Thread::WillResume(resume_state);
109 return true;
110}
111
112void
113ThreadGDBRemote::RefreshStateAfterStop()
114{
115 // Invalidate all registers in our register context
116 GetRegisterContext()->Invalidate();
117}
118
119Unwind *
120ThreadGDBRemote::GetUnwinder ()
121{
122 if (m_unwinder_ap.get() == NULL)
123 {
124 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
125 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
126 {
127 m_unwinder_ap.reset (new UnwindLibUnwind (*this, GetGDBProcess().GetLibUnwindAddressSpace()));
128 }
129 else
130 {
131 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
132 }
133 }
134 return m_unwinder_ap.get();
135}
136
137uint32_t
138ThreadGDBRemote::GetStackFrameCount()
139{
140 Unwind *unwinder = GetUnwinder ();
141 if (unwinder)
142 return unwinder->GetFrameCount();
143 return 0;
144}
145
146// Make sure that GetStackFrameAtIndex() does NOT call GetStackFrameCount() when
147// getting the stack frame at index zero! This way GetStackFrameCount() (via
148// GetStackFRameData()) can call this function to get the first frame in order
149// to provide the first frame to a lower call for efficiency sake (avoid
150// redundant lookups in the frame symbol context).
151lldb::StackFrameSP
152ThreadGDBRemote::GetStackFrameAtIndex (uint32_t idx)
153{
154
155 StackFrameSP frame_sp (m_frames.GetFrameAtIndex(idx));
156
157 if (frame_sp.get())
158 return frame_sp;
159
160 // Don't try and fetch a frame while process is running
161// FIXME: This check isn't right because IsRunning checks the Public state, but this
162// is work you need to do - for instance in ShouldStop & friends - before the public
163// state has been changed.
164// if (m_process.IsRunning())
165// return frame_sp;
166
167 // Special case the first frame (idx == 0) so that we don't need to
168 // know how many stack frames there are to get it. If we need any other
169 // frames, then we do need to know if "idx" is a valid index.
170 if (idx == 0)
171 {
172 // If this is the first frame, we want to share the thread register
173 // context with the stack frame at index zero.
174 GetRegisterContext();
175 assert (m_reg_context_sp.get());
176 frame_sp.reset (new StackFrame (idx, *this, m_reg_context_sp, m_reg_context_sp->GetSP(), m_reg_context_sp->GetPC()));
177 }
178 else if (idx < GetStackFrameCount())
179 {
180 Unwind *unwinder = GetUnwinder ();
181 if (unwinder)
182 {
183 addr_t pc, cfa;
184 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
185 frame_sp.reset (new StackFrame (idx, *this, cfa, pc));
186 }
187 }
188 m_frames.SetFrameAtIndex(idx, frame_sp);
189 return frame_sp;
190}
191
192void
193ThreadGDBRemote::ClearStackFrames ()
194{
195 Unwind *unwinder = GetUnwinder ();
196 if (unwinder)
197 unwinder->Clear();
198 Thread::ClearStackFrames();
199}
200
201
202bool
203ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
204{
205 return thread != 0;
206}
207
208void
209ThreadGDBRemote::Dump(Log *log, uint32_t index)
210{
211}
212
213
214bool
215ThreadGDBRemote::ShouldStop (bool &step_more)
216{
217 return true;
218}
219RegisterContext *
220ThreadGDBRemote::GetRegisterContext ()
221{
222 if (m_reg_context_sp.get() == NULL)
223 m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL));
224 return m_reg_context_sp.get();
225}
226
227RegisterContext *
228ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
229{
230 const bool read_all_registers_at_once = false;
231 uint32_t frame_idx = 0;
232
233 if (frame)
234 frame_idx = frame->GetID();
235
236 if (frame_idx == 0)
237 return new GDBRemoteRegisterContext (*this, frame, GetGDBProcess().m_register_info, read_all_registers_at_once);
238 else if (m_unwinder_ap.get() && frame_idx < m_unwinder_ap->GetFrameCount())
239 return m_unwinder_ap->CreateRegisterContextForFrame (frame);
240 return NULL;
241}
242
243bool
244ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
245{
246 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
247 if (frame_sp)
248 {
249 checkpoint.SetStackID(frame_sp->GetStackID());
250 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
251 }
252 return false;
253}
254
255bool
256ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
257{
258 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
259 if (frame_sp)
260 {
261 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
262 frame_sp->GetRegisterContext()->Invalidate();
263 ClearStackFrames();
264 return ret;
265 }
266 return false;
267}
268
269bool
270ThreadGDBRemote::GetRawStopReason (StopInfo *stop_info)
271{
272 if (m_stop_info_stop_id != m_process.GetStopID())
273 {
274 char packet[256];
275 const int packet_len = snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
276 assert (packet_len < (sizeof(packet) - 1));
277 StringExtractorGDBRemote stop_packet;
278 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
279 {
280 std::string copy(stop_packet.GetStringRef());
281 GetGDBProcess().SetThreadStopInfo (stop_packet);
282 // The process should have set the stop info stop ID and also
283 // filled this thread in with valid stop info
284 if (m_stop_info_stop_id != m_process.GetStopID())
285 {
286 //ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "warning: qThreadStopInfo problem: '%s' => '%s'", packet, stop_packet.GetStringRef().c_str());
287 printf("warning: qThreadStopInfo problem: '%s' => '%s'\n\torig '%s'\n", packet, stop_packet.GetStringRef().c_str(), copy.c_str()); /// REMOVE THIS
288 return false;
289 }
290 }
291 }
292 *stop_info = m_stop_info;
293 return true;
294}
295
296