blob: f1847d861daf7092903817a2c3fefebe401e1227 [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"
29
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// Thread Registers
35//----------------------------------------------------------------------
36
37ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
38 Thread(process, tid),
Chris Lattner24943d22010-06-08 16:52:24 +000039 m_thread_name (),
40 m_dispatch_queue_name (),
Jim Ingham71219082010-08-12 02:14:28 +000041 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43// ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD | GDBR_LOG_VERBOSE, "ThreadGDBRemote::ThreadGDBRemote ( pid = %i, tid = 0x%4.4x, )", m_process.GetID(), GetID());
44 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
45}
46
47ThreadGDBRemote::~ThreadGDBRemote ()
48{
49 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
50}
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{
81 // TODO: cache for next time in case we can match things up??
82 ClearStackFrames();
83 int signo = GetResumeSignal();
Greg Clayton643ee732010-08-04 01:40:35 +000084
Chris Lattner24943d22010-06-08 16:52:24 +000085 switch (resume_state)
86 {
87 case eStateSuspended:
88 case eStateStopped:
89 // Don't append anything for threads that should stay stopped.
90 break;
91
92 case eStateRunning:
93 if (m_process.GetUnixSignals().SignalIsValid (signo))
94 GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
95 else
96 GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
97 break;
98
99 case eStateStepping:
100 if (m_process.GetUnixSignals().SignalIsValid (signo))
101 GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
102 else
103 GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
104 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000105
106 default:
107 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000108 }
109 Thread::WillResume(resume_state);
110 return true;
111}
112
113void
114ThreadGDBRemote::RefreshStateAfterStop()
115{
116 // Invalidate all registers in our register context
117 GetRegisterContext()->Invalidate();
118}
119
120Unwind *
121ThreadGDBRemote::GetUnwinder ()
122{
123 if (m_unwinder_ap.get() == NULL)
124 {
125 const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
126 if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386"))
127 {
128 m_unwinder_ap.reset (new UnwindLibUnwind (*this, GetGDBProcess().GetLibUnwindAddressSpace()));
129 }
130 else
131 {
132 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
133 }
134 }
135 return m_unwinder_ap.get();
136}
137
Chris Lattner24943d22010-06-08 16:52:24 +0000138void
139ThreadGDBRemote::ClearStackFrames ()
140{
141 Unwind *unwinder = GetUnwinder ();
142 if (unwinder)
143 unwinder->Clear();
144 Thread::ClearStackFrames();
145}
146
147
148bool
149ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
150{
151 return thread != 0;
152}
153
154void
155ThreadGDBRemote::Dump(Log *log, uint32_t index)
156{
157}
158
159
160bool
161ThreadGDBRemote::ShouldStop (bool &step_more)
162{
163 return true;
164}
165RegisterContext *
166ThreadGDBRemote::GetRegisterContext ()
167{
168 if (m_reg_context_sp.get() == NULL)
169 m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL));
170 return m_reg_context_sp.get();
171}
172
173RegisterContext *
174ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
175{
176 const bool read_all_registers_at_once = false;
177 uint32_t frame_idx = 0;
178
179 if (frame)
Greg Clayton33ed1702010-08-24 00:45:41 +0000180 frame_idx = frame->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000181
182 if (frame_idx == 0)
183 return new GDBRemoteRegisterContext (*this, frame, GetGDBProcess().m_register_info, read_all_registers_at_once);
Greg Clayton33ed1702010-08-24 00:45:41 +0000184 else if (m_unwinder_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000185 return m_unwinder_ap->CreateRegisterContextForFrame (frame);
186 return NULL;
187}
188
189bool
190ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
191{
192 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
193 if (frame_sp)
194 {
195 checkpoint.SetStackID(frame_sp->GetStackID());
196 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
197 }
198 return false;
199}
200
201bool
202ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
203{
204 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
205 if (frame_sp)
206 {
207 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
208 frame_sp->GetRegisterContext()->Invalidate();
209 ClearStackFrames();
210 return ret;
211 }
212 return false;
213}
214
Greg Clayton643ee732010-08-04 01:40:35 +0000215lldb::StopInfoSP
216ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000217{
Greg Clayton643ee732010-08-04 01:40:35 +0000218 if (m_actual_stop_info_sp.get() == NULL || m_actual_stop_info_sp->IsValid() == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000219 {
Greg Clayton643ee732010-08-04 01:40:35 +0000220 m_actual_stop_info_sp.reset();
221
Chris Lattner24943d22010-06-08 16:52:24 +0000222 char packet[256];
Greg Clayton54e7afa2010-07-09 20:39:50 +0000223 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000224 StringExtractorGDBRemote stop_packet;
225 if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
226 {
227 std::string copy(stop_packet.GetStringRef());
228 GetGDBProcess().SetThreadStopInfo (stop_packet);
Chris Lattner24943d22010-06-08 16:52:24 +0000229 }
230 }
Greg Clayton643ee732010-08-04 01:40:35 +0000231 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000232}
233
234