blob: f5491cb9202728c86dd71a45b83e928c6ae28885 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "UnwindMacOSXFrameBackchain.h"
Jason Molenda8132f2d2010-11-04 09:51:29 +000027#include "UnwindLLDB.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028
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),
Chris Lattner24943d22010-06-08 16:52:24 +000038 m_thread_name (),
39 m_dispatch_queue_name (),
Jim Ingham71219082010-08-12 02:14:28 +000040 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +000041{
42// ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD | GDBR_LOG_VERBOSE, "ThreadGDBRemote::ThreadGDBRemote ( pid = %i, tid = 0x%4.4x, )", m_process.GetID(), GetID());
43 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
44}
45
46ThreadGDBRemote::~ThreadGDBRemote ()
47{
48 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 +000049 DestroyThread();
Chris Lattner24943d22010-06-08 16:52:24 +000050}
51
52
53const char *
54ThreadGDBRemote::GetInfo ()
55{
56 return NULL;
57}
58
59
60const char *
61ThreadGDBRemote::GetName ()
62{
63 if (m_thread_name.empty())
64 return NULL;
65 return m_thread_name.c_str();
66}
67
68
69const char *
70ThreadGDBRemote::GetQueueName ()
71{
72 // Always re-fetch the dispatch queue name since it can change
73 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
74 return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
75 return NULL;
76}
77
78bool
79ThreadGDBRemote::WillResume (StateType resume_state)
80{
Chris Lattner24943d22010-06-08 16:52:24 +000081 ClearStackFrames();
Greg Clayton8f6be2a2010-10-09 01:40:57 +000082 // Call the Thread::WillResume first. If we stop at a signal, the stop info
83 // class for signal will set the resume signal that we need below. The signal
84 // stuff obeys the Process::UnixSignal defaults.
85 Thread::WillResume(resume_state);
86
Chris Lattner24943d22010-06-08 16:52:24 +000087 int signo = GetResumeSignal();
Greg Clayton643ee732010-08-04 01:40:35 +000088
Chris Lattner24943d22010-06-08 16:52:24 +000089 switch (resume_state)
90 {
91 case eStateSuspended:
92 case eStateStopped:
93 // Don't append anything for threads that should stay stopped.
94 break;
95
96 case eStateRunning:
97 if (m_process.GetUnixSignals().SignalIsValid (signo))
98 GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
99 else
100 GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
101 break;
102
103 case eStateStepping:
104 if (m_process.GetUnixSignals().SignalIsValid (signo))
105 GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
106 else
107 GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
108 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000109
110 default:
111 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000112 }
Chris Lattner24943d22010-06-08 16:52:24 +0000113 return true;
114}
115
116void
117ThreadGDBRemote::RefreshStateAfterStop()
118{
119 // Invalidate all registers in our register context
120 GetRegisterContext()->Invalidate();
121}
122
Jason Molenda8132f2d2010-11-04 09:51:29 +0000123// Whether to use the new native unwinder (UnwindLLDB) or the libunwind-remote based unwinder for
124// stack walks on i386/x86_64
Jason Molendadc7422c2010-11-04 22:29:24 +0000125#define USE_NATIVE_UNWINDER
Jason Molenda8132f2d2010-11-04 09:51:29 +0000126
Chris Lattner24943d22010-06-08 16:52:24 +0000127Unwind *
128ThreadGDBRemote::GetUnwinder ()
129{
130 if (m_unwinder_ap.get() == NULL)
131 {
132 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
133 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
134 {
Jason Molenda8132f2d2010-11-04 09:51:29 +0000135#if defined (USE_NATIVE_UNWINDER)
136 m_unwinder_ap.reset (new UnwindLLDB (*this));
137#else
Jason Molenda96829fb2010-11-04 09:46:43 +0000138 m_unwinder_ap.reset (new UnwindLibUnwind (*this, GetGDBProcess().GetLibUnwindAddressSpace()));
Jason Molenda8132f2d2010-11-04 09:51:29 +0000139#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000140 }
141 else
142 {
143 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
144 }
145 }
146 return m_unwinder_ap.get();
147}
148
Chris Lattner24943d22010-06-08 16:52:24 +0000149void
150ThreadGDBRemote::ClearStackFrames ()
151{
152 Unwind *unwinder = GetUnwinder ();
153 if (unwinder)
154 unwinder->Clear();
155 Thread::ClearStackFrames();
156}
157
158
159bool
160ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
161{
162 return thread != 0;
163}
164
165void
166ThreadGDBRemote::Dump(Log *log, uint32_t index)
167{
168}
169
170
171bool
172ThreadGDBRemote::ShouldStop (bool &step_more)
173{
174 return true;
175}
176RegisterContext *
177ThreadGDBRemote::GetRegisterContext ()
178{
179 if (m_reg_context_sp.get() == NULL)
180 m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL));
181 return m_reg_context_sp.get();
182}
183
184RegisterContext *
185ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
186{
187 const bool read_all_registers_at_once = false;
188 uint32_t frame_idx = 0;
189
190 if (frame)
Greg Clayton33ed1702010-08-24 00:45:41 +0000191 frame_idx = frame->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000192
193 if (frame_idx == 0)
194 return new GDBRemoteRegisterContext (*this, frame, GetGDBProcess().m_register_info, read_all_registers_at_once);
Greg Clayton33ed1702010-08-24 00:45:41 +0000195 else if (m_unwinder_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000196 return m_unwinder_ap->CreateRegisterContextForFrame (frame);
197 return NULL;
198}
199
200bool
201ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
202{
203 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
204 if (frame_sp)
205 {
206 checkpoint.SetStackID(frame_sp->GetStackID());
207 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
208 }
209 return false;
210}
211
212bool
213ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
214{
215 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
216 if (frame_sp)
217 {
218 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
219 frame_sp->GetRegisterContext()->Invalidate();
220 ClearStackFrames();
221 return ret;
222 }
223 return false;
224}
225
Greg Clayton643ee732010-08-04 01:40:35 +0000226lldb::StopInfoSP
227ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000228{
Greg Clayton643ee732010-08-04 01:40:35 +0000229 if (m_actual_stop_info_sp.get() == NULL || m_actual_stop_info_sp->IsValid() == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000230 {
Greg Clayton643ee732010-08-04 01:40:35 +0000231 m_actual_stop_info_sp.reset();
232
Chris Lattner24943d22010-06-08 16:52:24 +0000233 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000234 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000235 StringExtractorGDBRemote stop_packet;
236 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
237 {
238 std::string copy(stop_packet.GetStringRef());
239 GetGDBProcess().SetThreadStopInfo (stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +0000240 }
241 }
Greg Clayton643ee732010-08-04 01:40:35 +0000242 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000243}
244
245