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();
177 case Value::eValueTypeFileAddress:
178 SetAddressTypeOfChildren(eAddressTypeFile);
180 case Value::eValueTypeHostAddress:
181 SetAddressTypeOfChildren(eAddressTypeHost);
183 case Value::eValueTypeLoadAddress:
184 case Value::eValueTypeScalar:
185 case Value::eValueTypeVector:
186 SetAddressTypeOfChildren(eAddressTypeLoad);
192 case Value::eValueTypeVector:
194 case Value::eValueTypeScalar:
195 // The variable value is in the Scalar value inside the m_value.
196 // We can point our m_data right to it.
197 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
200 case Value::eValueTypeFileAddress:
201 case Value::eValueTypeLoadAddress:
202 case Value::eValueTypeHostAddress:
203 // The DWARF expression result was an address in the inferior
204 // process. If this variable is an aggregate type, we just need
205 // the address as the main value as all child variable objects
206 // will rely upon this location and add an offset and then read
207 // their own values as needed. If this variable is a simple
208 // type, we read all data for it into m_data.
209 // Make sure this type has a value before we try and read it
211 // If we have a file address, convert it to a load address if we can.
212 Process *process = exe_ctx.GetProcessPtr();
213 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
215 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
216 if (file_addr != LLDB_INVALID_ADDRESS)
218 SymbolContext var_sc;
219 variable->CalculateSymbolContext(&var_sc);
220 if (var_sc.module_sp)
222 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
225 Address so_addr(file_addr, objfile->GetSectionList());
226 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
227 if (load_addr != LLDB_INVALID_ADDRESS)
229 m_value.SetValueType(Value::eValueTypeLoadAddress);
230 m_value.GetScalar() = load_addr;
237 if (GetClangType().IsAggregateType())
239 // this value object represents an aggregate type whose
240 // children have values, but this object does not. So we
241 // say we are changed if our location has changed.
242 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
246 // Copy the Value and set the context to use our Variable
247 // so it can extract read its value into m_data appropriately
248 Value value(m_value);
249 value.SetContext(Value::eContextTypeVariable, variable);
250 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
255 SetValueIsValid (m_error.Success());
259 // could not find location, won't allow editing
260 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
263 return m_error.Success();
269 ValueObjectVariable::IsInScope ()
271 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
272 if (exe_ctx_ref.HasFrameRef())
274 ExecutionContext exe_ctx (exe_ctx_ref);
275 StackFrame *frame = exe_ctx.GetFramePtr();
278 return m_variable_sp->IsInScope (frame);
282 // This ValueObject had a frame at one time, but now we
283 // can't locate it, so return false since we probably aren't
288 // We have a variable that wasn't tied to a frame, which
289 // means it is a global and is always in scope.
295 ValueObjectVariable::GetModule()
299 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
302 return sc_scope->CalculateSymbolContextModule();
305 return lldb::ModuleSP();
309 ValueObjectVariable::GetSymbolContextScope()
312 return m_variable_sp->GetSymbolContextScope();
317 ValueObjectVariable::GetDeclaration (Declaration &decl)
321 decl = m_variable_sp->GetDeclaration();
328 ValueObjectVariable::GetLocationAsCString ()
330 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
331 return GetLocationAsCStringImpl(m_resolved_value,
334 return ValueObject::GetLocationAsCString();
338 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
340 if (!UpdateValueIfNeeded())
342 error.SetErrorString("unable to update value before writing");
346 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
348 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
349 ExecutionContext exe_ctx(GetExecutionContextRef());
350 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
351 RegisterValue reg_value;
352 if (!reg_info || !reg_ctx)
354 error.SetErrorString("unable to retrieve register info");
357 error = reg_value.SetValueFromCString(reg_info, value_str);
360 if (reg_ctx->WriteRegister (reg_info, reg_value))
367 error.SetErrorString("unable to write back to register");
372 return ValueObject::SetValueFromCString(value_str, error);
376 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
378 if (!UpdateValueIfNeeded())
380 error.SetErrorString("unable to update value before writing");
384 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
386 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
387 ExecutionContext exe_ctx(GetExecutionContextRef());
388 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
389 RegisterValue reg_value;
390 if (!reg_info || !reg_ctx)
392 error.SetErrorString("unable to retrieve register info");
395 error = reg_value.SetValueFromData(reg_info, data, 0, true);
398 if (reg_ctx->WriteRegister (reg_info, reg_value))
405 error.SetErrorString("unable to write back to register");
410 return ValueObject::SetData(data, error);