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"
14 // Other libraries and framework includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/RegisterValue.h"
18 #include "lldb/Core/Value.h"
19 #include "lldb/Core/ValueObjectList.h"
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"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/RegisterContext.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
34 using namespace lldb_private;
37 ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
38 const lldb::VariableSP &var_sp) {
39 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
42 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
43 const lldb::VariableSP &var_sp)
44 : ValueObject(exe_scope), m_variable_sp(var_sp) {
45 // Do not attempt to construct one of these objects with no variable!
46 assert(m_variable_sp.get() != NULL);
47 m_name = var_sp->GetName();
50 ValueObjectVariable::~ValueObjectVariable() {}
52 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
53 Type *var_type = m_variable_sp->GetType();
55 return var_type->GetForwardCompilerType();
56 return CompilerType();
59 ConstString ValueObjectVariable::GetTypeName() {
60 Type *var_type = m_variable_sp->GetType();
62 return var_type->GetName();
66 ConstString ValueObjectVariable::GetDisplayTypeName() {
67 Type *var_type = m_variable_sp->GetType();
69 return var_type->GetForwardCompilerType().GetDisplayTypeName();
73 ConstString ValueObjectVariable::GetQualifiedTypeName() {
74 Type *var_type = m_variable_sp->GetType();
76 return var_type->GetQualifiedName();
80 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
81 CompilerType type(GetCompilerType());
86 const bool omit_empty_base_classes = true;
87 auto child_count = type.GetNumChildren(omit_empty_base_classes);
88 return child_count <= max ? child_count : max;
91 uint64_t ValueObjectVariable::GetByteSize() {
92 ExecutionContext exe_ctx(GetExecutionContextRef());
94 CompilerType type(GetCompilerType());
99 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
102 lldb::ValueType ValueObjectVariable::GetValueType() const {
104 return m_variable_sp->GetScope();
105 return lldb::eValueTypeInvalid;
108 bool ValueObjectVariable::UpdateValue() {
109 SetValueIsValid(false);
112 Variable *variable = m_variable_sp.get();
113 DWARFExpression &expr = variable->LocationExpression();
115 if (variable->GetLocationIsConstantValueData()) {
116 // expr doesn't contain DWARF bytes, it contains the constant variable
117 // value bytes themselves...
118 if (expr.GetExpressionData(m_data))
119 m_value.SetContext(Value::eContextTypeVariable, variable);
121 m_error.SetErrorString("empty constant data");
122 // constant bytes can't be edited - sorry
123 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
125 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
126 ExecutionContext exe_ctx(GetExecutionContextRef());
128 Target *target = exe_ctx.GetTargetPtr();
130 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
131 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
134 if (expr.IsLocationList()) {
136 variable->CalculateSymbolContext(&sc);
138 loclist_base_load_addr =
139 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
142 Value old_value(m_value);
143 if (expr.Evaluate(&exe_ctx, nullptr, nullptr, nullptr,
144 loclist_base_load_addr, nullptr, nullptr, m_value,
146 m_resolved_value = m_value;
147 m_value.SetContext(Value::eContextTypeVariable, variable);
149 CompilerType compiler_type = GetCompilerType();
150 if (compiler_type.IsValid())
151 m_value.SetCompilerType(compiler_type);
153 Value::ValueType value_type = m_value.GetValueType();
155 Process *process = exe_ctx.GetProcessPtr();
156 const bool process_is_alive = process && process->IsAlive();
157 const uint32_t type_info = compiler_type.GetTypeInfo();
158 const bool is_pointer_or_ref =
159 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
161 switch (value_type) {
162 case Value::eValueTypeFileAddress:
163 // If this type is a pointer, then its children will be considered load
165 // if the pointer or reference is dereferenced, but only if the process
168 // There could be global variables like in the following code:
169 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
172 // LinkedListNode g_second_node = { &g_foo2, NULL };
173 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
175 // When we aren't running, we should be able to look at these variables
177 // the "target variable" command. Children of the "g_first_node" always
179 // be of the same address type as the parent. But children of the "next"
181 // LinkedListNode will become load addresses if we have a live process,
183 // what a file address if it what a file address.
184 if (process_is_alive && is_pointer_or_ref)
185 SetAddressTypeOfChildren(eAddressTypeLoad);
187 SetAddressTypeOfChildren(eAddressTypeFile);
189 case Value::eValueTypeHostAddress:
190 // Same as above for load addresses, except children of pointer or refs
192 // load addresses. Host addresses are used to store freeze dried
193 // variables. If this
194 // type is a struct, the entire struct contents will be copied into the
196 // LLDB process, but we do not currrently follow any pointers.
197 if (is_pointer_or_ref)
198 SetAddressTypeOfChildren(eAddressTypeLoad);
200 SetAddressTypeOfChildren(eAddressTypeHost);
202 case Value::eValueTypeLoadAddress:
203 case Value::eValueTypeScalar:
204 case Value::eValueTypeVector:
205 SetAddressTypeOfChildren(eAddressTypeLoad);
209 switch (value_type) {
210 case Value::eValueTypeVector:
212 case Value::eValueTypeScalar:
213 // The variable value is in the Scalar value inside the m_value.
214 // We can point our m_data right to it.
216 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
219 case Value::eValueTypeFileAddress:
220 case Value::eValueTypeLoadAddress:
221 case Value::eValueTypeHostAddress:
222 // The DWARF expression result was an address in the inferior
223 // process. If this variable is an aggregate type, we just need
224 // the address as the main value as all child variable objects
225 // will rely upon this location and add an offset and then read
226 // their own values as needed. If this variable is a simple
227 // type, we read all data for it into m_data.
228 // Make sure this type has a value before we try and read it
230 // If we have a file address, convert it to a load address if we can.
231 if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
232 lldb::addr_t file_addr =
233 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
234 if (file_addr != LLDB_INVALID_ADDRESS) {
235 SymbolContext var_sc;
236 variable->CalculateSymbolContext(&var_sc);
237 if (var_sc.module_sp) {
238 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
240 Address so_addr(file_addr, objfile->GetSectionList());
241 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
242 if (load_addr != LLDB_INVALID_ADDRESS) {
243 m_value.SetValueType(Value::eValueTypeLoadAddress);
244 m_value.GetScalar() = load_addr;
251 if (!CanProvideValue()) {
252 // this value object represents an aggregate type whose
253 // children have values, but this object does not. So we
254 // say we are changed if our location has changed.
255 SetValueDidChange(value_type != old_value.GetValueType() ||
256 m_value.GetScalar() != old_value.GetScalar());
258 // Copy the Value and set the context to use our Variable
259 // so it can extract read its value into m_data appropriately
260 Value value(m_value);
261 value.SetContext(Value::eContextTypeVariable, variable);
263 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
265 SetValueDidChange(value_type != old_value.GetValueType() ||
266 m_value.GetScalar() != old_value.GetScalar());
271 SetValueIsValid(m_error.Success());
273 // could not find location, won't allow editing
274 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
277 return m_error.Success();
280 bool ValueObjectVariable::IsInScope() {
281 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
282 if (exe_ctx_ref.HasFrameRef()) {
283 ExecutionContext exe_ctx(exe_ctx_ref);
284 StackFrame *frame = exe_ctx.GetFramePtr();
286 return m_variable_sp->IsInScope(frame);
288 // This ValueObject had a frame at one time, but now we
289 // can't locate it, so return false since we probably aren't
294 // We have a variable that wasn't tied to a frame, which
295 // means it is a global and is always in scope.
299 lldb::ModuleSP ValueObjectVariable::GetModule() {
301 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
303 return sc_scope->CalculateSymbolContextModule();
306 return lldb::ModuleSP();
309 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
311 return m_variable_sp->GetSymbolContextScope();
315 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
317 decl = m_variable_sp->GetDeclaration();
323 const char *ValueObjectVariable::GetLocationAsCString() {
324 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
325 return GetLocationAsCStringImpl(m_resolved_value, m_data);
327 return ValueObject::GetLocationAsCString();
330 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
332 if (!UpdateValueIfNeeded()) {
333 error.SetErrorString("unable to update value before writing");
337 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
338 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
339 ExecutionContext exe_ctx(GetExecutionContextRef());
340 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
341 RegisterValue reg_value;
342 if (!reg_info || !reg_ctx) {
343 error.SetErrorString("unable to retrieve register info");
346 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
349 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
353 error.SetErrorString("unable to write back to register");
357 return ValueObject::SetValueFromCString(value_str, error);
360 bool ValueObjectVariable::SetData(DataExtractor &data, Error &error) {
361 if (!UpdateValueIfNeeded()) {
362 error.SetErrorString("unable to update value before writing");
366 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
367 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
368 ExecutionContext exe_ctx(GetExecutionContextRef());
369 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
370 RegisterValue reg_value;
371 if (!reg_info || !reg_ctx) {
372 error.SetErrorString("unable to retrieve register info");
375 error = reg_value.SetValueFromData(reg_info, data, 0, true);
378 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
382 error.SetErrorString("unable to write back to register");
386 return ValueObject::SetData(data, error);