1 //===-- MachThread.h --------------------------------------------*- 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 #ifndef __MachThread_h__ 15 #define __MachThread_h__ 16 17 #include <string> 18 #include <vector> 19 20 #include <libproc.h> 21 #include <mach/mach.h> 22 #include <pthread.h> 23 #include <sys/signal.h> 24 25 #include "PThreadCondition.h" 26 #include "PThreadMutex.h" 27 #include "MachException.h" 28 #include "DNBArch.h" 29 #include "DNBRegisterInfo.h" 30 31 class DNBBreakpoint; 32 class MachProcess; 33 class MachThreadList; 34 35 class MachThread 36 { 37 public: 38 39 MachThread (MachProcess *process, thread_t thread = 0); 40 ~MachThread (); 41 42 MachProcess * Process() { return m_process; } 43 const MachProcess * 44 Process() const { return m_process; } 45 nub_process_t ProcessID() const; 46 void Dump(uint32_t index); 47 thread_t ThreadID() const { return m_tid; } 48 thread_t InferiorThreadID() const; 49 50 uint32_t SequenceID() const { return m_seq_id; } 51 static bool ThreadIDIsValid(thread_t thread); 52 void Resume(bool others_stopped); 53 void Suspend(); 54 bool SetSuspendCountBeforeResume(bool others_stopped); 55 bool RestoreSuspendCountAfterStop(); 56 57 bool GetRegisterState(int flavor, bool force); 58 bool SetRegisterState(int flavor); 59 uint64_t GetPC(uint64_t failValue = INVALID_NUB_ADDRESS); // Get program counter 60 bool SetPC(uint64_t value); // Set program counter 61 uint64_t GetSP(uint64_t failValue = INVALID_NUB_ADDRESS); // Get stack pointer 62 63 nub_break_t CurrentBreakpoint(); 64 uint32_t EnableHardwareBreakpoint (const DNBBreakpoint *breakpoint); 65 uint32_t EnableHardwareWatchpoint (const DNBBreakpoint *watchpoint); 66 bool DisableHardwareBreakpoint (const DNBBreakpoint *breakpoint); 67 bool DisableHardwareWatchpoint (const DNBBreakpoint *watchpoint); 68 uint32_t NumSupportedHardwareWatchpoints () const; 69 bool RollbackTransForHWP(); 70 bool FinishTransForHWP(); 71 72 nub_state_t GetState(); 73 void SetState(nub_state_t state); 74 75 void ThreadWillResume (const DNBThreadResumeAction *thread_action, bool others_stopped = false); 76 bool ShouldStop(bool &step_more); 77 bool IsStepping(); 78 bool ThreadDidStop(); 79 bool NotifyException(MachException::Data& exc); 80 const MachException::Data& GetStopException() { return m_stop_exception; } 81 82 uint32_t GetNumRegistersInSet(int regSet) const; 83 const char * GetRegisterSetName(int regSet) const; 84 const DNBRegisterInfo * 85 GetRegisterInfo(int regSet, int regIndex) const; 86 void DumpRegisterState(int regSet); 87 const DNBRegisterSetInfo * 88 GetRegisterSetInfo(nub_size_t *num_reg_sets ) const; 89 bool GetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, DNBRegisterValue *reg_value ); 90 bool SetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, const DNBRegisterValue *reg_value ); 91 nub_size_t GetRegisterContext (void *buf, nub_size_t buf_len); 92 nub_size_t SetRegisterContext (const void *buf, nub_size_t buf_len); 93 void NotifyBreakpointChanged (const DNBBreakpoint *bp) 94 { 95 } 96 97 bool IsUserReady(); 98 struct thread_basic_info * 99 GetBasicInfo (); 100 const char * GetBasicInfoAsString () const; 101 const char * GetName (); 102 103 DNBArchProtocol* 104 GetArchProtocol() 105 { 106 return m_arch_ap.get(); 107 } 108 109 protected: 110 static bool GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info); 111 112 bool 113 GetIdentifierInfo (); 114 115 // const char * 116 // GetDispatchQueueName(); 117 // 118 MachProcess * m_process; // The process that owns this thread 119 thread_t m_tid; // The thread port for this thread 120 uint32_t m_seq_id; // A Sequential ID that increments with each new thread 121 nub_state_t m_state; // The state of our process 122 PThreadMutex m_state_mutex; // Multithreaded protection for m_state 123 nub_break_t m_break_id; // Breakpoint that this thread is (stopped)/was(running) at (NULL for none) 124 struct thread_basic_info m_basic_info; // Basic information for a thread used to see if a thread is valid 125 int32_t m_suspend_count; // The current suspend count > 0 means we have suspended m_suspendCount times, 126 // < 0 means we have resumed it m_suspendCount times. 127 MachException::Data m_stop_exception; // The best exception that describes why this thread is stopped 128 std::auto_ptr<DNBArchProtocol> m_arch_ap; // Arch specific information for register state and more 129 const DNBRegisterSetInfo * m_reg_sets; // Register set information for this thread 130 nub_size_t m_num_reg_sets; 131 #ifdef THREAD_IDENTIFIER_INFO_COUNT 132 thread_identifier_info_data_t m_ident_info; 133 struct proc_threadinfo m_proc_threadinfo; 134 std::string m_dispatch_queue_name; 135 #endif 136 137 private: 138 friend class MachThreadList; 139 void HardwareWatchpointStateChanged(); // Provide a chance to update the global view of the hardware watchpoint state 140 }; 141 142 typedef STD_SHARED_PTR(MachThread) MachThreadSP; 143 144 #endif 145