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