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