DNBArchImpl.h revision 33de0386250de1ded3dc2fdfb607970e52d83420
1//===-- DNBArchImpl.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/25/07.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef __DebugNubArchMachARM_h__
15#define __DebugNubArchMachARM_h__
16
17#if defined (__arm__)
18
19#include "DNBArch.h"
20#if defined (USE_ARM_DISASSEMBLER_FRAMEWORK)
21#include <ARMDisassembler/ARMDisassembler.h>
22#endif
23
24class MachThread;
25
26class DNBArchMachARM : public DNBArchProtocol
27{
28public:
29    enum { kMaxNumThumbITBreakpoints = 4 };
30
31    DNBArchMachARM(MachThread *thread) :
32        m_thread(thread),
33        m_state(),
34        m_hw_single_chained_step_addr(INVALID_NUB_ADDRESS),
35        m_sw_single_step_next_pc(INVALID_NUB_ADDRESS),
36        m_sw_single_step_break_id(INVALID_NUB_BREAK_ID),
37        m_sw_single_step_itblock_break_count(0),
38        m_last_decode_pc(INVALID_NUB_ADDRESS),
39        m_watchpoint_hw_index(-1),
40        m_watchpoint_did_occur(false)
41    {
42        memset(&m_dbg_save, 0, sizeof(m_dbg_save));
43#if defined (USE_ARM_DISASSEMBLER_FRAMEWORK)
44        ThumbStaticsInit(&m_last_decode_thumb);
45#endif
46        for (int i = 0; i < kMaxNumThumbITBreakpoints; i++)
47            m_sw_single_step_itblock_break_id[i] = INVALID_NUB_BREAK_ID;
48    }
49
50    virtual ~DNBArchMachARM()
51    {
52    }
53
54    static void Initialize();
55    static const DNBRegisterSetInfo *
56    GetRegisterSetInfo(nub_size_t *num_reg_sets);
57
58    virtual bool            GetRegisterValue(int set, int reg, DNBRegisterValue *value);
59    virtual bool            SetRegisterValue(int set, int reg, const DNBRegisterValue *value);
60    virtual nub_size_t      GetRegisterContext (void *buf, nub_size_t buf_len);
61    virtual nub_size_t      SetRegisterContext (const void *buf, nub_size_t buf_len);
62
63    virtual kern_return_t   GetRegisterState  (int set, bool force);
64    virtual kern_return_t   SetRegisterState  (int set);
65    virtual bool            RegisterSetStateIsValid (int set) const;
66
67    virtual uint64_t        GetPC(uint64_t failValue);    // Get program counter
68    virtual kern_return_t   SetPC(uint64_t value);
69    virtual uint64_t        GetSP(uint64_t failValue);    // Get stack pointer
70    virtual void            ThreadWillResume();
71    virtual bool            ThreadDidStop();
72    virtual bool            NotifyException(MachException::Data& exc);
73
74    static DNBArchProtocol *Create (MachThread *thread);
75    static const uint8_t * const SoftwareBreakpointOpcode (nub_size_t byte_size);
76    static uint32_t         GetCPUType();
77
78    virtual uint32_t        NumSupportedHardwareBreakpoints();
79    virtual uint32_t        NumSupportedHardwareWatchpoints();
80    virtual uint32_t        EnableHardwareBreakpoint (nub_addr_t addr, nub_size_t size);
81    virtual uint32_t        EnableHardwareWatchpoint (nub_addr_t addr, nub_size_t size, bool read, bool write);
82    virtual bool            DisableHardwareBreakpoint (uint32_t hw_break_index);
83    virtual bool            EnableHardwareWatchpoint (uint32_t hw_break_index);
84    virtual bool            DisableHardwareWatchpoint (uint32_t hw_break_index);
85    virtual bool            StepNotComplete ();
86    virtual void            HardwareWatchpointStateChanged ();
87    virtual uint32_t        GetHardwareWatchpointHit(nub_addr_t &addr);
88
89    typedef arm_debug_state_t DBG;
90
91protected:
92
93
94    kern_return_t           EnableHardwareSingleStep (bool enable);
95    kern_return_t           SetSingleStepSoftwareBreakpoints ();
96
97    bool                    ConditionPassed(uint8_t condition, uint32_t cpsr);
98#if defined (USE_ARM_DISASSEMBLER_FRAMEWORK)
99    bool                    ComputeNextPC(nub_addr_t currentPC, arm_decoded_instruction_t decodedInstruction, bool currentPCIsThumb, nub_addr_t *targetPC);
100    arm_error_t             DecodeInstructionUsingDisassembler(nub_addr_t curr_pc, uint32_t curr_cpsr, arm_decoded_instruction_t *decodedInstruction, thumb_static_data_t *thumbStaticData, nub_addr_t *next_pc);
101    void                    DecodeITBlockInstructions(nub_addr_t curr_pc);
102#endif
103    void                    EvaluateNextInstructionForSoftwareBreakpointSetup(nub_addr_t currentPC, uint32_t cpsr, bool currentPCIsThumb, nub_addr_t *nextPC, bool *nextPCIsThumb);
104    static nub_bool_t       BreakpointHit (nub_process_t pid, nub_thread_t tid, nub_break_t breakID, void *baton);
105
106    typedef enum RegisterSetTag
107    {
108        e_regSetALL = REGISTER_SET_ALL,
109        e_regSetGPR = ARM_THREAD_STATE,
110        e_regSetVFP = ARM_VFP_STATE,
111        e_regSetEXC = ARM_EXCEPTION_STATE,
112        e_regSetDBG = ARM_DEBUG_STATE,
113        kNumRegisterSets
114    } RegisterSet;
115
116    enum
117    {
118        Read = 0,
119        Write = 1,
120        kNumErrors = 2
121    };
122
123    typedef arm_thread_state_t GPR;
124    typedef arm_vfp_state_t FPU;
125    typedef arm_exception_state_t EXC;
126
127    static const DNBRegisterInfo g_gpr_registers[];
128    static const DNBRegisterInfo g_vfp_registers[];
129    static const DNBRegisterInfo g_exc_registers[];
130    static const DNBRegisterSetInfo g_reg_sets[];
131
132    static const size_t k_num_gpr_registers;
133    static const size_t k_num_vfp_registers;
134    static const size_t k_num_exc_registers;
135    static const size_t k_num_all_registers;
136    static const size_t k_num_register_sets;
137
138    struct Context
139    {
140        GPR gpr;
141        FPU vfp;
142        EXC exc;
143    };
144
145    // See also HardwareWatchpointStateChanged() which updates this class-wide variable.
146    static DBG Global_Debug_State;
147    static bool Valid_Global_Debug_State;
148
149    struct State
150    {
151        Context                 context;
152        DBG                     dbg;
153        kern_return_t           gpr_errs[2];    // Read/Write errors
154        kern_return_t           vfp_errs[2];    // Read/Write errors
155        kern_return_t           exc_errs[2];    // Read/Write errors
156        kern_return_t           dbg_errs[2];    // Read/Write errors
157        State()
158        {
159            uint32_t i;
160            for (i=0; i<kNumErrors; i++)
161            {
162                gpr_errs[i] = -1;
163                vfp_errs[i] = -1;
164                exc_errs[i] = -1;
165                dbg_errs[i] = -1;
166            }
167        }
168        void InvalidateRegisterSetState(int set)
169        {
170            SetError (set, Read, -1);
171        }
172        kern_return_t GetError (int set, uint32_t err_idx) const
173        {
174            if (err_idx < kNumErrors)
175            {
176                switch (set)
177                {
178                // When getting all errors, just OR all values together to see if
179                // we got any kind of error.
180                case e_regSetALL:   return gpr_errs[err_idx] |
181                                           vfp_errs[err_idx] |
182                                           exc_errs[err_idx] |
183                                           dbg_errs[err_idx] ;
184                case e_regSetGPR:   return gpr_errs[err_idx];
185                case e_regSetVFP:   return vfp_errs[err_idx];
186                case e_regSetEXC:   return exc_errs[err_idx];
187                case e_regSetDBG:   return dbg_errs[err_idx];
188                default: break;
189                }
190            }
191            return -1;
192        }
193        bool SetError (int set, uint32_t err_idx, kern_return_t err)
194        {
195            if (err_idx < kNumErrors)
196            {
197                switch (set)
198                {
199                case e_regSetALL:
200                    gpr_errs[err_idx] = err;
201                    vfp_errs[err_idx] = err;
202                    dbg_errs[err_idx] = err;
203                    exc_errs[err_idx] = err;
204                    return true;
205
206                case e_regSetGPR:
207                    gpr_errs[err_idx] = err;
208                    return true;
209
210                case e_regSetVFP:
211                    vfp_errs[err_idx] = err;
212                    return true;
213
214                case e_regSetEXC:
215                    exc_errs[err_idx] = err;
216                    return true;
217
218                case e_regSetDBG:
219                    dbg_errs[err_idx] = err;
220                    return true;
221                default: break;
222                }
223            }
224            return false;
225        }
226        bool RegsAreValid (int set) const
227        {
228            return GetError(set, Read) == KERN_SUCCESS;
229        }
230    };
231
232    kern_return_t GetGPRState (bool force);
233    kern_return_t GetVFPState (bool force);
234    kern_return_t GetEXCState (bool force);
235    kern_return_t GetDBGState (bool force);
236
237    kern_return_t SetGPRState ();
238    kern_return_t SetVFPState ();
239    kern_return_t SetEXCState ();
240    kern_return_t SetDBGState ();
241
242    // Helper functions for watchpoint implementaions.
243    static void ClearWatchpointOccurred();
244    static bool HasWatchpointOccurred();
245    static bool IsWatchpointHit(const DBG &debug_state, uint32_t hw_index);
246    static nub_addr_t GetWatchAddress(const DBG &debug_state, uint32_t hw_index);
247
248protected:
249    MachThread *    m_thread;
250    State           m_state;
251    DBG             m_dbg_save;
252    nub_addr_t      m_hw_single_chained_step_addr;
253    // Software single stepping support
254    nub_addr_t      m_sw_single_step_next_pc;
255    nub_break_t     m_sw_single_step_break_id;
256    nub_break_t     m_sw_single_step_itblock_break_id[kMaxNumThumbITBreakpoints];
257    nub_addr_t      m_sw_single_step_itblock_break_count;
258    // Disassembler state
259#if defined (USE_ARM_DISASSEMBLER_FRAMEWORK)
260    thumb_static_data_t m_last_decode_thumb;
261    arm_decoded_instruction_t m_last_decode_arm;
262#endif
263    nub_addr_t      m_last_decode_pc;
264    int32_t         m_watchpoint_hw_index;
265    bool            m_watchpoint_did_occur;
266
267};
268
269#endif    // #if defined (__arm__)
270#endif    // #ifndef __DebugNubArchMachARM_h__
271