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 <optional>
14#include <vector>
15
16#include "llvm/ADT/DenseMap.h"
17
20#include "lldb/lldb-public.h"
21#include "llvm/Support/ExtensibleRTTI.h"
22
23namespace lldb_private {
24
26 : public std::enable_shared_from_this<ExpressionVariable>,
27 public llvm::RTTIExtends<ExpressionVariable, llvm::RTTIRoot> {
28public:
29 /// LLVM RTTI support
30 static char ID;
31
33
34 virtual ~ExpressionVariable() = default;
35
36 llvm::Expected<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
37
38 ConstString GetName() { return m_frozen_sp->GetName(); }
39
41
42 uint8_t *GetValueBytes();
43
44 void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
45
47 return m_frozen_sp->GetValue().GetRegisterInfo();
48 }
49
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
55 CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
56
57 void SetCompilerType(const CompilerType &compiler_type) {
58 m_frozen_sp->GetValue().SetCompilerType(compiler_type);
59 }
60
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)
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 members should be private.
111 /// @{
112 /// A value object whose value's data lives in host (lldb's) memory.
114 /// The ValueObject counterpart to m_frozen_sp that tracks the value in
115 /// inferior memory. This object may not always exist; its presence depends on
116 /// whether it is logical for the value to exist in the inferior memory. For
117 /// example, when evaluating a C++ expression that generates an r-value, such
118 /// as a single function call, there is no memory address in the inferior to
119 /// track.
121 /// @}
122};
123
124/// \class ExpressionVariableList ExpressionVariable.h
125/// "lldb/Expression/ExpressionVariable.h"
126/// A list of variable references.
127///
128/// This class stores variables internally, acting as the permanent store.
130public:
131 /// Implementation of methods in ExpressionVariableListBase
132 size_t GetSize() { return m_variables.size(); }
133
136 if (index < m_variables.size())
137 var_sp = m_variables[index];
138 return var_sp;
139 }
140
142 m_variables.push_back(var_sp);
143 return m_variables.size() - 1;
144 }
145
148 lldb::ExpressionVariableSP var_sp(var);
149 m_variables.push_back(var_sp);
150 return m_variables.back();
151 }
152
154 const size_t size = m_variables.size();
155 for (size_t index = 0; index < size; ++index) {
156 if (m_variables[index].get() == var_sp.get())
157 return true;
158 }
159 return false;
160 }
161
162 /// Finds a variable by name in the list.
163 ///
164 /// \param[in] name
165 /// The name of the requested variable.
166 ///
167 /// \return
168 /// The variable requested, or nullptr if that variable is not in the
169 /// list.
172 for (size_t index = 0, size = GetSize(); index < size; ++index) {
173 var_sp = GetVariableAtIndex(index);
174 if (var_sp->GetName() == name)
175 return var_sp;
176 }
177 var_sp.reset();
178 return var_sp;
179 }
180
182 if (name.empty())
183 return nullptr;
184
185 for (size_t index = 0, size = GetSize(); index < size; ++index) {
186 auto var_sp = GetVariableAtIndex(index);
187 llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
188 if (var_name_str == name)
189 return var_sp;
190 }
191 return nullptr;
192 }
193
195 for (std::vector<lldb::ExpressionVariableSP>::iterator
196 vi = m_variables.begin(),
197 ve = m_variables.end();
198 vi != ve; ++vi) {
199 if (vi->get() == var_sp.get()) {
200 m_variables.erase(vi);
201 return;
202 }
203 }
204 }
205
206 void Clear() { m_variables.clear(); }
207
208private:
209 std::vector<lldb::ExpressionVariableSP> m_variables;
210};
211
213 : public ExpressionVariableList,
214 public llvm::RTTIExtends<PersistentExpressionState, llvm::RTTIRoot> {
215public:
216 /// LLVM RTTI support
217 static char ID;
218
220
222
225
228 ConstString name, const CompilerType &type,
229 lldb::ByteOrder byte_order,
230 uint32_t addr_byte_size) = 0;
231
232 /// Return a new persistent variable name with the specified prefix.
233 virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
234
235 virtual void
237
238 virtual std::optional<CompilerType>
240
242
243 void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
244
245protected:
246 virtual llvm::StringRef
247 GetPersistentVariablePrefix(bool is_error = false) const = 0;
248
249private:
250 typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
252 m_execution_units; ///< The execution units that contain valuable symbols.
253
254 typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
256 m_symbol_map; ///< The addresses of the symbols in m_execution_units.
257};
258
259} // namespace lldb_private
260
261#endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Expression/ExpressionVariable.h" A list of variable references.
lldb::ExpressionVariableSP GetVariable(llvm::StringRef name)
lldb::ExpressionVariableSP AddNewlyConstructedVariable(ExpressionVariable *var)
size_t GetSize()
Implementation of methods in ExpressionVariableListBase.
std::vector< lldb::ExpressionVariableSP > m_variables
lldb::ExpressionVariableSP GetVariableAtIndex(size_t index)
lldb::ExpressionVariableSP GetVariable(ConstString name)
Finds a variable by name in the list.
size_t AddVariable(const lldb::ExpressionVariableSP &var_sp)
void RemoveVariable(lldb::ExpressionVariableSP var_sp)
bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp)
lldb::ValueObjectSP m_live_sp
The ValueObject counterpart to m_frozen_sp that tracks the value in inferior memory.
lldb::ValueObjectSP m_frozen_sp
These members should be private.
void TransferAddress(bool force=false)
void SetRegisterInfo(const RegisterInfo *reg_info)
static char ID
LLVM RTTI support.
llvm::Expected< uint64_t > GetByteSize()
virtual ~ExpressionVariable()=default
void SetCompilerType(const CompilerType &compiler_type)
@ EVTypeIsReference
The original type of this variable is a reference, so materialize the value rather than the location.
@ EVIsLLDBAllocated
This variable is resident in a location specifically allocated for it by LLDB in the target process.
@ EVNeedsFreezeDry
Copy from m_live_sp to m_frozen_sp during dematerialization.
@ EVNeedsAllocation
Space for this variable has yet to be allocated in the target process.
@ EVIsProgramReference
This variable is a reference to a (possibly invalid) area managed by the target program.
@ EVIsFreezeDried
This variable's authoritative version is in m_frozen_sp (for example, for statically-computed results...
@ EVBareRegister
This variable is a direct reference to $pc or some other entity.
@ EVKeepInTarget
Keep the allocation after the expression is complete rather than freeze drying its contents and freei...
virtual void RemovePersistentVariable(lldb::ExpressionVariableSP variable)=0
virtual lldb::ExpressionVariableSP CreatePersistentVariable(ExecutionContextScope *exe_scope, ConstString name, const CompilerType &type, lldb::ByteOrder byte_order, uint32_t addr_byte_size)=0
virtual llvm::StringRef GetPersistentVariablePrefix(bool is_error=false) const =0
llvm::DenseMap< const char *, lldb::addr_t > SymbolMap
virtual std::optional< CompilerType > GetCompilerTypeFromPersistentDecl(ConstString type_name)=0
void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp)
virtual lldb::ExpressionVariableSP CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp)=0
std::set< lldb::IRExecutionUnitSP > ExecutionUnitSet
ExecutionUnitSet m_execution_units
The execution units that contain valuable symbols.
SymbolMap m_symbol_map
The addresses of the symbols in m_execution_units.
virtual ConstString GetNextPersistentVariableName(bool is_error=false)=0
Return a new persistent variable name with the specified prefix.
virtual lldb::addr_t LookupSymbol(ConstString name)
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
Definition Value.h:61
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::IRExecutionUnit > IRExecutionUnitSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
Every register is described in detail including its name, alternate name (optional),...