blob: 4eae1e6d41bad599fec03bea6c791f5059d7f3ef [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"
Greg Clayton54e7afa2010-07-09 20:39:50 +000025#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#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;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000107
108 default:
109 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000110 }
111 Thread::WillResume(resume_state);
112 return true;
113}
114
115void
116ThreadGDBRemote::RefreshStateAfterStop()
117{
118 // Invalidate all registers in our register context
119 GetRegisterContext()->Invalidate();
120}
121
122Unwind *
123ThreadGDBRemote::GetUnwinder ()
124{
125 if (m_unwinder_ap.get() == NULL)
126 {
127 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
128 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
129 {
130 m_unwinder_ap.reset (new UnwindLibUnwind (*this, GetGDBProcess().GetLibUnwindAddressSpace()));
131 }
132 else
133 {
134 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
135 }
136 }
137 return m_unwinder_ap.get();
138}
139
140uint32_t
141ThreadGDBRemote::GetStackFrameCount()
142{
143 Unwind *unwinder = GetUnwinder ();
144 if (unwinder)
145 return unwinder->GetFrameCount();
146 return 0;
147}
148
149// Make sure that GetStackFrameAtIndex() does NOT call GetStackFrameCount() when
150// getting the stack frame at index zero! This way GetStackFrameCount() (via
151// GetStackFRameData()) can call this function to get the first frame in order
152// to provide the first frame to a lower call for efficiency sake (avoid
153// redundant lookups in the frame symbol context).
154lldb::StackFrameSP
155ThreadGDBRemote::GetStackFrameAtIndex (uint32_t idx)
156{
157
158 StackFrameSP frame_sp (m_frames.GetFrameAtIndex(idx));
159
160 if (frame_sp.get())
161 return frame_sp;
162
163 // Don't try and fetch a frame while process is running
164// FIXME: This check isn't right because IsRunning checks the Public state, but this
165// is work you need to do - for instance in ShouldStop & friends - before the public
166// state has been changed.
167// if (m_process.IsRunning())
168// return frame_sp;
169
170 // Special case the first frame (idx == 0) so that we don't need to
171 // know how many stack frames there are to get it. If we need any other
172 // frames, then we do need to know if "idx" is a valid index.
173 if (idx == 0)
174 {
175 // If this is the first frame, we want to share the thread register
176 // context with the stack frame at index zero.
177 GetRegisterContext();
178 assert (m_reg_context_sp.get());
179 frame_sp.reset (new StackFrame (idx, *this, m_reg_context_sp, m_reg_context_sp->GetSP(), m_reg_context_sp->GetPC()));
180 }
181 else if (idx < GetStackFrameCount())
182 {
183 Unwind *unwinder = GetUnwinder ();
184 if (unwinder)
185 {
186 addr_t pc, cfa;
187 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
188 frame_sp.reset (new StackFrame (idx, *this, cfa, pc));
189 }
190 }
191 m_frames.SetFrameAtIndex(idx, frame_sp);
192 return frame_sp;
193}
194
195void
196ThreadGDBRemote::ClearStackFrames ()
197{
198 Unwind *unwinder = GetUnwinder ();
199 if (unwinder)
200 unwinder->Clear();
201 Thread::ClearStackFrames();
202}
203
204
205bool
206ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
207{
208 return thread != 0;
209}
210
211void
212ThreadGDBRemote::Dump(Log *log, uint32_t index)
213{
214}
215
216
217bool
218ThreadGDBRemote::ShouldStop (bool &step_more)
219{
220 return true;
221}
222RegisterContext *
223ThreadGDBRemote::GetRegisterContext ()
224{
225 if (m_reg_context_sp.get() == NULL)
226 m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL));
227 return m_reg_context_sp.get();
228}
229
230RegisterContext *
231ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
232{
233 const bool read_all_registers_at_once = false;
234 uint32_t frame_idx = 0;
235
236 if (frame)
237 frame_idx = frame->GetID();
238
239 if (frame_idx == 0)
240 return new GDBRemoteRegisterContext (*this, frame, GetGDBProcess().m_register_info, read_all_registers_at_once);
241 else if (m_unwinder_ap.get() && frame_idx < m_unwinder_ap->GetFrameCount())
242 return m_unwinder_ap->CreateRegisterContextForFrame (frame);
243 return NULL;
244}
245
246bool
247ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
248{
249 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
250 if (frame_sp)
251 {
252 checkpoint.SetStackID(frame_sp->GetStackID());
253 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
254 }
255 return false;
256}
257
258bool
259ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
260{
261 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
262 if (frame_sp)
263 {
264 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
265 frame_sp->GetRegisterContext()->Invalidate();
266 ClearStackFrames();
267 return ret;
268 }
269 return false;
270}
271
272bool
273ThreadGDBRemote::GetRawStopReason (StopInfo *stop_info)
274{
275 if (m_stop_info_stop_id != m_process.GetStopID())
276 {
277 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000278 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000279 StringExtractorGDBRemote stop_packet;
280 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
281 {
282 std::string copy(stop_packet.GetStringRef());
283 GetGDBProcess().SetThreadStopInfo (stop_packet);
284 // The process should have set the stop info stop ID and also
285 // filled this thread in with valid stop info
286 if (m_stop_info_stop_id != m_process.GetStopID())
287 {
288 //ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "warning: qThreadStopInfo problem: '%s' => '%s'", packet, stop_packet.GetStringRef().c_str());
289 printf("warning: qThreadStopInfo problem: '%s' => '%s'\n\torig '%s'\n", packet, stop_packet.GetStringRef().c_str(), copy.c_str()); /// REMOVE THIS
290 return false;
291 }
292 }
293 }
294 *stop_info = m_stop_info;
295 return true;
296}
297
298