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/Module.h"
15 #include "lldb/Core/RegisterValue.h"
16 #include "lldb/Core/Scalar.h" // for Scalar, operator!=
17 #include "lldb/Core/Value.h"
18 #include "lldb/Expression/DWARFExpression.h" // for DWARFExpression
19 #include "lldb/Symbol/Declaration.h" // for Declaration
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/ObjectFile.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Symbol/SymbolContextScope.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Symbol/Variable.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
31 #include "lldb/Utility/Status.h" // for Status
32 #include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy...
33 #include "lldb/lldb-types.h" // for addr_t
35 #include "llvm/ADT/StringRef.h" // for StringRef
37 #include <assert.h> // for assert
38 #include <memory> // for shared_ptr
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 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
57 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
58 const lldb::VariableSP &var_sp)
59 : ValueObject(exe_scope), m_variable_sp(var_sp) {
60 // Do not attempt to construct one of these objects with no variable!
61 assert(m_variable_sp.get() != NULL);
62 m_name = var_sp->GetName();
65 ValueObjectVariable::~ValueObjectVariable() {}
67 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
68 Type *var_type = m_variable_sp->GetType();
70 return var_type->GetForwardCompilerType();
71 return CompilerType();
74 ConstString ValueObjectVariable::GetTypeName() {
75 Type *var_type = m_variable_sp->GetType();
77 return var_type->GetName();
81 ConstString ValueObjectVariable::GetDisplayTypeName() {
82 Type *var_type = m_variable_sp->GetType();
84 return var_type->GetForwardCompilerType().GetDisplayTypeName();
88 ConstString ValueObjectVariable::GetQualifiedTypeName() {
89 Type *var_type = m_variable_sp->GetType();
91 return var_type->GetQualifiedName();
95 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
96 CompilerType type(GetCompilerType());
101 const bool omit_empty_base_classes = true;
102 auto child_count = type.GetNumChildren(omit_empty_base_classes);
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());
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, NULL);
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
179 // if the pointer or reference is dereferenced, but only if the process
182 // There could be global variables like in the following code:
183 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
186 // LinkedListNode g_second_node = { &g_foo2, NULL };
187 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
189 // When we aren't running, we should be able to look at these variables
191 // the "target variable" command. Children of the "g_first_node" always
193 // be of the same address type as the parent. But children of the "next"
195 // LinkedListNode will become load addresses if we have a live process,
197 // what a file address if it what a file address.
198 if (process_is_alive && is_pointer_or_ref)
199 SetAddressTypeOfChildren(eAddressTypeLoad);
201 SetAddressTypeOfChildren(eAddressTypeFile);
203 case Value::eValueTypeHostAddress:
204 // Same as above for load addresses, except children of pointer or refs
206 // load addresses. Host addresses are used to store freeze dried
207 // variables. If this
208 // type is a struct, the entire struct contents will be copied into the
210 // LLDB process, but we do not currrently follow any pointers.
211 if (is_pointer_or_ref)
212 SetAddressTypeOfChildren(eAddressTypeLoad);
214 SetAddressTypeOfChildren(eAddressTypeHost);
216 case Value::eValueTypeLoadAddress:
217 case Value::eValueTypeScalar:
218 case Value::eValueTypeVector:
219 SetAddressTypeOfChildren(eAddressTypeLoad);
223 switch (value_type) {
224 case Value::eValueTypeVector:
226 case Value::eValueTypeScalar:
227 // The variable value is in the Scalar value inside the m_value.
228 // We can point our m_data right to it.
230 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
233 case Value::eValueTypeFileAddress:
234 case Value::eValueTypeLoadAddress:
235 case Value::eValueTypeHostAddress:
236 // The DWARF expression result was an address in the inferior
237 // process. If this variable is an aggregate type, we just need
238 // the address as the main value as all child variable objects
239 // will rely upon this location and add an offset and then read
240 // their own values as needed. If this variable is a simple
241 // type, we read all data for it into m_data.
242 // Make sure this type has a value before we try and read it
244 // If we have a file address, convert it to a load address if we can.
245 if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
246 lldb::addr_t file_addr =
247 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
248 if (file_addr != LLDB_INVALID_ADDRESS) {
249 SymbolContext var_sc;
250 variable->CalculateSymbolContext(&var_sc);
251 if (var_sc.module_sp) {
252 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
254 Address so_addr(file_addr, objfile->GetSectionList());
255 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
256 if (load_addr != LLDB_INVALID_ADDRESS) {
257 m_value.SetValueType(Value::eValueTypeLoadAddress);
258 m_value.GetScalar() = load_addr;
265 if (!CanProvideValue()) {
266 // this value object represents an aggregate type whose
267 // children have values, but this object does not. So we
268 // say we are changed if our location has changed.
269 SetValueDidChange(value_type != old_value.GetValueType() ||
270 m_value.GetScalar() != old_value.GetScalar());
272 // Copy the Value and set the context to use our Variable
273 // so it can extract read its value into m_data appropriately
274 Value value(m_value);
275 value.SetContext(Value::eContextTypeVariable, variable);
277 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
279 SetValueDidChange(value_type != old_value.GetValueType() ||
280 m_value.GetScalar() != old_value.GetScalar());
285 SetValueIsValid(m_error.Success());
287 // could not find location, won't allow editing
288 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
291 return m_error.Success();
294 bool ValueObjectVariable::IsInScope() {
295 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
296 if (exe_ctx_ref.HasFrameRef()) {
297 ExecutionContext exe_ctx(exe_ctx_ref);
298 StackFrame *frame = exe_ctx.GetFramePtr();
300 return m_variable_sp->IsInScope(frame);
302 // This ValueObject had a frame at one time, but now we
303 // can't locate it, so return false since we probably aren't
308 // We have a variable that wasn't tied to a frame, which
309 // means it is a global and is always in scope.
313 lldb::ModuleSP ValueObjectVariable::GetModule() {
315 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
317 return sc_scope->CalculateSymbolContextModule();
320 return lldb::ModuleSP();
323 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
325 return m_variable_sp->GetSymbolContextScope();
329 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
331 decl = m_variable_sp->GetDeclaration();
337 const char *ValueObjectVariable::GetLocationAsCString() {
338 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
339 return GetLocationAsCStringImpl(m_resolved_value, m_data);
341 return ValueObject::GetLocationAsCString();
344 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
346 if (!UpdateValueIfNeeded()) {
347 error.SetErrorString("unable to update value before writing");
351 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
352 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
353 ExecutionContext exe_ctx(GetExecutionContextRef());
354 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
355 RegisterValue reg_value;
356 if (!reg_info || !reg_ctx) {
357 error.SetErrorString("unable to retrieve register info");
360 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
363 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
367 error.SetErrorString("unable to write back to register");
371 return ValueObject::SetValueFromCString(value_str, error);
374 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
375 if (!UpdateValueIfNeeded()) {
376 error.SetErrorString("unable to update value before writing");
380 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
381 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
382 ExecutionContext exe_ctx(GetExecutionContextRef());
383 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
384 RegisterValue reg_value;
385 if (!reg_info || !reg_ctx) {
386 error.SetErrorString("unable to retrieve register info");
389 error = reg_value.SetValueFromData(reg_info, data, 0, true);
392 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
396 error.SetErrorString("unable to write back to register");
400 return ValueObject::SetData(data, error);