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 LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
10 #define LLDB_EXPRESSION_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 
24  : public std::enable_shared_from_this<ExpressionVariable> {
25 public:
26  // See TypeSystem.h for how to add subclasses to this.
28 
29  LLVMCastKind getKind() const { return m_kind; }
30 
32 
33  virtual ~ExpressionVariable();
34 
35  llvm::Optional<uint64_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::ContextType::RegisterInfo, 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)
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  EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
102  ///some other entity.
103  };
104 
106 
107  FlagType m_flags; // takes elements of Flags
108 
109  // these should be private
110  lldb::ValueObjectSP m_frozen_sp;
111  lldb::ValueObjectSP m_live_sp;
113 };
114 
115 /// \class ExpressionVariableList ExpressionVariable.h
116 /// "lldb/Expression/ExpressionVariable.h"
117 /// A list of variable references.
118 ///
119 /// This class stores variables internally, acting as the permanent store.
121 public:
122  /// Implementation of methods in ExpressionVariableListBase
123  size_t GetSize() { return m_variables.size(); }
124 
125  lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
126  lldb::ExpressionVariableSP var_sp;
127  if (index < m_variables.size())
128  var_sp = m_variables[index];
129  return var_sp;
130  }
131 
132  size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
133  m_variables.push_back(var_sp);
134  return m_variables.size() - 1;
135  }
136 
137  lldb::ExpressionVariableSP
139  lldb::ExpressionVariableSP var_sp(var);
140  m_variables.push_back(var_sp);
141  return m_variables.back();
142  }
143 
144  bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
145  const size_t size = m_variables.size();
146  for (size_t index = 0; index < size; ++index) {
147  if (m_variables[index].get() == var_sp.get())
148  return true;
149  }
150  return false;
151  }
152 
153  /// Finds a variable by name in the list.
154  ///
155  /// \param[in] name
156  /// The name of the requested variable.
157  ///
158  /// \return
159  /// The variable requested, or nullptr if that variable is not in the
160  /// list.
161  lldb::ExpressionVariableSP GetVariable(ConstString name) {
162  lldb::ExpressionVariableSP var_sp;
163  for (size_t index = 0, size = GetSize(); index < size; ++index) {
164  var_sp = GetVariableAtIndex(index);
165  if (var_sp->GetName() == name)
166  return var_sp;
167  }
168  var_sp.reset();
169  return var_sp;
170  }
171 
172  lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
173  if (name.empty())
174  return nullptr;
175 
176  for (size_t index = 0, size = GetSize(); index < size; ++index) {
177  auto var_sp = GetVariableAtIndex(index);
178  llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
179  if (var_name_str == name)
180  return var_sp;
181  }
182  return nullptr;
183  }
184 
185  void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
186  for (std::vector<lldb::ExpressionVariableSP>::iterator
187  vi = m_variables.begin(),
188  ve = m_variables.end();
189  vi != ve; ++vi) {
190  if (vi->get() == var_sp.get()) {
191  m_variables.erase(vi);
192  return;
193  }
194  }
195  }
196 
197  void Clear() { m_variables.clear(); }
198 
199 private:
200  std::vector<lldb::ExpressionVariableSP> m_variables;
201 };
202 
204 public:
205  // See TypeSystem.h for how to add subclasses to this.
207 
208  LLVMCastKind getKind() const { return m_kind; }
209 
211 
212  virtual ~PersistentExpressionState();
213 
214  virtual lldb::ExpressionVariableSP
215  CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
216 
217  virtual lldb::ExpressionVariableSP
219  ConstString name, const CompilerType &type,
220  lldb::ByteOrder byte_order,
221  uint32_t addr_byte_size) = 0;
222 
223  /// Return a new persistent variable name with the specified prefix.
224  virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
225 
226  virtual void
227  RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
228 
229  virtual llvm::Optional<CompilerType>
231 
232  virtual lldb::addr_t LookupSymbol(ConstString name);
233 
234  void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
235 
236 protected:
237  virtual llvm::StringRef
238  GetPersistentVariablePrefix(bool is_error = false) const = 0;
239 
240 private:
242 
243  typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
245  m_execution_units; ///< The execution units that contain valuable symbols.
246 
247  typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
248  SymbolMap
249  m_symbol_map; ///< The addresses of the symbols in m_execution_units.
250 };
251 
252 } // namespace lldb_private
253 
254 #endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
lldb_private::ExpressionVariableList::GetSize
size_t GetSize()
Implementation of methods in ExpressionVariableListBase.
Definition: ExpressionVariable.h:123
lldb_private::PersistentExpressionState::~PersistentExpressionState
virtual ~PersistentExpressionState()
lldb_private::ExpressionVariable::TransferAddress
void TransferAddress(bool force=false)
Definition: ExpressionVariable.h:69
lldb-public.h
lldb_private::ExpressionVariableList::RemoveVariable
void RemoveVariable(lldb::ExpressionVariableSP var_sp)
Definition: ExpressionVariable.h:185
lldb_private::ExpressionVariable::EVTypeIsReference
@ EVTypeIsReference
The original type of this variable is a reference, so materialize the value rather than the location.
Definition: ExpressionVariable.h:98
lldb_private::ExpressionVariable::ValueUpdated
void ValueUpdated()
Definition: ExpressionVariable.h:43
lldb_private::ExpressionVariableList::Clear
void Clear()
Definition: ExpressionVariable.h:197
lldb_private::PersistentExpressionState::LLVMCastKind
LLVMCastKind
Definition: ExpressionVariable.h:206
lldb_private::ExpressionVariable::eKindClang
@ eKindClang
Definition: ExpressionVariable.h:27
lldb_private::PersistentExpressionState::SymbolMap
llvm::DenseMap< const char *, lldb::addr_t > SymbolMap
Definition: ExpressionVariable.h:247
lldb_private::ExpressionVariable::EVIsLLDBAllocated
@ EVIsLLDBAllocated
This variable is resident in a location specifically allocated for it by LLDB in the target process.
Definition: ExpressionVariable.h:82
lldb_private::ExpressionVariable::eKindSwift
@ eKindSwift
Definition: ExpressionVariable.h:27
lldb_private::ExpressionVariable::SetRegisterInfo
void SetRegisterInfo(const RegisterInfo *reg_info)
Definition: ExpressionVariable.h:49
lldb_private::ExpressionVariableList::AddNewlyConstructedVariable
lldb::ExpressionVariableSP AddNewlyConstructedVariable(ExpressionVariable *var)
Definition: ExpressionVariable.h:138
lldb_private::ExpressionVariable::GetRegisterInfo
RegisterInfo * GetRegisterInfo()
Definition: ExpressionVariable.h:45
lldb_private::ExpressionVariable::EVIsFreezeDried
@ EVIsFreezeDried
This variable's authoritative version is in m_frozen_sp (for example, for statically-computed results...
Definition: ExpressionVariable.h:90
lldb_private::ExpressionVariable::EVNone
@ EVNone
Definition: ExpressionVariable.h:81
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::PersistentExpressionState::PersistentExpressionState
PersistentExpressionState(LLVMCastKind kind)
Definition: ExpressionVariable.h:210
lldb_private::ExpressionVariable::GetValueBytes
uint8_t * GetValueBytes()
Definition: ExpressionVariable.cpp:18
lldb_private::ExpressionVariable::EVNeedsAllocation
@ EVNeedsAllocation
Space for this variable has yet to be allocated in the target process.
Definition: ExpressionVariable.h:88
lldb_private::PersistentExpressionState::getKind
LLVMCastKind getKind() const
Definition: ExpressionVariable.h:208
lldb_private::ExpressionVariable::m_frozen_sp
lldb::ValueObjectSP m_frozen_sp
Definition: ExpressionVariable.h:110
lldb_private::PersistentExpressionState::GetNextPersistentVariableName
virtual ConstString GetNextPersistentVariableName(bool is_error=false)=0
Return a new persistent variable name with the specified prefix.
lldb_private::ExpressionVariable::GetValueObject
lldb::ValueObjectSP GetValueObject()
Definition: ExpressionVariable.h:39
lldb_private::ExpressionVariableList::m_variables
std::vector< lldb::ExpressionVariableSP > m_variables
Definition: ExpressionVariable.h:200
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ExpressionVariable::m_kind
LLVMCastKind m_kind
Definition: ExpressionVariable.h:112
lldb_private::PersistentExpressionState::GetPersistentVariablePrefix
virtual llvm::StringRef GetPersistentVariablePrefix(bool is_error=false) const =0
lldb_private::PersistentExpressionState::eKindClang
@ eKindClang
Definition: ExpressionVariable.h:206
lldb_private::PersistentExpressionState::LookupSymbol
virtual lldb::addr_t LookupSymbol(ConstString name)
Definition: ExpressionVariable.cpp:33
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ExpressionVariableList::AddVariable
size_t AddVariable(const lldb::ExpressionVariableSP &var_sp)
Definition: ExpressionVariable.h:132
lldb_private::ExpressionVariable::m_flags
FlagType m_flags
Definition: ExpressionVariable.h:107
lldb_private::ExpressionVariable::getKind
LLVMCastKind getKind() const
Definition: ExpressionVariable.h:29
lldb_private::ExpressionVariableList
Definition: ExpressionVariable.h:120
lldb_private::ExpressionVariable::Flags
Flags
Definition: ExpressionVariable.h:80
lldb_private::ExpressionVariable::ExpressionVariable
ExpressionVariable(LLVMCastKind kind)
Definition: ExpressionVariable.h:31
lldb_private::ExpressionVariable::EVNeedsFreezeDry
@ EVNeedsFreezeDry
Copy from m_live_sp to m_frozen_sp during dematerialization.
Definition: ExpressionVariable.h:93
lldb_private::PersistentExpressionState::RegisterExecutionUnit
void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp)
Definition: ExpressionVariable.cpp:42
lldb_private::PersistentExpressionState::m_kind
LLVMCastKind m_kind
Definition: ExpressionVariable.h:241
lldb_private::PersistentExpressionState::eKindGo
@ eKindGo
Definition: ExpressionVariable.h:206
ValueObject.h
lldb_private::ExpressionVariable::~ExpressionVariable
virtual ~ExpressionVariable()
lldb_private::PersistentExpressionState::eKindSwift
@ eKindSwift
Definition: ExpressionVariable.h:206
lldb_private::ExpressionVariableList::GetVariable
lldb::ExpressionVariableSP GetVariable(ConstString name)
Finds a variable by name in the list.
Definition: ExpressionVariable.h:161
lldb_private::ExpressionVariable::GetByteSize
llvm::Optional< uint64_t > GetByteSize()
Definition: ExpressionVariable.h:35
lldb_private::ExpressionVariable
Definition: ExpressionVariable.h:23
lldb_private::ExpressionVariable::FlagType
uint16_t FlagType
Definition: ExpressionVariable.h:105
lldb_private::ExpressionVariable::m_live_sp
lldb::ValueObjectSP m_live_sp
Definition: ExpressionVariable.h:111
lldb_private::ExpressionVariable::eKindGo
@ eKindGo
Definition: ExpressionVariable.h:27
uint32_t
lldb_private::ExpressionVariable::EVKeepInTarget
@ EVKeepInTarget
Keep the allocation after the expression is complete rather than freeze drying its contents and freei...
Definition: ExpressionVariable.h:95
lldb_private::ExpressionVariable::EVIsProgramReference
@ EVIsProgramReference
This variable is a reference to a (possibly invalid) area managed by the target program.
Definition: ExpressionVariable.h:85
lldb_private::PersistentExpressionState::kNumKinds
@ kNumKinds
Definition: ExpressionVariable.h:206
uint16_t
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Value::ContextType::RegisterInfo
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
lldb_private::PersistentExpressionState
Definition: ExpressionVariable.h:203
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ExpressionVariableList::ContainsVariable
bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp)
Definition: ExpressionVariable.h:144
lldb_private::ExpressionVariable::LLVMCastKind
LLVMCastKind
Definition: ExpressionVariable.h:27
lldb_private::ExpressionVariableList::GetVariableAtIndex
lldb::ExpressionVariableSP GetVariableAtIndex(size_t index)
Definition: ExpressionVariable.h:125
lldb_private::PersistentExpressionState::GetCompilerTypeFromPersistentDecl
virtual llvm::Optional< CompilerType > GetCompilerTypeFromPersistentDecl(ConstString type_name)=0
ConstString.h
lldb_private::PersistentExpressionState::CreatePersistentVariable
virtual lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp)=0
lldb_private::PersistentExpressionState::m_execution_units
ExecutionUnitSet m_execution_units
The execution units that contain valuable symbols.
Definition: ExpressionVariable.h:245
lldb_private::ExpressionVariable::GetCompilerType
CompilerType GetCompilerType()
Definition: ExpressionVariable.h:54
lldb_private::ExpressionVariable::SetName
void SetName(ConstString name)
Definition: ExpressionVariable.h:60
lldb_private::PersistentExpressionState::RemovePersistentVariable
virtual void RemovePersistentVariable(lldb::ExpressionVariableSP variable)=0
lldb_private::PersistentExpressionState::m_symbol_map
SymbolMap m_symbol_map
The addresses of the symbols in m_execution_units.
Definition: ExpressionVariable.h:249
lldb_private::ExpressionVariableList::GetVariable
lldb::ExpressionVariableSP GetVariable(llvm::StringRef name)
Definition: ExpressionVariable.h:172
lldb_private::ExpressionVariable::GetName
ConstString GetName()
Definition: ExpressionVariable.h:37
lldb_private::ExpressionVariable::kNumKinds
@ kNumKinds
Definition: ExpressionVariable.h:27
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
lldb_private::ExpressionVariable::SetCompilerType
void SetCompilerType(const CompilerType &compiler_type)
Definition: ExpressionVariable.h:56
lldb_private::PersistentExpressionState::ExecutionUnitSet
std::set< lldb::IRExecutionUnitSP > ExecutionUnitSet
Definition: ExpressionVariable.h:243
lldb_private::ExpressionVariable::EVBareRegister
@ EVBareRegister
This variable is a direct reference to $pc or some other entity.
Definition: ExpressionVariable.h:101