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::GetClangTypeImpl ()
60 Type *var_type = m_variable_sp->GetType();
62 return var_type->GetClangForwardType();
63 return ClangASTType();
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->GetClangForwardType().GetDisplayTypeName();
85 ValueObjectVariable::GetQualifiedTypeName()
87 Type * var_type = m_variable_sp->GetType();
89 return var_type->GetQualifiedName();
94 ValueObjectVariable::CalculateNumChildren()
96 ClangASTType type(GetClangType());
101 const bool omit_empty_base_classes = true;
102 return type.GetNumChildren(omit_empty_base_classes);
106 ValueObjectVariable::GetByteSize()
108 ExecutionContext exe_ctx(GetExecutionContextRef());
110 ClangASTType type(GetClangType());
115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
119 ValueObjectVariable::GetValueType() const
122 return m_variable_sp->GetScope();
123 return lldb::eValueTypeInvalid;
127 ValueObjectVariable::UpdateValue ()
129 SetValueIsValid (false);
132 Variable *variable = m_variable_sp.get();
133 DWARFExpression &expr = variable->LocationExpression();
135 if (variable->GetLocationIsConstantValueData())
137 // expr doesn't contain DWARF bytes, it contains the constant variable
138 // value bytes themselves...
139 if (expr.GetExpressionData(m_data))
140 m_value.SetContext(Value::eContextTypeVariable, variable);
142 m_error.SetErrorString ("empty constant data");
143 // constant bytes can't be edited - sorry
144 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
148 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
149 ExecutionContext exe_ctx (GetExecutionContextRef());
151 Target *target = exe_ctx.GetTargetPtr();
154 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
155 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
158 if (expr.IsLocationList())
161 variable->CalculateSymbolContext (&sc);
163 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
165 Value old_value(m_value);
166 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
168 m_resolved_value = m_value;
169 m_value.SetContext(Value::eContextTypeVariable, variable);
171 ClangASTType clang_type = GetClangType();
172 if (clang_type.IsValid())
173 m_value.SetClangType(clang_type);
175 Value::ValueType value_type = m_value.GetValueType();
177 Process *process = exe_ctx.GetProcessPtr();
178 const bool process_is_alive = process && process->IsAlive();
179 const uint32_t type_info = clang_type.GetTypeInfo();
180 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
184 case Value::eValueTypeFileAddress:
185 // If this type is a pointer, then its children will be considered load addresses
186 // if the pointer or reference is dereferenced, but only if the process is alive.
188 // There could be global variables like in the following code:
189 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
192 // LinkedListNode g_second_node = { &g_foo2, NULL };
193 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
195 // When we aren't running, we should be able to look at these variables using
196 // the "target variable" command. Children of the "g_first_node" always will
197 // be of the same address type as the parent. But children of the "next" member of
198 // LinkedListNode will become load addresses if we have a live process, or remain
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 are always
207 // load addresses. Host addresses are used to store freeze dried variables. If this
208 // type is a struct, the entire struct contents will be copied into the heap of the
209 // LLDB process, but we do not currrently follow any pointers.
210 if (is_pointer_or_ref)
211 SetAddressTypeOfChildren(eAddressTypeLoad);
213 SetAddressTypeOfChildren(eAddressTypeHost);
215 case Value::eValueTypeLoadAddress:
216 case Value::eValueTypeScalar:
217 case Value::eValueTypeVector:
218 SetAddressTypeOfChildren(eAddressTypeLoad);
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.
229 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
232 case Value::eValueTypeFileAddress:
233 case Value::eValueTypeLoadAddress:
234 case Value::eValueTypeHostAddress:
235 // The DWARF expression result was an address in the inferior
236 // process. If this variable is an aggregate type, we just need
237 // the address as the main value as all child variable objects
238 // will rely upon this location and add an offset and then read
239 // their own values as needed. If this variable is a simple
240 // type, we read all data for it into m_data.
241 // Make sure this type has a value before we try and read it
243 // If we have a file address, convert it to a load address if we can.
244 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
246 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
247 if (file_addr != LLDB_INVALID_ADDRESS)
249 SymbolContext var_sc;
250 variable->CalculateSymbolContext(&var_sc);
251 if (var_sc.module_sp)
253 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)
260 m_value.SetValueType(Value::eValueTypeLoadAddress);
261 m_value.GetScalar() = load_addr;
268 if (!CanProvideValue())
270 // this value object represents an aggregate type whose
271 // children have values, but this object does not. So we
272 // say we are changed if our location has changed.
273 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
277 // Copy the Value and set the context to use our Variable
278 // so it can extract read its value into m_data appropriately
279 Value value(m_value);
280 value.SetContext(Value::eContextTypeVariable, variable);
281 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
283 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
288 SetValueIsValid (m_error.Success());
292 // could not find location, won't allow editing
293 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
296 return m_error.Success();
302 ValueObjectVariable::IsInScope ()
304 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
305 if (exe_ctx_ref.HasFrameRef())
307 ExecutionContext exe_ctx (exe_ctx_ref);
308 StackFrame *frame = exe_ctx.GetFramePtr();
311 return m_variable_sp->IsInScope (frame);
315 // This ValueObject had a frame at one time, but now we
316 // can't locate it, so return false since we probably aren't
321 // We have a variable that wasn't tied to a frame, which
322 // means it is a global and is always in scope.
328 ValueObjectVariable::GetModule()
332 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
335 return sc_scope->CalculateSymbolContextModule();
338 return lldb::ModuleSP();
342 ValueObjectVariable::GetSymbolContextScope()
345 return m_variable_sp->GetSymbolContextScope();
350 ValueObjectVariable::GetDeclaration (Declaration &decl)
354 decl = m_variable_sp->GetDeclaration();
361 ValueObjectVariable::GetLocationAsCString ()
363 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
364 return GetLocationAsCStringImpl(m_resolved_value,
367 return ValueObject::GetLocationAsCString();
371 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
373 if (!UpdateValueIfNeeded())
375 error.SetErrorString("unable to update value before writing");
379 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)
387 error.SetErrorString("unable to retrieve register info");
390 error = reg_value.SetValueFromCString(reg_info, value_str);
393 if (reg_ctx->WriteRegister (reg_info, reg_value))
400 error.SetErrorString("unable to write back to register");
405 return ValueObject::SetValueFromCString(value_str, error);
409 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
411 if (!UpdateValueIfNeeded())
413 error.SetErrorString("unable to update value before writing");
417 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
419 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
420 ExecutionContext exe_ctx(GetExecutionContextRef());
421 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
422 RegisterValue reg_value;
423 if (!reg_info || !reg_ctx)
425 error.SetErrorString("unable to retrieve register info");
428 error = reg_value.SetValueFromData(reg_info, data, 0, true);
431 if (reg_ctx->WriteRegister (reg_info, reg_value))
438 error.SetErrorString("unable to write back to register");
443 return ValueObject::SetData(data, error);