1 //===-- NativeRegisterContext.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 #ifndef liblldb_NativeRegisterContext_h_
11 #define liblldb_NativeRegisterContext_h_
12 
13 #include "lldb/Host/common/NativeWatchpointList.h"
14 #include "lldb/lldb-private.h"
15 
16 namespace lldb_private {
17 
18 class NativeThreadProtocol;
19 
20 class NativeRegisterContext
21     : public std::enable_shared_from_this<NativeRegisterContext> {
22 public:
23   //------------------------------------------------------------------
24   // Constructors and Destructors
25   //------------------------------------------------------------------
26   NativeRegisterContext(NativeThreadProtocol &thread);
27 
28   virtual ~NativeRegisterContext();
29 
30   // void
31   // InvalidateIfNeeded (bool force);
32 
33   //------------------------------------------------------------------
34   // Subclasses must override these functions
35   //------------------------------------------------------------------
36   // virtual void
37   // InvalidateAllRegisters () = 0;
38 
39   virtual uint32_t GetRegisterCount() const = 0;
40 
41   virtual uint32_t GetUserRegisterCount() const = 0;
42 
43   virtual const RegisterInfo *GetRegisterInfoAtIndex(uint32_t reg) const = 0;
44 
45   const char *GetRegisterSetNameForRegisterAtIndex(uint32_t reg_index) const;
46 
47   virtual uint32_t GetRegisterSetCount() const = 0;
48 
49   virtual const RegisterSet *GetRegisterSet(uint32_t set_index) const = 0;
50 
51   virtual Status ReadRegister(const RegisterInfo *reg_info,
52                               RegisterValue &reg_value) = 0;
53 
54   virtual Status WriteRegister(const RegisterInfo *reg_info,
55                                const RegisterValue &reg_value) = 0;
56 
57   virtual Status ReadAllRegisterValues(lldb::DataBufferSP &data_sp) = 0;
58 
59   virtual Status WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) = 0;
60 
61   uint32_t ConvertRegisterKindToRegisterNumber(uint32_t kind,
62                                                uint32_t num) const;
63 
64   //------------------------------------------------------------------
65   // Subclasses can override these functions if desired
66   //------------------------------------------------------------------
67   virtual uint32_t NumSupportedHardwareBreakpoints();
68 
69   virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size);
70 
71   virtual bool ClearHardwareBreakpoint(uint32_t hw_idx);
72 
73   virtual Status ClearAllHardwareBreakpoints();
74 
75   virtual Status GetHardwareBreakHitIndex(uint32_t &bp_index,
76                                           lldb::addr_t trap_addr);
77 
78   virtual uint32_t NumSupportedHardwareWatchpoints();
79 
80   virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
81                                          uint32_t watch_flags);
82 
83   virtual bool ClearHardwareWatchpoint(uint32_t hw_index);
84 
85   virtual Status ClearAllHardwareWatchpoints();
86 
87   virtual Status IsWatchpointHit(uint32_t wp_index, bool &is_hit);
88 
89   virtual Status GetWatchpointHitIndex(uint32_t &wp_index,
90                                        lldb::addr_t trap_addr);
91 
92   virtual Status IsWatchpointVacant(uint32_t wp_index, bool &is_vacant);
93 
94   virtual lldb::addr_t GetWatchpointAddress(uint32_t wp_index);
95 
96   // MIPS Linux kernel returns a masked address (last 3bits are masked)
97   // when a HW watchpoint is hit. However user may not have set a watchpoint on
98   // this address. This function emulates the instruction at PC and finds the
99   // base address used in the load/store instruction. This gives the exact
100   // address used to read/write the variable being watched. For example: 'n' is
101   // at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at 'm',
102   // then watch exception is generated even when 'n' is read/written. This
103   // function returns address of 'n' so that client can check whether a
104   // watchpoint is set on this address or not.
105   virtual lldb::addr_t GetWatchpointHitAddress(uint32_t wp_index);
106 
107   virtual bool HardwareSingleStep(bool enable);
108 
109   virtual Status
110   ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info,
111                               lldb::addr_t src_addr, size_t src_len,
112                               RegisterValue &reg_value);
113 
114   virtual Status
115   WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info,
116                              lldb::addr_t dst_addr, size_t dst_len,
117                              const RegisterValue &reg_value);
118 
119   //------------------------------------------------------------------
120   // Subclasses should not override these
121   //------------------------------------------------------------------
122   virtual lldb::tid_t GetThreadID() const;
123 
GetThread()124   virtual NativeThreadProtocol &GetThread() { return m_thread; }
125 
126   const RegisterInfo *GetRegisterInfoByName(llvm::StringRef reg_name,
127                                             uint32_t start_idx = 0);
128 
129   const RegisterInfo *GetRegisterInfo(uint32_t reg_kind, uint32_t reg_num);
130 
131   lldb::addr_t GetPC(lldb::addr_t fail_value = LLDB_INVALID_ADDRESS);
132 
133   virtual lldb::addr_t
134   GetPCfromBreakpointLocation(lldb::addr_t fail_value = LLDB_INVALID_ADDRESS);
135 
136   Status SetPC(lldb::addr_t pc);
137 
138   lldb::addr_t GetSP(lldb::addr_t fail_value = LLDB_INVALID_ADDRESS);
139 
140   Status SetSP(lldb::addr_t sp);
141 
142   lldb::addr_t GetFP(lldb::addr_t fail_value = LLDB_INVALID_ADDRESS);
143 
144   Status SetFP(lldb::addr_t fp);
145 
146   const char *GetRegisterName(uint32_t reg);
147 
148   lldb::addr_t GetReturnAddress(lldb::addr_t fail_value = LLDB_INVALID_ADDRESS);
149 
150   lldb::addr_t GetFlags(lldb::addr_t fail_value = 0);
151 
152   lldb::addr_t ReadRegisterAsUnsigned(uint32_t reg, lldb::addr_t fail_value);
153 
154   lldb::addr_t ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
155                                       lldb::addr_t fail_value);
156 
157   Status WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval);
158 
159   Status WriteRegisterFromUnsigned(const RegisterInfo *reg_info, uint64_t uval);
160 
161   // uint32_t
162   // GetStopID () const
163   // {
164   //     return m_stop_id;
165   // }
166 
167   // void
168   // SetStopID (uint32_t stop_id)
169   // {
170   //     m_stop_id = stop_id;
171   // }
172 
173 protected:
174   //------------------------------------------------------------------
175   // Classes that inherit from RegisterContext can see and modify these
176   //------------------------------------------------------------------
177   NativeThreadProtocol
178       &m_thread; // The thread that this register context belongs to.
179   // uint32_t m_stop_id;             // The stop ID that any data in this
180   // context is valid for
181 
182 private:
183   //------------------------------------------------------------------
184   // For RegisterContext only
185   //------------------------------------------------------------------
186   DISALLOW_COPY_AND_ASSIGN(NativeRegisterContext);
187 };
188 
189 } // namespace lldb_private
190 
191 #endif // liblldb_NativeRegisterContext_h_
192