1 //===-- NativeThreadDarwin.cpp -------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "NativeThreadDarwin.h"
16 #include "lldb/Core/Stream.h"
18 #include "NativeProcessDarwin.h"
21 using namespace lldb_private;
22 using namespace lldb_private::process_darwin;
24 uint64_t NativeThreadDarwin::GetGloballyUniqueThreadIDForMachPortID(
25 ::thread_t mach_port_id) {
26 thread_identifier_info_data_t tident;
27 mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT;
29 auto mach_err = ::thread_info(mach_port_id, THREAD_IDENTIFIER_INFO,
30 (thread_info_t)&tident, &tident_count);
31 if (mach_err != KERN_SUCCESS) {
32 // When we fail to get thread info for the supposed port, assume it is
33 // really a globally unique thread id already, or return the best thing
34 // we can, which is the thread port.
37 return tident.thread_id;
40 NativeThreadDarwin::NativeThreadDarwin(NativeProcessDarwin *process,
42 lldb::tid_t unique_thread_id,
43 ::thread_t mach_thread_port)
44 : NativeThreadProtocol(process, unique_thread_id),
45 m_mach_thread_port(mach_thread_port), m_basic_info(),
46 m_proc_threadinfo() {}
48 bool NativeThreadDarwin::GetIdentifierInfo() {
49 // Don't try to get the thread info once and cache it for the life of the
50 // thread. It changes over time, for instance
51 // if the thread name changes, then the thread_handle also changes... So you
52 // have to refetch it every time.
53 mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
54 kern_return_t kret = ::thread_info(m_mach_thread_port, THREAD_IDENTIFIER_INFO,
55 (thread_info_t)&m_ident_info, &count);
56 return kret == KERN_SUCCESS;
61 std::string NativeThreadDarwin::GetName() {
64 if (GetIdentifierInfo()) {
65 auto process_sp = GetProcess();
67 name = "<unavailable>";
71 int len = ::proc_pidinfo(process_sp->GetID(), PROC_PIDTHREADINFO,
72 m_ident_info.thread_handle, &m_proc_threadinfo,
73 sizeof(m_proc_threadinfo));
75 if (len && m_proc_threadinfo.pth_name[0])
76 name = m_proc_threadinfo.pth_name;
81 lldb::StateType NativeThreadDarwin::GetState() {
86 bool NativeThreadDarwin::GetStopReason(ThreadStopInfo &stop_info,
87 std::string &description) {
92 NativeRegisterContextSP NativeThreadDarwin::GetRegisterContext() {
94 return NativeRegisterContextSP();
97 Error NativeThreadDarwin::SetWatchpoint(lldb::addr_t addr, size_t size,
98 uint32_t watch_flags, bool hardware) {
100 error.SetErrorString("not yet implemented");
104 Error NativeThreadDarwin::RemoveWatchpoint(lldb::addr_t addr) {
106 error.SetErrorString("not yet implemented");
110 void NativeThreadDarwin::Dump(Stream &stream) const {
111 // This is what we really want once we have the thread class wired up.
113 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",
117 GetPC(INVALID_NUB_ADDRESS),
118 GetSP(INVALID_NUB_ADDRESS),
119 m_basic_info.user_time.seconds, m_basic_info.user_time.microseconds,
120 m_basic_info.system_time.seconds, m_basic_info.system_time.microseconds,
121 m_basic_info.cpu_usage,
123 m_basic_info.run_state,
126 m_basic_info.suspend_count, m_suspend_count,
127 m_basic_info.sleep_time);
130 // Here's all we have right now.
131 stream.Printf("tid: 0x%8.8" PRIx64 ", thread port: 0x%4.4x", GetID(),
136 bool NativeThreadDarwin::NotifyException(MachException::Data &exc) {
137 // TODO implement this.
139 // Allow the arch specific protocol to process (MachException::Data &)exc
140 // first before possible reassignment of m_stop_exception with exc.
141 // See also MachThread::GetStopException().
142 bool handled = m_arch_ap->NotifyException(exc);
144 if (m_stop_exception.IsValid())
146 // We may have more than one exception for a thread, but we need to
147 // only remember the one that we will say is the reason we stopped.
148 // We may have been single stepping and also gotten a signal exception,
149 // so just remember the most pertinent one.
150 if (m_stop_exception.IsBreakpoint())
151 m_stop_exception = exc;
155 m_stop_exception = exc;
160 // Pretend we handled it.
165 bool NativeThreadDarwin::ShouldStop(bool &step_more) const {
166 // TODO: implement this
168 // See if this thread is at a breakpoint?
169 DNBBreakpoint *bp = CurrentBreakpoint();
173 // This thread is sitting at a breakpoint, ask the breakpoint
174 // if we should be stopping here.
179 if (m_arch_ap->StepNotComplete())
184 // The thread state is used to let us know what the thread was
185 // trying to do. MachThread::ThreadWillResume() will set the
186 // thread state to various values depending if the thread was
187 // the current thread and if it was to be single stepped, or
189 if (GetState() == eStateRunning)
191 // If our state is running, then we should continue as we are in
192 // the process of stepping over a breakpoint.
197 // Stop if we have any kind of valid exception for this
199 if (GetStopException().IsValid())
209 void NativeThreadDarwin::ThreadDidStop() {
210 // TODO implement this.
212 // This thread has existed prior to resuming under debug nub control,
213 // and has just been stopped. Do any cleanup that needs to be done
216 // The thread state and breakpoint will still have the same values
217 // as they had prior to resuming the thread, so it makes it easy to check
218 // if we were trying to step a thread, or we tried to resume while being
221 // When this method gets called, the process state is still in the
222 // state it was in while running so we can act accordingly.
223 m_arch_ap->ThreadDidStop();
226 // We may have suspended this thread so the primary thread could step
227 // without worrying about race conditions, so lets restore our suspend
229 RestoreSuspendCountAfterStop();
231 // Update the basic information for a thread
232 MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info);
234 if (m_basic_info.suspend_count > 0)
235 SetState(eStateSuspended);
237 SetState(eStateStopped);
241 bool NativeThreadDarwin::MachPortNumberIsValid(::thread_t thread) {
242 return thread != (::thread_t)(0);
245 const struct thread_basic_info *NativeThreadDarwin::GetBasicInfo() const {
246 if (GetBasicInfo(m_mach_thread_port, &m_basic_info))
247 return &m_basic_info;
251 bool NativeThreadDarwin::GetBasicInfo(::thread_t thread,
252 struct thread_basic_info *basicInfoPtr) {
253 if (MachPortNumberIsValid(thread)) {
254 unsigned int info_count = THREAD_BASIC_INFO_COUNT;
255 kern_return_t err = ::thread_info(thread, THREAD_BASIC_INFO,
256 (thread_info_t)basicInfoPtr, &info_count);
257 if (err == KERN_SUCCESS)
260 ::memset(basicInfoPtr, 0, sizeof(struct thread_basic_info));
264 bool NativeThreadDarwin::IsUserReady() const {
265 if (m_basic_info.run_state == 0)
268 switch (m_basic_info.run_state) {
270 case TH_STATE_UNINTERRUPTIBLE:
273 case TH_STATE_RUNNING:
274 case TH_STATE_STOPPED:
275 case TH_STATE_WAITING:
276 case TH_STATE_HALTED:
282 NativeProcessDarwinSP NativeThreadDarwin::GetNativeProcessDarwinSP() {
283 return std::static_pointer_cast<NativeProcessDarwin>(GetProcess());