blob: 4e475c80bdab3553e1e729a3b36502a52ba9b083 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/Target.h"
23#include "lldb/Target/Unwind.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "ProcessGDBRemote.h"
26#include "ProcessGDBRemoteLog.h"
Greg Claytonc982c762010-07-09 20:39:50 +000027#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29using namespace lldb;
30using namespace lldb_private;
31
32//----------------------------------------------------------------------
33// Thread Registers
34//----------------------------------------------------------------------
35
Jim Ingham4f465cf2012-10-10 18:32:14 +000036ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
37 Thread(process, tid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038 m_thread_name (),
39 m_dispatch_queue_name (),
Jim Ingham77787032011-01-20 02:03:18 +000040 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
Greg Clayton1ac04c32012-02-21 00:09:25 +000042 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
43 this,
Jim Ingham4f465cf2012-10-10 18:32:14 +000044 process.GetID(),
Greg Clayton1ac04c32012-02-21 00:09:25 +000045 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
48ThreadGDBRemote::~ThreadGDBRemote ()
49{
Greg Clayton1ac04c32012-02-21 00:09:25 +000050 ProcessSP process_sp(GetProcess());
51 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
52 this,
53 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
54 GetID());
Jim Ingham773d9812010-11-18 02:47:07 +000055 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058const char *
59ThreadGDBRemote::GetName ()
60{
61 if (m_thread_name.empty())
62 return NULL;
63 return m_thread_name.c_str();
64}
65
66
67const char *
68ThreadGDBRemote::GetQueueName ()
69{
70 // Always re-fetch the dispatch queue name since it can change
Greg Clayton1ac04c32012-02-21 00:09:25 +000071
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Clayton1ac04c32012-02-21 00:09:25 +000073 {
74 ProcessSP process_sp (GetProcess());
75 if (process_sp)
76 {
Jason Molenda3dc4f442013-10-18 05:55:24 +000077 PlatformSP platform_sp (process_sp->GetTarget().GetPlatform());
78 if (platform_sp)
79 {
80 m_dispatch_queue_name = platform_sp->GetQueueNameForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr);
81 }
82 if (m_dispatch_queue_name.length() > 0)
83 {
84 return m_dispatch_queue_name.c_str();
85 }
Greg Clayton1ac04c32012-02-21 00:09:25 +000086 }
87 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 return NULL;
89}
90
Jason Molenda3dc4f442013-10-18 05:55:24 +000091queue_id_t
92ThreadGDBRemote::GetQueueID ()
93{
94 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
95 {
96 ProcessSP process_sp (GetProcess());
97 if (process_sp)
98 {
99 PlatformSP platform_sp (process_sp->GetTarget().GetPlatform());
100 if (platform_sp)
101 {
102 return platform_sp->GetQueueIDForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr);
103 }
104 }
105 }
106 return LLDB_INVALID_QUEUE_ID;
107}
108
Greg Clayton160c9d82013-05-01 21:54:04 +0000109void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110ThreadGDBRemote::WillResume (StateType resume_state)
111{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +0000113 const lldb::user_id_t tid = GetProtocolID();
Greg Clayton5160ce52013-03-27 23:08:40 +0000114 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +0000115 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000116 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000117
Greg Clayton1ac04c32012-02-21 00:09:25 +0000118 ProcessSP process_sp (GetProcess());
119 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000121 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
122 switch (resume_state)
123 {
124 case eStateSuspended:
125 case eStateStopped:
126 // Don't append anything for threads that should stay stopped.
127 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128
Greg Clayton1ac04c32012-02-21 00:09:25 +0000129 case eStateRunning:
130 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000131 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000132 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000133 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000134 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Greg Clayton1ac04c32012-02-21 00:09:25 +0000136 case eStateStepping:
137 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000138 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000139 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000140 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000141 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000142
Greg Clayton1ac04c32012-02-21 00:09:25 +0000143 default:
144 break;
145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
149void
150ThreadGDBRemote::RefreshStateAfterStop()
151{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000152 // Invalidate all registers in our register context. We don't set "force" to
153 // true because the stop reply packet might have had some register values
154 // that were expedited and these will already be copied into the register
155 // context by the time this function gets called. The GDBRemoteRegisterContext
156 // class has been made smart enough to detect when it needs to invalidate
157 // which registers are valid by putting hooks in the register read and
158 // register supply functions where they check the process stop ID and do
159 // the right thing.
160 const bool force = false;
161 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164bool
165ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
166{
167 return thread != 0;
168}
169
170void
171ThreadGDBRemote::Dump(Log *log, uint32_t index)
172{
173}
174
175
176bool
177ThreadGDBRemote::ShouldStop (bool &step_more)
178{
179 return true;
180}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000181lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182ThreadGDBRemote::GetRegisterContext ()
183{
184 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000185 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
186 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187}
188
Greg Clayton5ccbd292011-01-06 22:15:06 +0000189lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000190ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000192 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000193 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194
195 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000196 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197
Greg Clayton1ac04c32012-02-21 00:09:25 +0000198
Greg Clayton5ccbd292011-01-06 22:15:06 +0000199 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000200 {
201 ProcessSP process_sp (GetProcess());
202 if (process_sp)
203 {
204 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000205 // read_all_registers_at_once will be true if 'p' packet is not supported.
Sean Callananb1de1142013-09-04 23:24:15 +0000206 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
Greg Clayton1ac04c32012-02-21 00:09:25 +0000207 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
208 }
209 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000210 else
211 {
212 Unwind *unwinder = GetUnwinder ();
213 if (unwinder)
214 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
215 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000216 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
Greg Claytone576ab22011-02-15 00:19:15 +0000219bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000220ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
221{
222 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
223 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000224 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000225}
226
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000227bool
228ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000230 ProcessSP process_sp (GetProcess());
231 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000233 StringExtractorGDBRemote stop_packet;
234 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
235 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
236 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000238 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239}
240
241