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