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