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 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ValueObjectMemory.h"
11 #include "lldb/Core/Scalar.h" // for Scalar, operator!=
12 #include "lldb/Core/Value.h"
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Symbol/Type.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/DataExtractor.h" // for DataExtractor
18 #include "lldb/Utility/Status.h" // for Status
19 #include "lldb/lldb-types.h" // for addr_t
20 #include "llvm/Support/ErrorHandling.h" // for llvm_unreachable
22 #include <assert.h> // for assert
23 #include <memory> // for shared_ptr
25 namespace lldb_private {
26 class ExecutionContextScope;
30 using namespace lldb_private;
32 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
34 const Address &address,
35 lldb::TypeSP &type_sp) {
36 return (new ValueObjectMemory(exe_scope, name, address, type_sp))->GetSP();
39 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
41 const Address &address,
42 const CompilerType &ast_type) {
43 return (new ValueObjectMemory(exe_scope, name, address, ast_type))->GetSP();
46 ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
48 const Address &address,
49 lldb::TypeSP &type_sp)
50 : ValueObject(exe_scope), m_address(address), m_type_sp(type_sp),
52 // Do not attempt to construct one of these objects with no variable!
53 assert(m_type_sp.get() != NULL);
54 SetName(ConstString(name));
55 m_value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
56 TargetSP target_sp(GetTargetSP());
57 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
58 if (load_address != LLDB_INVALID_ADDRESS) {
59 m_value.SetValueType(Value::eValueTypeLoadAddress);
60 m_value.GetScalar() = load_address;
62 lldb::addr_t file_address = m_address.GetFileAddress();
63 if (file_address != LLDB_INVALID_ADDRESS) {
64 m_value.SetValueType(Value::eValueTypeFileAddress);
65 m_value.GetScalar() = file_address;
67 m_value.GetScalar() = m_address.GetOffset();
68 m_value.SetValueType(Value::eValueTypeScalar);
73 ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
75 const Address &address,
76 const CompilerType &ast_type)
77 : ValueObject(exe_scope), m_address(address), m_type_sp(),
78 m_compiler_type(ast_type) {
79 // Do not attempt to construct one of these objects with no variable!
80 assert(m_compiler_type.GetTypeSystem());
81 assert(m_compiler_type.GetOpaqueQualType());
83 TargetSP target_sp(GetTargetSP());
85 SetName(ConstString(name));
86 // m_value.SetContext(Value::eContextTypeClangType,
87 // m_compiler_type.GetOpaqueQualType());
88 m_value.SetCompilerType(m_compiler_type);
89 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
90 if (load_address != LLDB_INVALID_ADDRESS) {
91 m_value.SetValueType(Value::eValueTypeLoadAddress);
92 m_value.GetScalar() = load_address;
94 lldb::addr_t file_address = m_address.GetFileAddress();
95 if (file_address != LLDB_INVALID_ADDRESS) {
96 m_value.SetValueType(Value::eValueTypeFileAddress);
97 m_value.GetScalar() = file_address;
99 m_value.GetScalar() = m_address.GetOffset();
100 m_value.SetValueType(Value::eValueTypeScalar);
105 ValueObjectMemory::~ValueObjectMemory() {}
107 CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
109 return m_type_sp->GetForwardCompilerType();
110 return m_compiler_type;
113 ConstString ValueObjectMemory::GetTypeName() {
115 return m_type_sp->GetName();
116 return m_compiler_type.GetConstTypeName();
119 ConstString ValueObjectMemory::GetDisplayTypeName() {
121 return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
122 return m_compiler_type.GetDisplayTypeName();
125 size_t ValueObjectMemory::CalculateNumChildren(uint32_t max) {
127 auto child_count = m_type_sp->GetNumChildren(true);
128 return child_count <= max ? child_count : max;
131 const bool omit_empty_base_classes = true;
132 auto child_count = m_compiler_type.GetNumChildren(omit_empty_base_classes);
133 return child_count <= max ? child_count : max;
136 uint64_t ValueObjectMemory::GetByteSize() {
138 return m_type_sp->GetByteSize();
139 return m_compiler_type.GetByteSize(nullptr);
142 lldb::ValueType ValueObjectMemory::GetValueType() const {
143 // RETHINK: Should this be inherited from somewhere?
144 return lldb::eValueTypeVariableGlobal;
147 bool ValueObjectMemory::UpdateValue() {
148 SetValueIsValid(false);
151 ExecutionContext exe_ctx(GetExecutionContextRef());
153 Target *target = exe_ctx.GetTargetPtr();
155 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
156 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
159 Value old_value(m_value);
160 if (m_address.IsValid()) {
161 Value::ValueType value_type = m_value.GetValueType();
163 switch (value_type) {
165 llvm_unreachable("Unhandled expression result value kind...");
167 case Value::eValueTypeScalar:
168 // The variable value is in the Scalar value inside the m_value.
169 // We can point our m_data right to it.
170 m_error = m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
173 case Value::eValueTypeFileAddress:
174 case Value::eValueTypeLoadAddress:
175 case Value::eValueTypeHostAddress:
176 // The DWARF expression result was an address in the inferior
177 // process. If this variable is an aggregate type, we just need
178 // the address as the main value as all child variable objects
179 // will rely upon this location and add an offset and then read
180 // their own values as needed. If this variable is a simple
181 // type, we read all data for it into m_data.
182 // Make sure this type has a value before we try and read it
184 // If we have a file address, convert it to a load address if we can.
185 if (value_type == Value::eValueTypeFileAddress &&
186 exe_ctx.GetProcessPtr()) {
187 lldb::addr_t load_addr = m_address.GetLoadAddress(target);
188 if (load_addr != LLDB_INVALID_ADDRESS) {
189 m_value.SetValueType(Value::eValueTypeLoadAddress);
190 m_value.GetScalar() = load_addr;
194 if (!CanProvideValue()) {
195 // this value object represents an aggregate type whose
196 // children have values, but this object does not. So we
197 // say we are changed if our location has changed.
198 SetValueDidChange(value_type != old_value.GetValueType() ||
199 m_value.GetScalar() != old_value.GetScalar());
201 // Copy the Value and set the context to use our Variable
202 // so it can extract read its value into m_data appropriately
203 Value value(m_value);
205 value.SetContext(Value::eContextTypeLLDBType, m_type_sp.get());
207 // value.SetContext(Value::eContextTypeClangType,
208 // m_compiler_type.GetOpaqueQualType());
209 value.SetCompilerType(m_compiler_type);
212 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
217 SetValueIsValid(m_error.Success());
219 return m_error.Success();
222 bool ValueObjectMemory::IsInScope() {
223 // FIXME: Maybe try to read the memory address, and if that works, then
228 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }