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