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() {
37 return GetValueObject()->GetByteSize();
38 }
39
40 ConstString GetName() { return m_frozen_sp->GetName(); }
41
43 lldb::ValueObjectSP dyn_sp =
45 if (dyn_sp && dyn_sp->UpdateValueIfNeeded())
46 return dyn_sp;
47 return m_frozen_sp;
48 }
49
50 uint8_t *GetValueBytes();
51
52 void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
53
55 return m_frozen_sp->GetValue().GetRegisterInfo();
56 }
57
58 void SetRegisterInfo(const RegisterInfo *reg_info) {
59 return m_frozen_sp->GetValue().SetContext(
60 Value::ContextType::RegisterInfo, const_cast<RegisterInfo *>(reg_info));
61 }
62
63 CompilerType GetCompilerType() { return GetValueObject()->GetCompilerType(); }
64
65 void SetCompilerType(const CompilerType &compiler_type) {
66 m_frozen_sp->GetValue().SetCompilerType(compiler_type);
67 }
68
69 void SetName(ConstString name) { m_frozen_sp->SetName(name); }
70
71 /// This function is used to copy the address-of m_live_sp into m_frozen_sp.
72 /// It is necessary because the results of certain cast and pointer-
73 /// arithmetic operations (such as those described in bugzilla issues 11588
74 /// and 11618) generate frozen objects that do not have a valid address-of,
75 /// which can be troublesome when using synthetic children providers.
76 /// Transferring the address-of the live object solves these issues and
77 /// provides the expected user-level behavior.
78 /// The other job we do in TransferAddress is adjust the value in the live
79 /// address slot in the target for the "offset to top" in multiply inherited
80 /// class hierarchies.
81 void TransferAddress(bool force = false);
82
83 /// When we build an expression variable we know whether we're going to use
84 /// the static or dynamic result. If we present the dynamic value once, we
85 /// should use the dynamic value in future references to the variable, so we
86 /// record that fact here.
88 m_dyn_option = dyn_type;
89 }
90 /// We don't try to get the dynamic value of the live object when we fetch
91 /// it here. The live object describes the container of the value in the
92 /// target, but it's type is of the object for convenience. So it can't
93 /// produce the dynamic value. Instead, we use TransferAddress to adjust the
94 /// value held by the LiveObject.
96
97 enum Flags {
98 EVNone = 0,
99 EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
100 ///specifically allocated for it by LLDB in the
101 ///target process
102 EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
103 ///(possibly invalid) area managed by the
104 ///target program
105 EVNeedsAllocation = 1 << 2, ///< Space for this variable has yet to be
106 ///allocated in the target process
107 EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
108 ///m_frozen_sp (for example, for
109 ///statically-computed results)
111 1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
112 EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
113 ///complete rather than freeze drying its contents
114 ///and freeing it
115 EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
116 ///reference, so materialize the value rather
117 ///than the location
118 EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
119 ///some other entity.
120 };
121
122 typedef uint16_t FlagType;
123
124 FlagType m_flags; // takes elements of Flags
125
126 /// These members should be private.
127 /// @{
128 /// A value object whose value's data lives in host (lldb's) memory.
129 /// The m_frozen_sp holds the data & type of the expression variable or result
130 /// in the host. The m_frozen_sp also can present a dynamic value if one is
131 /// available.
132 /// The m_frozen_sp manages the copy of this value in m_frozen_sp that we
133 /// insert in the target so that it can be referred to in future expressions.
134 /// We don't actually use the contents of the live_sp to create the value in
135 /// the target, that comes from the frozen sp. The live_sp is mostly to track
136 /// the target-side of the value.
138 /// The ValueObject counterpart to m_frozen_sp that tracks the value in
139 /// inferior memory. This object may not always exist; its presence depends on
140 /// whether it is logical for the value to exist in the inferior memory. For
141 /// example, when evaluating a C++ expression that generates an r-value, such
142 /// as a single function call, there is no memory address in the inferior to
143 /// track.
145 /// @}
146
148};
149
150/// \class ExpressionVariableList ExpressionVariable.h
151/// "lldb/Expression/ExpressionVariable.h"
152/// A list of variable references.
153///
154/// This class stores variables internally, acting as the permanent store.
156public:
157 /// Implementation of methods in ExpressionVariableListBase
158 size_t GetSize() { return m_variables.size(); }
159
162 if (index < m_variables.size())
163 var_sp = m_variables[index];
164 return var_sp;
165 }
166
168 m_variables.push_back(var_sp);
169 return m_variables.size() - 1;
170 }
171
174 lldb::ExpressionVariableSP var_sp(var);
175 m_variables.push_back(var_sp);
176 return m_variables.back();
177 }
178
180 const size_t size = m_variables.size();
181 for (size_t index = 0; index < size; ++index) {
182 if (m_variables[index].get() == var_sp.get())
183 return true;
184 }
185 return false;
186 }
187
188 /// Finds a variable by name in the list.
189 ///
190 /// \param[in] name
191 /// The name of the requested variable.
192 ///
193 /// \return
194 /// The variable requested, or nullptr if that variable is not in the
195 /// list.
198 for (size_t index = 0, size = GetSize(); index < size; ++index) {
199 var_sp = GetVariableAtIndex(index);
200 if (var_sp->GetName() == name)
201 return var_sp;
202 }
203 var_sp.reset();
204 return var_sp;
205 }
206
208 if (name.empty())
209 return nullptr;
210
211 for (size_t index = 0, size = GetSize(); index < size; ++index) {
212 auto var_sp = GetVariableAtIndex(index);
213 llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
214 if (var_name_str == name)
215 return var_sp;
216 }
217 return nullptr;
218 }
219
221 for (std::vector<lldb::ExpressionVariableSP>::iterator
222 vi = m_variables.begin(),
223 ve = m_variables.end();
224 vi != ve; ++vi) {
225 if (vi->get() == var_sp.get()) {
226 m_variables.erase(vi);
227 return;
228 }
229 }
230 }
231
232 void Clear() { m_variables.clear(); }
233
234private:
235 std::vector<lldb::ExpressionVariableSP> m_variables;
236};
237
239 : public ExpressionVariableList,
240 public llvm::RTTIExtends<PersistentExpressionState, llvm::RTTIRoot> {
241public:
242 /// LLVM RTTI support
243 static char ID;
244
246
248
251
254 ConstString name, const CompilerType &type,
255 lldb::ByteOrder byte_order,
256 uint32_t addr_byte_size) = 0;
257
258 /// Return a new persistent variable name with the specified prefix.
259 virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
260
261 virtual void
263
264 virtual std::optional<CompilerType>
266
268
269 void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
270
271protected:
272 virtual llvm::StringRef
273 GetPersistentVariablePrefix(bool is_error = false) const = 0;
274
275private:
276 typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
278 m_execution_units; ///< The execution units that contain valuable symbols.
279
280 typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
282 m_symbol_map; ///< The addresses of the symbols in m_execution_units.
283};
284
285} // namespace lldb_private
286
287#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.
void TransferAddress(bool force=false)
This function is used to copy the address-of m_live_sp into m_frozen_sp.
lldb::ValueObjectSP m_frozen_sp
These members should be private.
void SetRegisterInfo(const RegisterInfo *reg_info)
static char ID
LLVM RTTI support.
llvm::Expected< uint64_t > GetByteSize()
lldb::ValueObjectSP GetLiveObject()
We don't try to get the dynamic value of the live object when we fetch it here.
void PreserveDynamicOption(lldb::DynamicValueType dyn_type)
When we build an expression variable we know whether we're going to use the static or dynamic result.
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
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
@ eDynamicDontRunTarget
Every register is described in detail including its name, alternate name (optional),...