blob: e4108edc03ff539bf71ac199978c256faeeaf624 [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 (),
Jim Ingham77787032011-01-20 02:03:18 +000043 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
Greg Clayton1ac04c32012-02-21 00:09:25 +000045 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
46 this,
Jim Ingham4f465cf2012-10-10 18:32:14 +000047 process.GetID(),
Greg Clayton1ac04c32012-02-21 00:09:25 +000048 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049}
50
51ThreadGDBRemote::~ThreadGDBRemote ()
52{
Greg Clayton1ac04c32012-02-21 00:09:25 +000053 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());
Jim Ingham773d9812010-11-18 02:47:07 +000058 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059}
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061const char *
62ThreadGDBRemote::GetName ()
63{
64 if (m_thread_name.empty())
65 return NULL;
66 return m_thread_name.c_str();
67}
68
69
70const char *
71ThreadGDBRemote::GetQueueName ()
72{
73 // Always re-fetch the dispatch queue name since it can change
Greg Clayton1ac04c32012-02-21 00:09:25 +000074
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Clayton1ac04c32012-02-21 00:09:25 +000076 {
77 ProcessSP process_sp (GetProcess());
78 if (process_sp)
79 {
Jason Molenda2fd83352014-02-05 05:44:54 +000080 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
81 if (runtime)
Jason Molenda3dc4f442013-10-18 05:55:24 +000082 {
Jason Molenda2fd83352014-02-05 05:44:54 +000083 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
Jason Molenda3dc4f442013-10-18 05:55:24 +000084 }
85 if (m_dispatch_queue_name.length() > 0)
86 {
87 return m_dispatch_queue_name.c_str();
88 }
Greg Clayton1ac04c32012-02-21 00:09:25 +000089 }
90 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 return NULL;
92}
93
Jason Molenda3dc4f442013-10-18 05:55:24 +000094queue_id_t
95ThreadGDBRemote::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 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000102 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
103 if (runtime)
Jason Molenda3dc4f442013-10-18 05:55:24 +0000104 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000105 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
Jason Molenda3dc4f442013-10-18 05:55:24 +0000106 }
107 }
108 }
109 return LLDB_INVALID_QUEUE_ID;
110}
111
Jason Molendab9ffa982014-04-25 00:01:15 +0000112QueueSP
113ThreadGDBRemote::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
Jason Molendaaac16e02014-03-13 02:54:54 +0000128addr_t
129ThreadGDBRemote::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
Jason Molenda705b1802014-06-13 02:37:02 +0000147StructuredData::ObjectSP
148ThreadGDBRemote::FetchThreadExtendedInfo ()
149{
150 StructuredData::ObjectSP object_sp;
151 const lldb::user_id_t tid = GetProtocolID();
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000152 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jason Molenda705b1802014-06-13 02:37:02 +0000153 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
Greg Clayton160c9d82013-05-01 21:54:04 +0000164void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165ThreadGDBRemote::WillResume (StateType resume_state)
166{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000168 const lldb::user_id_t tid = GetProtocolID();
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000169 Log *log(GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000170 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000171 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000172
Greg Clayton1ac04c32012-02-21 00:09:25 +0000173 ProcessSP process_sp (GetProcess());
174 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000176 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183
Greg Clayton1ac04c32012-02-21 00:09:25 +0000184 case eStateRunning:
185 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000186 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000187 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000188 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000189 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190
Greg Clayton1ac04c32012-02-21 00:09:25 +0000191 case eStateStepping:
192 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000193 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000194 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000195 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000196 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000197
Greg Clayton1ac04c32012-02-21 00:09:25 +0000198 default:
199 break;
200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
204void
205ThreadGDBRemote::RefreshStateAfterStop()
206{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000207 // 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219bool
220ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
221{
222 return thread != 0;
223}
224
225void
226ThreadGDBRemote::Dump(Log *log, uint32_t index)
227{
228}
229
230
231bool
232ThreadGDBRemote::ShouldStop (bool &step_more)
233{
234 return true;
235}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000236lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237ThreadGDBRemote::GetRegisterContext ()
238{
239 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000240 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
241 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Greg Clayton5ccbd292011-01-06 22:15:06 +0000244lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000245ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000247 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000248 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249
250 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000251 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
Greg Clayton1ac04c32012-02-21 00:09:25 +0000253
Greg Clayton5ccbd292011-01-06 22:15:06 +0000254 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000255 {
256 ProcessSP process_sp (GetProcess());
257 if (process_sp)
258 {
259 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000260 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000261 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000262 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
263 }
264 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000265 else
266 {
267 Unwind *unwinder = GetUnwinder ();
268 if (unwinder)
269 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
270 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000271 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272}
273
Greg Claytone576ab22011-02-15 00:19:15 +0000274bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000275ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
276{
277 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
278 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000279 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000280}
281
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000282bool
283ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000285 ProcessSP process_sp (GetProcess());
286 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000288 StringExtractorGDBRemote stop_packet;
289 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
290 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
291 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000293 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
296