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