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 //===----------------------------------------------------------------------===//
11 #include "lldb/Core/ValueObjectVariable.h"
15 // Other libraries and framework includes
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/RegisterValue.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/Value.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/SymbolContextScope.h"
26 #include "lldb/Symbol/Type.h"
27 #include "lldb/Symbol/Variable.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
36 using namespace lldb_private;
39 ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
41 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
44 ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
45 ValueObject(exe_scope),
48 // Do not attempt to construct one of these objects with no variable!
49 assert (m_variable_sp.get() != NULL);
50 m_name = var_sp->GetName();
53 ValueObjectVariable::~ValueObjectVariable()
58 ValueObjectVariable::GetCompilerTypeImpl ()
60 Type *var_type = m_variable_sp->GetType();
62 return var_type->GetForwardCompilerType ();
63 return CompilerType();
67 ValueObjectVariable::GetTypeName()
69 Type * var_type = m_variable_sp->GetType();
71 return var_type->GetName();
76 ValueObjectVariable::GetDisplayTypeName()
78 Type * var_type = m_variable_sp->GetType();
80 return var_type->GetForwardCompilerType ().GetDisplayTypeName();
85 ValueObjectVariable::GetQualifiedTypeName()
87 Type * var_type = m_variable_sp->GetType();
89 return var_type->GetQualifiedName();
94 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;
107 ValueObjectVariable::GetByteSize()
109 ExecutionContext exe_ctx(GetExecutionContextRef());
111 CompilerType type(GetCompilerType());
116 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
120 ValueObjectVariable::GetValueType() const
123 return m_variable_sp->GetScope();
124 return lldb::eValueTypeInvalid;
128 ValueObjectVariable::UpdateValue ()
130 SetValueIsValid (false);
133 Variable *variable = m_variable_sp.get();
134 DWARFExpression &expr = variable->LocationExpression();
136 if (variable->GetLocationIsConstantValueData())
138 // expr doesn't contain DWARF bytes, it contains the constant variable
139 // value bytes themselves...
140 if (expr.GetExpressionData(m_data))
141 m_value.SetContext(Value::eContextTypeVariable, variable);
143 m_error.SetErrorString ("empty constant data");
144 // constant bytes can't be edited - sorry
145 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
149 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
150 ExecutionContext exe_ctx (GetExecutionContextRef());
152 Target *target = exe_ctx.GetTargetPtr();
155 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
156 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
159 if (expr.IsLocationList())
162 variable->CalculateSymbolContext (&sc);
164 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
166 Value old_value(m_value);
167 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
169 m_resolved_value = m_value;
170 m_value.SetContext(Value::eContextTypeVariable, variable);
172 CompilerType compiler_type = GetCompilerType();
173 if (compiler_type.IsValid())
174 m_value.SetCompilerType(compiler_type);
176 Value::ValueType value_type = m_value.GetValueType();
178 Process *process = exe_ctx.GetProcessPtr();
179 const bool process_is_alive = process && process->IsAlive();
180 const uint32_t type_info = compiler_type.GetTypeInfo();
181 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
185 case Value::eValueTypeFileAddress:
186 // If this type is a pointer, then its children will be considered load addresses
187 // if the pointer or reference is dereferenced, but only if the process is alive.
189 // There could be global variables like in the following code:
190 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
193 // LinkedListNode g_second_node = { &g_foo2, NULL };
194 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
196 // When we aren't running, we should be able to look at these variables using
197 // the "target variable" command. Children of the "g_first_node" always will
198 // be of the same address type as the parent. But children of the "next" member of
199 // LinkedListNode will become load addresses if we have a live process, or remain
200 // what a file address if it what a file address.
201 if (process_is_alive && is_pointer_or_ref)
202 SetAddressTypeOfChildren(eAddressTypeLoad);
204 SetAddressTypeOfChildren(eAddressTypeFile);
206 case Value::eValueTypeHostAddress:
207 // Same as above for load addresses, except children of pointer or refs are always
208 // load addresses. Host addresses are used to store freeze dried variables. If this
209 // type is a struct, the entire struct contents will be copied into the heap of 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);
225 case Value::eValueTypeVector:
227 case Value::eValueTypeScalar:
228 // The variable value is in the Scalar value inside the m_value.
229 // We can point our m_data right to it.
230 m_error = 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)
247 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
248 if (file_addr != LLDB_INVALID_ADDRESS)
250 SymbolContext var_sc;
251 variable->CalculateSymbolContext(&var_sc);
252 if (var_sc.module_sp)
254 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
257 Address so_addr(file_addr, objfile->GetSectionList());
258 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
259 if (load_addr != LLDB_INVALID_ADDRESS)
261 m_value.SetValueType(Value::eValueTypeLoadAddress);
262 m_value.GetScalar() = load_addr;
269 if (!CanProvideValue())
271 // this value object represents an aggregate type whose
272 // children have values, but this object does not. So we
273 // say we are changed if our location has changed.
274 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
278 // Copy the Value and set the context to use our Variable
279 // so it can extract read its value into m_data appropriately
280 Value value(m_value);
281 value.SetContext(Value::eContextTypeVariable, variable);
282 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
284 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
289 SetValueIsValid (m_error.Success());
293 // could not find location, won't allow editing
294 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
297 return m_error.Success();
303 ValueObjectVariable::IsInScope ()
305 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
306 if (exe_ctx_ref.HasFrameRef())
308 ExecutionContext exe_ctx (exe_ctx_ref);
309 StackFrame *frame = exe_ctx.GetFramePtr();
312 return m_variable_sp->IsInScope (frame);
316 // This ValueObject had a frame at one time, but now we
317 // can't locate it, so return false since we probably aren't
322 // We have a variable that wasn't tied to a frame, which
323 // means it is a global and is always in scope.
329 ValueObjectVariable::GetModule()
333 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
336 return sc_scope->CalculateSymbolContextModule();
339 return lldb::ModuleSP();
343 ValueObjectVariable::GetSymbolContextScope()
346 return m_variable_sp->GetSymbolContextScope();
351 ValueObjectVariable::GetDeclaration (Declaration &decl)
355 decl = m_variable_sp->GetDeclaration();
362 ValueObjectVariable::GetLocationAsCString ()
364 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
365 return GetLocationAsCStringImpl(m_resolved_value,
368 return ValueObject::GetLocationAsCString();
372 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
374 if (!UpdateValueIfNeeded())
376 error.SetErrorString("unable to update value before writing");
380 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
382 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
383 ExecutionContext exe_ctx(GetExecutionContextRef());
384 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
385 RegisterValue reg_value;
386 if (!reg_info || !reg_ctx)
388 error.SetErrorString("unable to retrieve register info");
391 error = reg_value.SetValueFromCString(reg_info, value_str);
394 if (reg_ctx->WriteRegister (reg_info, reg_value))
401 error.SetErrorString("unable to write back to register");
406 return ValueObject::SetValueFromCString(value_str, error);
410 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
412 if (!UpdateValueIfNeeded())
414 error.SetErrorString("unable to update value before writing");
418 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
420 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
421 ExecutionContext exe_ctx(GetExecutionContextRef());
422 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
423 RegisterValue reg_value;
424 if (!reg_info || !reg_ctx)
426 error.SetErrorString("unable to retrieve register info");
429 error = reg_value.SetValueFromData(reg_info, data, 0, true);
432 if (reg_ctx->WriteRegister (reg_info, reg_value))
439 error.SetErrorString("unable to write back to register");
444 return ValueObject::SetData(data, error);