Home | History | Annotate | Download | only in MacOSX
      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, uint64_t unique_thread_id = 0, thread_t mach_port_number = 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     uint64_t        ThreadID() const { return m_unique_id; }
     48     thread_t        MachPortNumber() const { return m_mach_port_number; }
     49     thread_t        InferiorThreadID() const;
     50 
     51     uint32_t        SequenceID() const { return m_seq_id; }
     52     static bool     ThreadIDIsValid(uint64_t thread);       // The 64-bit system-wide unique thread identifier
     53     static bool     MachPortNumberIsValid(thread_t thread); // The mach port # for this thread in debugserver namespace
     54     void            Resume(bool others_stopped);
     55     void            Suspend();
     56     bool            SetSuspendCountBeforeResume(bool others_stopped);
     57     bool            RestoreSuspendCountAfterStop();
     58 
     59     bool            GetRegisterState(int flavor, bool force);
     60     bool            SetRegisterState(int flavor);
     61     uint64_t        GetPC(uint64_t failValue = INVALID_NUB_ADDRESS);    // Get program counter
     62     bool            SetPC(uint64_t value);                              // Set program counter
     63     uint64_t        GetSP(uint64_t failValue = INVALID_NUB_ADDRESS);    // Get stack pointer
     64 
     65     DNBBreakpoint * CurrentBreakpoint();
     66     uint32_t        EnableHardwareBreakpoint (const DNBBreakpoint *breakpoint);
     67     uint32_t        EnableHardwareWatchpoint (const DNBBreakpoint *watchpoint, bool also_set_on_task);
     68     bool            DisableHardwareBreakpoint (const DNBBreakpoint *breakpoint);
     69     bool            DisableHardwareWatchpoint (const DNBBreakpoint *watchpoint, bool also_set_on_task);
     70     uint32_t        NumSupportedHardwareWatchpoints () const;
     71     bool            RollbackTransForHWP();
     72     bool            FinishTransForHWP();
     73 
     74     nub_state_t     GetState();
     75     void            SetState(nub_state_t state);
     76 
     77     void            ThreadWillResume (const DNBThreadResumeAction *thread_action, bool others_stopped = false);
     78     bool            ShouldStop(bool &step_more);
     79     bool            IsStepping();
     80     bool            ThreadDidStop();
     81     bool            NotifyException(MachException::Data& exc);
     82     const MachException::Data& GetStopException() { return m_stop_exception; }
     83 
     84     uint32_t        GetNumRegistersInSet(int regSet) const;
     85     const char *    GetRegisterSetName(int regSet) const;
     86     const DNBRegisterInfo *
     87                     GetRegisterInfo(int regSet, int regIndex) const;
     88     void            DumpRegisterState(int regSet);
     89     const DNBRegisterSetInfo *
     90                     GetRegisterSetInfo(nub_size_t *num_reg_sets ) const;
     91     bool            GetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, DNBRegisterValue *reg_value );
     92     bool            SetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, const DNBRegisterValue *reg_value );
     93     nub_size_t      GetRegisterContext (void *buf, nub_size_t buf_len);
     94     nub_size_t      SetRegisterContext (const void *buf, nub_size_t buf_len);
     95     void            NotifyBreakpointChanged (const DNBBreakpoint *bp)
     96                     {
     97                     }
     98 
     99     bool            IsUserReady();
    100     struct thread_basic_info *
    101                     GetBasicInfo ();
    102     const char *    GetBasicInfoAsString () const;
    103     const char *    GetName ();
    104 
    105     DNBArchProtocol*
    106     GetArchProtocol()
    107     {
    108         return m_arch_ap.get();
    109     }
    110 
    111     static uint64_t GetGloballyUniqueThreadIDForMachPortID (thread_t mach_port_id);
    112 
    113 protected:
    114     static bool     GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info);
    115 
    116     bool
    117     GetIdentifierInfo ();
    118 
    119 //    const char *
    120 //    GetDispatchQueueName();
    121 //
    122     MachProcess *                   m_process;      // The process that owns this thread
    123     uint64_t                        m_unique_id;    // The globally unique ID for this thread (nub_thread_t)
    124     thread_t                        m_mach_port_number;  // The mach port # for this thread in debugserver namesp.
    125     uint32_t                        m_seq_id;       // A Sequential ID that increments with each new thread
    126     nub_state_t                     m_state;        // The state of our process
    127     PThreadMutex                    m_state_mutex;  // Multithreaded protection for m_state
    128     struct thread_basic_info        m_basic_info;   // Basic information for a thread used to see if a thread is valid
    129     int32_t                         m_suspend_count; // The current suspend count > 0 means we have suspended m_suspendCount times,
    130                                                     //                           < 0 means we have resumed it m_suspendCount times.
    131     MachException::Data             m_stop_exception; // The best exception that describes why this thread is stopped
    132     std::unique_ptr<DNBArchProtocol> m_arch_ap;      // Arch specific information for register state and more
    133     const DNBRegisterSetInfo *      m_reg_sets;      // Register set information for this thread
    134     nub_size_t                      m_num_reg_sets;
    135     thread_identifier_info_data_t   m_ident_info;
    136     struct proc_threadinfo          m_proc_threadinfo;
    137     std::string                     m_dispatch_queue_name;
    138 
    139 private:
    140     friend class MachThreadList;
    141 };
    142 
    143 typedef std::shared_ptr<MachThread> MachThreadSP;
    144 
    145 #endif
    146