blob: 36fa418cf2b027cec9f21b03449b487bc1e147d9 [file] [log] [blame]
Chris Lattner24943d22010-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 Inghamf9f40c22011-02-08 05:20:59 +000016#include "lldb/Core/State.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000019#include "lldb/Target/StopInfo.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Target/Target.h"
21#include "lldb/Target/Unwind.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000022#include "lldb/Breakpoint/Watchpoint.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "ProcessGDBRemote.h"
25#include "ProcessGDBRemoteLog.h"
Greg Clayton54e7afa2010-07-09 20:39:50 +000026#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31//----------------------------------------------------------------------
32// Thread Registers
33//----------------------------------------------------------------------
34
Jim Ingham94a5d0d2012-10-10 18:32:14 +000035ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
36 Thread(process, tid),
Chris Lattner24943d22010-06-08 16:52:24 +000037 m_thread_name (),
38 m_dispatch_queue_name (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +000039 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +000040{
Greg Claytonf4124de2012-02-21 00:09:25 +000041 ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
42 this,
Jim Ingham94a5d0d2012-10-10 18:32:14 +000043 process.GetID(),
Greg Claytonf4124de2012-02-21 00:09:25 +000044 GetID());
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47ThreadGDBRemote::~ThreadGDBRemote ()
48{
Greg Claytonf4124de2012-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 Inghamcdea2362010-11-18 02:47:07 +000054 DestroyThread();
Chris Lattner24943d22010-06-08 16:52:24 +000055}
56
Chris Lattner24943d22010-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 Claytonf4124de2012-02-21 00:09:25 +000070
Chris Lattner24943d22010-06-08 16:52:24 +000071 if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
Greg Claytonf4124de2012-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 Lattner24943d22010-06-08 16:52:24 +000080 return NULL;
81}
82
83bool
84ThreadGDBRemote::WillResume (StateType resume_state)
85{
Greg Clayton8f6be2a2010-10-09 01:40:57 +000086 // Call the Thread::WillResume first. If we stop at a signal, the stop info
87 // class for signal will set the resume signal that we need below. The signal
88 // stuff obeys the Process::UnixSignal defaults.
Jim Ingham0c8fa2d2012-09-01 01:02:41 +000089 // If the thread's WillResume returns false, that means that we aren't going to actually resume,
90 // in which case we should not do the rest of our "resume" work.
91
92 if (!Thread::WillResume(resume_state))
93 return false;
94
95 ClearStackFrames();
Greg Clayton8f6be2a2010-10-09 01:40:57 +000096
Chris Lattner24943d22010-06-08 16:52:24 +000097 int signo = GetResumeSignal();
Jim Ingham6bb73372011-10-15 00:21:37 +000098 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
Jim Inghamf9f40c22011-02-08 05:20:59 +000099 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000100 log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state));
Greg Clayton643ee732010-08-04 01:40:35 +0000101
Greg Claytonf4124de2012-02-21 00:09:25 +0000102 ProcessSP process_sp (GetProcess());
103 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000104 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000105 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
106 switch (resume_state)
107 {
108 case eStateSuspended:
109 case eStateStopped:
110 // Don't append anything for threads that should stay stopped.
111 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000112
Greg Claytonf4124de2012-02-21 00:09:25 +0000113 case eStateRunning:
114 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
115 gdb_process->m_continue_C_tids.push_back(std::make_pair(GetID(), signo));
116 else
117 gdb_process->m_continue_c_tids.push_back(GetID());
118 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000119
Greg Claytonf4124de2012-02-21 00:09:25 +0000120 case eStateStepping:
121 if (gdb_process->GetUnixSignals().SignalIsValid (signo))
122 gdb_process->m_continue_S_tids.push_back(std::make_pair(GetID(), signo));
123 else
124 gdb_process->m_continue_s_tids.push_back(GetID());
125 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000126
Greg Claytonf4124de2012-02-21 00:09:25 +0000127 default:
128 break;
129 }
130 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000131 }
Greg Claytonf4124de2012-02-21 00:09:25 +0000132 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000133}
134
135void
136ThreadGDBRemote::RefreshStateAfterStop()
137{
Greg Claytona875b642011-01-09 21:07:35 +0000138 // Invalidate all registers in our register context. We don't set "force" to
139 // true because the stop reply packet might have had some register values
140 // that were expedited and these will already be copied into the register
141 // context by the time this function gets called. The GDBRemoteRegisterContext
142 // class has been made smart enough to detect when it needs to invalidate
143 // which registers are valid by putting hooks in the register read and
144 // register supply functions where they check the process stop ID and do
145 // the right thing.
146 const bool force = false;
147 GetRegisterContext()->InvalidateIfNeeded (force);
Chris Lattner24943d22010-06-08 16:52:24 +0000148}
149
Chris Lattner24943d22010-06-08 16:52:24 +0000150void
151ThreadGDBRemote::ClearStackFrames ()
152{
153 Unwind *unwinder = GetUnwinder ();
154 if (unwinder)
155 unwinder->Clear();
156 Thread::ClearStackFrames();
157}
158
159
160bool
161ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
162{
163 return thread != 0;
164}
165
166void
167ThreadGDBRemote::Dump(Log *log, uint32_t index)
168{
169}
170
171
172bool
173ThreadGDBRemote::ShouldStop (bool &step_more)
174{
175 return true;
176}
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000177lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000178ThreadGDBRemote::GetRegisterContext ()
179{
180 if (m_reg_context_sp.get() == NULL)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000181 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
182 return m_reg_context_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000183}
184
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000185lldb::RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +0000186ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
187{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000188 lldb::RegisterContextSP reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000189 const bool read_all_registers_at_once = false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000190 uint32_t concrete_frame_idx = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000191
192 if (frame)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000193 concrete_frame_idx = frame->GetConcreteFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000194
Greg Claytonf4124de2012-02-21 00:09:25 +0000195
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000196 if (concrete_frame_idx == 0)
Greg Claytonf4124de2012-02-21 00:09:25 +0000197 {
198 ProcessSP process_sp (GetProcess());
199 if (process_sp)
200 {
201 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
202 reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
203 }
204 }
Greg Clayton33ed1702010-08-24 00:45:41 +0000205 else if (m_unwinder_ap.get())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000206 reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
207 return reg_ctx_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000208}
209
Greg Claytonc3c46612011-02-15 00:19:15 +0000210bool
Greg Claytona875b642011-01-09 21:07:35 +0000211ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
212{
213 GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
214 assert (gdb_reg_ctx);
Greg Claytonc3c46612011-02-15 00:19:15 +0000215 return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
Greg Claytona875b642011-01-09 21:07:35 +0000216}
217
Greg Clayton643ee732010-08-04 01:40:35 +0000218lldb::StopInfoSP
219ThreadGDBRemote::GetPrivateStopReason ()
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
Greg Claytonf4124de2012-02-21 00:09:25 +0000221 ProcessSP process_sp (GetProcess());
222 if (process_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000223 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000224 const uint32_t process_stop_id = process_sp->GetStopID();
225 if (m_thread_stop_reason_stop_id != process_stop_id ||
226 (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
227 {
Jim Ingham6bc24c12012-10-16 00:09:33 +0000228 if (IsStillAtLastBreakpointHit())
229 return m_actual_stop_info_sp;
230
Greg Claytonf4124de2012-02-21 00:09:25 +0000231 // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
232 // for this thread, then m_actual_stop_info_sp will not ever contain
233 // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
234 // check will never be able to tell us if we have the correct stop info
235 // for this thread and we will continually send qThreadStopInfo packets
236 // down to the remote GDB server, so we need to keep our own notion
237 // of the stop ID that m_actual_stop_info_sp is valid for (even if it
238 // contains nothing). We use m_thread_stop_reason_stop_id for this below.
239 m_thread_stop_reason_stop_id = process_stop_id;
240 m_actual_stop_info_sp.reset();
Greg Clayton643ee732010-08-04 01:40:35 +0000241
Greg Claytonf4124de2012-02-21 00:09:25 +0000242 StringExtractorGDBRemote stop_packet;
243 ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
244 if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetID(), stop_packet))
245 gdb_process->SetThreadStopInfo (stop_packet);
246 }
Chris Lattner24943d22010-06-08 16:52:24 +0000247 }
Greg Clayton643ee732010-08-04 01:40:35 +0000248 return m_actual_stop_info_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000249}
250
251