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