blob: 7e06e78cfed16ce7901446a2ab01c9802d0c810e [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
23#include "LibUnwindRegisterContext.h"
24#include "ProcessGDBRemote.h"
25#include "ProcessGDBRemoteLog.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000026#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "UnwindLibUnwind.h"
28#include "UnwindMacOSXFrameBackchain.h"
Jason Molendaa6b71de2010-11-04 09:40:56 +000029#include "UnwindLLDB.h"
30#include "RegisterContextLLDB.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031
32using 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 Ingham71219082010-08-12 02:14:28 +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());
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{
Chris Lattner24943d22010-06-08 16:52:24 +000083 ClearStackFrames();
Greg Clayton8f6be2a2010-10-09 01:40:57 +000084 // Call the Thread::WillResume first. If we stop at a signal, the stop info
85 // class for signal will set the resume signal that we need below. The signal
86 // stuff obeys the Process::UnixSignal defaults.
87 Thread::WillResume(resume_state);
88
Chris Lattner24943d22010-06-08 16:52:24 +000089 int signo = GetResumeSignal();
Greg Clayton643ee732010-08-04 01:40:35 +000090
Chris Lattner24943d22010-06-08 16:52:24 +000091 switch (resume_state)
92 {
93 case eStateSuspended:
94 case eStateStopped:
95 // Don't append anything for threads that should stay stopped.
96 break;
97
98 case eStateRunning:
99 if (m_process.GetUnixSignals().SignalIsValid (signo))
100 GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
101 else
102 GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
103 break;
104
105 case eStateStepping:
106 if (m_process.GetUnixSignals().SignalIsValid (signo))
107 GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
108 else
109 GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
110 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000111
112 default:
113 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000114 }
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return true;
116}
117
118void
119ThreadGDBRemote::RefreshStateAfterStop()
120{
121 // Invalidate all registers in our register context
122 GetRegisterContext()->Invalidate();
123}
124
125Unwind *
126ThreadGDBRemote::GetUnwinder ()
127{
128 if (m_unwinder_ap.get() == NULL)
129 {
130 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
131 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
132 {
Jason Molendaa6b71de2010-11-04 09:40:56 +0000133 m_unwinder_ap.reset (new UnwindLLDB (*this));
Chris Lattner24943d22010-06-08 16:52:24 +0000134 }
135 else
136 {
137 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
138 }
139 }
140 return m_unwinder_ap.get();
141}
142
Chris Lattner24943d22010-06-08 16:52:24 +0000143void
144ThreadGDBRemote::ClearStackFrames ()
145{
146 Unwind *unwinder = GetUnwinder ();
147 if (unwinder)
148 unwinder->Clear();
149 Thread::ClearStackFrames();
150}
151
152
153bool
154ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
155{
156 return thread != 0;
157}
158
159void
160ThreadGDBRemote::Dump(Log *log, uint32_t index)
161{
162}
163
164
165bool
166ThreadGDBRemote::ShouldStop (bool &step_more)
167{
168 return true;
169}
170RegisterContext *
171ThreadGDBRemote::GetRegisterContext ()
172{
173 if (m_reg_context_sp.get() == NULL)
174 m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL));
175 return m_reg_context_sp.get();
176}
177
178RegisterContext *
179ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
180{
181 const bool read_all_registers_at_once = false;
182 uint32_t frame_idx = 0;
183
184 if (frame)
Greg Clayton33ed1702010-08-24 00:45:41 +0000185 frame_idx = frame->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000186
187 if (frame_idx == 0)
188 return new GDBRemoteRegisterContext (*this, frame, GetGDBProcess().m_register_info, read_all_registers_at_once);
Greg Clayton33ed1702010-08-24 00:45:41 +0000189 else if (m_unwinder_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000190 return m_unwinder_ap->CreateRegisterContextForFrame (frame);
191 return NULL;
192}
193
194bool
195ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
196{
197 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
198 if (frame_sp)
199 {
200 checkpoint.SetStackID(frame_sp->GetStackID());
201 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
202 }
203 return false;
204}
205
206bool
207ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
208{
209 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
210 if (frame_sp)
211 {
212 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
213 frame_sp->GetRegisterContext()->Invalidate();
214 ClearStackFrames();
215 return ret;
216 }
217 return false;
218}
219
Greg Clayton643ee732010-08-04 01:40:35 +0000220lldb::StopInfoSP
221ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
Greg Clayton643ee732010-08-04 01:40:35 +0000223 if (m_actual_stop_info_sp.get() == NULL || m_actual_stop_info_sp->IsValid() == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000224 {
Greg Clayton643ee732010-08-04 01:40:35 +0000225 m_actual_stop_info_sp.reset();
226
Chris Lattner24943d22010-06-08 16:52:24 +0000227 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000228 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000229 StringExtractorGDBRemote stop_packet;
230 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
231 {
232 std::string copy(stop_packet.GetStringRef());
233 GetGDBProcess().SetThreadStopInfo (stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +0000234 }
235 }
Greg Clayton643ee732010-08-04 01:40:35 +0000236 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000237}
238
239