1 //===-- ValueObjectVariable.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/ValueObject/ValueObjectVariable.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Core/Declaration.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Value.h"
16 #include "lldb/Expression/DWARFExpressionList.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/SymbolContextScope.h"
21 #include "lldb/Symbol/Type.h"
22 #include "lldb/Symbol/Variable.h"
23 #include "lldb/Target/ExecutionContext.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/DataExtractor.h"
28 #include "lldb/Utility/RegisterValue.h"
29 #include "lldb/Utility/Scalar.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/lldb-private-enumerations.h"
32 #include "lldb/lldb-types.h"
34 #include "llvm/ADT/StringRef.h"
40 namespace lldb_private
{
41 class ExecutionContextScope
;
43 namespace lldb_private
{
46 namespace lldb_private
{
49 using namespace lldb_private
;
52 ValueObjectVariable::Create(ExecutionContextScope
*exe_scope
,
53 const lldb::VariableSP
&var_sp
) {
54 auto manager_sp
= ValueObjectManager::Create();
55 return (new ValueObjectVariable(exe_scope
, *manager_sp
, var_sp
))->GetSP();
58 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope
*exe_scope
,
59 ValueObjectManager
&manager
,
60 const lldb::VariableSP
&var_sp
)
61 : ValueObject(exe_scope
, manager
), m_variable_sp(var_sp
) {
62 // Do not attempt to construct one of these objects with no variable!
63 assert(m_variable_sp
.get() != nullptr);
64 m_name
= var_sp
->GetName();
67 ValueObjectVariable::~ValueObjectVariable() = default;
69 CompilerType
ValueObjectVariable::GetCompilerTypeImpl() {
70 Type
*var_type
= m_variable_sp
->GetType();
72 return var_type
->GetForwardCompilerType();
73 return CompilerType();
76 ConstString
ValueObjectVariable::GetTypeName() {
77 Type
*var_type
= m_variable_sp
->GetType();
79 return var_type
->GetName();
83 ConstString
ValueObjectVariable::GetDisplayTypeName() {
84 Type
*var_type
= m_variable_sp
->GetType();
86 return var_type
->GetForwardCompilerType().GetDisplayTypeName();
90 ConstString
ValueObjectVariable::GetQualifiedTypeName() {
91 Type
*var_type
= m_variable_sp
->GetType();
93 return var_type
->GetQualifiedName();
97 llvm::Expected
<uint32_t>
98 ValueObjectVariable::CalculateNumChildren(uint32_t max
) {
99 CompilerType
type(GetCompilerType());
102 return llvm::make_error
<llvm::StringError
>("invalid type",
103 llvm::inconvertibleErrorCode());
105 ExecutionContext
exe_ctx(GetExecutionContextRef());
106 const bool omit_empty_base_classes
= true;
107 auto child_count
= type
.GetNumChildren(omit_empty_base_classes
, &exe_ctx
);
110 return *child_count
<= max
? *child_count
: max
;
113 std::optional
<uint64_t> ValueObjectVariable::GetByteSize() {
114 ExecutionContext
exe_ctx(GetExecutionContextRef());
116 CompilerType
type(GetCompilerType());
121 return type
.GetByteSize(exe_ctx
.GetBestExecutionContextScope());
124 lldb::ValueType
ValueObjectVariable::GetValueType() const {
126 return m_variable_sp
->GetScope();
127 return lldb::eValueTypeInvalid
;
130 bool ValueObjectVariable::UpdateValue() {
131 SetValueIsValid(false);
134 Variable
*variable
= m_variable_sp
.get();
135 DWARFExpressionList
&expr_list
= variable
->LocationExpressionList();
137 if (variable
->GetLocationIsConstantValueData()) {
138 // expr doesn't contain DWARF bytes, it contains the constant variable
139 // value bytes themselves...
140 if (expr_list
.GetExpressionData(m_data
)) {
141 if (m_data
.GetDataStart() && m_data
.GetByteSize())
142 m_value
.SetBytes(m_data
.GetDataStart(), m_data
.GetByteSize());
143 m_value
.SetContext(Value::ContextType::Variable
, variable
);
145 m_error
= Status::FromErrorString("empty constant data");
146 // constant bytes can't be edited - sorry
147 m_resolved_value
.SetContext(Value::ContextType::Invalid
, nullptr);
149 lldb::addr_t loclist_base_load_addr
= LLDB_INVALID_ADDRESS
;
150 ExecutionContext
exe_ctx(GetExecutionContextRef());
152 Target
*target
= exe_ctx
.GetTargetPtr();
154 m_data
.SetByteOrder(target
->GetArchitecture().GetByteOrder());
155 m_data
.SetAddressByteSize(target
->GetArchitecture().GetAddressByteSize());
158 if (!expr_list
.IsAlwaysValidSingleExpr()) {
160 variable
->CalculateSymbolContext(&sc
);
162 loclist_base_load_addr
=
163 sc
.function
->GetAddressRange().GetBaseAddress().GetLoadAddress(
166 Value
old_value(m_value
);
167 llvm::Expected
<Value
> maybe_value
= expr_list
.Evaluate(
168 &exe_ctx
, nullptr, loclist_base_load_addr
, nullptr, nullptr);
171 m_value
= *maybe_value
;
172 m_resolved_value
= m_value
;
173 m_value
.SetContext(Value::ContextType::Variable
, variable
);
175 CompilerType compiler_type
= GetCompilerType();
176 if (compiler_type
.IsValid())
177 m_value
.SetCompilerType(compiler_type
);
179 Value::ValueType value_type
= m_value
.GetValueType();
181 // The size of the buffer within m_value can be less than the size
182 // prescribed by its type. E.g. this can happen when an expression only
183 // partially describes an object (say, because it contains DW_OP_piece).
185 // In this case, grow m_value to the expected size. An alternative way to
186 // handle this is to teach Value::GetValueAsData() and ValueObjectChild
187 // not to read past the end of a host buffer, but this gets impractically
188 // complicated as a Value's host buffer may be shared with a distant
189 // ancestor or sibling in the ValueObject hierarchy.
191 // FIXME: When we grow m_value, we should represent the added bits as
192 // undefined somehow instead of as 0's.
193 if (value_type
== Value::ValueType::HostAddress
&&
194 compiler_type
.IsValid()) {
195 if (size_t value_buf_size
= m_value
.GetBuffer().GetByteSize()) {
196 size_t value_size
= m_value
.GetValueByteSize(&m_error
, &exe_ctx
);
197 if (m_error
.Success() && value_buf_size
< value_size
)
198 m_value
.ResizeData(value_size
);
202 Process
*process
= exe_ctx
.GetProcessPtr();
203 const bool process_is_alive
= process
&& process
->IsAlive();
205 switch (value_type
) {
206 case Value::ValueType::Invalid
:
207 m_error
= Status::FromErrorString("invalid value");
209 case Value::ValueType::Scalar
:
210 // The variable value is in the Scalar value inside the m_value. We can
211 // point our m_data right to it.
212 m_error
= m_value
.GetValueAsData(&exe_ctx
, m_data
, GetModule().get());
215 case Value::ValueType::FileAddress
:
216 case Value::ValueType::LoadAddress
:
217 case Value::ValueType::HostAddress
:
218 // The DWARF expression result was an address in the inferior process.
219 // If this variable is an aggregate type, we just need the address as
220 // the main value as all child variable objects will rely upon this
221 // location and add an offset and then read their own values as needed.
222 // If this variable is a simple type, we read all data for it into
223 // m_data. Make sure this type has a value before we try and read it
225 // If we have a file address, convert it to a load address if we can.
226 if (value_type
== Value::ValueType::FileAddress
&& process_is_alive
)
227 m_value
.ConvertToLoadAddress(GetModule().get(), target
);
229 if (!CanProvideValue()) {
230 // this value object represents an aggregate type whose children have
231 // values, but this object does not. So we say we are changed if our
232 // location has changed.
233 SetValueDidChange(value_type
!= old_value
.GetValueType() ||
234 m_value
.GetScalar() != old_value
.GetScalar());
236 // Copy the Value and set the context to use our Variable so it can
237 // extract read its value into m_data appropriately
238 Value
value(m_value
);
239 value
.SetContext(Value::ContextType::Variable
, variable
);
240 m_error
= value
.GetValueAsData(&exe_ctx
, m_data
, GetModule().get());
242 SetValueDidChange(value_type
!= old_value
.GetValueType() ||
243 m_value
.GetScalar() != old_value
.GetScalar());
248 SetValueIsValid(m_error
.Success());
250 m_error
= Status::FromError(maybe_value
.takeError());
251 // could not find location, won't allow editing
252 m_resolved_value
.SetContext(Value::ContextType::Invalid
, nullptr);
256 return m_error
.Success();
259 void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject
&valobj
) {
260 Value::ValueType value_type
= valobj
.GetValue().GetValueType();
261 ExecutionContext
exe_ctx(GetExecutionContextRef());
262 Process
*process
= exe_ctx
.GetProcessPtr();
263 const bool process_is_alive
= process
&& process
->IsAlive();
264 const uint32_t type_info
= valobj
.GetCompilerType().GetTypeInfo();
265 const bool is_pointer_or_ref
=
266 (type_info
& (lldb::eTypeIsPointer
| lldb::eTypeIsReference
)) != 0;
268 switch (value_type
) {
269 case Value::ValueType::Invalid
:
271 case Value::ValueType::FileAddress
:
272 // If this type is a pointer, then its children will be considered load
273 // addresses if the pointer or reference is dereferenced, but only if
274 // the process is alive.
276 // There could be global variables like in the following code:
277 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
280 // LinkedListNode g_second_node = { &g_foo2, NULL };
281 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
283 // When we aren't running, we should be able to look at these variables
284 // using the "target variable" command. Children of the "g_first_node"
285 // always will be of the same address type as the parent. But children
286 // of the "next" member of LinkedListNode will become load addresses if
287 // we have a live process, or remain a file address if it was a file
289 if (process_is_alive
&& is_pointer_or_ref
)
290 valobj
.SetAddressTypeOfChildren(eAddressTypeLoad
);
292 valobj
.SetAddressTypeOfChildren(eAddressTypeFile
);
294 case Value::ValueType::HostAddress
:
295 // Same as above for load addresses, except children of pointer or refs
296 // are always load addresses. Host addresses are used to store freeze
297 // dried variables. If this type is a struct, the entire struct
298 // contents will be copied into the heap of the
299 // LLDB process, but we do not currently follow any pointers.
300 if (is_pointer_or_ref
)
301 valobj
.SetAddressTypeOfChildren(eAddressTypeLoad
);
303 valobj
.SetAddressTypeOfChildren(eAddressTypeHost
);
305 case Value::ValueType::LoadAddress
:
306 case Value::ValueType::Scalar
:
307 valobj
.SetAddressTypeOfChildren(eAddressTypeLoad
);
312 bool ValueObjectVariable::IsInScope() {
313 const ExecutionContextRef
&exe_ctx_ref
= GetExecutionContextRef();
314 if (exe_ctx_ref
.HasFrameRef()) {
315 ExecutionContext
exe_ctx(exe_ctx_ref
);
316 StackFrame
*frame
= exe_ctx
.GetFramePtr();
318 return m_variable_sp
->IsInScope(frame
);
320 // This ValueObject had a frame at one time, but now we can't locate it,
321 // so return false since we probably aren't in scope.
325 // We have a variable that wasn't tied to a frame, which means it is a global
326 // and is always in scope.
330 lldb::ModuleSP
ValueObjectVariable::GetModule() {
332 SymbolContextScope
*sc_scope
= m_variable_sp
->GetSymbolContextScope();
334 return sc_scope
->CalculateSymbolContextModule();
337 return lldb::ModuleSP();
340 SymbolContextScope
*ValueObjectVariable::GetSymbolContextScope() {
342 return m_variable_sp
->GetSymbolContextScope();
346 bool ValueObjectVariable::GetDeclaration(Declaration
&decl
) {
348 decl
= m_variable_sp
->GetDeclaration();
354 const char *ValueObjectVariable::GetLocationAsCString() {
355 if (m_resolved_value
.GetContextType() == Value::ContextType::RegisterInfo
)
356 return GetLocationAsCStringImpl(m_resolved_value
, m_data
);
358 return ValueObject::GetLocationAsCString();
361 bool ValueObjectVariable::SetValueFromCString(const char *value_str
,
363 if (!UpdateValueIfNeeded()) {
364 error
= Status::FromErrorString("unable to update value before writing");
368 if (m_resolved_value
.GetContextType() == Value::ContextType::RegisterInfo
) {
369 RegisterInfo
*reg_info
= m_resolved_value
.GetRegisterInfo();
370 ExecutionContext
exe_ctx(GetExecutionContextRef());
371 RegisterContext
*reg_ctx
= exe_ctx
.GetRegisterContext();
372 RegisterValue reg_value
;
373 if (!reg_info
|| !reg_ctx
) {
374 error
= Status::FromErrorString("unable to retrieve register info");
377 error
= reg_value
.SetValueFromString(reg_info
, llvm::StringRef(value_str
));
380 if (reg_ctx
->WriteRegister(reg_info
, reg_value
)) {
384 error
= Status::FromErrorString("unable to write back to register");
388 return ValueObject::SetValueFromCString(value_str
, error
);
391 bool ValueObjectVariable::SetData(DataExtractor
&data
, Status
&error
) {
392 if (!UpdateValueIfNeeded()) {
393 error
= Status::FromErrorString("unable to update value before writing");
397 if (m_resolved_value
.GetContextType() == Value::ContextType::RegisterInfo
) {
398 RegisterInfo
*reg_info
= m_resolved_value
.GetRegisterInfo();
399 ExecutionContext
exe_ctx(GetExecutionContextRef());
400 RegisterContext
*reg_ctx
= exe_ctx
.GetRegisterContext();
401 RegisterValue reg_value
;
402 if (!reg_info
|| !reg_ctx
) {
403 error
= Status::FromErrorString("unable to retrieve register info");
406 error
= reg_value
.SetValueFromData(*reg_info
, data
, 0, true);
409 if (reg_ctx
->WriteRegister(reg_info
, reg_value
)) {
413 error
= Status::FromErrorString("unable to write back to register");
417 return ValueObject::SetData(data
, error
);