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     uint32_t        SaveRegisterState ();
96     bool            RestoreRegisterState (uint32_t save_id);
97 
98     void            NotifyBreakpointChanged (const DNBBreakpoint *bp)
99                     {
100                     }
101 
102     bool            IsUserReady();
103     struct thread_basic_info *
104                     GetBasicInfo ();
105     const char *    GetBasicInfoAsString () const;
106     const char *    GetName ();
107 
108     DNBArchProtocol*
109     GetArchProtocol()
110     {
111         return m_arch_ap.get();
112     }
113 
114     static uint64_t GetGloballyUniqueThreadIDForMachPortID (thread_t mach_port_id);
115 
116 protected:
117     static bool     GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info);
118 
119     bool
120     GetIdentifierInfo ();
121 
122 //    const char *
123 //    GetDispatchQueueName();
124 //
125     MachProcess *                   m_process;      // The process that owns this thread
126     uint64_t                        m_unique_id;    // The globally unique ID for this thread (nub_thread_t)
127     thread_t                        m_mach_port_number;  // The mach port # for this thread in debugserver namesp.
128     uint32_t                        m_seq_id;       // A Sequential ID that increments with each new thread
129     nub_state_t                     m_state;        // The state of our process
130     PThreadMutex                    m_state_mutex;  // Multithreaded protection for m_state
131     struct thread_basic_info        m_basic_info;   // Basic information for a thread used to see if a thread is valid
132     int32_t                         m_suspend_count; // The current suspend count > 0 means we have suspended m_suspendCount times,
133                                                     //                           < 0 means we have resumed it m_suspendCount times.
134     MachException::Data             m_stop_exception; // The best exception that describes why this thread is stopped
135     std::unique_ptr<DNBArchProtocol> m_arch_ap;      // Arch specific information for register state and more
136     const DNBRegisterSetInfo *      m_reg_sets;      // Register set information for this thread
137     nub_size_t                      m_num_reg_sets;
138     thread_identifier_info_data_t   m_ident_info;
139     struct proc_threadinfo          m_proc_threadinfo;
140     std::string                     m_dispatch_queue_name;
141 
142 private:
143     friend class MachThreadList;
144 };
145 
146 typedef std::shared_ptr<MachThread> MachThreadSP;
147 
148 #endif
149