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"
14#include "lldb/Core/dwarf.h"
16#include "lldb/Utility/Scalar.h"
17#include "lldb/Utility/Status.h"
18#include "lldb/lldb-private.h"
19#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
20#include "llvm/Support/Error.h"
21#include <functional>
22
23namespace 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.
37public:
38 using Stack = std::vector<Value>;
39
40 class Delegate {
41 public:
42 Delegate() = default;
43 virtual ~Delegate() = default;
44
45 virtual uint16_t GetVersion() const = 0;
46 virtual dw_addr_t GetBaseAddress() const = 0;
47 virtual uint8_t GetAddressByteSize() const = 0;
48 virtual llvm::Expected<std::pair<uint64_t, bool>>
49 GetDIEBitSizeAndSign(uint64_t relative_die_offset) const = 0;
50 virtual dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const = 0;
51 virtual lldb::offset_t
53 const lldb::offset_t data_offset,
54 const uint8_t op) const = 0;
55 virtual bool
56 ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
57 lldb::offset_t &offset, RegisterContext *reg_ctx,
58 lldb::RegisterKind reg_kind, Stack &stack) const = 0;
59
60 Delegate(const Delegate &) = delete;
61 Delegate &operator=(const Delegate &) = delete;
62 };
63
65
66 /// Constructor
67 ///
68 /// \param[in] data
69 /// A data extractor configured to read the DWARF location expression's
70 /// bytecode.
71 DWARFExpression(const DataExtractor &data);
72
73 /// Destructor
75
76 /// Return true if the location expression contains data
77 bool IsValid() const;
78
79 /// Return the address specified by the first
80 /// DW_OP_{addr, addrx, GNU_addr_index} in the operation stream.
81 ///
82 /// \param[in] dwarf_cu
83 /// The dwarf unit this expression belongs to. Only required to resolve
84 /// DW_OP{addrx, GNU_addr_index}.
85 ///
86 /// \return
87 /// The address specified by the operation, if the operation exists, or
88 /// an llvm::Error otherwise.
89 llvm::Expected<lldb::addr_t>
90 GetLocation_DW_OP_addr(const Delegate *dwarf_cu) const;
91
92 bool Update_DW_OP_addr(const Delegate *dwarf_cu, lldb::addr_t file_addr);
93
94 void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size,
95 uint8_t addr_byte_size);
96
97 bool ContainsThreadLocalStorage(const Delegate *dwarf_cu) const;
98
100 const Delegate *dwarf_cu,
101 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
102 &link_address_callback);
103
104 /// Return the call-frame-info style register kind
106
107 /// Set the call-frame-info style register kind
108 ///
109 /// \param[in] reg_kind
110 /// The register kind.
111 void SetRegisterKind(lldb::RegisterKind reg_kind);
112
113 /// Evaluate a DWARF location expression in a particular context
114 ///
115 /// \param[in] exe_ctx
116 /// The execution context in which to evaluate the location
117 /// expression. The location expression may access the target's
118 /// memory, especially if it comes from the expression parser.
119 ///
120 /// \param[in] opcode_ctx
121 /// The module which defined the expression.
122 ///
123 /// \param[in] opcodes
124 /// This is a static method so the opcodes need to be provided
125 /// explicitly.
126 ///
127 /// \param[in] reg_ctx
128 /// An optional parameter which provides a RegisterContext for use
129 /// when evaluating the expression (i.e. for fetching register values).
130 /// Normally this will come from the ExecutionContext's StackFrame but
131 /// in the case where an expression needs to be evaluated while building
132 /// the stack frame list, this short-cut is available.
133 ///
134 /// \param[in] reg_set
135 /// The call-frame-info style register kind.
136 ///
137 /// \param[in] initial_value_ptr
138 /// A value to put on top of the interpreter stack before evaluating
139 /// the expression, if the expression is parametrized. Can be NULL.
140 ///
141 /// \param[in] result
142 /// A value into which the result of evaluating the expression is
143 /// to be placed.
144 ///
145 /// \param[in] error_ptr
146 /// If non-NULL, used to report errors in expression evaluation.
147 ///
148 /// \return
149 /// True on success; false otherwise. If error_ptr is non-NULL,
150 /// details of the failure are provided through it.
151 static llvm::Expected<Value>
152 Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
153 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
154 const Delegate *dwarf_cu, const lldb::RegisterKind reg_set,
155 const Value *initial_value_ptr, const Value *object_address_ptr);
156
158 data = m_data;
159 return data.GetByteSize() > 0;
160 }
161
162 void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi,
163 llvm::DIDumpOptions options = {}) const;
164
165 bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const;
166
167 static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
168 lldb::RegisterKind reg_kind,
169 uint32_t reg_num, Value &value);
170
171private:
172 /// A data extractor capable of reading opcode bytes
174
175 /// One of the defines that starts with LLDB_REGKIND_
177};
178
179} // namespace lldb_private
180
181#endif // LLDB_EXPRESSION_DWARFEXPRESSION_H
Delegate & operator=(const Delegate &)=delete
Delegate(const Delegate &)=delete
virtual uint16_t GetVersion() const =0
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes, lldb::offset_t &offset, RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, Stack &stack) const =0
virtual lldb::offset_t GetVendorDWARFOpcodeSize(const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op) const =0
virtual dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const =0
virtual dw_addr_t GetBaseAddress() const =0
virtual llvm::Expected< std::pair< uint64_t, bool > > GetDIEBitSizeAndSign(uint64_t relative_die_offset) const =0
virtual uint8_t GetAddressByteSize() const =0
DataExtractor m_data
A data extractor capable of reading opcode bytes.
bool GetExpressionData(DataExtractor &data) const
llvm::Expected< lldb::addr_t > GetLocation_DW_OP_addr(const Delegate *dwarf_cu) const
Return the address specified by the first DW_OP_{addr, addrx, GNU_addr_index} in the operation stream...
void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size, uint8_t addr_byte_size)
static llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::ModuleSP module_sp, const DataExtractor &opcodes, const Delegate *dwarf_cu, const lldb::RegisterKind reg_set, const Value *initial_value_ptr, const Value *object_address_ptr)
Evaluate a DWARF location expression in a particular context.
bool ContainsThreadLocalStorage(const Delegate *dwarf_cu) const
bool LinkThreadLocalStorage(const Delegate *dwarf_cu, std::function< lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback)
lldb::RegisterKind m_reg_kind
One of the defines that starts with LLDB_REGKIND_.
bool Update_DW_OP_addr(const Delegate *dwarf_cu, lldb::addr_t file_addr)
void SetRegisterKind(lldb::RegisterKind reg_kind)
Set the call-frame-info style register kind.
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, uint32_t reg_num, Value &value)
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const
void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi, llvm::DIDumpOptions options={}) const
lldb::RegisterKind GetRegisterKind() const
Return the call-frame-info style register kind.
bool IsValid() const
Return true if the location expression contains data.
An data extractor class.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
uint64_t dw_addr_t
Definition dwarf.h:20
A class that represents a running process on the host machine.
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
uint64_t offset_t
Definition lldb-types.h:85
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
RegisterKind
Register numbering types.
@ eRegisterKindDWARF
the register numbers seen DWARF