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 ClangASTType type(GetClangType());
113 return type.GetByteSize();
117 ValueObjectVariable::GetValueType() const
120 return m_variable_sp->GetScope();
121 return lldb::eValueTypeInvalid;
125 ValueObjectVariable::UpdateValue ()
127 SetValueIsValid (false);
130 Variable *variable = m_variable_sp.get();
131 DWARFExpression &expr = variable->LocationExpression();
133 if (variable->GetLocationIsConstantValueData())
135 // expr doesn't contain DWARF bytes, it contains the constant variable
136 // value bytes themselves...
137 if (expr.GetExpressionData(m_data))
138 m_value.SetContext(Value::eContextTypeVariable, variable);
140 m_error.SetErrorString ("empty constant data");
141 // constant bytes can't be edited - sorry
142 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
146 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
147 ExecutionContext exe_ctx (GetExecutionContextRef());
149 Target *target = exe_ctx.GetTargetPtr();
152 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
153 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
156 if (expr.IsLocationList())
159 variable->CalculateSymbolContext (&sc);
161 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
163 Value old_value(m_value);
164 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
166 m_resolved_value = m_value;
167 m_value.SetContext(Value::eContextTypeVariable, variable);
169 ClangASTType clang_type = GetClangType();
170 if (clang_type.IsValid())
171 m_value.SetClangType(clang_type);
173 Value::ValueType value_type = m_value.GetValueType();
175 Process *process = exe_ctx.GetProcessPtr();
176 const bool process_is_alive = process && process->IsAlive();
177 const uint32_t type_info = clang_type.GetTypeInfo();
178 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
182 case Value::eValueTypeFileAddress:
183 // If this type is a pointer, then its children will be considered load addresses
184 // if the pointer or reference is dereferenced, but only if the process is alive.
186 // There could be global variables like in the following code:
187 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
190 // LinkedListNode g_second_node = { &g_foo2, NULL };
191 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
193 // When we aren't running, we should be able to look at these variables using
194 // the "target variable" command. Children of the "g_first_node" always will
195 // be of the same address type as the parent. But children of the "next" member of
196 // LinkedListNode will become load addresses if we have a live process, or remain
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 are always
205 // load addresses. Host addresses are used to store freeze dried variables. If this
206 // type is a struct, the entire struct contents will be copied into the heap of the
207 // LLDB process, but we do not currrently follow any pointers.
208 if (is_pointer_or_ref)
209 SetAddressTypeOfChildren(eAddressTypeLoad);
211 SetAddressTypeOfChildren(eAddressTypeHost);
213 case Value::eValueTypeLoadAddress:
214 case Value::eValueTypeScalar:
215 case Value::eValueTypeVector:
216 SetAddressTypeOfChildren(eAddressTypeLoad);
222 case Value::eValueTypeVector:
224 case Value::eValueTypeScalar:
225 // The variable value is in the Scalar value inside the m_value.
226 // We can point our m_data right to it.
227 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
230 case Value::eValueTypeFileAddress:
231 case Value::eValueTypeLoadAddress:
232 case Value::eValueTypeHostAddress:
233 // The DWARF expression result was an address in the inferior
234 // process. If this variable is an aggregate type, we just need
235 // the address as the main value as all child variable objects
236 // will rely upon this location and add an offset and then read
237 // their own values as needed. If this variable is a simple
238 // type, we read all data for it into m_data.
239 // Make sure this type has a value before we try and read it
241 // If we have a file address, convert it to a load address if we can.
242 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
244 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
245 if (file_addr != LLDB_INVALID_ADDRESS)
247 SymbolContext var_sc;
248 variable->CalculateSymbolContext(&var_sc);
249 if (var_sc.module_sp)
251 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)
258 m_value.SetValueType(Value::eValueTypeLoadAddress);
259 m_value.GetScalar() = load_addr;
266 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() || m_value.GetScalar() != old_value.GetScalar());
275 // Copy the Value and set the context to use our Variable
276 // so it can extract read its value into m_data appropriately
277 Value value(m_value);
278 value.SetContext(Value::eContextTypeVariable, variable);
279 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
281 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
286 SetValueIsValid (m_error.Success());
290 // could not find location, won't allow editing
291 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
294 return m_error.Success();
300 ValueObjectVariable::IsInScope ()
302 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
303 if (exe_ctx_ref.HasFrameRef())
305 ExecutionContext exe_ctx (exe_ctx_ref);
306 StackFrame *frame = exe_ctx.GetFramePtr();
309 return m_variable_sp->IsInScope (frame);
313 // This ValueObject had a frame at one time, but now we
314 // can't locate it, so return false since we probably aren't
319 // We have a variable that wasn't tied to a frame, which
320 // means it is a global and is always in scope.
326 ValueObjectVariable::GetModule()
330 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
333 return sc_scope->CalculateSymbolContextModule();
336 return lldb::ModuleSP();
340 ValueObjectVariable::GetSymbolContextScope()
343 return m_variable_sp->GetSymbolContextScope();
348 ValueObjectVariable::GetDeclaration (Declaration &decl)
352 decl = m_variable_sp->GetDeclaration();
359 ValueObjectVariable::GetLocationAsCString ()
361 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
362 return GetLocationAsCStringImpl(m_resolved_value,
365 return ValueObject::GetLocationAsCString();
369 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
371 if (!UpdateValueIfNeeded())
373 error.SetErrorString("unable to update value before writing");
377 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
379 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
380 ExecutionContext exe_ctx(GetExecutionContextRef());
381 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
382 RegisterValue reg_value;
383 if (!reg_info || !reg_ctx)
385 error.SetErrorString("unable to retrieve register info");
388 error = reg_value.SetValueFromCString(reg_info, value_str);
391 if (reg_ctx->WriteRegister (reg_info, reg_value))
398 error.SetErrorString("unable to write back to register");
403 return ValueObject::SetValueFromCString(value_str, error);
407 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
409 if (!UpdateValueIfNeeded())
411 error.SetErrorString("unable to update value before writing");
415 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
417 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
418 ExecutionContext exe_ctx(GetExecutionContextRef());
419 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
420 RegisterValue reg_value;
421 if (!reg_info || !reg_ctx)
423 error.SetErrorString("unable to retrieve register info");
426 error = reg_value.SetValueFromData(reg_info, data, 0, true);
429 if (reg_ctx->WriteRegister (reg_info, reg_value))
436 error.SetErrorString("unable to write back to register");
441 return ValueObject::SetData(data, error);