LLDB  mainline
RegisterContext.h
Go to the documentation of this file.
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 
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  virtual size_t GetRegisterSetCount() = 0;
35 
36  virtual const RegisterSet *GetRegisterSet(size_t reg_set) = 0;
37 
38  virtual lldb::ByteOrder GetByteOrder();
39 
40  virtual bool ReadRegister(const RegisterInfo *reg_info,
41  RegisterValue &reg_value) = 0;
42 
43  virtual bool WriteRegister(const RegisterInfo *reg_info,
44  const RegisterValue &reg_value) = 0;
45 
46  virtual bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
47  return false;
48  }
49 
50  virtual bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) {
51  return false;
52  }
53 
54  // These two functions are used to implement "push" and "pop" of register
55  // states. They are used primarily for expression evaluation, where we need
56  // to push a new state (storing the old one in data_sp) and then restoring
57  // the original state by passing the data_sp we got from ReadAllRegisters to
58  // WriteAllRegisterValues. ReadAllRegisters will do what is necessary to
59  // return a coherent set of register values for this thread, which may mean
60  // e.g. interrupting a thread that is sitting in a kernel trap. That is a
61  // somewhat disruptive operation, so these API's should only be used when
62  // this behavior is needed.
63 
64  virtual bool
66 
67  virtual bool WriteAllRegisterValues(
68  const lldb_private::RegisterCheckpoint &reg_checkpoint);
69 
70  bool CopyFromRegisterContext(lldb::RegisterContextSP context);
71 
72  /// Convert from a given register numbering scheme to the lldb register
73  /// numbering scheme
74  ///
75  /// There may be multiple ways to enumerate the registers for a given
76  /// architecture. ABI references will specify one to be used with
77  /// DWARF, the register numberings from process plugin, there may
78  /// be a variation used for eh_frame unwind instructions (e.g. on Darwin),
79  /// and so on. Register 5 by itself is meaningless - RegisterKind
80  /// enumeration tells you what context that number should be translated as.
81  ///
82  /// Inside lldb, register numbers are in the eRegisterKindLLDB scheme;
83  /// arguments which take a register number should take one in that
84  /// scheme.
85  ///
86  /// eRegisterKindGeneric is a special numbering scheme which gives us
87  /// constant values for the pc, frame register, stack register, etc., for
88  /// use within lldb. They may not be defined for all architectures but
89  /// it allows generic code to translate these common registers into the
90  /// lldb numbering scheme.
91  ///
92  /// This method translates a given register kind + register number into
93  /// the eRegisterKindLLDB register numbering.
94  ///
95  /// \param [in] kind
96  /// The register numbering scheme (RegisterKind) that the following
97  /// register number is in.
98  ///
99  /// \param [in] num
100  /// A register number in the 'kind' register numbering scheme.
101  ///
102  /// \return
103  /// The equivalent register number in the eRegisterKindLLDB
104  /// numbering scheme, if possible, else LLDB_INVALID_REGNUM.
106  uint32_t num);
107 
108  // Subclasses can override these functions if desired
110 
111  virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size);
112 
113  virtual bool ClearHardwareBreakpoint(uint32_t hw_idx);
114 
116 
117  virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size,
118  bool read, bool write);
119 
120  virtual bool ClearHardwareWatchpoint(uint32_t hw_index);
121 
122  virtual bool HardwareSingleStep(bool enable);
123 
124  virtual Status
125  ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info,
126  lldb::addr_t src_addr, uint32_t src_len,
127  RegisterValue &reg_value);
128 
129  virtual Status
130  WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info,
131  lldb::addr_t dst_addr, uint32_t dst_len,
132  const RegisterValue &reg_value);
133 
134  // Subclasses should not override these
135  virtual lldb::tid_t GetThreadID() const;
136 
137  virtual Thread &GetThread() { return m_thread; }
138 
139  const RegisterInfo *GetRegisterInfoByName(llvm::StringRef reg_name,
140  uint32_t start_idx = 0);
141 
142  const RegisterInfo *GetRegisterInfo(lldb::RegisterKind reg_kind,
143  uint32_t reg_num);
144 
145  uint64_t GetPC(uint64_t fail_value = LLDB_INVALID_ADDRESS);
146 
147  /// Get an address suitable for symbolication.
148  /// When symbolicating -- computing line, block, function --
149  /// for a function in the middle of the stack, using the return
150  /// address can lead to unexpected results for the user.
151  /// A function that ends in a tail-call may have another function
152  /// as the "return" address, but it will never actually return.
153  /// Or a noreturn call in the middle of a function is the end of
154  /// a block of instructions, and a DWARF location list entry for
155  /// the return address may be a very different code path with
156  /// incorrect results when printing variables for this frame.
157  ///
158  /// At a source line view, the user expects the current-line indictation
159  /// to point to the function call they're under, not the next source line.
160  ///
161  /// The return address (GetPC()) should always be shown to the user,
162  /// but when computing context, keeping within the bounds of the
163  /// call instruction is what the user expects to see.
164  ///
165  /// \param [out] address
166  /// An Address object that will be filled in, if a PC can be retrieved.
167  ///
168  /// \return
169  /// Returns true if the Address param was filled in.
170  bool GetPCForSymbolication(Address &address);
171 
172  bool SetPC(uint64_t pc);
173 
174  bool SetPC(Address addr);
175 
176  uint64_t GetSP(uint64_t fail_value = LLDB_INVALID_ADDRESS);
177 
178  bool SetSP(uint64_t sp);
179 
180  uint64_t GetFP(uint64_t fail_value = LLDB_INVALID_ADDRESS);
181 
182  bool SetFP(uint64_t fp);
183 
184  const char *GetRegisterName(uint32_t reg);
185 
186  uint64_t GetReturnAddress(uint64_t fail_value = LLDB_INVALID_ADDRESS);
187 
188  uint64_t GetFlags(uint64_t fail_value = 0);
189 
190  uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value);
191 
192  uint64_t ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
193  uint64_t fail_value);
194 
195  bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval);
196 
197  bool WriteRegisterFromUnsigned(const RegisterInfo *reg_info, uint64_t uval);
198 
200  uint32_t source_regnum,
201  lldb::RegisterKind target_rk,
202  uint32_t &target_regnum);
203 
204  // lldb::ExecutionContextScope pure virtual functions
205  lldb::TargetSP CalculateTarget() override;
206 
207  lldb::ProcessSP CalculateProcess() override;
208 
209  lldb::ThreadSP CalculateThread() override;
210 
211  lldb::StackFrameSP CalculateStackFrame() override;
212 
213  void CalculateExecutionContext(ExecutionContext &exe_ctx) override;
214 
215  uint32_t GetStopID() const { return m_stop_id; }
216 
217  void SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
218 
219 protected:
220  /// Indicates that this frame is currently executing code,
221  /// that the PC value is not a return-pc but an actual executing
222  /// instruction. Some places in lldb will treat a return-pc
223  /// value differently than the currently-executing-pc value,
224  /// and this method can indicate if that should be done.
225  /// The base class implementation only uses the frame index,
226  /// but subclasses may have additional information that they
227  /// can use to detect frames in this state, for instance a
228  /// frame above a trap handler (sigtramp etc)..
229  virtual bool BehavesLikeZerothFrame() const {
230  return m_concrete_frame_idx == 0;
231  }
232 
233  // Classes that inherit from RegisterContext can see and modify these
234  Thread &m_thread; // The thread that this register context belongs to.
235  uint32_t m_concrete_frame_idx; // The concrete frame index for this register
236  // context
237  uint32_t m_stop_id; // The stop ID that any data in this context is valid for
238 private:
239  // For RegisterContext only
240  RegisterContext(const RegisterContext &) = delete;
241  const RegisterContext &operator=(const RegisterContext &) = delete;
242 };
243 
244 } // namespace lldb_private
245 
246 #endif // LLDB_TARGET_REGISTERCONTEXT_H
lldb_private::RegisterContext::WriteRegister
virtual bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue &reg_value)=0
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::RegisterContext::GetRegisterCount
virtual size_t GetRegisterCount()=0
lldb_private::RegisterContext::GetStopID
uint32_t GetStopID() const
Definition: RegisterContext.h:215
lldb_private::RegisterValue
Definition: RegisterValue.h:28
lldb_private::RegisterContext::WriteRegisterValueToMemory
virtual Status WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue &reg_value)
Definition: RegisterContext.cpp:368
lldb_private::RegisterContext::GetRegisterName
const char * GetRegisterName(uint32_t reg)
Definition: RegisterContext.cpp:88
lldb_private::RegisterContext::CalculateStackFrame
lldb::StackFrameSP CalculateStackFrame() override
Definition: RegisterContext.cpp:439
lldb_private::RegisterContext::GetRegisterSet
virtual const RegisterSet * GetRegisterSet(size_t reg_set)=0
lldb_private::RegisterContext::ReadRegister
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
lldb_private::RegisterContext::ReadAllRegisterValues
virtual bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp)
Definition: RegisterContext.h:46
lldb_private::RegisterContext::ConvertRegisterKindToRegisterNumber
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
Definition: RegisterContext.cpp:268
lldb_private::RegisterContext::ReadRegisterAsUnsigned
uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value)
Definition: RegisterContext.cpp:188
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
pc
@ pc
Definition: CompactUnwindInfo.cpp:1250
lldb_private::RegisterContext::GetPCForSymbolication
bool GetPCForSymbolication(Address &address)
Get an address suitable for symbolication.
Definition: RegisterContext.cpp:127
lldb_private::RegisterContext::GetRegisterInfoAtIndex
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
lldb_private::RegisterContext::InvalidateIfNeeded
void InvalidateIfNeeded(bool force)
Definition: RegisterContext.cpp:32
lldb_private::RegisterContext::GetThreadID
virtual lldb::tid_t GetThreadID() const
Definition: RegisterContext.cpp:256
lldb_private::RegisterContext::NumSupportedHardwareBreakpoints
virtual uint32_t NumSupportedHardwareBreakpoints()
Definition: RegisterContext.cpp:258
lldb_private::RegisterContext::BehavesLikeZerothFrame
virtual bool BehavesLikeZerothFrame() const
Indicates that this frame is currently executing code, that the PC value is not a return-pc but an ac...
Definition: RegisterContext.h:229
lldb_private::RegisterContext::ClearHardwareBreakpoint
virtual bool ClearHardwareBreakpoint(uint32_t hw_idx)
Definition: RegisterContext.cpp:283
lldb_private::RegisterContext::m_thread
Thread & m_thread
Definition: RegisterContext.h:234
lldb_private::RegisterContext::CalculateProcess
lldb::ProcessSP CalculateProcess() override
Definition: RegisterContext.cpp:431
lldb_private::RegisterContext::CalculateExecutionContext
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Definition: RegisterContext.cpp:446
lldb_private::RegisterContext::HardwareSingleStep
virtual bool HardwareSingleStep(bool enable)
Definition: RegisterContext.cpp:296
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::RegisterContext::GetPC
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:95
lldb_private::RegisterContext::~RegisterContext
~RegisterContext() override
lldb_private::Thread
Definition: Thread.h:60
lldb_private::RegisterContext::GetReturnAddress
uint64_t GetReturnAddress(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:176
lldb_private::RegisterContext::GetRegisterInfo
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
Definition: RegisterContext.cpp:80
lldb_private::RegisterContext::SetPC
bool SetPC(uint64_t pc)
Definition: RegisterContext.cpp:112
lldb_private::RegisterContext::SetHardwareWatchpoint
virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, bool read, bool write)
Definition: RegisterContext.cpp:287
sp
@ sp
Definition: CompactUnwindInfo.cpp:1249
lldb_private::RegisterContext::GetFP
uint64_t GetFP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:164
lldb_private::RegisterContext::ReadRegisterValueFromMemory
virtual Status ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue &reg_value)
Definition: RegisterContext.cpp:298
lldb_private::RegisterContext::GetSP
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
Definition: RegisterContext.cpp:152
lldb_private::RegisterContext
Definition: RegisterContext.h:17
lldb::RegisterKind
RegisterKind
Register numbering types.
Definition: lldb-enumerations.h:227
lldb-private.h
lldb_private::RegisterContext::GetByteOrder
virtual lldb::ByteOrder GetByteOrder()
Definition: RegisterContext.cpp:406
lldb_private::RegisterContext::GetRegisterSetCount
virtual size_t GetRegisterSetCount()=0
lldb_private::RegisterContext::m_stop_id
uint32_t m_stop_id
Definition: RegisterContext.h:237
lldb_private::RegisterContext::RegisterContext
RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
Definition: RegisterContext.cpp:26
lldb_private::RegisterContext::SetHardwareBreakpoint
virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
Definition: RegisterContext.cpp:260
lldb_private::RegisterContext::NumSupportedHardwareWatchpoints
virtual uint32_t NumSupportedHardwareWatchpoints()
Definition: RegisterContext.cpp:285
lldb_private::RegisterContext::SetFP
bool SetFP(uint64_t fp)
Definition: RegisterContext.cpp:170
lldb_private::Status
Definition: Status.h:44
lldb_private::RegisterContext::m_concrete_frame_idx
uint32_t m_concrete_frame_idx
Definition: RegisterContext.h:235
lldb_private::RegisterContext::WriteRegisterFromUnsigned
bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
Definition: RegisterContext.cpp:205
uint32_t
lldb_private::RegisterContext::operator=
const RegisterContext & operator=(const RegisterContext &)=delete
lldb_private::Address
Definition: Address.h:59
ExecutionContextScope.h
lldb_private::RegisterCheckpoint
Definition: RegisterCheckpoint.h:21
lldb_private::RegisterContext::GetThread
virtual Thread & GetThread()
Definition: RegisterContext.h:137
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::RegisterContext::SetStopID
void SetStopID(uint32_t stop_id)
Definition: RegisterContext.h:217
lldb_private::RegisterContext::CalculateThread
lldb::ThreadSP CalculateThread() override
Definition: RegisterContext.cpp:435
lldb_private::RegisterContext::SetSP
bool SetSP(uint64_t sp)
Definition: RegisterContext.cpp:158
lldb_private::RegisterContext::InvalidateAllRegisters
virtual void InvalidateAllRegisters()=0
lldb_private::RegisterContext::CalculateTarget
lldb::TargetSP CalculateTarget() override
Definition: RegisterContext.cpp:427
lldb_private::RegisterContext::WriteAllRegisterValues
virtual bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp)
Definition: RegisterContext.h:50
lldb_private::RegisterContext::ClearHardwareWatchpoint
virtual bool ClearHardwareWatchpoint(uint32_t hw_index)
Definition: RegisterContext.cpp:292
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::RegisterContext::ConvertBetweenRegisterKinds
bool ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk, uint32_t source_regnum, lldb::RegisterKind target_rk, uint32_t &target_regnum)
Definition: RegisterContext.cpp:450
lldb_private::RegisterContext::GetFlags
uint64_t GetFlags(uint64_t fail_value=0)
Definition: RegisterContext.cpp:182
fp
@ fp
Definition: CompactUnwindInfo.cpp:1247
lldb_private::RegisterContext::CopyFromRegisterContext
bool CopyFromRegisterContext(lldb::RegisterContextSP context)
Definition: RegisterContext.cpp:221
lldb_private::RegisterContext::GetRegisterInfoByName
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
Definition: RegisterContext.cpp:52
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138