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"
14 #include "lldb/Utility/DataExtractor.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.
36 class DWARFExpression {
37 public:
38   DWARFExpression();
39 
40   /// Constructor
41   ///
42   /// \param[in] data
43   ///     A data extractor configured to read the DWARF location expression's
44   ///     bytecode.
45   DWARFExpression(const DataExtractor &data);
46 
47   /// Destructor
48   virtual ~DWARFExpression();
49 
50   /// Return true if the location expression contains data
51   bool IsValid() const;
52 
53   /// Return the address specified by the first
54   /// DW_OP_{addr, addrx, GNU_addr_index} in the operation stream.
55   ///
56   /// \param[in] dwarf_cu
57   ///     The dwarf unit this expression belongs to. Only required to resolve
58   ///     DW_OP{addrx, GNU_addr_index}.
59   ///
60   /// \param[out] error
61   ///     If the location stream contains unknown DW_OP opcodes or the
62   ///     data is missing, \a error will be set to \b true.
63   ///
64   /// \return
65   ///     The address specified by the operation, if the operation exists, or
66   ///     LLDB_INVALID_ADDRESS otherwise.
67   lldb::addr_t GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu,
68                                       bool &error) const;
69 
70   bool Update_DW_OP_addr(const DWARFUnit *dwarf_cu, lldb::addr_t file_addr);
71 
72   void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size,
73                    uint8_t addr_byte_size);
74 
75   bool ContainsThreadLocalStorage(const DWARFUnit *dwarf_cu) const;
76 
77   bool LinkThreadLocalStorage(
78       const DWARFUnit *dwarf_cu,
79       std::function<lldb::addr_t(lldb::addr_t file_addr)> const
80           &link_address_callback);
81 
82   /// Return the call-frame-info style register kind
83   lldb::RegisterKind GetRegisterKind() const;
84 
85   /// Set the call-frame-info style register kind
86   ///
87   /// \param[in] reg_kind
88   ///     The register kind.
89   void SetRegisterKind(lldb::RegisterKind reg_kind);
90 
91   /// Evaluate a DWARF location expression in a particular context
92   ///
93   /// \param[in] exe_ctx
94   ///     The execution context in which to evaluate the location
95   ///     expression.  The location expression may access the target's
96   ///     memory, especially if it comes from the expression parser.
97   ///
98   /// \param[in] opcode_ctx
99   ///     The module which defined the expression.
100   ///
101   /// \param[in] opcodes
102   ///     This is a static method so the opcodes need to be provided
103   ///     explicitly.
104   ///
105   ///  \param[in] reg_ctx
106   ///     An optional parameter which provides a RegisterContext for use
107   ///     when evaluating the expression (i.e. for fetching register values).
108   ///     Normally this will come from the ExecutionContext's StackFrame but
109   ///     in the case where an expression needs to be evaluated while building
110   ///     the stack frame list, this short-cut is available.
111   ///
112   /// \param[in] reg_set
113   ///     The call-frame-info style register kind.
114   ///
115   /// \param[in] initial_value_ptr
116   ///     A value to put on top of the interpreter stack before evaluating
117   ///     the expression, if the expression is parametrized.  Can be NULL.
118   ///
119   /// \param[in] result
120   ///     A value into which the result of evaluating the expression is
121   ///     to be placed.
122   ///
123   /// \param[in] error_ptr
124   ///     If non-NULL, used to report errors in expression evaluation.
125   ///
126   /// \return
127   ///     True on success; false otherwise.  If error_ptr is non-NULL,
128   ///     details of the failure are provided through it.
129   static bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
130                        lldb::ModuleSP module_sp, const DataExtractor &opcodes,
131                        const DWARFUnit *dwarf_cu,
132                        const lldb::RegisterKind reg_set,
133                        const Value *initial_value_ptr,
134                        const Value *object_address_ptr, Value &result,
135                        Status *error_ptr);
136 
137   static bool ParseDWARFLocationList(const DWARFUnit *dwarf_cu,
138                                      const DataExtractor &data,
139                                      DWARFExpressionList *loc_list);
140 
141   bool GetExpressionData(DataExtractor &data) const {
142     data = m_data;
143     return data.GetByteSize() > 0;
144   }
145 
146   void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi) const;
147 
148   bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const;
149 
150 private:
151   /// A data extractor capable of reading opcode bytes
152   DataExtractor m_data;
153 
154   /// One of the defines that starts with LLDB_REGKIND_
155   lldb::RegisterKind m_reg_kind = lldb::eRegisterKindDWARF;
156 };
157 
158 } // namespace lldb_private
159 
160 #endif // LLDB_EXPRESSION_DWARFEXPRESSION_H
161