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::GetQualifiedTypeName()
78 Type * var_type = m_variable_sp->GetType();
80 return var_type->GetQualifiedName();
85 ValueObjectVariable::CalculateNumChildren()
87 ClangASTType type(GetClangType());
92 const bool omit_empty_base_classes = true;
93 return type.GetNumChildren(omit_empty_base_classes);
97 ValueObjectVariable::GetByteSize()
99 ClangASTType type(GetClangType());
104 return type.GetByteSize();
108 ValueObjectVariable::GetValueType() const
111 return m_variable_sp->GetScope();
112 return lldb::eValueTypeInvalid;
116 ValueObjectVariable::UpdateValue ()
118 SetValueIsValid (false);
121 Variable *variable = m_variable_sp.get();
122 DWARFExpression &expr = variable->LocationExpression();
124 if (variable->GetLocationIsConstantValueData())
126 // expr doesn't contain DWARF bytes, it contains the constant variable
127 // value bytes themselves...
128 if (expr.GetExpressionData(m_data))
129 m_value.SetContext(Value::eContextTypeVariable, variable);
131 m_error.SetErrorString ("empty constant data");
132 // constant bytes can't be edited - sorry
133 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
137 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
138 ExecutionContext exe_ctx (GetExecutionContextRef());
140 Target *target = exe_ctx.GetTargetPtr();
143 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
144 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
147 if (expr.IsLocationList())
150 variable->CalculateSymbolContext (&sc);
152 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
154 Value old_value(m_value);
155 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
157 m_resolved_value = m_value;
158 m_value.SetContext(Value::eContextTypeVariable, variable);
160 ClangASTType clang_type = GetClangType();
161 if (clang_type.IsValid())
162 m_value.SetClangType(clang_type);
164 Value::ValueType value_type = m_value.GetValueType();
168 case Value::eValueTypeFileAddress:
169 SetAddressTypeOfChildren(eAddressTypeFile);
171 case Value::eValueTypeHostAddress:
172 SetAddressTypeOfChildren(eAddressTypeHost);
174 case Value::eValueTypeLoadAddress:
175 case Value::eValueTypeScalar:
176 case Value::eValueTypeVector:
177 SetAddressTypeOfChildren(eAddressTypeLoad);
183 case Value::eValueTypeVector:
185 case Value::eValueTypeScalar:
186 // The variable value is in the Scalar value inside the m_value.
187 // We can point our m_data right to it.
188 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
191 case Value::eValueTypeFileAddress:
192 case Value::eValueTypeLoadAddress:
193 case Value::eValueTypeHostAddress:
194 // The DWARF expression result was an address in the inferior
195 // process. If this variable is an aggregate type, we just need
196 // the address as the main value as all child variable objects
197 // will rely upon this location and add an offset and then read
198 // their own values as needed. If this variable is a simple
199 // type, we read all data for it into m_data.
200 // Make sure this type has a value before we try and read it
202 // If we have a file address, convert it to a load address if we can.
203 Process *process = exe_ctx.GetProcessPtr();
204 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
206 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
207 if (file_addr != LLDB_INVALID_ADDRESS)
209 SymbolContext var_sc;
210 variable->CalculateSymbolContext(&var_sc);
211 if (var_sc.module_sp)
213 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
216 Address so_addr(file_addr, objfile->GetSectionList());
217 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
218 if (load_addr != LLDB_INVALID_ADDRESS)
220 m_value.SetValueType(Value::eValueTypeLoadAddress);
221 m_value.GetScalar() = load_addr;
228 if (GetClangType().IsAggregateType())
230 // this value object represents an aggregate type whose
231 // children have values, but this object does not. So we
232 // say we are changed if our location has changed.
233 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
237 // Copy the Value and set the context to use our Variable
238 // so it can extract read its value into m_data appropriately
239 Value value(m_value);
240 value.SetContext(Value::eContextTypeVariable, variable);
241 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
246 SetValueIsValid (m_error.Success());
250 // could not find location, won't allow editing
251 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
254 return m_error.Success();
260 ValueObjectVariable::IsInScope ()
262 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
263 if (exe_ctx_ref.HasFrameRef())
265 ExecutionContext exe_ctx (exe_ctx_ref);
266 StackFrame *frame = exe_ctx.GetFramePtr();
269 return m_variable_sp->IsInScope (frame);
273 // This ValueObject had a frame at one time, but now we
274 // can't locate it, so return false since we probably aren't
279 // We have a variable that wasn't tied to a frame, which
280 // means it is a global and is always in scope.
286 ValueObjectVariable::GetModule()
290 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
293 return sc_scope->CalculateSymbolContextModule();
296 return lldb::ModuleSP();
300 ValueObjectVariable::GetSymbolContextScope()
303 return m_variable_sp->GetSymbolContextScope();
308 ValueObjectVariable::GetDeclaration (Declaration &decl)
312 decl = m_variable_sp->GetDeclaration();
319 ValueObjectVariable::GetLocationAsCString ()
321 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
322 return GetLocationAsCStringImpl(m_resolved_value,
325 return ValueObject::GetLocationAsCString();
329 ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
331 if (!UpdateValueIfNeeded())
333 error.SetErrorString("unable to update value before writing");
337 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
339 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
340 ExecutionContext exe_ctx(GetExecutionContextRef());
341 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
342 RegisterValue reg_value;
343 if (!reg_info || !reg_ctx)
345 error.SetErrorString("unable to retrieve register info");
348 error = reg_value.SetValueFromCString(reg_info, value_str);
351 if (reg_ctx->WriteRegister (reg_info, reg_value))
358 error.SetErrorString("unable to write back to register");
363 return ValueObject::SetValueFromCString(value_str, error);
367 ValueObjectVariable::SetData (DataExtractor &data, Error &error)
369 if (!UpdateValueIfNeeded())
371 error.SetErrorString("unable to update value before writing");
375 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
377 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
378 ExecutionContext exe_ctx(GetExecutionContextRef());
379 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
380 RegisterValue reg_value;
381 if (!reg_info || !reg_ctx)
383 error.SetErrorString("unable to retrieve register info");
386 error = reg_value.SetValueFromData(reg_info, data, 0, true);
389 if (reg_ctx->WriteRegister (reg_info, reg_value))
396 error.SetErrorString("unable to write back to register");
401 return ValueObject::SetData(data, error);