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 #include <tr1/memory> // for std::tr1::shared_ptr
20 
21 #include <libproc.h>
22 #include <mach/mach.h>
23 #include <pthread.h>
24 #include <sys/signal.h>
25 
26 #include "PThreadCondition.h"
27 #include "PThreadMutex.h"
28 #include "MachException.h"
29 #include "DNBArch.h"
30 #include "DNBRegisterInfo.h"
31 
32 class DNBBreakpoint;
33 class MachProcess;
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     uint32_t        Resume();
53     uint32_t        Suspend();
54     uint32_t        SuspendCount() const { return m_suspendCount; }
55     bool            RestoreSuspendCount();
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() const { return m_breakID; }
64     void            SetCurrentBreakpoint(nub_break_t breakID) { m_breakID = breakID; }
65     uint32_t        EnableHardwareBreakpoint (const DNBBreakpoint *breakpoint);
66     uint32_t        EnableHardwareWatchpoint (const DNBBreakpoint *watchpoint);
67     bool            DisableHardwareBreakpoint (const DNBBreakpoint *breakpoint);
68     bool            DisableHardwareWatchpoint (const DNBBreakpoint *watchpoint);
69 
70     nub_state_t     GetState();
71     void            SetState(nub_state_t state);
72 
73     void            ThreadWillResume (const DNBThreadResumeAction *thread_action);
74     bool            ShouldStop(bool &step_more);
75     bool            IsStepping();
76     bool            ThreadDidStop();
77     bool            NotifyException(MachException::Data& exc);
78     const MachException::Data& GetStopException() { return m_stop_exception; }
79 
80     uint32_t        GetNumRegistersInSet(int regSet) const;
81     const char *    GetRegisterSetName(int regSet) const;
82     const DNBRegisterInfo *
83                     GetRegisterInfo(int regSet, int regIndex) const;
84     void            DumpRegisterState(int regSet);
85     const DNBRegisterSetInfo *
86                     GetRegisterSetInfo(nub_size_t *num_reg_sets ) const;
87     bool            GetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, DNBRegisterValue *reg_value );
88     bool            SetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, const DNBRegisterValue *reg_value );
89     nub_size_t      GetRegisterContext (void *buf, nub_size_t buf_len);
90     nub_size_t      SetRegisterContext (const void *buf, nub_size_t buf_len);
91     void            NotifyBreakpointChanged (const DNBBreakpoint *bp);
92     const char *    GetBasicInfoAsString () const;
93     const char *    GetName ();
94 protected:
95     static bool     GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info);
96 
97     bool
98     GetIdentifierInfo ();
99 
100 //    const char *
101 //    GetDispatchQueueName();
102 //
103     MachProcess *                   m_process;      // The process that owns this thread
104     thread_t                        m_tid;          // The thread port for this thread
105     uint32_t                        m_seq_id;       // A Sequential ID that increments with each new thread
106     nub_state_t                     m_state;        // The state of our process
107     PThreadMutex                    m_state_mutex;  // Multithreaded protection for m_state
108     nub_break_t                     m_breakID;      // Breakpoint that this thread is (stopped)/was(running) at (NULL for none)
109     struct thread_basic_info        m_basicInfo;    // Basic information for a thread used to see if a thread is valid
110     uint32_t                        m_suspendCount; // The current suspend count
111     MachException::Data             m_stop_exception; // The best exception that describes why this thread is stopped
112     DNBArch                         m_arch;         // Arch specific information for register state and more
113     std::vector<DNBRegisterSetInfo> m_regSets;      // Register set information for this thread
114 #ifdef THREAD_IDENTIFIER_INFO_COUNT
115     thread_identifier_info_data_t   m_ident_info;
116     struct proc_threadinfo          m_proc_threadinfo;
117     std::string                     m_dispatch_queue_name;
118 #endif
119 
120 };
121 
122 typedef std::tr1::shared_ptr<MachThread> MachThreadSP;
123 
124 #endif
125