blob: fb524deda813706f72d61d530e735864a12783a1 [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
Greg Clayton160c9d82013-05-01 21:54:04 +0000110void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111ThreadGDBRemote::WillResume (StateType resume_state)
112{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000114 const lldb::user_id_t tid = GetProtocolID();
Greg Clayton5160ce52013-03-27 23:08:40 +0000115 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000116 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000117 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000118
Greg Clayton1ac04c32012-02-21 00:09:25 +0000119 ProcessSP process_sp (GetProcess());
120 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000122 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
123 switch (resume_state)
124 {
125 case eStateSuspended:
126 case eStateStopped:
127 // Don't append anything for threads that should stay stopped.
128 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129
Greg Clayton1ac04c32012-02-21 00:09:25 +0000130 case eStateRunning:
131 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000132 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000133 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000134 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000135 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136
Greg Clayton1ac04c32012-02-21 00:09:25 +0000137 case eStateStepping:
138 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000139 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000140 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000141 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000142 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000143
Greg Clayton1ac04c32012-02-21 00:09:25 +0000144 default:
145 break;
146 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148}
149
150void
151ThreadGDBRemote::RefreshStateAfterStop()
152{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000153 // Invalidate all registers in our register context. We don't set "force" to
154 // true because the stop reply packet might have had some register values
155 // that were expedited and these will already be copied into the register
156 // context by the time this function gets called. The GDBRemoteRegisterContext
157 // class has been made smart enough to detect when it needs to invalidate
158 // which registers are valid by putting hooks in the register read and
159 // register supply functions where they check the process stop ID and do
160 // the right thing.
161 const bool force = false;
162 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163}
164
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165bool
166ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
167{
168 return thread != 0;
169}
170
171void
172ThreadGDBRemote::Dump(Log *log, uint32_t index)
173{
174}
175
176
177bool
178ThreadGDBRemote::ShouldStop (bool &step_more)
179{
180 return true;
181}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000182lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183ThreadGDBRemote::GetRegisterContext ()
184{
185 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000186 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
187 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188}
189
Greg Clayton5ccbd292011-01-06 22:15:06 +0000190lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000191ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000193 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000194 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
196 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000197 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198
Greg Clayton1ac04c32012-02-21 00:09:25 +0000199
Greg Clayton5ccbd292011-01-06 22:15:06 +0000200 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000201 {
202 ProcessSP process_sp (GetProcess());
203 if (process_sp)
204 {
205 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000206 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000207 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000208 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
209 }
210 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000211 else
212 {
213 Unwind *unwinder = GetUnwinder ();
214 if (unwinder)
215 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
216 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000217 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218}
219
Greg Claytone576ab22011-02-15 00:19:15 +0000220bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000221ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
222{
223 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
224 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000225 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000226}
227
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000228bool
229ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000231 ProcessSP process_sp (GetProcess());
232 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000234 StringExtractorGDBRemote stop_packet;
235 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
236 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
237 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000239 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
242