1 //===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ThreadGDBRemote.h"
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"
27 #include "ProcessGDBRemote.h"
28 #include "ProcessGDBRemoteLog.h"
29 #include "Utility/StringExtractorGDBRemote.h"
32 using namespace lldb_private;
33 using namespace lldb_private::process_gdb_remote;
35 //----------------------------------------------------------------------
37 //----------------------------------------------------------------------
39 ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
42 m_dispatch_queue_name (),
43 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
44 m_dispatch_queue_t (LLDB_INVALID_ADDRESS),
45 m_queue_kind (eQueueKindUnknown),
46 m_queue_serial_number (LLDB_INVALID_QUEUE_ID),
47 m_associated_with_libdispatch_queue (eLazyBoolCalculate)
49 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
55 ThreadGDBRemote::~ThreadGDBRemote ()
57 ProcessSP process_sp(GetProcess());
58 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
60 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
66 ThreadGDBRemote::GetName ()
68 if (m_thread_name.empty())
70 return m_thread_name.c_str();
74 ThreadGDBRemote::ClearQueueInfo ()
76 m_dispatch_queue_name.clear();
77 m_queue_kind = eQueueKindUnknown;
78 m_queue_serial_number = 0;
79 m_dispatch_queue_t = LLDB_INVALID_ADDRESS;
80 m_associated_with_libdispatch_queue = eLazyBoolCalculate;
84 ThreadGDBRemote::SetQueueInfo (std::string &&queue_name, QueueKind queue_kind, uint64_t queue_serial, addr_t dispatch_queue_t, LazyBool associated_with_libdispatch_queue)
86 m_dispatch_queue_name = queue_name;
87 m_queue_kind = queue_kind;
88 m_queue_serial_number = queue_serial;
89 m_dispatch_queue_t = dispatch_queue_t;
90 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
95 ThreadGDBRemote::GetQueueName ()
97 // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...)
98 // with valid information that was gleaned from the stop reply packet. In this case we trust
99 // that the info is valid in m_dispatch_queue_name without refetching it
100 if (CachedQueueInfoIsValid())
102 if (m_dispatch_queue_name.empty())
105 return m_dispatch_queue_name.c_str();
107 // Always re-fetch the dispatch queue name since it can change
109 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
112 if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
114 ProcessSP process_sp (GetProcess());
117 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
119 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
121 m_dispatch_queue_name.clear();
123 if (!m_dispatch_queue_name.empty())
124 return m_dispatch_queue_name.c_str();
131 ThreadGDBRemote::GetQueueKind ()
133 // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...)
134 // with valid information that was gleaned from the stop reply packet. In this case we trust
135 // that the info is valid in m_dispatch_queue_name without refetching it
136 if (CachedQueueInfoIsValid())
141 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
142 return eQueueKindUnknown;
144 if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
146 ProcessSP process_sp (GetProcess());
149 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
151 m_queue_kind = runtime->GetQueueKind (m_thread_dispatch_qaddr);
155 return eQueueKindUnknown;
160 ThreadGDBRemote::GetQueueID ()
162 // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...)
163 // with valid information that was gleaned from the stop reply packet. In this case we trust
164 // that the info is valid in m_dispatch_queue_name without refetching it
165 if (CachedQueueInfoIsValid())
166 return m_queue_serial_number;
168 if (m_associated_with_libdispatch_queue == eLazyBoolNo)
169 return LLDB_INVALID_QUEUE_ID;
171 if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
173 ProcessSP process_sp (GetProcess());
176 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
179 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
183 return LLDB_INVALID_QUEUE_ID;
187 ThreadGDBRemote::GetQueue ()
189 queue_id_t queue_id = GetQueueID();
191 if (queue_id != LLDB_INVALID_QUEUE_ID)
193 ProcessSP process_sp (GetProcess());
196 queue = process_sp->GetQueueList().FindQueueByID (queue_id);
203 ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
205 if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS)
207 if (m_thread_dispatch_qaddr != 0 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
209 ProcessSP process_sp (GetProcess());
212 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
215 m_dispatch_queue_t = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
220 return m_dispatch_queue_t;
224 ThreadGDBRemote::SetQueueLibdispatchQueueAddress (lldb::addr_t dispatch_queue_t)
226 m_dispatch_queue_t = dispatch_queue_t;
230 ThreadGDBRemote::ThreadHasQueueInformation () const
232 if (m_thread_dispatch_qaddr != 0
233 && m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS
234 && m_dispatch_queue_t != LLDB_INVALID_ADDRESS
235 && m_queue_kind != eQueueKindUnknown
236 && m_queue_serial_number != 0)
244 ThreadGDBRemote::GetAssociatedWithLibdispatchQueue ()
246 return m_associated_with_libdispatch_queue;
250 ThreadGDBRemote::SetAssociatedWithLibdispatchQueue (LazyBool associated_with_libdispatch_queue)
252 m_associated_with_libdispatch_queue = associated_with_libdispatch_queue;
255 StructuredData::ObjectSP
256 ThreadGDBRemote::FetchThreadExtendedInfo ()
258 StructuredData::ObjectSP object_sp;
259 const lldb::user_id_t tid = GetProtocolID();
260 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
262 log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid);
263 ProcessSP process_sp (GetProcess());
266 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
267 object_sp = gdb_process->GetExtendedInfoForThread (tid);
273 ThreadGDBRemote::WillResume (StateType resume_state)
275 int signo = GetResumeSignal();
276 const lldb::user_id_t tid = GetProtocolID();
277 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
279 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
281 ProcessSP process_sp (GetProcess());
284 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
285 switch (resume_state)
287 case eStateSuspended:
289 // Don't append anything for threads that should stay stopped.
293 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
294 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
296 gdb_process->m_continue_c_tids.push_back(tid);
300 if (gdb_process->GetUnixSignals()->SignalIsValid(signo))
301 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
303 gdb_process->m_continue_s_tids.push_back(tid);
313 ThreadGDBRemote::RefreshStateAfterStop()
315 // Invalidate all registers in our register context. We don't set "force" to
316 // true because the stop reply packet might have had some register values
317 // that were expedited and these will already be copied into the register
318 // context by the time this function gets called. The GDBRemoteRegisterContext
319 // class has been made smart enough to detect when it needs to invalidate
320 // which registers are valid by putting hooks in the register read and
321 // register supply functions where they check the process stop ID and do
323 const bool force = false;
324 GetRegisterContext()->InvalidateIfNeeded (force);
328 ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
334 ThreadGDBRemote::Dump(Log *log, uint32_t index)
340 ThreadGDBRemote::ShouldStop (bool &step_more)
344 lldb::RegisterContextSP
345 ThreadGDBRemote::GetRegisterContext ()
347 if (m_reg_context_sp.get() == NULL)
348 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
349 return m_reg_context_sp;
352 lldb::RegisterContextSP
353 ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
355 lldb::RegisterContextSP reg_ctx_sp;
356 uint32_t concrete_frame_idx = 0;
359 concrete_frame_idx = frame->GetConcreteFrameIndex ();
362 if (concrete_frame_idx == 0)
364 ProcessSP process_sp (GetProcess());
367 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
368 // read_all_registers_at_once will be true if 'p' packet is not supported.
369 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
370 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
375 Unwind *unwinder = GetUnwinder ();
377 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
383 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
385 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
386 assert (gdb_reg_ctx);
387 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
391 ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, uint64_t regval)
393 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
394 assert (gdb_reg_ctx);
395 return gdb_reg_ctx->PrivateSetRegisterValue (reg, regval);
399 ThreadGDBRemote::CalculateStopInfo ()
401 ProcessSP process_sp (GetProcess());
403 return static_cast<ProcessGDBRemote *>(process_sp.get())->CalculateThreadStopInfo(this);