1 //===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ValueObjectVariable.h"
12 #include "lldb/Core/Address.h" // for Address
13 #include "lldb/Core/AddressRange.h" // for AddressRange
14 #include "lldb/Core/ArchSpec.h" // for ArchSpec
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/RegisterValue.h"
17 #include "lldb/Core/Scalar.h" // for Scalar, operator!=
18 #include "lldb/Core/Value.h"
19 #include "lldb/Expression/DWARFExpression.h" // for DWARFExpression
20 #include "lldb/Symbol/Declaration.h" // for Declaration
21 #include "lldb/Symbol/Function.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/SymbolContextScope.h"
25 #include "lldb/Symbol/Type.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
32 #include "lldb/Utility/Status.h" // for Status
33 #include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy...
34 #include "lldb/lldb-types.h" // for addr_t
36 #include "llvm/ADT/StringRef.h" // for StringRef
38 #include <assert.h> // for assert
39 #include <memory> // for shared_ptr
41 namespace lldb_private {
42 class ExecutionContextScope;
44 namespace lldb_private {
47 namespace lldb_private {
50 using namespace lldb_private;
53 ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
54 const lldb::VariableSP &var_sp) {
55 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
58 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
59 const lldb::VariableSP &var_sp)
60 : ValueObject(exe_scope), m_variable_sp(var_sp) {
61 // Do not attempt to construct one of these objects with no variable!
62 assert(m_variable_sp.get() != NULL);
63 m_name = var_sp->GetName();
66 ValueObjectVariable::~ValueObjectVariable() {}
68 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
69 Type *var_type = m_variable_sp->GetType();
71 return var_type->GetForwardCompilerType();
72 return CompilerType();
75 ConstString ValueObjectVariable::GetTypeName() {
76 Type *var_type = m_variable_sp->GetType();
78 return var_type->GetName();
82 ConstString ValueObjectVariable::GetDisplayTypeName() {
83 Type *var_type = m_variable_sp->GetType();
85 return var_type->GetForwardCompilerType().GetDisplayTypeName();
89 ConstString ValueObjectVariable::GetQualifiedTypeName() {
90 Type *var_type = m_variable_sp->GetType();
92 return var_type->GetQualifiedName();
96 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
97 CompilerType type(GetCompilerType());
102 const bool omit_empty_base_classes = true;
103 auto child_count = type.GetNumChildren(omit_empty_base_classes);
104 return child_count <= max ? child_count : max;
107 uint64_t ValueObjectVariable::GetByteSize() {
108 ExecutionContext exe_ctx(GetExecutionContextRef());
110 CompilerType type(GetCompilerType());
115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
118 lldb::ValueType ValueObjectVariable::GetValueType() const {
120 return m_variable_sp->GetScope();
121 return lldb::eValueTypeInvalid;
124 bool ValueObjectVariable::UpdateValue() {
125 SetValueIsValid(false);
128 Variable *variable = m_variable_sp.get();
129 DWARFExpression &expr = variable->LocationExpression();
131 if (variable->GetLocationIsConstantValueData()) {
132 // expr doesn't contain DWARF bytes, it contains the constant variable
133 // value bytes themselves...
134 if (expr.GetExpressionData(m_data))
135 m_value.SetContext(Value::eContextTypeVariable, variable);
137 m_error.SetErrorString("empty constant data");
138 // constant bytes can't be edited - sorry
139 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
141 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
142 ExecutionContext exe_ctx(GetExecutionContextRef());
144 Target *target = exe_ctx.GetTargetPtr();
146 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
147 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
150 if (expr.IsLocationList()) {
152 variable->CalculateSymbolContext(&sc);
154 loclist_base_load_addr =
155 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
158 Value old_value(m_value);
159 if (expr.Evaluate(&exe_ctx, nullptr, nullptr, nullptr,
160 loclist_base_load_addr, nullptr, nullptr, m_value,
162 m_resolved_value = m_value;
163 m_value.SetContext(Value::eContextTypeVariable, variable);
165 CompilerType compiler_type = GetCompilerType();
166 if (compiler_type.IsValid())
167 m_value.SetCompilerType(compiler_type);
169 Value::ValueType value_type = m_value.GetValueType();
171 Process *process = exe_ctx.GetProcessPtr();
172 const bool process_is_alive = process && process->IsAlive();
173 const uint32_t type_info = compiler_type.GetTypeInfo();
174 const bool is_pointer_or_ref =
175 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
177 switch (value_type) {
178 case Value::eValueTypeFileAddress:
179 // If this type is a pointer, then its children will be considered load
181 // if the pointer or reference is dereferenced, but only if the process
184 // There could be global variables like in the following code:
185 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
188 // LinkedListNode g_second_node = { &g_foo2, NULL };
189 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
191 // When we aren't running, we should be able to look at these variables
193 // the "target variable" command. Children of the "g_first_node" always
195 // be of the same address type as the parent. But children of the "next"
197 // LinkedListNode will become load addresses if we have a live process,
199 // what a file address if it what a file address.
200 if (process_is_alive && is_pointer_or_ref)
201 SetAddressTypeOfChildren(eAddressTypeLoad);
203 SetAddressTypeOfChildren(eAddressTypeFile);
205 case Value::eValueTypeHostAddress:
206 // Same as above for load addresses, except children of pointer or refs
208 // load addresses. Host addresses are used to store freeze dried
209 // variables. If this
210 // type is a struct, the entire struct contents will be copied into the
212 // LLDB process, but we do not currrently follow any pointers.
213 if (is_pointer_or_ref)
214 SetAddressTypeOfChildren(eAddressTypeLoad);
216 SetAddressTypeOfChildren(eAddressTypeHost);
218 case Value::eValueTypeLoadAddress:
219 case Value::eValueTypeScalar:
220 case Value::eValueTypeVector:
221 SetAddressTypeOfChildren(eAddressTypeLoad);
225 switch (value_type) {
226 case Value::eValueTypeVector:
228 case Value::eValueTypeScalar:
229 // The variable value is in the Scalar value inside the m_value.
230 // We can point our m_data right to it.
232 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
235 case Value::eValueTypeFileAddress:
236 case Value::eValueTypeLoadAddress:
237 case Value::eValueTypeHostAddress:
238 // The DWARF expression result was an address in the inferior
239 // process. If this variable is an aggregate type, we just need
240 // the address as the main value as all child variable objects
241 // will rely upon this location and add an offset and then read
242 // their own values as needed. If this variable is a simple
243 // type, we read all data for it into m_data.
244 // Make sure this type has a value before we try and read it
246 // If we have a file address, convert it to a load address if we can.
247 if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
248 lldb::addr_t file_addr =
249 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
250 if (file_addr != LLDB_INVALID_ADDRESS) {
251 SymbolContext var_sc;
252 variable->CalculateSymbolContext(&var_sc);
253 if (var_sc.module_sp) {
254 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
256 Address so_addr(file_addr, objfile->GetSectionList());
257 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
258 if (load_addr != LLDB_INVALID_ADDRESS) {
259 m_value.SetValueType(Value::eValueTypeLoadAddress);
260 m_value.GetScalar() = load_addr;
267 if (!CanProvideValue()) {
268 // this value object represents an aggregate type whose
269 // children have values, but this object does not. So we
270 // say we are changed if our location has changed.
271 SetValueDidChange(value_type != old_value.GetValueType() ||
272 m_value.GetScalar() != old_value.GetScalar());
274 // Copy the Value and set the context to use our Variable
275 // so it can extract read its value into m_data appropriately
276 Value value(m_value);
277 value.SetContext(Value::eContextTypeVariable, variable);
279 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
281 SetValueDidChange(value_type != old_value.GetValueType() ||
282 m_value.GetScalar() != old_value.GetScalar());
287 SetValueIsValid(m_error.Success());
289 // could not find location, won't allow editing
290 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
293 return m_error.Success();
296 bool ValueObjectVariable::IsInScope() {
297 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
298 if (exe_ctx_ref.HasFrameRef()) {
299 ExecutionContext exe_ctx(exe_ctx_ref);
300 StackFrame *frame = exe_ctx.GetFramePtr();
302 return m_variable_sp->IsInScope(frame);
304 // This ValueObject had a frame at one time, but now we
305 // can't locate it, so return false since we probably aren't
310 // We have a variable that wasn't tied to a frame, which
311 // means it is a global and is always in scope.
315 lldb::ModuleSP ValueObjectVariable::GetModule() {
317 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
319 return sc_scope->CalculateSymbolContextModule();
322 return lldb::ModuleSP();
325 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
327 return m_variable_sp->GetSymbolContextScope();
331 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
333 decl = m_variable_sp->GetDeclaration();
339 const char *ValueObjectVariable::GetLocationAsCString() {
340 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
341 return GetLocationAsCStringImpl(m_resolved_value, m_data);
343 return ValueObject::GetLocationAsCString();
346 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
348 if (!UpdateValueIfNeeded()) {
349 error.SetErrorString("unable to update value before writing");
353 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
354 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
355 ExecutionContext exe_ctx(GetExecutionContextRef());
356 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
357 RegisterValue reg_value;
358 if (!reg_info || !reg_ctx) {
359 error.SetErrorString("unable to retrieve register info");
362 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
365 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
369 error.SetErrorString("unable to write back to register");
373 return ValueObject::SetValueFromCString(value_str, error);
376 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
377 if (!UpdateValueIfNeeded()) {
378 error.SetErrorString("unable to update value before writing");
382 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
383 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
384 ExecutionContext exe_ctx(GetExecutionContextRef());
385 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
386 RegisterValue reg_value;
387 if (!reg_info || !reg_ctx) {
388 error.SetErrorString("unable to retrieve register info");
391 error = reg_value.SetValueFromData(reg_info, data, 0, true);
394 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
398 error.SetErrorString("unable to write back to register");
402 return ValueObject::SetData(data, error);