blob: 59c701d75a686f61b806dcaa1f05a4337d181b8b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Jason Molenda3dc4f442013-10-18 05:55:24 +000013#include "lldb/Breakpoint/Watchpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataExtractor.h"
Jim Ingham0f16e732011-02-08 05:20:59 +000016#include "lldb/Core/State.h"
Jason Molenda3dc4f442013-10-18 05:55:24 +000017#include "lldb/Core/StreamString.h"
18#include "lldb/Target/Platform.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/Process.h"
20#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000021#include "lldb/Target/StopInfo.h"
Jason Molenda2fd83352014-02-05 05:44:54 +000022#include "lldb/Target/SystemRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Target/Target.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000024#include "lldb/Target/UnixSignals.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Unwind.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "ProcessGDBRemote.h"
28#include "ProcessGDBRemoteLog.h"
Greg Claytonc982c762010-07-09 20:39:50 +000029#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033using namespace lldb_private::process_gdb_remote;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35//----------------------------------------------------------------------
36// Thread Registers
37//----------------------------------------------------------------------
38
Jim Ingham4f465cf2012-10-10 18:32:14 +000039ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
40 Thread(process, tid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 m_thread_name (),
42 m_dispatch_queue_name (),
Greg Clayton0b90be12015-06-23 21:27:50 +000043 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS),
44 m_queue_kind(eQueueKindUnknown),
45 m_queue_serial(0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046{
Greg Clayton0b90be12015-06-23 21:27:50 +000047 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
Greg Clayton1ac04c32012-02-21 00:09:25 +000048 this,
Jim Ingham4f465cf2012-10-10 18:32:14 +000049 process.GetID(),
Greg Clayton1ac04c32012-02-21 00:09:25 +000050 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
53ThreadGDBRemote::~ThreadGDBRemote ()
54{
Greg Clayton1ac04c32012-02-21 00:09:25 +000055 ProcessSP process_sp(GetProcess());
56 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
57 this,
58 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
59 GetID());
Jim Ingham773d9812010-11-18 02:47:07 +000060 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063const char *
64ThreadGDBRemote::GetName ()
65{
66 if (m_thread_name.empty())
67 return NULL;
68 return m_thread_name.c_str();
69}
70
Greg Clayton0b90be12015-06-23 21:27:50 +000071void
72ThreadGDBRemote::ClearQueueInfo ()
73{
74 m_dispatch_queue_name.clear();
75 m_queue_kind = eQueueKindUnknown;
76 m_queue_serial = 0;
77}
78
79void
80ThreadGDBRemote::SetQueueInfo (std::string &&queue_name, QueueKind queue_kind, uint64_t queue_serial)
81{
82 m_dispatch_queue_name = queue_name;
83 m_queue_kind = queue_kind;
84 m_queue_serial = queue_serial;
85}
86
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
88const char *
89ThreadGDBRemote::GetQueueName ()
90{
Greg Clayton0b90be12015-06-23 21:27:50 +000091 // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...)
92 // with valid information that was gleaned from the stop reply packet. In this case we trust
93 // that the info is valid in m_dispatch_queue_name without refetching it
94 if (CachedQueueInfoIsValid())
95 {
96 if (m_dispatch_queue_name.empty())
97 return nullptr;
98 else
99 return m_dispatch_queue_name.c_str();
100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 // Always re-fetch the dispatch queue name since it can change
Greg Clayton1ac04c32012-02-21 00:09:25 +0000102
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000104 {
105 ProcessSP process_sp (GetProcess());
106 if (process_sp)
107 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000108 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
109 if (runtime)
Jason Molenda2fd83352014-02-05 05:44:54 +0000110 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
Greg Clayton0b90be12015-06-23 21:27:50 +0000111 else
112 m_dispatch_queue_name.clear();
113
114 if (!m_dispatch_queue_name.empty())
Jason Molenda3dc4f442013-10-18 05:55:24 +0000115 return m_dispatch_queue_name.c_str();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000116 }
117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 return NULL;
119}
120
Jason Molenda3dc4f442013-10-18 05:55:24 +0000121queue_id_t
122ThreadGDBRemote::GetQueueID ()
123{
Greg Clayton0b90be12015-06-23 21:27:50 +0000124 // If our cached queue info is valid, then someone called ThreadGDBRemote::SetQueueInfo(...)
125 // with valid information that was gleaned from the stop reply packet. In this case we trust
126 // that the info is valid in m_dispatch_queue_name without refetching it
127 if (CachedQueueInfoIsValid())
128 return m_queue_serial;
129
Jason Molenda3dc4f442013-10-18 05:55:24 +0000130 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
131 {
132 ProcessSP process_sp (GetProcess());
133 if (process_sp)
134 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000135 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
136 if (runtime)
Jason Molenda3dc4f442013-10-18 05:55:24 +0000137 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000138 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
Jason Molenda3dc4f442013-10-18 05:55:24 +0000139 }
140 }
141 }
142 return LLDB_INVALID_QUEUE_ID;
143}
144
Jason Molendab9ffa982014-04-25 00:01:15 +0000145QueueSP
146ThreadGDBRemote::GetQueue ()
147{
148 queue_id_t queue_id = GetQueueID();
149 QueueSP queue;
150 if (queue_id != LLDB_INVALID_QUEUE_ID)
151 {
152 ProcessSP process_sp (GetProcess());
153 if (process_sp)
154 {
155 queue = process_sp->GetQueueList().FindQueueByID (queue_id);
156 }
157 }
158 return queue;
159}
160
Jason Molendaaac16e02014-03-13 02:54:54 +0000161addr_t
162ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
163{
164 addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS;
165 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
166 {
167 ProcessSP process_sp (GetProcess());
168 if (process_sp)
169 {
170 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
171 if (runtime)
172 {
173 dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
174 }
175 }
176 }
177 return dispatch_queue_t_addr;
178}
179
Jason Molenda705b1802014-06-13 02:37:02 +0000180StructuredData::ObjectSP
181ThreadGDBRemote::FetchThreadExtendedInfo ()
182{
183 StructuredData::ObjectSP object_sp;
184 const lldb::user_id_t tid = GetProtocolID();
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000185 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jason Molenda705b1802014-06-13 02:37:02 +0000186 if (log)
187 log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid);
188 ProcessSP process_sp (GetProcess());
189 if (process_sp)
190 {
191 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
192 object_sp = gdb_process->GetExtendedInfoForThread (tid);
193 }
194 return object_sp;
195}
196
Greg Clayton160c9d82013-05-01 21:54:04 +0000197void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198ThreadGDBRemote::WillResume (StateType resume_state)
199{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000201 const lldb::user_id_t tid = GetProtocolID();
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000202 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000203 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000204 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000205
Greg Clayton1ac04c32012-02-21 00:09:25 +0000206 ProcessSP process_sp (GetProcess());
207 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000209 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
210 switch (resume_state)
211 {
212 case eStateSuspended:
213 case eStateStopped:
214 // Don't append anything for threads that should stay stopped.
215 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216
Greg Clayton1ac04c32012-02-21 00:09:25 +0000217 case eStateRunning:
218 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000219 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000220 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000221 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000222 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223
Greg Clayton1ac04c32012-02-21 00:09:25 +0000224 case eStateStepping:
225 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000226 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000227 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000228 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000229 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000230
Greg Clayton1ac04c32012-02-21 00:09:25 +0000231 default:
232 break;
233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
237void
238ThreadGDBRemote::RefreshStateAfterStop()
239{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000240 // Invalidate all registers in our register context. We don't set "force" to
241 // true because the stop reply packet might have had some register values
242 // that were expedited and these will already be copied into the register
243 // context by the time this function gets called. The GDBRemoteRegisterContext
244 // class has been made smart enough to detect when it needs to invalidate
245 // which registers are valid by putting hooks in the register read and
246 // register supply functions where they check the process stop ID and do
247 // the right thing.
248 const bool force = false;
249 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250}
251
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252bool
253ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
254{
255 return thread != 0;
256}
257
258void
259ThreadGDBRemote::Dump(Log *log, uint32_t index)
260{
261}
262
263
264bool
265ThreadGDBRemote::ShouldStop (bool &step_more)
266{
267 return true;
268}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000269lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270ThreadGDBRemote::GetRegisterContext ()
271{
272 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000273 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
274 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275}
276
Greg Clayton5ccbd292011-01-06 22:15:06 +0000277lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000278ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000280 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000281 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
283 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000284 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
Greg Clayton1ac04c32012-02-21 00:09:25 +0000286
Greg Clayton5ccbd292011-01-06 22:15:06 +0000287 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000288 {
289 ProcessSP process_sp (GetProcess());
290 if (process_sp)
291 {
292 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000293 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000294 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000295 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
296 }
297 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000298 else
299 {
300 Unwind *unwinder = GetUnwinder ();
301 if (unwinder)
302 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
303 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000304 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305}
306
Greg Claytone576ab22011-02-15 00:19:15 +0000307bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000308ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
309{
310 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
311 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000312 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000313}
314
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000315bool
316ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000318 ProcessSP process_sp (GetProcess());
319 if (process_sp)
Greg Clayton358cf1e2015-06-25 21:46:34 +0000320 return static_cast<ProcessGDBRemote *>(process_sp.get())->CalculateThreadStopInfo(this);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000321 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322}
323
324