MachThread.h revision c71899ef308e6134d1b0ca5f30cbc64414855e1a
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();
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
69    nub_state_t     GetState();
70    void            SetState(nub_state_t state);
71
72    void            ThreadWillResume (const DNBThreadResumeAction *thread_action);
73    bool            ShouldStop(bool &step_more);
74    bool            IsStepping();
75    bool            ThreadDidStop();
76    bool            NotifyException(MachException::Data& exc);
77    const MachException::Data& GetStopException() { return m_stop_exception; }
78
79    uint32_t        GetNumRegistersInSet(int regSet) const;
80    const char *    GetRegisterSetName(int regSet) const;
81    const DNBRegisterInfo *
82                    GetRegisterInfo(int regSet, int regIndex) const;
83    void            DumpRegisterState(int regSet);
84    const DNBRegisterSetInfo *
85                    GetRegisterSetInfo(nub_size_t *num_reg_sets ) const;
86    bool            GetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, DNBRegisterValue *reg_value );
87    bool            SetRegisterValue ( uint32_t reg_set_idx, uint32_t reg_idx, const DNBRegisterValue *reg_value );
88    nub_size_t      GetRegisterContext (void *buf, nub_size_t buf_len);
89    nub_size_t      SetRegisterContext (const void *buf, nub_size_t buf_len);
90    void            NotifyBreakpointChanged (const DNBBreakpoint *bp)
91                    {
92                    }
93
94    bool            IsUserReady();
95    struct thread_basic_info *
96                    GetBasicInfo ();
97    const char *    GetBasicInfoAsString () const;
98    const char *    GetName ();
99
100    DNBArchProtocol*
101    GetArchProtocol()
102    {
103        return m_arch_ap.get();
104    }
105
106protected:
107    static bool     GetBasicInfo(thread_t threadID, struct thread_basic_info *basic_info);
108
109    bool
110    GetIdentifierInfo ();
111
112//    const char *
113//    GetDispatchQueueName();
114//
115    MachProcess *                   m_process;      // The process that owns this thread
116    thread_t                        m_tid;          // The thread port for this thread
117    uint32_t                        m_seq_id;       // A Sequential ID that increments with each new thread
118    nub_state_t                     m_state;        // The state of our process
119    PThreadMutex                    m_state_mutex;  // Multithreaded protection for m_state
120    nub_break_t                     m_breakID;      // Breakpoint that this thread is (stopped)/was(running) at (NULL for none)
121    struct thread_basic_info        m_basicInfo;    // Basic information for a thread used to see if a thread is valid
122    uint32_t                        m_suspendCount; // The current suspend count
123    MachException::Data             m_stop_exception; // The best exception that describes why this thread is stopped
124    std::auto_ptr<DNBArchProtocol>  m_arch_ap;      // Arch specific information for register state and more
125    const DNBRegisterSetInfo *const m_reg_sets;      // Register set information for this thread
126    nub_size_t                      n_num_reg_sets;
127#ifdef THREAD_IDENTIFIER_INFO_COUNT
128    thread_identifier_info_data_t   m_ident_info;
129    struct proc_threadinfo          m_proc_threadinfo;
130    std::string                     m_dispatch_queue_name;
131#endif
132
133};
134
135typedef std::tr1::shared_ptr<MachThread> MachThreadSP;
136
137#endif
138