blob: 9f68d104942eb4fe16ca714f57dcd24b47a54d7d [file] [log] [blame]
Greg Clayton0fa51242011-07-19 03:57:15 +00001//===-- ThreadKDP.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 "ThreadKDP.h"
12
13#include "llvm/Support/MachO.h"
14
15#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/StreamString.h"
18#include "lldb/Core/State.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Target/StopInfo.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Unwind.h"
Johnny Chenecd4feb2011-10-14 00:42:25 +000024#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000025
26#include "ProcessKDP.h"
27#include "ProcessKDPLog.h"
28#include "RegisterContextKDP_arm.h"
29#include "RegisterContextKDP_i386.h"
30#include "RegisterContextKDP_x86_64.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000031
32using namespace lldb;
33using namespace lldb_private;
34
35//----------------------------------------------------------------------
36// Thread Registers
37//----------------------------------------------------------------------
38
Greg Claytonf4124de2012-02-21 00:09:25 +000039ThreadKDP::ThreadKDP (const lldb::ProcessSP &process_sp, lldb::tid_t tid) :
40 Thread(process_sp, tid),
Greg Clayton0fa51242011-07-19 03:57:15 +000041 m_thread_name (),
42 m_dispatch_queue_name (),
43 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
44{
Greg Claytonf4124de2012-02-21 00:09:25 +000045 ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this, GetID());
Greg Clayton0fa51242011-07-19 03:57:15 +000046}
47
48ThreadKDP::~ThreadKDP ()
49{
Greg Claytonf4124de2012-02-21 00:09:25 +000050 ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this, GetID());
Greg Clayton0fa51242011-07-19 03:57:15 +000051 DestroyThread();
52}
53
Greg Clayton0fa51242011-07-19 03:57:15 +000054const char *
55ThreadKDP::GetName ()
56{
57 if (m_thread_name.empty())
58 return NULL;
59 return m_thread_name.c_str();
60}
61
62const char *
63ThreadKDP::GetQueueName ()
64{
65 return NULL;
66}
67
68bool
69ThreadKDP::WillResume (StateType resume_state)
70{
71 ClearStackFrames();
72 // Call the Thread::WillResume first. If we stop at a signal, the stop info
73 // class for signal will set the resume signal that we need below. The signal
74 // stuff obeys the Process::UnixSignal defaults.
75 Thread::WillResume(resume_state);
76
77 lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP));
78 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +000079 log->Printf ("Resuming thread: %4.4llx with state: %s.", GetID(), StateAsCString(resume_state));
Greg Clayton0fa51242011-07-19 03:57:15 +000080
81// ProcessKDP &process = GetKDPProcess();
82// switch (resume_state)
83// {
84// case eStateSuspended:
85// case eStateStopped:
86// // Don't append anything for threads that should stay stopped.
87// break;
88//
89// case eStateRunning:
90// case eStateStepping:
91// break;
92//
93// default:
94// break;
95// }
96 return true;
97}
98
99void
100ThreadKDP::RefreshStateAfterStop()
101{
102 // Invalidate all registers in our register context. We don't set "force" to
103 // true because the stop reply packet might have had some register values
104 // that were expedited and these will already be copied into the register
105 // context by the time this function gets called. The KDPRegisterContext
106 // class has been made smart enough to detect when it needs to invalidate
107 // which registers are valid by putting hooks in the register read and
108 // register supply functions where they check the process stop ID and do
109 // the right thing.
110 const bool force = false;
111 GetRegisterContext()->InvalidateIfNeeded (force);
112}
113
Greg Clayton0fa51242011-07-19 03:57:15 +0000114void
115ThreadKDP::ClearStackFrames ()
116{
117 Unwind *unwinder = GetUnwinder ();
118 if (unwinder)
119 unwinder->Clear();
120 Thread::ClearStackFrames();
121}
122
123
124bool
125ThreadKDP::ThreadIDIsValid (lldb::tid_t thread)
126{
127 return thread != 0;
128}
129
130void
131ThreadKDP::Dump(Log *log, uint32_t index)
132{
133}
134
135
136bool
137ThreadKDP::ShouldStop (bool &step_more)
138{
139 return true;
140}
141lldb::RegisterContextSP
142ThreadKDP::GetRegisterContext ()
143{
144 if (m_reg_context_sp.get() == NULL)
145 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
146 return m_reg_context_sp;
147}
148
149lldb::RegisterContextSP
150ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame)
151{
152 lldb::RegisterContextSP reg_ctx_sp;
153 uint32_t concrete_frame_idx = 0;
154
155 if (frame)
156 concrete_frame_idx = frame->GetConcreteFrameIndex ();
157
158 if (concrete_frame_idx == 0)
159 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000160 ProcessSP process_sp (CalculateProcess());
161 if (process_sp)
Greg Clayton0fa51242011-07-19 03:57:15 +0000162 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000163 switch (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().GetCPUType())
164 {
165 case llvm::MachO::CPUTypeARM:
166 reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx));
167 break;
168 case llvm::MachO::CPUTypeI386:
169 reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx));
170 break;
171 case llvm::MachO::CPUTypeX86_64:
172 reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx));
173 break;
174 default:
175 assert (!"Add CPU type support in KDP");
176 break;
177 }
Greg Clayton0fa51242011-07-19 03:57:15 +0000178 }
179 }
180 else if (m_unwinder_ap.get())
181 reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
182 return reg_ctx_sp;
183}
184
185lldb::StopInfoSP
186ThreadKDP::GetPrivateStopReason ()
187{
Greg Claytonf4124de2012-02-21 00:09:25 +0000188 ProcessSP process_sp (GetProcess());
189 if (process_sp)
Greg Clayton0fa51242011-07-19 03:57:15 +0000190 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000191 const uint32_t process_stop_id = process_sp->GetStopID();
192 if (m_thread_stop_reason_stop_id != process_stop_id ||
193 (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
194 {
195 // TODO: can we query the initial state of the thread here?
196 // For now I am just going to pretend that a SIGSTOP happened.
Greg Clayton0fa51242011-07-19 03:57:15 +0000197
Greg Claytonf4124de2012-02-21 00:09:25 +0000198 SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
Greg Clayton0fa51242011-07-19 03:57:15 +0000199
Greg Claytonf4124de2012-02-21 00:09:25 +0000200 // If GetKDPProcess().SetThreadStopInfo() doesn't find a stop reason
201 // for this thread, then m_actual_stop_info_sp will not ever contain
202 // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
203 // check will never be able to tell us if we have the correct stop info
204 // for this thread and we will continually send qThreadStopInfo packets
205 // down to the remote KDP server, so we need to keep our own notion
206 // of the stop ID that m_actual_stop_info_sp is valid for (even if it
207 // contains nothing). We use m_thread_stop_reason_stop_id for this below.
208 // m_thread_stop_reason_stop_id = process_stop_id;
209 // m_actual_stop_info_sp.reset();
Greg Clayton0fa51242011-07-19 03:57:15 +0000210
Greg Claytonf4124de2012-02-21 00:09:25 +0000211 }
Greg Clayton0fa51242011-07-19 03:57:15 +0000212 }
213 return m_actual_stop_info_sp;
214}
215
216