1 //===-- 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/StopInfo.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Target/Unwind.h"
21 #include "lldb/Breakpoint/WatchpointLocation.h"
22 
23 #include "ProcessGDBRemote.h"
24 #include "ProcessGDBRemoteLog.h"
25 #include "Utility/StringExtractorGDBRemote.h"
26 #include "UnwindMacOSXFrameBackchain.h"
27 #include "UnwindLLDB.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 //----------------------------------------------------------------------
33 // Thread Registers
34 //----------------------------------------------------------------------
35 
36 ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) :
37     Thread(process, tid),
38     m_thread_name (),
39     m_dispatch_queue_name (),
40     m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
41     m_thread_stop_reason_stop_id (0)
42 {
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 
47 ThreadGDBRemote::~ThreadGDBRemote ()
48 {
49     ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID());
50     DestroyThread();
51 }
52 
53 
54 const char *
55 ThreadGDBRemote::GetInfo ()
56 {
57     return NULL;
58 }
59 
60 
61 const char *
62 ThreadGDBRemote::GetName ()
63 {
64     if (m_thread_name.empty())
65         return NULL;
66     return m_thread_name.c_str();
67 }
68 
69 
70 const char *
71 ThreadGDBRemote::GetQueueName ()
72 {
73     // Always re-fetch the dispatch queue name since it can change
74     if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
75         return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
76     return NULL;
77 }
78 
79 bool
80 ThreadGDBRemote::WillResume (StateType resume_state)
81 {
82     ClearStackFrames();
83     // Call the Thread::WillResume first. If we stop at a signal, the stop info
84     // class for signal will set the resume signal that we need below. The signal
85     // stuff obeys the Process::UnixSignal defaults.
86     Thread::WillResume(resume_state);
87 
88     int signo = GetResumeSignal();
89 
90     switch (resume_state)
91     {
92     case eStateSuspended:
93     case eStateStopped:
94         // Don't append anything for threads that should stay stopped.
95         break;
96 
97     case eStateRunning:
98         if (m_process.GetUnixSignals().SignalIsValid (signo))
99             GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID());
100         else
101             GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID());
102         break;
103 
104     case eStateStepping:
105         if (m_process.GetUnixSignals().SignalIsValid (signo))
106             GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID());
107         else
108             GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID());
109         break;
110 
111     default:
112         break;
113     }
114     return true;
115 }
116 
117 void
118 ThreadGDBRemote::RefreshStateAfterStop()
119 {
120     // Invalidate all registers in our register context. We don't set "force" to
121     // true because the stop reply packet might have had some register values
122     // that were expedited and these will already be copied into the register
123     // context by the time this function gets called. The GDBRemoteRegisterContext
124     // class has been made smart enough to detect when it needs to invalidate
125     // which registers are valid by putting hooks in the register read and
126     // register supply functions where they check the process stop ID and do
127     // the right thing.
128     const bool force = false;
129     GetRegisterContext()->InvalidateIfNeeded (force);
130 }
131 
132 Unwind *
133 ThreadGDBRemote::GetUnwinder ()
134 {
135     if (m_unwinder_ap.get() == NULL)
136     {
137         const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ());
138         if (target_arch == ArchSpec("x86_64") ||  target_arch == ArchSpec("i386"))
139         {
140             m_unwinder_ap.reset (new UnwindLLDB (*this));
141         }
142         else
143         {
144             m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
145         }
146     }
147     return m_unwinder_ap.get();
148 }
149 
150 void
151 ThreadGDBRemote::ClearStackFrames ()
152 {
153     Unwind *unwinder = GetUnwinder ();
154     if (unwinder)
155         unwinder->Clear();
156     Thread::ClearStackFrames();
157 }
158 
159 
160 bool
161 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
162 {
163     return thread != 0;
164 }
165 
166 void
167 ThreadGDBRemote::Dump(Log *log, uint32_t index)
168 {
169 }
170 
171 
172 bool
173 ThreadGDBRemote::ShouldStop (bool &step_more)
174 {
175     return true;
176 }
177 lldb::RegisterContextSP
178 ThreadGDBRemote::GetRegisterContext ()
179 {
180     if (m_reg_context_sp.get() == NULL)
181         m_reg_context_sp = CreateRegisterContextForFrame (NULL);
182     return m_reg_context_sp;
183 }
184 
185 lldb::RegisterContextSP
186 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
187 {
188     lldb::RegisterContextSP reg_ctx_sp;
189     const bool read_all_registers_at_once = false;
190     uint32_t concrete_frame_idx = 0;
191 
192     if (frame)
193         concrete_frame_idx = frame->GetConcreteFrameIndex ();
194 
195     if (concrete_frame_idx == 0)
196         reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, GetGDBProcess().m_register_info, read_all_registers_at_once));
197     else if (m_unwinder_ap.get())
198         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
199     return reg_ctx_sp;
200 }
201 
202 void
203 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
204 {
205     GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
206     assert (gdb_reg_ctx);
207     gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
208 }
209 
210 bool
211 ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
212 {
213     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
214     if (frame_sp)
215     {
216         checkpoint.SetStackID(frame_sp->GetStackID());
217         return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
218     }
219     return false;
220 }
221 
222 bool
223 ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
224 {
225     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
226     if (frame_sp)
227     {
228         bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
229         frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
230         ClearStackFrames();
231         return ret;
232     }
233     return false;
234 }
235 
236 lldb::StopInfoSP
237 ThreadGDBRemote::GetPrivateStopReason ()
238 {
239     const uint32_t process_stop_id = GetProcess().GetStopID();
240     if (m_thread_stop_reason_stop_id != process_stop_id ||
241         (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
242     {
243         // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
244         // for this thread, then m_actual_stop_info_sp will not ever contain
245         // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
246         // check will never be able to tell us if we have the correct stop info
247         // for this thread and we will continually send qThreadStopInfo packets
248         // down to the remote GDB server, so we need to keep our own notion
249         // of the stop ID that m_actual_stop_info_sp is valid for (even if it
250         // contains nothing). We use m_thread_stop_reason_stop_id for this below.
251         m_thread_stop_reason_stop_id = process_stop_id;
252         m_actual_stop_info_sp.reset();
253 
254         char packet[256];
255         ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID());
256         StringExtractorGDBRemote stop_packet;
257         if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false))
258         {
259             GetGDBProcess().SetThreadStopInfo (stop_packet);
260         }
261     }
262     return m_actual_stop_info_sp;
263 }
264 
265 
266