1 //===-- MachThread.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 //  Created by Greg Clayton on 6/19/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include <inttypes.h>
15 #include "MachThread.h"
16 #include "MachProcess.h"
17 #include "DNBLog.h"
18 #include "DNB.h"
19 
20 static uint32_t
21 GetSequenceID()
22 {
23     static uint32_t g_nextID = 0;
24     return ++g_nextID;
25 }
26 
27 MachThread::MachThread (MachProcess *process, uint64_t unique_thread_id, thread_t mach_port_num) :
28     m_process (process),
29     m_unique_id (unique_thread_id),
30     m_mach_port_number (mach_port_num),
31     m_seq_id (GetSequenceID()),
32     m_state (eStateUnloaded),
33     m_state_mutex (PTHREAD_MUTEX_RECURSIVE),
34     m_break_id (INVALID_NUB_BREAK_ID),
35     m_suspend_count (0),
36     m_stop_exception (),
37     m_arch_ap (DNBArchProtocol::Create (this)),
38     m_reg_sets (NULL),
39     m_num_reg_sets (0),
40     m_ident_info(),
41     m_proc_threadinfo(),
42     m_dispatch_queue_name()
43 {
44     nub_size_t num_reg_sets = 0;
45     m_reg_sets = m_arch_ap->GetRegisterSetInfo (&num_reg_sets);
46     m_num_reg_sets = num_reg_sets;
47 
48     // Get the thread state so we know if a thread is in a state where we can't
49     // muck with it and also so we get the suspend count correct in case it was
50     // already suspended
51     GetBasicInfo();
52     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::MachThread ( process = %p, tid = 0x%8.8" PRIx64 ", seq_id = %u )", &m_process, m_unique_id, m_seq_id);
53 }
54 
55 MachThread::~MachThread()
56 {
57     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::~MachThread() for tid = 0x%8.8" PRIx64 " (%u)", m_unique_id, m_seq_id);
58 }
59 
60 
61 
62 void
63 MachThread::Suspend()
64 {
65     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
66     if (MachPortNumberIsValid(m_mach_port_number))
67     {
68         DNBError err(::thread_suspend (m_mach_port_number), DNBError::MachKernel);
69         if (err.Success())
70             m_suspend_count++;
71         if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
72             err.LogThreaded("::thread_suspend (%4.4" PRIx32 ")", m_mach_port_number);
73     }
74 }
75 
76 void
77 MachThread::Resume(bool others_stopped)
78 {
79     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
80     if (MachPortNumberIsValid(m_mach_port_number))
81     {
82         SetSuspendCountBeforeResume(others_stopped);
83     }
84 }
85 
86 bool
87 MachThread::SetSuspendCountBeforeResume(bool others_stopped)
88 {
89     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
90     DNBError err;
91     if (MachPortNumberIsValid(m_mach_port_number) == false)
92         return false;
93 
94     size_t times_to_resume;
95 
96     if (others_stopped)
97     {
98         if (GetBasicInfo())
99         {
100             times_to_resume = m_basic_info.suspend_count;
101             m_suspend_count = - (times_to_resume - m_suspend_count);
102         }
103         else
104             times_to_resume = 0;
105     }
106     else
107     {
108         times_to_resume = m_suspend_count;
109         m_suspend_count = 0;
110     }
111 
112     if (times_to_resume > 0)
113     {
114         while (times_to_resume > 0)
115         {
116             err = ::thread_resume (m_mach_port_number);
117             if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
118                 err.LogThreaded("::thread_resume (%4.4" PRIx32 ")", m_mach_port_number);
119             if (err.Success())
120                 --times_to_resume;
121             else
122             {
123                 if (GetBasicInfo())
124                     times_to_resume = m_basic_info.suspend_count;
125                 else
126                     times_to_resume = 0;
127             }
128         }
129     }
130     return true;
131 }
132 
133 bool
134 MachThread::RestoreSuspendCountAfterStop ()
135 {
136     DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", __FUNCTION__);
137     DNBError err;
138     if (MachPortNumberIsValid(m_mach_port_number) == false)
139         return false;
140 
141     if (m_suspend_count > 0)
142     {
143         while (m_suspend_count > 0)
144         {
145             err = ::thread_resume (m_mach_port_number);
146             if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
147                 err.LogThreaded("::thread_resume (%4.4" PRIx32 ")", m_mach_port_number);
148             if (err.Success())
149                 --m_suspend_count;
150             else
151             {
152                 if (GetBasicInfo())
153                     m_suspend_count = m_basic_info.suspend_count;
154                 else
155                     m_suspend_count = 0;
156                 return false; // ???
157             }
158         }
159     }
160     else if (m_suspend_count < 0)
161     {
162         while (m_suspend_count < 0)
163         {
164             err = ::thread_suspend (m_mach_port_number);
165             if (err.Success())
166                 ++m_suspend_count;
167             if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
168             {
169                 err.LogThreaded("::thread_suspend (%4.4" PRIx32 ")", m_mach_port_number);
170                 return false;
171             }
172         }
173     }
174     return true;
175 }
176 
177 
178 const char *
179 MachThread::GetBasicInfoAsString () const
180 {
181     static char g_basic_info_string[1024];
182     struct thread_basic_info basicInfo;
183 
184     if (GetBasicInfo(m_mach_port_number, &basicInfo))
185     {
186 
187 //        char run_state_str[32];
188 //        size_t run_state_str_size = sizeof(run_state_str);
189 //        switch (basicInfo.run_state)
190 //        {
191 //        case TH_STATE_RUNNING:          strncpy(run_state_str, "running", run_state_str_size); break;
192 //        case TH_STATE_STOPPED:          strncpy(run_state_str, "stopped", run_state_str_size); break;
193 //        case TH_STATE_WAITING:          strncpy(run_state_str, "waiting", run_state_str_size); break;
194 //        case TH_STATE_UNINTERRUPTIBLE:  strncpy(run_state_str, "uninterruptible", run_state_str_size); break;
195 //        case TH_STATE_HALTED:           strncpy(run_state_str, "halted", run_state_str_size); break;
196 //        default:                        snprintf(run_state_str, run_state_str_size, "%d", basicInfo.run_state); break;    // ???
197 //        }
198         float user = (float)basicInfo.user_time.seconds + (float)basicInfo.user_time.microseconds / 1000000.0f;
199         float system = (float)basicInfo.user_time.seconds + (float)basicInfo.user_time.microseconds / 1000000.0f;
200         snprintf(g_basic_info_string, sizeof(g_basic_info_string), "Thread 0x%8.8" PRIx64 ": user=%f system=%f cpu=%d sleep_time=%d",
201             m_unique_id,
202             user,
203             system,
204             basicInfo.cpu_usage,
205             basicInfo.sleep_time);
206 
207         return g_basic_info_string;
208     }
209     return NULL;
210 }
211 
212 // Finds the Mach port number for a given thread in the inferior process' port namespace.
213 thread_t
214 MachThread::InferiorThreadID() const
215 {
216     mach_msg_type_number_t i;
217     mach_port_name_array_t names;
218     mach_port_type_array_t types;
219     mach_msg_type_number_t ncount, tcount;
220     thread_t inferior_tid = INVALID_NUB_THREAD;
221     task_t my_task = ::mach_task_self();
222     task_t task = m_process->Task().TaskPort();
223 
224     kern_return_t kret = ::mach_port_names (task, &names, &ncount, &types, &tcount);
225     if (kret == KERN_SUCCESS)
226     {
227 
228         for (i = 0; i < ncount; i++)
229         {
230             mach_port_t my_name;
231             mach_msg_type_name_t my_type;
232 
233             kret = ::mach_port_extract_right (task, names[i], MACH_MSG_TYPE_COPY_SEND, &my_name, &my_type);
234             if (kret == KERN_SUCCESS)
235             {
236                 ::mach_port_deallocate (my_task, my_name);
237                 if (my_name == m_mach_port_number)
238                 {
239                     inferior_tid = names[i];
240                     break;
241                 }
242             }
243         }
244         // Free up the names and types
245         ::vm_deallocate (my_task, (vm_address_t) names, ncount * sizeof (mach_port_name_t));
246         ::vm_deallocate (my_task, (vm_address_t) types, tcount * sizeof (mach_port_type_t));
247     }
248     return inferior_tid;
249 }
250 
251 bool
252 MachThread::IsUserReady()
253 {
254     if (m_basic_info.run_state == 0)
255         GetBasicInfo ();
256 
257     switch (m_basic_info.run_state)
258     {
259     default:
260     case TH_STATE_UNINTERRUPTIBLE:
261         break;
262 
263     case TH_STATE_RUNNING:
264     case TH_STATE_STOPPED:
265     case TH_STATE_WAITING:
266     case TH_STATE_HALTED:
267         return true;
268     }
269     return false;
270 }
271 
272 struct thread_basic_info *
273 MachThread::GetBasicInfo ()
274 {
275     if (MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info))
276         return &m_basic_info;
277     return NULL;
278 }
279 
280 
281 bool
282 MachThread::GetBasicInfo(thread_t thread, struct thread_basic_info *basicInfoPtr)
283 {
284     if (MachPortNumberIsValid(thread))
285     {
286         unsigned int info_count = THREAD_BASIC_INFO_COUNT;
287         kern_return_t err = ::thread_info (thread, THREAD_BASIC_INFO, (thread_info_t) basicInfoPtr, &info_count);
288         if (err == KERN_SUCCESS)
289             return true;
290     }
291     ::memset (basicInfoPtr, 0, sizeof (struct thread_basic_info));
292     return false;
293 }
294 
295 
296 bool
297 MachThread::ThreadIDIsValid(uint64_t thread)
298 {
299     return thread != 0;
300 }
301 
302 bool
303 MachThread::MachPortNumberIsValid(thread_t thread)
304 {
305     return thread != THREAD_NULL;
306 }
307 
308 bool
309 MachThread::GetRegisterState(int flavor, bool force)
310 {
311     return m_arch_ap->GetRegisterState(flavor, force) == KERN_SUCCESS;
312 }
313 
314 bool
315 MachThread::SetRegisterState(int flavor)
316 {
317     return m_arch_ap->SetRegisterState(flavor) == KERN_SUCCESS;
318 }
319 
320 uint64_t
321 MachThread::GetPC(uint64_t failValue)
322 {
323     // Get program counter
324     return m_arch_ap->GetPC(failValue);
325 }
326 
327 bool
328 MachThread::SetPC(uint64_t value)
329 {
330     // Set program counter
331     return m_arch_ap->SetPC(value);
332 }
333 
334 uint64_t
335 MachThread::GetSP(uint64_t failValue)
336 {
337     // Get stack pointer
338     return m_arch_ap->GetSP(failValue);
339 }
340 
341 nub_process_t
342 MachThread::ProcessID() const
343 {
344     if (m_process)
345         return m_process->ProcessID();
346     return INVALID_NUB_PROCESS;
347 }
348 
349 void
350 MachThread::Dump(uint32_t index)
351 {
352     const char * thread_run_state = NULL;
353 
354     switch (m_basic_info.run_state)
355     {
356     case TH_STATE_RUNNING:          thread_run_state = "running"; break;    // 1 thread is running normally
357     case TH_STATE_STOPPED:          thread_run_state = "stopped"; break;    // 2 thread is stopped
358     case TH_STATE_WAITING:          thread_run_state = "waiting"; break;    // 3 thread is waiting normally
359     case TH_STATE_UNINTERRUPTIBLE:  thread_run_state = "uninter"; break;    // 4 thread is in an uninterruptible wait
360     case TH_STATE_HALTED:           thread_run_state = "halted "; break;     // 5 thread is halted at a
361     default:                        thread_run_state = "???"; break;
362     }
363 
364     DNBLogThreaded("[%3u] #%3u tid: 0x%8.8" PRIx64 ", pc: 0x%16.16" PRIx64 ", sp: 0x%16.16" PRIx64 ", breakID: %3d, 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",
365         index,
366         m_seq_id,
367         m_unique_id,
368         GetPC(INVALID_NUB_ADDRESS),
369         GetSP(INVALID_NUB_ADDRESS),
370         m_break_id,
371         m_basic_info.user_time.seconds,      m_basic_info.user_time.microseconds,
372         m_basic_info.system_time.seconds,    m_basic_info.system_time.microseconds,
373         m_basic_info.cpu_usage,
374         m_basic_info.policy,
375         m_basic_info.run_state,
376         thread_run_state,
377         m_basic_info.flags,
378         m_basic_info.suspend_count, m_suspend_count,
379         m_basic_info.sleep_time);
380     //DumpRegisterState(0);
381 }
382 
383 void
384 MachThread::ThreadWillResume(const DNBThreadResumeAction *thread_action, bool others_stopped)
385 {
386     if (thread_action->addr != INVALID_NUB_ADDRESS)
387         SetPC (thread_action->addr);
388 
389     SetState (thread_action->state);
390     switch (thread_action->state)
391     {
392     case eStateStopped:
393     case eStateSuspended:
394         assert (others_stopped == false);
395         Suspend();
396         break;
397 
398     case eStateRunning:
399     case eStateStepping:
400         Resume(others_stopped);
401         break;
402     default:
403         break;
404     }
405     m_arch_ap->ThreadWillResume();
406     m_stop_exception.Clear();
407 }
408 
409 nub_break_t
410 MachThread::CurrentBreakpoint()
411 {
412     return m_process->Breakpoints().FindIDByAddress(GetPC());
413 }
414 
415 bool
416 MachThread::ShouldStop(bool &step_more)
417 {
418     // See if this thread is at a breakpoint?
419     nub_break_t breakID = CurrentBreakpoint();
420 
421     if (NUB_BREAK_ID_IS_VALID(breakID))
422     {
423         // This thread is sitting at a breakpoint, ask the breakpoint
424         // if we should be stopping here.
425         if (Process()->Breakpoints().ShouldStop(ProcessID(), ThreadID(), breakID))
426             return true;
427         else
428         {
429             // The breakpoint said we shouldn't stop, but we may have gotten
430             // a signal or the user may have requested to stop in some other
431             // way. Stop if we have a valid exception (this thread won't if
432             // another thread was the reason this process stopped) and that
433             // exception, is NOT a breakpoint exception (a common case would
434             // be a SIGINT signal).
435             if (GetStopException().IsValid() && !GetStopException().IsBreakpoint())
436                 return true;
437         }
438     }
439     else
440     {
441         if (m_arch_ap->StepNotComplete())
442         {
443             step_more = true;
444             return false;
445         }
446         // The thread state is used to let us know what the thread was
447         // trying to do. MachThread::ThreadWillResume() will set the
448         // thread state to various values depending if the thread was
449         // the current thread and if it was to be single stepped, or
450         // resumed.
451         if (GetState() == eStateRunning)
452         {
453             // If our state is running, then we should continue as we are in
454             // the process of stepping over a breakpoint.
455             return false;
456         }
457         else
458         {
459             // Stop if we have any kind of valid exception for this
460             // thread.
461             if (GetStopException().IsValid())
462                 return true;
463         }
464     }
465     return false;
466 }
467 bool
468 MachThread::IsStepping()
469 {
470 #if ENABLE_AUTO_STEPPING_OVER_BP
471     // Return true if this thread is currently being stepped.
472     // MachThread::ThreadWillResume currently determines this by looking if we
473     // have been asked to single step, or if we are at a breakpoint instruction
474     // and have been asked to resume. In the latter case we need to disable the
475     // breakpoint we are at, single step, re-enable and continue.
476     nub_state_t state = GetState();
477     return ((state == eStateStepping) ||
478             (state == eStateRunning && NUB_BREAK_ID_IS_VALID(CurrentBreakpoint())));
479 #else
480     return GetState() == eStateStepping;
481 #endif
482 }
483 
484 
485 bool
486 MachThread::ThreadDidStop()
487 {
488     // This thread has existed prior to resuming under debug nub control,
489     // and has just been stopped. Do any cleanup that needs to be done
490     // after running.
491 
492     // The thread state and breakpoint will still have the same values
493     // as they had prior to resuming the thread, so it makes it easy to check
494     // if we were trying to step a thread, or we tried to resume while being
495     // at a breakpoint.
496 
497     // When this method gets called, the process state is still in the
498     // state it was in while running so we can act accordingly.
499     m_arch_ap->ThreadDidStop();
500 
501 
502     // We may have suspended this thread so the primary thread could step
503     // without worrying about race conditions, so lets restore our suspend
504     // count.
505     RestoreSuspendCountAfterStop();
506 
507     // Update the basic information for a thread
508     MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info);
509 
510 #if ENABLE_AUTO_STEPPING_OVER_BP
511     // See if we were at a breakpoint when we last resumed that we disabled,
512     // re-enable it.
513     nub_break_t breakID = CurrentBreakpoint();
514 
515     if (NUB_BREAK_ID_IS_VALID(breakID))
516     {
517         m_process->EnableBreakpoint(breakID);
518         if (m_basic_info.suspend_count > 0)
519         {
520             SetState(eStateSuspended);
521         }
522         else
523         {
524             // If we last were at a breakpoint and we single stepped, our state
525             // will be "running" to indicate we need to continue after stepping
526             // over the breakpoint instruction. If we step over a breakpoint
527             // instruction, we need to stop.
528             if (GetState() == eStateRunning)
529             {
530                 // Leave state set to running so we will continue automatically
531                 // from this breakpoint
532             }
533             else
534             {
535                 SetState(eStateStopped);
536             }
537         }
538     }
539     else
540     {
541         if (m_basic_info.suspend_count > 0)
542         {
543             SetState(eStateSuspended);
544         }
545         else
546         {
547             SetState(eStateStopped);
548         }
549     }
550 #else
551     if (m_basic_info.suspend_count > 0)
552         SetState(eStateSuspended);
553     else
554         SetState(eStateStopped);
555 #endif
556     return true;
557 }
558 
559 bool
560 MachThread::NotifyException(MachException::Data& exc)
561 {
562     // Allow the arch specific protocol to process (MachException::Data &)exc
563     // first before possible reassignment of m_stop_exception with exc.
564     // See also MachThread::GetStopException().
565     bool handled = m_arch_ap->NotifyException(exc);
566 
567     if (m_stop_exception.IsValid())
568     {
569         // We may have more than one exception for a thread, but we need to
570         // only remember the one that we will say is the reason we stopped.
571         // We may have been single stepping and also gotten a signal exception,
572         // so just remember the most pertinent one.
573         if (m_stop_exception.IsBreakpoint())
574             m_stop_exception = exc;
575     }
576     else
577     {
578         m_stop_exception = exc;
579     }
580 
581     return handled;
582 }
583 
584 
585 nub_state_t
586 MachThread::GetState()
587 {
588     // If any other threads access this we will need a mutex for it
589     PTHREAD_MUTEX_LOCKER (locker, m_state_mutex);
590     return m_state;
591 }
592 
593 void
594 MachThread::SetState(nub_state_t state)
595 {
596     PTHREAD_MUTEX_LOCKER (locker, m_state_mutex);
597     m_state = state;
598     DNBLogThreadedIf(LOG_THREAD, "MachThread::SetState ( %s ) for tid = 0x%8.8" PRIx64 "", DNBStateAsString(state), m_unique_id);
599 }
600 
601 uint32_t
602 MachThread::GetNumRegistersInSet(int regSet) const
603 {
604     if (regSet < m_num_reg_sets)
605         return m_reg_sets[regSet].num_registers;
606     return 0;
607 }
608 
609 const char *
610 MachThread::GetRegisterSetName(int regSet) const
611 {
612     if (regSet < m_num_reg_sets)
613         return m_reg_sets[regSet].name;
614     return NULL;
615 }
616 
617 const DNBRegisterInfo *
618 MachThread::GetRegisterInfo(int regSet, int regIndex) const
619 {
620     if (regSet < m_num_reg_sets)
621         if (regIndex < m_reg_sets[regSet].num_registers)
622             return &m_reg_sets[regSet].registers[regIndex];
623     return NULL;
624 }
625 void
626 MachThread::DumpRegisterState(int regSet)
627 {
628     if (regSet == REGISTER_SET_ALL)
629     {
630         for (regSet = 1; regSet < m_num_reg_sets; regSet++)
631             DumpRegisterState(regSet);
632     }
633     else
634     {
635         if (m_arch_ap->RegisterSetStateIsValid(regSet))
636         {
637             const size_t numRegisters = GetNumRegistersInSet(regSet);
638             size_t regIndex = 0;
639             DNBRegisterValueClass reg;
640             for (regIndex = 0; regIndex < numRegisters; ++regIndex)
641             {
642                 if (m_arch_ap->GetRegisterValue(regSet, regIndex, &reg))
643                 {
644                     reg.Dump(NULL, NULL);
645                 }
646             }
647         }
648         else
649         {
650             DNBLog("%s: registers are not currently valid.", GetRegisterSetName(regSet));
651         }
652     }
653 }
654 
655 const DNBRegisterSetInfo *
656 MachThread::GetRegisterSetInfo(nub_size_t *num_reg_sets ) const
657 {
658     *num_reg_sets = m_num_reg_sets;
659     return &m_reg_sets[0];
660 }
661 
662 bool
663 MachThread::GetRegisterValue ( uint32_t set, uint32_t reg, DNBRegisterValue *value )
664 {
665     return m_arch_ap->GetRegisterValue(set, reg, value);
666 }
667 
668 bool
669 MachThread::SetRegisterValue ( uint32_t set, uint32_t reg, const DNBRegisterValue *value )
670 {
671     return m_arch_ap->SetRegisterValue(set, reg, value);
672 }
673 
674 nub_size_t
675 MachThread::GetRegisterContext (void *buf, nub_size_t buf_len)
676 {
677     return m_arch_ap->GetRegisterContext(buf, buf_len);
678 }
679 
680 nub_size_t
681 MachThread::SetRegisterContext (const void *buf, nub_size_t buf_len)
682 {
683     return m_arch_ap->SetRegisterContext(buf, buf_len);
684 }
685 
686 uint32_t
687 MachThread::EnableHardwareBreakpoint (const DNBBreakpoint *bp)
688 {
689     if (bp != NULL && bp->IsBreakpoint())
690         return m_arch_ap->EnableHardwareBreakpoint(bp->Address(), bp->ByteSize());
691     return INVALID_NUB_HW_INDEX;
692 }
693 
694 uint32_t
695 MachThread::EnableHardwareWatchpoint (const DNBBreakpoint *wp)
696 {
697     if (wp != NULL && wp->IsWatchpoint())
698         return m_arch_ap->EnableHardwareWatchpoint(wp->Address(), wp->ByteSize(), wp->WatchpointRead(), wp->WatchpointWrite());
699     return INVALID_NUB_HW_INDEX;
700 }
701 
702 // Provide a chance to update the global view of the hardware watchpoint state.
703 void
704 MachThread::HardwareWatchpointStateChanged ()
705 {
706     m_arch_ap->HardwareWatchpointStateChanged();
707 }
708 
709 bool
710 MachThread::RollbackTransForHWP()
711 {
712     return m_arch_ap->RollbackTransForHWP();
713 }
714 
715 bool
716 MachThread::FinishTransForHWP()
717 {
718     return m_arch_ap->FinishTransForHWP();
719 }
720 
721 bool
722 MachThread::DisableHardwareBreakpoint (const DNBBreakpoint *bp)
723 {
724     if (bp != NULL && bp->IsHardware())
725         return m_arch_ap->DisableHardwareBreakpoint(bp->GetHardwareIndex());
726     return false;
727 }
728 
729 bool
730 MachThread::DisableHardwareWatchpoint (const DNBBreakpoint *wp)
731 {
732     if (wp != NULL && wp->IsHardware())
733         return m_arch_ap->DisableHardwareWatchpoint(wp->GetHardwareIndex());
734     return false;
735 }
736 
737 uint32_t
738 MachThread::NumSupportedHardwareWatchpoints () const
739 {
740     return m_arch_ap->NumSupportedHardwareWatchpoints();
741 }
742 
743 bool
744 MachThread::GetIdentifierInfo ()
745 {
746         // Don't try to get the thread info once and cache it for the life of the thread.  It changes over time, for instance
747         // if the thread name changes, then the thread_handle also changes...  So you have to refetch it every time.
748         mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
749         kern_return_t kret = ::thread_info (m_mach_port_number, THREAD_IDENTIFIER_INFO, (thread_info_t) &m_ident_info, &count);
750         return kret == KERN_SUCCESS;
751 
752     return false;
753 }
754 
755 
756 const char *
757 MachThread::GetName ()
758 {
759     if (GetIdentifierInfo ())
760     {
761         int len = ::proc_pidinfo (m_process->ProcessID(), PROC_PIDTHREADINFO, m_ident_info.thread_handle, &m_proc_threadinfo, sizeof (m_proc_threadinfo));
762 
763         if (len && m_proc_threadinfo.pth_name[0])
764             return m_proc_threadinfo.pth_name;
765     }
766     return NULL;
767 }
768 
769 
770 uint64_t
771 MachThread::GetGloballyUniqueThreadIDForMachPortID (thread_t mach_port_id)
772 {
773     kern_return_t kr;
774     thread_identifier_info_data_t tident;
775     mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT;
776     kr = thread_info (mach_port_id, THREAD_IDENTIFIER_INFO,
777                       (thread_info_t) &tident, &tident_count);
778     if (kr != KERN_SUCCESS)
779     {
780         return mach_port_id;
781     }
782     return tident.thread_id;
783 }
784