1 //===-- ExpressionVariable.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_EXPRESSIONVARIABLE_H
10 #define LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
11 
12 #include <memory>
13 #include <optional>
14 #include <vector>
15 
16 #include "llvm/ADT/DenseMap.h"
17 
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/lldb-public.h"
21 
22 namespace lldb_private {
23 
24 class ExpressionVariable
25     : public std::enable_shared_from_this<ExpressionVariable> {
26 public:
27   // See TypeSystem.h for how to add subclasses to this.
28   enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
29 
getKind()30   LLVMCastKind getKind() const { return m_kind; }
31 
ExpressionVariable(LLVMCastKind kind)32   ExpressionVariable(LLVMCastKind kind) : m_flags(0), m_kind(kind) {}
33 
34   virtual ~ExpressionVariable();
35 
GetByteSize()36   std::optional<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
37 
GetName()38   ConstString GetName() { return m_frozen_sp->GetName(); }
39 
GetValueObject()40   lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
41 
42   uint8_t *GetValueBytes();
43 
ValueUpdated()44   void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
45 
GetRegisterInfo()46   RegisterInfo *GetRegisterInfo() {
47     return m_frozen_sp->GetValue().GetRegisterInfo();
48   }
49 
SetRegisterInfo(const RegisterInfo * reg_info)50   void SetRegisterInfo(const RegisterInfo *reg_info) {
51     return m_frozen_sp->GetValue().SetContext(
52         Value::ContextType::RegisterInfo, const_cast<RegisterInfo *>(reg_info));
53   }
54 
GetCompilerType()55   CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
56 
SetCompilerType(const CompilerType & compiler_type)57   void SetCompilerType(const CompilerType &compiler_type) {
58     m_frozen_sp->GetValue().SetCompilerType(compiler_type);
59   }
60 
SetName(ConstString name)61   void SetName(ConstString name) { m_frozen_sp->SetName(name); }
62 
63   // this function is used to copy the address-of m_live_sp into m_frozen_sp
64   // this is necessary because the results of certain cast and pointer-
65   // arithmetic operations (such as those described in bugzilla issues 11588
66   // and 11618) generate frozen objects that do not have a valid address-of,
67   // which can be troublesome when using synthetic children providers.
68   // Transferring the address-of the live object solves these issues and
69   // provides the expected user-level behavior
70   void TransferAddress(bool force = false) {
71     if (m_live_sp.get() == nullptr)
72       return;
73 
74     if (m_frozen_sp.get() == nullptr)
75       return;
76 
77     if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
78       m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
79   }
80 
81   enum Flags {
82     EVNone = 0,
83     EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
84                                 ///specifically allocated for it by LLDB in the
85                                 ///target process
86     EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
87                                    ///(possibly invalid) area managed by the
88                                    ///target program
89     EVNeedsAllocation = 1 << 2,    ///< Space for this variable has yet to be
90                                    ///allocated in the target process
91     EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
92                               ///m_frozen_sp (for example, for
93                               ///statically-computed results)
94     EVNeedsFreezeDry =
95         1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
96     EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
97                              ///complete rather than freeze drying its contents
98                              ///and freeing it
99     EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
100                                 ///reference, so materialize the value rather
101                                 ///than the location
102     EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
103                             ///some other entity.
104   };
105 
106   typedef uint16_t FlagType;
107 
108   FlagType m_flags; // takes elements of Flags
109 
110   // these should be private
111   lldb::ValueObjectSP m_frozen_sp;
112   lldb::ValueObjectSP m_live_sp;
113   LLVMCastKind m_kind;
114 };
115 
116 /// \class ExpressionVariableList ExpressionVariable.h
117 /// "lldb/Expression/ExpressionVariable.h"
118 /// A list of variable references.
119 ///
120 /// This class stores variables internally, acting as the permanent store.
121 class ExpressionVariableList {
122 public:
123   /// Implementation of methods in ExpressionVariableListBase
GetSize()124   size_t GetSize() { return m_variables.size(); }
125 
GetVariableAtIndex(size_t index)126   lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
127     lldb::ExpressionVariableSP var_sp;
128     if (index < m_variables.size())
129       var_sp = m_variables[index];
130     return var_sp;
131   }
132 
AddVariable(const lldb::ExpressionVariableSP & var_sp)133   size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
134     m_variables.push_back(var_sp);
135     return m_variables.size() - 1;
136   }
137 
138   lldb::ExpressionVariableSP
AddNewlyConstructedVariable(ExpressionVariable * var)139   AddNewlyConstructedVariable(ExpressionVariable *var) {
140     lldb::ExpressionVariableSP var_sp(var);
141     m_variables.push_back(var_sp);
142     return m_variables.back();
143   }
144 
ContainsVariable(const lldb::ExpressionVariableSP & var_sp)145   bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
146     const size_t size = m_variables.size();
147     for (size_t index = 0; index < size; ++index) {
148       if (m_variables[index].get() == var_sp.get())
149         return true;
150     }
151     return false;
152   }
153 
154   /// Finds a variable by name in the list.
155   ///
156   /// \param[in] name
157   ///     The name of the requested variable.
158   ///
159   /// \return
160   ///     The variable requested, or nullptr if that variable is not in the
161   ///     list.
GetVariable(ConstString name)162   lldb::ExpressionVariableSP GetVariable(ConstString name) {
163     lldb::ExpressionVariableSP var_sp;
164     for (size_t index = 0, size = GetSize(); index < size; ++index) {
165       var_sp = GetVariableAtIndex(index);
166       if (var_sp->GetName() == name)
167         return var_sp;
168     }
169     var_sp.reset();
170     return var_sp;
171   }
172 
GetVariable(llvm::StringRef name)173   lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
174     if (name.empty())
175       return nullptr;
176 
177     for (size_t index = 0, size = GetSize(); index < size; ++index) {
178       auto var_sp = GetVariableAtIndex(index);
179       llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
180       if (var_name_str == name)
181         return var_sp;
182     }
183     return nullptr;
184   }
185 
RemoveVariable(lldb::ExpressionVariableSP var_sp)186   void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
187     for (std::vector<lldb::ExpressionVariableSP>::iterator
188              vi = m_variables.begin(),
189              ve = m_variables.end();
190          vi != ve; ++vi) {
191       if (vi->get() == var_sp.get()) {
192         m_variables.erase(vi);
193         return;
194       }
195     }
196   }
197 
Clear()198   void Clear() { m_variables.clear(); }
199 
200 private:
201   std::vector<lldb::ExpressionVariableSP> m_variables;
202 };
203 
204 class PersistentExpressionState : public ExpressionVariableList {
205 public:
206   // See TypeSystem.h for how to add subclasses to this.
207   enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
208 
getKind()209   LLVMCastKind getKind() const { return m_kind; }
210 
PersistentExpressionState(LLVMCastKind kind)211   PersistentExpressionState(LLVMCastKind kind) : m_kind(kind) {}
212 
213   virtual ~PersistentExpressionState();
214 
215   virtual lldb::ExpressionVariableSP
216   CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
217 
218   virtual lldb::ExpressionVariableSP
219   CreatePersistentVariable(ExecutionContextScope *exe_scope,
220                            ConstString name, const CompilerType &type,
221                            lldb::ByteOrder byte_order,
222                            uint32_t addr_byte_size) = 0;
223 
224   /// Return a new persistent variable name with the specified prefix.
225   virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
226 
227   virtual void
228   RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
229 
230   virtual std::optional<CompilerType>
231   GetCompilerTypeFromPersistentDecl(ConstString type_name) = 0;
232 
233   virtual lldb::addr_t LookupSymbol(ConstString name);
234 
235   void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
236 
237 protected:
238   virtual llvm::StringRef
239   GetPersistentVariablePrefix(bool is_error = false) const = 0;
240 
241 private:
242   LLVMCastKind m_kind;
243 
244   typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
245   ExecutionUnitSet
246       m_execution_units; ///< The execution units that contain valuable symbols.
247 
248   typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
249   SymbolMap
250       m_symbol_map; ///< The addresses of the symbols in m_execution_units.
251 };
252 
253 } // namespace lldb_private
254 
255 #endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
256