MachThread.h revision 20d338fad87eba91de65aa9bec76e01c04472848
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
32class DNBBreakpoint;
33class MachProcess;
34
35class MachThread
36{
37public:
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
93    bool            IsUserReady();
94    struct thread_basic_info *
95                    GetBasicInfo ();
96    const char *    GetBasicInfoAsString () const;
97    const char *    GetName ();
98
99    DNBArchProtocol*
100    GetArchProtocol()
101    {
102        return m_arch_ap.get();
103    }
104
105protected:
106    static bool     GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info);
107
108    bool
109    GetIdentifierInfo ();
110
111//    const char *
112//    GetDispatchQueueName();
113//
114    MachProcess *                   m_process;      // The process that owns this thread
115    thread_t                        m_tid;          // The thread port for this thread
116    uint32_t                        m_seq_id;       // A Sequential ID that increments with each new thread
117    nub_state_t                     m_state;        // The state of our process
118    PThreadMutex                    m_state_mutex;  // Multithreaded protection for m_state
119    nub_break_t                     m_breakID;      // Breakpoint that this thread is (stopped)/was(running) at (NULL for none)
120    struct thread_basic_info        m_basicInfo;    // Basic information for a thread used to see if a thread is valid
121    uint32_t                        m_suspendCount; // The current suspend count
122    MachException::Data             m_stop_exception; // The best exception that describes why this thread is stopped
123    std::auto_ptr<DNBArchProtocol>  m_arch_ap;      // Arch specific information for register state and more
124    const DNBRegisterSetInfo *const m_reg_sets;      // Register set information for this thread
125    nub_size_t                      n_num_reg_sets;
126#ifdef THREAD_IDENTIFIER_INFO_COUNT
127    thread_identifier_info_data_t   m_ident_info;
128    struct proc_threadinfo          m_proc_threadinfo;
129    std::string                     m_dispatch_queue_name;
130#endif
131
132};
133
134typedef std::tr1::shared_ptr<MachThread> MachThreadSP;
135
136#endif
137