1 //===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
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/Core/ValueObjectVariable.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Value.h"
15 #include "lldb/Expression/DWARFExpression.h"
16 #include "lldb/Symbol/Declaration.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"
39 namespace lldb_private {
40 class ExecutionContextScope;
42 namespace lldb_private {
45 namespace lldb_private {
48 using namespace lldb_private;
51 ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
52 const lldb::VariableSP &var_sp) {
53 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
56 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
57 const lldb::VariableSP &var_sp)
58 : ValueObject(exe_scope), m_variable_sp(var_sp) {
59 // Do not attempt to construct one of these objects with no variable!
60 assert(m_variable_sp.get() != nullptr);
61 m_name = var_sp->GetName();
64 ValueObjectVariable::~ValueObjectVariable() {}
66 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
67 Type *var_type = m_variable_sp->GetType();
69 return var_type->GetForwardCompilerType();
70 return CompilerType();
73 ConstString ValueObjectVariable::GetTypeName() {
74 Type *var_type = m_variable_sp->GetType();
76 return var_type->GetName();
80 ConstString ValueObjectVariable::GetDisplayTypeName() {
81 Type *var_type = m_variable_sp->GetType();
83 return var_type->GetForwardCompilerType().GetDisplayTypeName();
87 ConstString ValueObjectVariable::GetQualifiedTypeName() {
88 Type *var_type = m_variable_sp->GetType();
90 return var_type->GetQualifiedName();
94 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
95 CompilerType type(GetCompilerType());
100 ExecutionContext exe_ctx(GetExecutionContextRef());
101 const bool omit_empty_base_classes = true;
102 auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
103 return child_count <= max ? child_count : max;
106 uint64_t ValueObjectVariable::GetByteSize() {
107 ExecutionContext exe_ctx(GetExecutionContextRef());
109 CompilerType type(GetCompilerType());
114 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()).getValueOr(0);
117 lldb::ValueType ValueObjectVariable::GetValueType() const {
119 return m_variable_sp->GetScope();
120 return lldb::eValueTypeInvalid;
123 bool ValueObjectVariable::UpdateValue() {
124 SetValueIsValid(false);
127 Variable *variable = m_variable_sp.get();
128 DWARFExpression &expr = variable->LocationExpression();
130 if (variable->GetLocationIsConstantValueData()) {
131 // expr doesn't contain DWARF bytes, it contains the constant variable
132 // value bytes themselves...
133 if (expr.GetExpressionData(m_data))
134 m_value.SetContext(Value::eContextTypeVariable, variable);
136 m_error.SetErrorString("empty constant data");
137 // constant bytes can't be edited - sorry
138 m_resolved_value.SetContext(Value::eContextTypeInvalid, nullptr);
140 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
141 ExecutionContext exe_ctx(GetExecutionContextRef());
143 Target *target = exe_ctx.GetTargetPtr();
145 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
146 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
149 if (expr.IsLocationList()) {
151 variable->CalculateSymbolContext(&sc);
153 loclist_base_load_addr =
154 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
157 Value old_value(m_value);
158 if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
159 nullptr, m_value, &m_error)) {
160 m_resolved_value = m_value;
161 m_value.SetContext(Value::eContextTypeVariable, variable);
163 CompilerType compiler_type = GetCompilerType();
164 if (compiler_type.IsValid())
165 m_value.SetCompilerType(compiler_type);
167 Value::ValueType value_type = m_value.GetValueType();
169 Process *process = exe_ctx.GetProcessPtr();
170 const bool process_is_alive = process && process->IsAlive();
171 const uint32_t type_info = compiler_type.GetTypeInfo();
172 const bool is_pointer_or_ref =
173 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
175 switch (value_type) {
176 case Value::eValueTypeFileAddress:
177 // If this type is a pointer, then its children will be considered load
178 // addresses if the pointer or reference is dereferenced, but only if
179 // the process is alive.
181 // There could be global variables like in the following code:
182 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
185 // LinkedListNode g_second_node = { &g_foo2, NULL };
186 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
188 // When we aren't running, we should be able to look at these variables
189 // using the "target variable" command. Children of the "g_first_node"
190 // always will be of the same address type as the parent. But children
191 // of the "next" member of LinkedListNode will become load addresses if
192 // we have a live process, or remain what a file address if it what a
194 if (process_is_alive && is_pointer_or_ref)
195 SetAddressTypeOfChildren(eAddressTypeLoad);
197 SetAddressTypeOfChildren(eAddressTypeFile);
199 case Value::eValueTypeHostAddress:
200 // Same as above for load addresses, except children of pointer or refs
201 // are always load addresses. Host addresses are used to store freeze
202 // dried variables. If this type is a struct, the entire struct
203 // contents will be copied into the heap of the
204 // LLDB process, but we do not currently follow any pointers.
205 if (is_pointer_or_ref)
206 SetAddressTypeOfChildren(eAddressTypeLoad);
208 SetAddressTypeOfChildren(eAddressTypeHost);
210 case Value::eValueTypeLoadAddress:
211 case Value::eValueTypeScalar:
212 case Value::eValueTypeVector:
213 SetAddressTypeOfChildren(eAddressTypeLoad);
217 switch (value_type) {
218 case Value::eValueTypeVector:
220 case Value::eValueTypeScalar:
221 // The variable value is in the Scalar value inside the m_value. We can
222 // point our m_data right to it.
224 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
227 case Value::eValueTypeFileAddress:
228 case Value::eValueTypeLoadAddress:
229 case Value::eValueTypeHostAddress:
230 // The DWARF expression result was an address in the inferior process.
231 // If this variable is an aggregate type, we just need the address as
232 // the main value as all child variable objects will rely upon this
233 // location and add an offset and then read their own values as needed.
234 // If this variable is a simple type, we read all data for it into
235 // m_data. Make sure this type has a value before we try and read it
237 // If we have a file address, convert it to a load address if we can.
238 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
239 m_value.ConvertToLoadAddress(GetModule().get(), target);
241 if (!CanProvideValue()) {
242 // this value object represents an aggregate type whose children have
243 // values, but this object does not. So we say we are changed if our
244 // location has changed.
245 SetValueDidChange(value_type != old_value.GetValueType() ||
246 m_value.GetScalar() != old_value.GetScalar());
248 // Copy the Value and set the context to use our Variable so it can
249 // extract read its value into m_data appropriately
250 Value value(m_value);
251 value.SetContext(Value::eContextTypeVariable, variable);
253 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
255 SetValueDidChange(value_type != old_value.GetValueType() ||
256 m_value.GetScalar() != old_value.GetScalar());
261 SetValueIsValid(m_error.Success());
263 // could not find location, won't allow editing
264 m_resolved_value.SetContext(Value::eContextTypeInvalid, nullptr);
267 return m_error.Success();
270 bool ValueObjectVariable::IsInScope() {
271 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
272 if (exe_ctx_ref.HasFrameRef()) {
273 ExecutionContext exe_ctx(exe_ctx_ref);
274 StackFrame *frame = exe_ctx.GetFramePtr();
276 return m_variable_sp->IsInScope(frame);
278 // This ValueObject had a frame at one time, but now we can't locate it,
279 // so return false since we probably aren't in scope.
283 // We have a variable that wasn't tied to a frame, which means it is a global
284 // and is always in scope.
288 lldb::ModuleSP ValueObjectVariable::GetModule() {
290 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
292 return sc_scope->CalculateSymbolContextModule();
295 return lldb::ModuleSP();
298 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
300 return m_variable_sp->GetSymbolContextScope();
304 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
306 decl = m_variable_sp->GetDeclaration();
312 const char *ValueObjectVariable::GetLocationAsCString() {
313 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
314 return GetLocationAsCStringImpl(m_resolved_value, m_data);
316 return ValueObject::GetLocationAsCString();
319 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
321 if (!UpdateValueIfNeeded()) {
322 error.SetErrorString("unable to update value before writing");
326 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
327 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
328 ExecutionContext exe_ctx(GetExecutionContextRef());
329 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
330 RegisterValue reg_value;
331 if (!reg_info || !reg_ctx) {
332 error.SetErrorString("unable to retrieve register info");
335 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
338 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
342 error.SetErrorString("unable to write back to register");
346 return ValueObject::SetValueFromCString(value_str, error);
349 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
350 if (!UpdateValueIfNeeded()) {
351 error.SetErrorString("unable to update value before writing");
355 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
356 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
357 ExecutionContext exe_ctx(GetExecutionContextRef());
358 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
359 RegisterValue reg_value;
360 if (!reg_info || !reg_ctx) {
361 error.SetErrorString("unable to retrieve register info");
364 error = reg_value.SetValueFromData(reg_info, data, 0, true);
367 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
371 error.SetErrorString("unable to write back to register");
375 return ValueObject::SetData(data, error);