DWARFExpression.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //===-- DWARFExpression.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_EXPRESSION_DWARFEXPRESSION_H
  9. #define LLDB_EXPRESSION_DWARFEXPRESSION_H
  10. #include "lldb/Core/Address.h"
  11. #include "lldb/Core/Disassembler.h"
  12. #include "lldb/Utility/DataExtractor.h"
  13. #include "lldb/Utility/Scalar.h"
  14. #include "lldb/Utility/Status.h"
  15. #include "lldb/lldb-private.h"
  16. #include <functional>
  17. class DWARFUnit;
  18. namespace lldb_private {
  19. /// \class DWARFExpression DWARFExpression.h
  20. /// "lldb/Expression/DWARFExpression.h" Encapsulates a DWARF location
  21. /// expression and interprets it.
  22. ///
  23. /// DWARF location expressions are used in two ways by LLDB. The first
  24. /// use is to find entities specified in the debug information, since their
  25. /// locations are specified in precisely this language. The second is to
  26. /// interpret expressions without having to run the target in cases where the
  27. /// overhead from copying JIT-compiled code into the target is too high or
  28. /// where the target cannot be run. This class encapsulates a single DWARF
  29. /// location expression or a location list and interprets it.
  30. class DWARFExpression {
  31. public:
  32. DWARFExpression();
  33. /// Constructor
  34. ///
  35. /// \param[in] data
  36. /// A data extractor configured to read the DWARF location expression's
  37. /// bytecode.
  38. DWARFExpression(lldb::ModuleSP module, const DataExtractor &data,
  39. const DWARFUnit *dwarf_cu);
  40. /// Destructor
  41. virtual ~DWARFExpression();
  42. /// Print the description of the expression to a stream
  43. ///
  44. /// \param[in] s
  45. /// The stream to print to.
  46. ///
  47. /// \param[in] level
  48. /// The level of verbosity to use.
  49. ///
  50. /// \param[in] location_list_base_addr
  51. /// If this is a location list based expression, this is the
  52. /// address of the object that owns it. NOTE: this value is
  53. /// different from the DWARF version of the location list base
  54. /// address which is compile unit relative. This base address
  55. /// is the address of the object that owns the location list.
  56. ///
  57. /// \param[in] abi
  58. /// An optional ABI plug-in that can be used to resolve register
  59. /// names.
  60. void GetDescription(Stream *s, lldb::DescriptionLevel level,
  61. lldb::addr_t location_list_base_addr, ABI *abi) const;
  62. /// Return true if the location expression contains data
  63. bool IsValid() const;
  64. /// Return true if a location list was provided
  65. bool IsLocationList() const;
  66. /// Search for a load address in the location list
  67. ///
  68. /// \param[in] func_load_addr
  69. /// The actual address of the function containing this location list.
  70. ///
  71. /// \param[in] addr
  72. /// The address to resolve
  73. ///
  74. /// \return
  75. /// True if IsLocationList() is true and the address was found;
  76. /// false otherwise.
  77. // bool
  78. // LocationListContainsLoadAddress (Process* process, const Address &addr)
  79. // const;
  80. //
  81. bool LocationListContainsAddress(lldb::addr_t func_load_addr,
  82. lldb::addr_t addr) const;
  83. /// If a location is not a location list, return true if the location
  84. /// contains a DW_OP_addr () opcode in the stream that matches \a file_addr.
  85. /// If file_addr is LLDB_INVALID_ADDRESS, the this function will return true
  86. /// if the variable there is any DW_OP_addr in a location that (yet still is
  87. /// NOT a location list). This helps us detect if a variable is a global or
  88. /// static variable since there is no other indication from DWARF debug
  89. /// info.
  90. ///
  91. /// \param[in] op_addr_idx
  92. /// The DW_OP_addr index to retrieve in case there is more than
  93. /// one DW_OP_addr opcode in the location byte stream.
  94. ///
  95. /// \param[out] error
  96. /// If the location stream contains unknown DW_OP opcodes or the
  97. /// data is missing, \a error will be set to \b true.
  98. ///
  99. /// \return
  100. /// LLDB_INVALID_ADDRESS if the location doesn't contain a
  101. /// DW_OP_addr for \a op_addr_idx, otherwise a valid file address
  102. lldb::addr_t GetLocation_DW_OP_addr(uint32_t op_addr_idx, bool &error) const;
  103. bool Update_DW_OP_addr(lldb::addr_t file_addr);
  104. void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size,
  105. uint8_t addr_byte_size);
  106. void SetModule(const lldb::ModuleSP &module) { m_module_wp = module; }
  107. bool ContainsThreadLocalStorage() const;
  108. bool LinkThreadLocalStorage(
  109. lldb::ModuleSP new_module_sp,
  110. std::function<lldb::addr_t(lldb::addr_t file_addr)> const
  111. &link_address_callback);
  112. /// Tells the expression that it refers to a location list.
  113. ///
  114. /// \param[in] cu_file_addr
  115. /// The base address to use for interpreting relative location list
  116. /// entries.
  117. /// \param[in] func_file_addr
  118. /// The file address of the function containing this location list. This
  119. /// address will be used to relocate the location list on the fly (in
  120. /// conjuction with the func_load_addr arguments).
  121. void SetLocationListAddresses(lldb::addr_t cu_file_addr,
  122. lldb::addr_t func_file_addr);
  123. /// Return the call-frame-info style register kind
  124. int GetRegisterKind();
  125. /// Set the call-frame-info style register kind
  126. ///
  127. /// \param[in] reg_kind
  128. /// The register kind.
  129. void SetRegisterKind(lldb::RegisterKind reg_kind);
  130. /// Wrapper for the static evaluate function that accepts an
  131. /// ExecutionContextScope instead of an ExecutionContext and uses member
  132. /// variables to populate many operands
  133. bool Evaluate(ExecutionContextScope *exe_scope, lldb::addr_t func_load_addr,
  134. const Value *initial_value_ptr, const Value *object_address_ptr,
  135. Value &result, Status *error_ptr) const;
  136. /// Wrapper for the static evaluate function that uses member variables to
  137. /// populate many operands
  138. bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  139. lldb::addr_t loclist_base_load_addr,
  140. const Value *initial_value_ptr, const Value *object_address_ptr,
  141. Value &result, Status *error_ptr) const;
  142. /// Evaluate a DWARF location expression in a particular context
  143. ///
  144. /// \param[in] exe_ctx
  145. /// The execution context in which to evaluate the location
  146. /// expression. The location expression may access the target's
  147. /// memory, especially if it comes from the expression parser.
  148. ///
  149. /// \param[in] opcode_ctx
  150. /// The module which defined the expression.
  151. ///
  152. /// \param[in] opcodes
  153. /// This is a static method so the opcodes need to be provided
  154. /// explicitly.
  155. ///
  156. /// \param[in] reg_ctx
  157. /// An optional parameter which provides a RegisterContext for use
  158. /// when evaluating the expression (i.e. for fetching register values).
  159. /// Normally this will come from the ExecutionContext's StackFrame but
  160. /// in the case where an expression needs to be evaluated while building
  161. /// the stack frame list, this short-cut is available.
  162. ///
  163. /// \param[in] reg_set
  164. /// The call-frame-info style register kind.
  165. ///
  166. /// \param[in] initial_value_ptr
  167. /// A value to put on top of the interpreter stack before evaluating
  168. /// the expression, if the expression is parametrized. Can be NULL.
  169. ///
  170. /// \param[in] result
  171. /// A value into which the result of evaluating the expression is
  172. /// to be placed.
  173. ///
  174. /// \param[in] error_ptr
  175. /// If non-NULL, used to report errors in expression evaluation.
  176. ///
  177. /// \return
  178. /// True on success; false otherwise. If error_ptr is non-NULL,
  179. /// details of the failure are provided through it.
  180. static bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
  181. lldb::ModuleSP opcode_ctx, const DataExtractor &opcodes,
  182. const DWARFUnit *dwarf_cu,
  183. const lldb::RegisterKind reg_set,
  184. const Value *initial_value_ptr,
  185. const Value *object_address_ptr, Value &result,
  186. Status *error_ptr);
  187. bool GetExpressionData(DataExtractor &data) const {
  188. data = m_data;
  189. return data.GetByteSize() > 0;
  190. }
  191. bool DumpLocationForAddress(Stream *s, lldb::DescriptionLevel level,
  192. lldb::addr_t func_load_addr, lldb::addr_t address,
  193. ABI *abi);
  194. bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op);
  195. llvm::Optional<DataExtractor>
  196. GetLocationExpression(lldb::addr_t load_function_start,
  197. lldb::addr_t addr) const;
  198. private:
  199. /// Pretty-prints the location expression to a stream
  200. ///
  201. /// \param[in] s
  202. /// The stream to use for pretty-printing.
  203. ///
  204. /// \param[in] data
  205. /// The data extractor.
  206. ///
  207. /// \param[in] level
  208. /// The level of detail to use in pretty-printing.
  209. ///
  210. /// \param[in] abi
  211. /// An optional ABI plug-in that can be used to resolve register
  212. /// names.
  213. void DumpLocation(Stream *s, const DataExtractor &data,
  214. lldb::DescriptionLevel level, ABI *abi) const;
  215. /// Module which defined this expression.
  216. lldb::ModuleWP m_module_wp;
  217. /// A data extractor capable of reading opcode bytes
  218. DataExtractor m_data;
  219. /// The DWARF compile unit this expression belongs to. It is used to evaluate
  220. /// values indexing into the .debug_addr section (e.g. DW_OP_GNU_addr_index,
  221. /// DW_OP_GNU_const_index)
  222. const DWARFUnit *m_dwarf_cu;
  223. /// One of the defines that starts with LLDB_REGKIND_
  224. lldb::RegisterKind m_reg_kind;
  225. struct LoclistAddresses {
  226. lldb::addr_t cu_file_addr;
  227. lldb::addr_t func_file_addr;
  228. };
  229. llvm::Optional<LoclistAddresses> m_loclist_addresses;
  230. };
  231. } // namespace lldb_private
  232. #endif // LLDB_EXPRESSION_DWARFEXPRESSION_H