blob: 08d1a8da92bd87002bcd0f8025a9737f722c8651 [file] [log] [blame]
Todd Fialae77fce02016-09-04 00:18:56 +00001//===-- NativeThreadDarwin.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#include "NativeThreadDarwin.h"
11
12// C includes
13#include <libproc.h>
14
15// LLDB includes
16#include "lldb/Core/Stream.h"
17
18#include "NativeProcessDarwin.h"
19
20using namespace lldb;
21using namespace lldb_private;
22using namespace lldb_private::process_darwin;
23
24uint64_t
25NativeThreadDarwin::GetGloballyUniqueThreadIDForMachPortID(
26 ::thread_t mach_port_id)
27{
28 thread_identifier_info_data_t tident;
29 mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT;
30
31 auto mach_err = ::thread_info(mach_port_id, THREAD_IDENTIFIER_INFO,
32 (thread_info_t) &tident, &tident_count);
33 if (mach_err != KERN_SUCCESS)
34 {
35 // When we fail to get thread info for the supposed port, assume it is
36 // really a globally unique thread id already, or return the best thing
37 // we can, which is the thread port.
38 return mach_port_id;
39 }
40 return tident.thread_id;
41}
42
43NativeThreadDarwin::NativeThreadDarwin(NativeProcessDarwin *process,
44 bool is_64_bit,
45 lldb::tid_t unique_thread_id,
46 ::thread_t mach_thread_port) :
47 NativeThreadProtocol(process, unique_thread_id),
48 m_mach_thread_port(mach_thread_port),
49 m_basic_info(),
50 m_proc_threadinfo()
51{
52}
53
54bool
55NativeThreadDarwin::GetIdentifierInfo()
56{
57 // Don't try to get the thread info once and cache it for the life of the thread. It changes over time, for instance
58 // if the thread name changes, then the thread_handle also changes... So you have to refetch it every time.
59 mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
60 kern_return_t kret = ::thread_info(m_mach_thread_port,
61 THREAD_IDENTIFIER_INFO,
62 (thread_info_t) &m_ident_info, &count);
63 return kret == KERN_SUCCESS;
64
65 return false;
66}
67
68std::string
69NativeThreadDarwin::GetName()
70{
71 std::string name;
72
73 if (GetIdentifierInfo())
74 {
75 auto process_sp = GetProcess();
76 if (!process_sp)
77 {
78 name = "<unavailable>";
79 return name;
80 }
81
82 int len = ::proc_pidinfo(process_sp->GetID(), PROC_PIDTHREADINFO,
83 m_ident_info.thread_handle, &m_proc_threadinfo,
84 sizeof(m_proc_threadinfo));
85
86 if (len && m_proc_threadinfo.pth_name[0])
87 name = m_proc_threadinfo.pth_name;
88 }
89 return name;
90}
91
92lldb::StateType
93NativeThreadDarwin::GetState()
94{
95 // TODO implement
96 return eStateInvalid;
97}
98
99bool
100NativeThreadDarwin::GetStopReason(ThreadStopInfo &stop_info,
101 std::string& description)
102{
103 // TODO implement
104 return false;
105}
106
107NativeRegisterContextSP
108NativeThreadDarwin::GetRegisterContext()
109{
110 // TODO implement
111 return NativeRegisterContextSP();
112}
113
114Error
115NativeThreadDarwin::SetWatchpoint(lldb::addr_t addr, size_t size,
116 uint32_t watch_flags, bool hardware)
117{
118 Error error;
119 error.SetErrorString("not yet implemented");
120 return error;
121}
122
123Error
124NativeThreadDarwin::RemoveWatchpoint(lldb::addr_t addr)
125{
126 Error error;
127 error.SetErrorString("not yet implemented");
128 return error;
129}
130
131void
132NativeThreadDarwin::Dump(Stream &stream) const
133{
134 // This is what we really want once we have the thread class wired up.
135#if 0
136 DNBLogThreaded("[%3u] #%3u tid: 0x%8.8" PRIx64 ", pc: 0x%16.16" PRIx64 ", sp: 0x%16.16" PRIx64 ", user: %d.%6.6d, system: %d.%6.6d, cpu: %2d, policy: %2d, run_state: %2d (%s), flags: %2d, suspend_count: %2d (current %2d), sleep_time: %d",
137 index,
138 m_seq_id,
139 m_unique_id,
140 GetPC(INVALID_NUB_ADDRESS),
141 GetSP(INVALID_NUB_ADDRESS),
142 m_basic_info.user_time.seconds, m_basic_info.user_time.microseconds,
143 m_basic_info.system_time.seconds, m_basic_info.system_time.microseconds,
144 m_basic_info.cpu_usage,
145 m_basic_info.policy,
146 m_basic_info.run_state,
147 thread_run_state,
148 m_basic_info.flags,
149 m_basic_info.suspend_count, m_suspend_count,
150 m_basic_info.sleep_time);
151
152#else
153 // Here's all we have right now.
154 stream.Printf("tid: 0x%8.8" PRIx64 ", thread port: 0x%4.4x",
155 GetID(), m_mach_thread_port);
156#endif
157}
158
159bool
160NativeThreadDarwin::NotifyException(MachException::Data &exc)
161{
162 // TODO implement this.
163#if 0
164 // Allow the arch specific protocol to process (MachException::Data &)exc
165 // first before possible reassignment of m_stop_exception with exc.
166 // See also MachThread::GetStopException().
167 bool handled = m_arch_ap->NotifyException(exc);
168
169 if (m_stop_exception.IsValid())
170 {
171 // We may have more than one exception for a thread, but we need to
172 // only remember the one that we will say is the reason we stopped.
173 // We may have been single stepping and also gotten a signal exception,
174 // so just remember the most pertinent one.
175 if (m_stop_exception.IsBreakpoint())
176 m_stop_exception = exc;
177 }
178 else
179 {
180 m_stop_exception = exc;
181 }
182
183 return handled;
184#else
185 // Pretend we handled it.
186 return true;
187#endif
188}
189
190bool
191NativeThreadDarwin::ShouldStop(bool &step_more) const
192{
193 // TODO: implement this
194#if 0
195 // See if this thread is at a breakpoint?
196 DNBBreakpoint *bp = CurrentBreakpoint();
197
198 if (bp)
199 {
200 // This thread is sitting at a breakpoint, ask the breakpoint
201 // if we should be stopping here.
202 return true;
203 }
204 else
205 {
206 if (m_arch_ap->StepNotComplete())
207 {
208 step_more = true;
209 return false;
210 }
211 // The thread state is used to let us know what the thread was
212 // trying to do. MachThread::ThreadWillResume() will set the
213 // thread state to various values depending if the thread was
214 // the current thread and if it was to be single stepped, or
215 // resumed.
216 if (GetState() == eStateRunning)
217 {
218 // If our state is running, then we should continue as we are in
219 // the process of stepping over a breakpoint.
220 return false;
221 }
222 else
223 {
224 // Stop if we have any kind of valid exception for this
225 // thread.
226 if (GetStopException().IsValid())
227 return true;
228 }
229 }
230 return false;
231#else
232 return false;
233#endif
234}
235
236void
237NativeThreadDarwin::ThreadDidStop()
238{
239 // TODO implement this.
240#if 0
241 // This thread has existed prior to resuming under debug nub control,
242 // and has just been stopped. Do any cleanup that needs to be done
243 // after running.
244
245 // The thread state and breakpoint will still have the same values
246 // as they had prior to resuming the thread, so it makes it easy to check
247 // if we were trying to step a thread, or we tried to resume while being
248 // at a breakpoint.
249
250 // When this method gets called, the process state is still in the
251 // state it was in while running so we can act accordingly.
252 m_arch_ap->ThreadDidStop();
253
254
255 // We may have suspended this thread so the primary thread could step
256 // without worrying about race conditions, so lets restore our suspend
257 // count.
258 RestoreSuspendCountAfterStop();
259
260 // Update the basic information for a thread
261 MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info);
262
263 if (m_basic_info.suspend_count > 0)
264 SetState(eStateSuspended);
265 else
266 SetState(eStateStopped);
267#endif
268}
269
270bool
271NativeThreadDarwin::MachPortNumberIsValid(::thread_t thread)
272{
273 return thread != (::thread_t)(0);
274}
275
276const struct thread_basic_info *
277NativeThreadDarwin::GetBasicInfo() const
278{
279 if (GetBasicInfo(m_mach_thread_port, &m_basic_info))
280 return &m_basic_info;
281 return NULL;
282}
283
284bool
285NativeThreadDarwin::GetBasicInfo(::thread_t thread,
286 struct thread_basic_info *basicInfoPtr)
287{
288 if (MachPortNumberIsValid(thread))
289 {
290 unsigned int info_count = THREAD_BASIC_INFO_COUNT;
291 kern_return_t err = ::thread_info (thread, THREAD_BASIC_INFO, (thread_info_t) basicInfoPtr, &info_count);
292 if (err == KERN_SUCCESS)
293 return true;
294 }
295 ::memset (basicInfoPtr, 0, sizeof (struct thread_basic_info));
296 return false;
297}
298
299bool
300NativeThreadDarwin::IsUserReady() const
301{
302 if (m_basic_info.run_state == 0)
303 GetBasicInfo();
304
305 switch (m_basic_info.run_state)
306 {
307 default:
308 case TH_STATE_UNINTERRUPTIBLE:
309 break;
310
311 case TH_STATE_RUNNING:
312 case TH_STATE_STOPPED:
313 case TH_STATE_WAITING:
314 case TH_STATE_HALTED:
315 return true;
316 }
317 return false;
318}
319
320NativeProcessDarwinSP
321NativeThreadDarwin::GetNativeProcessDarwinSP()
322{
323 return std::static_pointer_cast<NativeProcessDarwin>(GetProcess());
324}