blob: 395f8c6e0560e19e5a393bdfd74baba982b401b6 [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 Molendaaac16e02014-03-13 02:54:54 +0000110addr_t
111ThreadGDBRemote::GetQueueLibdispatchQueueAddress ()
112{
113 addr_t dispatch_queue_t_addr = LLDB_INVALID_ADDRESS;
114 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
115 {
116 ProcessSP process_sp (GetProcess());
117 if (process_sp)
118 {
119 SystemRuntime *runtime = process_sp->GetSystemRuntime ();
120 if (runtime)
121 {
122 dispatch_queue_t_addr = runtime->GetLibdispatchQueueAddressFromThreadQAddress (m_thread_dispatch_qaddr);
123 }
124 }
125 }
126 return dispatch_queue_t_addr;
127}
128
Greg Clayton160c9d82013-05-01 21:54:04 +0000129void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130ThreadGDBRemote::WillResume (StateType resume_state)
131{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000133 const lldb::user_id_t tid = GetProtocolID();
Greg Clayton5160ce52013-03-27 23:08:40 +0000134 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000135 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000136 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000137
Greg Clayton1ac04c32012-02-21 00:09:25 +0000138 ProcessSP process_sp (GetProcess());
139 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000141 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
142 switch (resume_state)
143 {
144 case eStateSuspended:
145 case eStateStopped:
146 // Don't append anything for threads that should stay stopped.
147 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Greg Clayton1ac04c32012-02-21 00:09:25 +0000149 case eStateRunning:
150 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000151 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000152 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000153 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000154 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
Greg Clayton1ac04c32012-02-21 00:09:25 +0000156 case eStateStepping:
157 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000158 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000159 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000160 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000161 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000162
Greg Clayton1ac04c32012-02-21 00:09:25 +0000163 default:
164 break;
165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169void
170ThreadGDBRemote::RefreshStateAfterStop()
171{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000172 // Invalidate all registers in our register context. We don't set "force" to
173 // true because the stop reply packet might have had some register values
174 // that were expedited and these will already be copied into the register
175 // context by the time this function gets called. The GDBRemoteRegisterContext
176 // class has been made smart enough to detect when it needs to invalidate
177 // which registers are valid by putting hooks in the register read and
178 // register supply functions where they check the process stop ID and do
179 // the right thing.
180 const bool force = false;
181 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184bool
185ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
186{
187 return thread != 0;
188}
189
190void
191ThreadGDBRemote::Dump(Log *log, uint32_t index)
192{
193}
194
195
196bool
197ThreadGDBRemote::ShouldStop (bool &step_more)
198{
199 return true;
200}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000201lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202ThreadGDBRemote::GetRegisterContext ()
203{
204 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000205 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
206 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Greg Clayton5ccbd292011-01-06 22:15:06 +0000209lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000210ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000212 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000213 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214
215 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000216 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
Greg Clayton1ac04c32012-02-21 00:09:25 +0000218
Greg Clayton5ccbd292011-01-06 22:15:06 +0000219 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000220 {
221 ProcessSP process_sp (GetProcess());
222 if (process_sp)
223 {
224 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000225 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000226 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000227 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
228 }
229 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000230 else
231 {
232 Unwind *unwinder = GetUnwinder ();
233 if (unwinder)
234 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
235 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000236 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Greg Claytone576ab22011-02-15 00:19:15 +0000239bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000240ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
241{
242 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
243 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000244 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000245}
246
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000247bool
248ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000250 ProcessSP process_sp (GetProcess());
251 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000253 StringExtractorGDBRemote stop_packet;
254 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
255 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
256 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000258 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259}
260
261