RegisterContext.h 9.6 KB

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