1 //===-- RegisterContext.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_TARGET_REGISTERCONTEXT_H
10 #define LLDB_TARGET_REGISTERCONTEXT_H
11 
12 #include "lldb/Target/ExecutionContextScope.h"
13 #include "lldb/lldb-private.h"
14 
15 namespace lldb_private {
16 
17 class RegisterContext : public std::enable_shared_from_this<RegisterContext>,
18                         public ExecutionContextScope {
19 public:
20   // Constructors and Destructors
21   RegisterContext(Thread &thread, uint32_t concrete_frame_idx);
22 
23   ~RegisterContext() override;
24 
25   void InvalidateIfNeeded(bool force);
26 
27   // Subclasses must override these functions
28   virtual void InvalidateAllRegisters() = 0;
29 
30   virtual size_t GetRegisterCount() = 0;
31 
32   virtual const RegisterInfo *GetRegisterInfoAtIndex(size_t reg) = 0;
33 
34   // Detect the register size dynamically.
35   uint32_t UpdateDynamicRegisterSize(const lldb_private::ArchSpec &arch,
36                                      RegisterInfo *reg_info);
37 
38   virtual size_t GetRegisterSetCount() = 0;
39 
40   virtual const RegisterSet *GetRegisterSet(size_t reg_set) = 0;
41 
42   virtual lldb::ByteOrder GetByteOrder();
43 
44   virtual bool ReadRegister(const RegisterInfo *reg_info,
45                             RegisterValue &reg_value) = 0;
46 
47   virtual bool WriteRegister(const RegisterInfo *reg_info,
48                              const RegisterValue &reg_value) = 0;
49 
50   virtual bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
51     return false;
52   }
53 
54   virtual bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) {
55     return false;
56   }
57 
58   // These two functions are used to implement "push" and "pop" of register
59   // states.  They are used primarily for expression evaluation, where we need
60   // to push a new state (storing the old one in data_sp) and then restoring
61   // the original state by passing the data_sp we got from ReadAllRegisters to
62   // WriteAllRegisterValues. ReadAllRegisters will do what is necessary to
63   // return a coherent set of register values for this thread, which may mean
64   // e.g. interrupting a thread that is sitting in a kernel trap.  That is a
65   // somewhat disruptive operation, so these API's should only be used when
66   // this behavior is needed.
67 
68   virtual bool
69   ReadAllRegisterValues(lldb_private::RegisterCheckpoint &reg_checkpoint);
70 
71   virtual bool WriteAllRegisterValues(
72       const lldb_private::RegisterCheckpoint &reg_checkpoint);
73 
74   bool CopyFromRegisterContext(lldb::RegisterContextSP context);
75 
76   /// Convert from a given register numbering scheme to the lldb register
77   /// numbering scheme
78   ///
79   /// There may be multiple ways to enumerate the registers for a given
80   /// architecture.  ABI references will specify one to be used with
81   /// DWARF, the register numberings from process plugin, there may
82   /// be a variation used for eh_frame unwind instructions (e.g. on Darwin),
83   /// and so on.  Register 5 by itself is meaningless - RegisterKind
84   /// enumeration tells you what context that number should be translated as.
85   ///
86   /// Inside lldb, register numbers are in the eRegisterKindLLDB scheme;
87   /// arguments which take a register number should take one in that
88   /// scheme.
89   ///
90   /// eRegisterKindGeneric is a special numbering scheme which gives us
91   /// constant values for the pc, frame register, stack register, etc., for
92   /// use within lldb.  They may not be defined for all architectures but
93   /// it allows generic code to translate these common registers into the
94   /// lldb numbering scheme.
95   ///
96   /// This method translates a given register kind + register number into
97   /// the eRegisterKindLLDB register numbering.
98   ///
99   /// \param [in] kind
100   ///     The register numbering scheme (RegisterKind) that the following
101   ///     register number is in.
102   ///
103   /// \param [in] num
104   ///     A register number in the 'kind' register numbering scheme.
105   ///
106   /// \return
107   ///     The equivalent register number in the eRegisterKindLLDB
108   ///     numbering scheme, if possible, else LLDB_INVALID_REGNUM.
109   virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind,
110                                                        uint32_t num);
111 
112   // Subclasses can override these functions if desired
113   virtual uint32_t NumSupportedHardwareBreakpoints();
114 
115   virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size);
116 
117   virtual bool ClearHardwareBreakpoint(uint32_t hw_idx);
118 
119   virtual uint32_t NumSupportedHardwareWatchpoints();
120 
121   virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
122                                          bool read, bool write);
123 
124   virtual bool ClearHardwareWatchpoint(uint32_t hw_index);
125 
126   virtual bool HardwareSingleStep(bool enable);
127 
128   virtual Status
129   ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info,
130                               lldb::addr_t src_addr, uint32_t src_len,
131                               RegisterValue &reg_value);
132 
133   virtual Status
134   WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info,
135                              lldb::addr_t dst_addr, uint32_t dst_len,
136                              const RegisterValue &reg_value);
137 
138   // Subclasses should not override these
139   virtual lldb::tid_t GetThreadID() const;
140 
141   virtual Thread &GetThread() { return m_thread; }
142 
143   const RegisterInfo *GetRegisterInfoByName(llvm::StringRef reg_name,
144                                             uint32_t start_idx = 0);
145 
146   const RegisterInfo *GetRegisterInfo(lldb::RegisterKind reg_kind,
147                                       uint32_t reg_num);
148 
149   uint64_t GetPC(uint64_t fail_value = LLDB_INVALID_ADDRESS);
150 
151   bool SetPC(uint64_t pc);
152 
153   bool SetPC(Address addr);
154 
155   uint64_t GetSP(uint64_t fail_value = LLDB_INVALID_ADDRESS);
156 
157   bool SetSP(uint64_t sp);
158 
159   uint64_t GetFP(uint64_t fail_value = LLDB_INVALID_ADDRESS);
160 
161   bool SetFP(uint64_t fp);
162 
163   const char *GetRegisterName(uint32_t reg);
164 
165   uint64_t GetReturnAddress(uint64_t fail_value = LLDB_INVALID_ADDRESS);
166 
167   uint64_t GetFlags(uint64_t fail_value = 0);
168 
169   uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value);
170 
171   uint64_t ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
172                                   uint64_t fail_value);
173 
174   bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval);
175 
176   bool WriteRegisterFromUnsigned(const RegisterInfo *reg_info, uint64_t uval);
177 
178   bool ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk,
179                                    uint32_t source_regnum,
180                                    lldb::RegisterKind target_rk,
181                                    uint32_t &target_regnum);
182 
183   // lldb::ExecutionContextScope pure virtual functions
184   lldb::TargetSP CalculateTarget() override;
185 
186   lldb::ProcessSP CalculateProcess() override;
187 
188   lldb::ThreadSP CalculateThread() override;
189 
190   lldb::StackFrameSP CalculateStackFrame() override;
191 
192   void CalculateExecutionContext(ExecutionContext &exe_ctx) override;
193 
194   uint32_t GetStopID() const { return m_stop_id; }
195 
196   void SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
197 
198 protected:
199   // Classes that inherit from RegisterContext can see and modify these
200   Thread &m_thread; // The thread that this register context belongs to.
201   uint32_t m_concrete_frame_idx; // The concrete frame index for this register
202                                  // context
203   uint32_t m_stop_id; // The stop ID that any data in this context is valid for
204 private:
205   // For RegisterContext only
206   RegisterContext(const RegisterContext &) = delete;
207   const RegisterContext &operator=(const RegisterContext &) = delete;
208 };
209 
210 } // namespace lldb_private
211 
212 #endif // LLDB_TARGET_REGISTERCONTEXT_H
213