1 //===-- ValueObjectMemory.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/ValueObjectMemory.h"
15 // Other libraries and framework includes
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/ValueObjectList.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
34 using namespace lldb_private;
37 ValueObjectMemory::Create (ExecutionContextScope *exe_scope,
39 const Address &address,
40 lldb::TypeSP &type_sp)
42 return (new ValueObjectMemory (exe_scope, name, address, type_sp))->GetSP();
46 ValueObjectMemory::Create (ExecutionContextScope *exe_scope,
48 const Address &address,
49 const CompilerType &ast_type)
51 return (new ValueObjectMemory (exe_scope, name, address, ast_type))->GetSP();
54 ValueObjectMemory::ValueObjectMemory (ExecutionContextScope *exe_scope,
56 const Address &address,
57 lldb::TypeSP &type_sp) :
58 ValueObject(exe_scope),
63 // Do not attempt to construct one of these objects with no variable!
64 assert (m_type_sp.get() != NULL);
65 SetName (ConstString(name));
66 m_value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
67 TargetSP target_sp (GetTargetSP());
68 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
69 if (load_address != LLDB_INVALID_ADDRESS)
71 m_value.SetValueType(Value::eValueTypeLoadAddress);
72 m_value.GetScalar() = load_address;
76 lldb::addr_t file_address = m_address.GetFileAddress();
77 if (file_address != LLDB_INVALID_ADDRESS)
79 m_value.SetValueType(Value::eValueTypeFileAddress);
80 m_value.GetScalar() = file_address;
84 m_value.GetScalar() = m_address.GetOffset();
85 m_value.SetValueType (Value::eValueTypeScalar);
90 ValueObjectMemory::ValueObjectMemory (ExecutionContextScope *exe_scope,
92 const Address &address,
93 const CompilerType &ast_type) :
94 ValueObject(exe_scope),
97 m_compiler_type(ast_type)
99 // Do not attempt to construct one of these objects with no variable!
100 assert (m_compiler_type.GetTypeSystem());
101 assert (m_compiler_type.GetOpaqueQualType());
103 TargetSP target_sp (GetTargetSP());
105 SetName (ConstString(name));
106 // m_value.SetContext(Value::eContextTypeClangType, m_compiler_type.GetOpaqueQualType());
107 m_value.SetCompilerType(m_compiler_type);
108 lldb::addr_t load_address = m_address.GetLoadAddress (target_sp.get());
109 if (load_address != LLDB_INVALID_ADDRESS)
111 m_value.SetValueType(Value::eValueTypeLoadAddress);
112 m_value.GetScalar() = load_address;
116 lldb::addr_t file_address = m_address.GetFileAddress();
117 if (file_address != LLDB_INVALID_ADDRESS)
119 m_value.SetValueType(Value::eValueTypeFileAddress);
120 m_value.GetScalar() = file_address;
124 m_value.GetScalar() = m_address.GetOffset();
125 m_value.SetValueType (Value::eValueTypeScalar);
130 ValueObjectMemory::~ValueObjectMemory()
135 ValueObjectMemory::GetCompilerTypeImpl ()
138 return m_type_sp->GetForwardCompilerType ();
139 return m_compiler_type;
143 ValueObjectMemory::GetTypeName()
146 return m_type_sp->GetName();
147 return m_compiler_type.GetConstTypeName();
151 ValueObjectMemory::GetDisplayTypeName()
154 return m_type_sp->GetForwardCompilerType ().GetDisplayTypeName();
155 return m_compiler_type.GetDisplayTypeName();
159 ValueObjectMemory::CalculateNumChildren(uint32_t max)
163 auto child_count = m_type_sp->GetNumChildren(true);
164 return child_count <= max ? child_count : max;
167 const bool omit_empty_base_classes = true;
168 auto child_count = m_compiler_type.GetNumChildren (omit_empty_base_classes);
169 return child_count <= max ? child_count : max;
173 ValueObjectMemory::GetByteSize()
176 return m_type_sp->GetByteSize();
177 return m_compiler_type.GetByteSize (nullptr);
181 ValueObjectMemory::GetValueType() const
183 // RETHINK: Should this be inherited from somewhere?
184 return lldb::eValueTypeVariableGlobal;
188 ValueObjectMemory::UpdateValue ()
190 SetValueIsValid (false);
193 ExecutionContext exe_ctx (GetExecutionContextRef());
195 Target *target = exe_ctx.GetTargetPtr();
198 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
199 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
202 Value old_value(m_value);
203 if (m_address.IsValid())
205 Value::ValueType value_type = m_value.GetValueType();
210 assert(!"Unhandled expression result value kind...");
213 case Value::eValueTypeScalar:
214 // The variable value is in the Scalar value inside the m_value.
215 // We can point our m_data right to it.
216 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
219 case Value::eValueTypeFileAddress:
220 case Value::eValueTypeLoadAddress:
221 case Value::eValueTypeHostAddress:
222 // The DWARF expression result was an address in the inferior
223 // process. If this variable is an aggregate type, we just need
224 // the address as the main value as all child variable objects
225 // will rely upon this location and add an offset and then read
226 // their own values as needed. If this variable is a simple
227 // type, we read all data for it into m_data.
228 // Make sure this type has a value before we try and read it
230 // If we have a file address, convert it to a load address if we can.
231 if (value_type == Value::eValueTypeFileAddress && exe_ctx.GetProcessPtr())
233 lldb::addr_t load_addr = m_address.GetLoadAddress(target);
234 if (load_addr != LLDB_INVALID_ADDRESS)
236 m_value.SetValueType(Value::eValueTypeLoadAddress);
237 m_value.GetScalar() = load_addr;
241 if (!CanProvideValue())
243 // this value object represents an aggregate type whose
244 // children have values, but this object does not. So we
245 // say we are changed if our location has changed.
246 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
250 // Copy the Value and set the context to use our Variable
251 // so it can extract read its value into m_data appropriately
252 Value value(m_value);
254 value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
257 //value.SetContext(Value::eContextTypeClangType, m_compiler_type.GetOpaqueQualType());
258 value.SetCompilerType(m_compiler_type);
261 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
266 SetValueIsValid (m_error.Success());
268 return m_error.Success();
274 ValueObjectMemory::IsInScope ()
276 // FIXME: Maybe try to read the memory address, and if that works, then
283 ValueObjectMemory::GetModule()
285 return m_address.GetModule();