1 //===-- DNBArch.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/24/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef __DebugNubArch_h__
15 #define __DebugNubArch_h__
16 
17 #include "DNBDefs.h"
18 #include "MacOSX/MachException.h"
19 
20 #include <mach/mach.h>
21 #include <stdio.h>
22 
23 struct DNBRegisterValue;
24 struct DNBRegisterSetInfo;
25 class DNBArchProtocol;
26 class MachThread;
27 
28 typedef DNBArchProtocol * (* DNBArchCallbackCreate)(MachThread *thread);
29 typedef const DNBRegisterSetInfo * (* DNBArchCallbackGetRegisterSetInfo)(nub_size_t *num_reg_sets);
30 typedef const uint8_t * const (* DNBArchCallbackGetBreakpointOpcode)(nub_size_t byte_size);
31 
32 typedef struct DNBArchPluginInfoTag
33 {
34     uint32_t cpu_type;
35     DNBArchCallbackCreate               Create;
36     DNBArchCallbackGetRegisterSetInfo   GetRegisterSetInfo;
37     DNBArchCallbackGetBreakpointOpcode  GetBreakpointOpcode;
38 } DNBArchPluginInfo;
39 
40 class DNBArchProtocol
41 {
42 public:
43     static DNBArchProtocol *
44     Create (MachThread *thread);
45 
46     static const DNBRegisterSetInfo *
47     GetRegisterSetInfo (nub_size_t *num_reg_sets);
48 
49     static const uint8_t * const
50     GetBreakpointOpcode (nub_size_t byte_size);
51 
52     static void
53     RegisterArchPlugin (const DNBArchPluginInfo &arch_info);
54 
55     static uint32_t
56     GetArchitecture ();
57 
58     static bool
59     SetArchitecture (uint32_t cpu_type);
60 
61     DNBArchProtocol () :
62         m_save_id(0)
63     {
64 
65     }
66 
67     virtual ~DNBArchProtocol ()
68     {
69 
70     }
71     virtual bool            GetRegisterValue (int set, int reg, DNBRegisterValue *value) = 0;
72     virtual bool            SetRegisterValue (int set, int reg, const DNBRegisterValue *value) = 0;
73     virtual nub_size_t      GetRegisterContext (void *buf, nub_size_t buf_len) = 0;
74     virtual nub_size_t      SetRegisterContext (const void *buf, nub_size_t buf_len) = 0;
75     virtual uint32_t        SaveRegisterState () = 0;
76     virtual bool            RestoreRegisterState (uint32_t save_id) = 0;
77 
78     virtual kern_return_t   GetRegisterState (int set, bool force) = 0;
79     virtual kern_return_t   SetRegisterState (int set) = 0;
80     virtual bool            RegisterSetStateIsValid (int set) const = 0;
81 
82     virtual uint64_t        GetPC (uint64_t failValue) = 0;    // Get program counter
83     virtual kern_return_t   SetPC (uint64_t value) = 0;
84     virtual uint64_t        GetSP (uint64_t failValue) = 0;    // Get stack pointer
85     virtual void            ThreadWillResume () = 0;
86     virtual bool            ThreadDidStop () = 0;
87     virtual bool            NotifyException (MachException::Data& exc) { return false; }
88     virtual uint32_t        NumSupportedHardwareBreakpoints() { return 0; }
89     virtual uint32_t        NumSupportedHardwareWatchpoints() { return 0; }
90     virtual uint32_t        EnableHardwareBreakpoint (nub_addr_t addr, nub_size_t size) { return INVALID_NUB_HW_INDEX; }
91     virtual uint32_t        EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write, bool also_set_on_task) { return INVALID_NUB_HW_INDEX; }
92     virtual bool            DisableHardwareBreakpoint (uint32_t hw_index) { return false; }
93     virtual bool            DisableHardwareWatchpoint (uint32_t hw_index, bool also_set_on_task) { return false; }
94     virtual uint32_t        GetHardwareWatchpointHit() { return INVALID_NUB_HW_INDEX; }
95     virtual bool            StepNotComplete () { return false; }
96 
97 protected:
98     friend class MachThread;
99 
100     uint32_t                GetNextRegisterStateSaveID ()
101                             {
102                                 return ++m_save_id;
103                             }
104 
105     enum
106     {
107         Trans_Pending = 0,      // Transaction is pending, and checkpoint state has been snapshotted.
108         Trans_Done = 1,         // Transaction is done, the current state is committed, and checkpoint state is irrelevant.
109         Trans_Rolled_Back = 2   // Transaction is done, the current state has been rolled back to the checkpoint state.
110     };
111     virtual bool StartTransForHWP() { return true; }
112     virtual bool RollbackTransForHWP() { return true; }
113     virtual bool FinishTransForHWP() { return true; }
114 
115     uint32_t m_save_id;         // An always incrementing integer ID used with SaveRegisterState/RestoreRegisterState
116 
117 };
118 
119 
120 #include "MacOSX/arm/DNBArchImpl.h"
121 #include "MacOSX/i386/DNBArchImplI386.h"
122 #include "MacOSX/x86_64/DNBArchImplX86_64.h"
123 #include "MacOSX/ppc/DNBArchImpl.h"
124 
125 #endif
126