blob: 5c70cb5427cbd79c005656c9bc7f8b4354126027 [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"
24#include "lldb/Target/Unwind.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "ProcessGDBRemote.h"
27#include "ProcessGDBRemoteLog.h"
Greg Claytonc982c762010-07-09 20:39:50 +000028#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// Thread Registers
35//----------------------------------------------------------------------
36
Jim Ingham4f465cf2012-10-10 18:32:14 +000037ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
38 Thread(process, tid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039 m_thread_name (),
40 m_dispatch_queue_name (),
Jim Ingham77787032011-01-20 02:03:18 +000041 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042{
Greg Clayton1ac04c32012-02-21 00:09:25 +000043 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
44 this,
Jim Ingham4f465cf2012-10-10 18:32:14 +000045 process.GetID(),
Greg Clayton1ac04c32012-02-21 00:09:25 +000046 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
49ThreadGDBRemote::~ThreadGDBRemote ()
50{
Greg Clayton1ac04c32012-02-21 00:09:25 +000051 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());
Jim Ingham773d9812010-11-18 02:47:07 +000056 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057}
58
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059const char *
60ThreadGDBRemote::GetName ()
61{
62 if (m_thread_name.empty())
63 return NULL;
64 return m_thread_name.c_str();
65}
66
67
68const char *
69ThreadGDBRemote::GetQueueName ()
70{
71 // Always re-fetch the dispatch queue name since it can change
Greg Clayton1ac04c32012-02-21 00:09:25 +000072
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Clayton1ac04c32012-02-21 00:09:25 +000074 {
75 ProcessSP process_sp (GetProcess());
76 if (process_sp)
77 {
Jason Molenda2fd83352014-02-05 05:44:54 +000078 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
79 if (runtime)
Jason Molenda3dc4f442013-10-18 05:55:24 +000080 {
Jason Molenda2fd83352014-02-05 05:44:54 +000081 m_dispatch_queue_name = runtime->GetQueueNameFromThreadQAddress (m_thread_dispatch_qaddr);
Jason Molenda3dc4f442013-10-18 05:55:24 +000082 }
83 if (m_dispatch_queue_name.length() > 0)
84 {
85 return m_dispatch_queue_name.c_str();
86 }
Greg Clayton1ac04c32012-02-21 00:09:25 +000087 }
88 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 return NULL;
90}
91
Jason Molenda3dc4f442013-10-18 05:55:24 +000092queue_id_t
93ThreadGDBRemote::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 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000100 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
101 if (runtime)
Jason Molenda3dc4f442013-10-18 05:55:24 +0000102 {
Jason Molenda2fd83352014-02-05 05:44:54 +0000103 return runtime->GetQueueIDFromThreadQAddress (m_thread_dispatch_qaddr);
Jason Molenda3dc4f442013-10-18 05:55:24 +0000104 }
105 }
106 }
107 return LLDB_INVALID_QUEUE_ID;
108}
109
Jason Molendab9ffa982014-04-25 00:01:15 +0000110QueueSP
111ThreadGDBRemote::GetQueue ()
112{
113 queue_id_t queue_id = GetQueueID();
114 QueueSP queue;
115 if (queue_id != LLDB_INVALID_QUEUE_ID)
116 {
117 ProcessSP process_sp (GetProcess());
118 if (process_sp)
119 {
120 queue = process_sp->GetQueueList().FindQueueByID (queue_id);
121 }
122 }
123 return queue;
124}
125
Jason Molendaaac16e02014-03-13 02:54:54 +0000126addr_t
127ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
128{
129 addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS;
130 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
131 {
132 ProcessSP process_sp (GetProcess());
133 if (process_sp)
134 {
135 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
136 if (runtime)
137 {
138 dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
139 }
140 }
141 }
142 return dispatch_queue_t_addr;
143}
144
Jason Molenda705b1802014-06-13 02:37:02 +0000145StructuredData::ObjectSP
146ThreadGDBRemote::FetchThreadExtendedInfo ()
147{
148 StructuredData::ObjectSP object_sp;
149 const lldb::user_id_t tid = GetProtocolID();
150 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
151 if (log)
152 log->Printf ("Fetching extended information for thread %4.4" PRIx64, tid);
153 ProcessSP process_sp (GetProcess());
154 if (process_sp)
155 {
156 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
157 object_sp = gdb_process->GetExtendedInfoForThread (tid);
158 }
159 return object_sp;
160}
161
Greg Clayton160c9d82013-05-01 21:54:04 +0000162void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163ThreadGDBRemote::WillResume (StateType resume_state)
164{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000166 const lldb::user_id_t tid = GetProtocolID();
Greg Clayton5160ce52013-03-27 23:08:40 +0000167 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000168 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000169 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000170
Greg Clayton1ac04c32012-02-21 00:09:25 +0000171 ProcessSP process_sp (GetProcess());
172 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000174 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
175 switch (resume_state)
176 {
177 case eStateSuspended:
178 case eStateStopped:
179 // Don't append anything for threads that should stay stopped.
180 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181
Greg Clayton1ac04c32012-02-21 00:09:25 +0000182 case eStateRunning:
183 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000184 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000185 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000186 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000187 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
Greg Clayton1ac04c32012-02-21 00:09:25 +0000189 case eStateStepping:
190 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000191 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000192 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000193 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000194 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000195
Greg Clayton1ac04c32012-02-21 00:09:25 +0000196 default:
197 break;
198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200}
201
202void
203ThreadGDBRemote::RefreshStateAfterStop()
204{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000205 // Invalidate all registers in our register context. We don't set "force" to
206 // true because the stop reply packet might have had some register values
207 // that were expedited and these will already be copied into the register
208 // context by the time this function gets called. The GDBRemoteRegisterContext
209 // class has been made smart enough to detect when it needs to invalidate
210 // which registers are valid by putting hooks in the register read and
211 // register supply functions where they check the process stop ID and do
212 // the right thing.
213 const bool force = false;
214 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217bool
218ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
219{
220 return thread != 0;
221}
222
223void
224ThreadGDBRemote::Dump(Log *log, uint32_t index)
225{
226}
227
228
229bool
230ThreadGDBRemote::ShouldStop (bool &step_more)
231{
232 return true;
233}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000234lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235ThreadGDBRemote::GetRegisterContext ()
236{
237 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000238 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
239 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Greg Clayton5ccbd292011-01-06 22:15:06 +0000242lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000243ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000245 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000246 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247
248 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000249 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Greg Clayton1ac04c32012-02-21 00:09:25 +0000251
Greg Clayton5ccbd292011-01-06 22:15:06 +0000252 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000253 {
254 ProcessSP process_sp (GetProcess());
255 if (process_sp)
256 {
257 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000258 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000259 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000260 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
261 }
262 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000263 else
264 {
265 Unwind *unwinder = GetUnwinder ();
266 if (unwinder)
267 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
268 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000269 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270}
271
Greg Claytone576ab22011-02-15 00:19:15 +0000272bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000273ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
274{
275 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
276 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000277 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000278}
279
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000280bool
281ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000283 ProcessSP process_sp (GetProcess());
284 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000286 StringExtractorGDBRemote stop_packet;
287 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
288 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
289 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000291 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292}
293
294