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