blob: b405f03463cc34a87e112bec0c338c323cd53469 [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
13#include "lldb/Core/ArchSpec.h"
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham0f16e732011-02-08 05:20:59 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000019#include "lldb/Target/StopInfo.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Target/Target.h"
21#include "lldb/Target/Unwind.h"
Johnny Chen01a67862011-10-14 00:42:25 +000022#include "lldb/Breakpoint/Watchpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "ProcessGDBRemote.h"
25#include "ProcessGDBRemoteLog.h"
Greg Claytonc982c762010-07-09 20:39:50 +000026#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31//----------------------------------------------------------------------
32// Thread Registers
33//----------------------------------------------------------------------
34
Jim Ingham4f465cf2012-10-10 18:32:14 +000035ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
36 Thread(process, tid),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037 m_thread_name (),
38 m_dispatch_queue_name (),
Jim Ingham77787032011-01-20 02:03:18 +000039 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040{
Greg Clayton1ac04c32012-02-21 00:09:25 +000041 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
42 this,
Jim Ingham4f465cf2012-10-10 18:32:14 +000043 process.GetID(),
Greg Clayton1ac04c32012-02-21 00:09:25 +000044 GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
47ThreadGDBRemote::~ThreadGDBRemote ()
48{
Greg Clayton1ac04c32012-02-21 00:09:25 +000049 ProcessSP process_sp(GetProcess());
50 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
51 this,
52 process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
53 GetID());
Jim Ingham773d9812010-11-18 02:47:07 +000054 DestroyThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057const char *
58ThreadGDBRemote::GetName ()
59{
60 if (m_thread_name.empty())
61 return NULL;
62 return m_thread_name.c_str();
63}
64
65
66const char *
67ThreadGDBRemote::GetQueueName ()
68{
69 // Always re-fetch the dispatch queue name since it can change
Greg Clayton1ac04c32012-02-21 00:09:25 +000070
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Clayton1ac04c32012-02-21 00:09:25 +000072 {
73 ProcessSP process_sp (GetProcess());
74 if (process_sp)
75 {
76 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
77 return gdb_process->GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
78 }
79 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 return NULL;
81}
82
Greg Clayton160c9d82013-05-01 21:54:04 +000083void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084ThreadGDBRemote::WillResume (StateType resume_state)
85{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 int signo = GetResumeSignal();
Greg Clayton160c9d82013-05-01 21:54:04 +000087 const lldb::user_id_t tid = GetProtocolID();
Greg Clayton5160ce52013-03-27 23:08:40 +000088 Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Ingham0f16e732011-02-08 05:20:59 +000089 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +000090 log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
Greg Claytonf4b47e12010-08-04 01:40:35 +000091
Greg Clayton1ac04c32012-02-21 00:09:25 +000092 ProcessSP process_sp (GetProcess());
93 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 {
Greg Clayton1ac04c32012-02-21 00:09:25 +000095 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
96 switch (resume_state)
97 {
98 case eStateSuspended:
99 case eStateStopped:
100 // Don't append anything for threads that should stay stopped.
101 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Greg Clayton1ac04c32012-02-21 00:09:25 +0000103 case eStateRunning:
104 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000105 gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000106 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000107 gdb_process->m_continue_c_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000108 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
Greg Clayton1ac04c32012-02-21 00:09:25 +0000110 case eStateStepping:
111 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
Greg Clayton160c9d82013-05-01 21:54:04 +0000112 gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
Greg Clayton1ac04c32012-02-21 00:09:25 +0000113 else
Greg Clayton160c9d82013-05-01 21:54:04 +0000114 gdb_process->m_continue_s_tids.push_back(tid);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000115 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000116
Greg Clayton1ac04c32012-02-21 00:09:25 +0000117 default:
118 break;
119 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
123void
124ThreadGDBRemote::RefreshStateAfterStop()
125{
Greg Clayton3e06bd92011-01-09 21:07:35 +0000126 // Invalidate all registers in our register context. We don't set "force" to
127 // true because the stop reply packet might have had some register values
128 // that were expedited and these will already be copied into the register
129 // context by the time this function gets called. The GDBRemoteRegisterContext
130 // class has been made smart enough to detect when it needs to invalidate
131 // which registers are valid by putting hooks in the register read and
132 // register supply functions where they check the process stop ID and do
133 // the right thing.
134 const bool force = false;
135 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138bool
139ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
140{
141 return thread != 0;
142}
143
144void
145ThreadGDBRemote::Dump(Log *log, uint32_t index)
146{
147}
148
149
150bool
151ThreadGDBRemote::ShouldStop (bool &step_more)
152{
153 return true;
154}
Greg Clayton5ccbd292011-01-06 22:15:06 +0000155lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156ThreadGDBRemote::GetRegisterContext ()
157{
158 if (m_reg_context_sp.get() == NULL)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000159 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
160 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
Greg Clayton5ccbd292011-01-06 22:15:06 +0000163lldb::RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
165{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000166 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000167 uint32_t concrete_frame_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
169 if (frame)
Greg Clayton5ccbd292011-01-06 22:15:06 +0000170 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
Greg Clayton1ac04c32012-02-21 00:09:25 +0000172
Greg Clayton5ccbd292011-01-06 22:15:06 +0000173 if (concrete_frame_idx == 0)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000174 {
175 ProcessSP process_sp (GetProcess());
176 if (process_sp)
177 {
178 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000179 // read_all_registers_at_once will be true if 'p' packet is not supported.
180 bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported ();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000181 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
182 }
183 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000184 else
185 {
186 Unwind *unwinder = GetUnwinder ();
187 if (unwinder)
188 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
189 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000190 return reg_ctx_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191}
192
Greg Claytone576ab22011-02-15 00:19:15 +0000193bool
Greg Clayton3e06bd92011-01-09 21:07:35 +0000194ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
195{
196 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
197 assert (gdb_reg_ctx);
Greg Claytone576ab22011-02-15 00:19:15 +0000198 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Clayton3e06bd92011-01-09 21:07:35 +0000199}
200
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000201bool
202ThreadGDBRemote::CalculateStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000204 ProcessSP process_sp (GetProcess());
205 if (process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000207 StringExtractorGDBRemote stop_packet;
208 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
209 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
210 return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000212 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213}
214
215