1cfca06d7SDimitry Andric //===-- ValueObjectMemory.cpp ---------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Core/ValueObjectMemory.h"
10f034231aSEd Maste #include "lldb/Core/Value.h"
11f034231aSEd Maste #include "lldb/Core/ValueObject.h"
12f034231aSEd Maste #include "lldb/Symbol/Type.h"
13f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
14f034231aSEd Maste #include "lldb/Target/Target.h"
1594994d37SDimitry Andric #include "lldb/Utility/DataExtractor.h"
1694994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
1794994d37SDimitry Andric #include "lldb/Utility/Status.h"
1894994d37SDimitry Andric #include "lldb/lldb-types.h"
1994994d37SDimitry Andric #include "llvm/Support/ErrorHandling.h"
2074a628f7SDimitry Andric
21344a3780SDimitry Andric #include <cassert>
2294994d37SDimitry Andric #include <memory>
23e3b55780SDimitry Andric #include <optional>
2474a628f7SDimitry Andric
2574a628f7SDimitry Andric namespace lldb_private {
2674a628f7SDimitry Andric class ExecutionContextScope;
2774a628f7SDimitry Andric }
28f034231aSEd Maste
29f034231aSEd Maste using namespace lldb;
30f034231aSEd Maste using namespace lldb_private;
31f034231aSEd Maste
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)3214f1b3e8SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
3314f1b3e8SDimitry Andric llvm::StringRef name,
34f034231aSEd Maste const Address &address,
3514f1b3e8SDimitry Andric lldb::TypeSP &type_sp) {
36cfca06d7SDimitry Andric auto manager_sp = ValueObjectManager::Create();
37cfca06d7SDimitry Andric return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
38cfca06d7SDimitry Andric ->GetSP();
39f034231aSEd Maste }
40f034231aSEd Maste
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,const CompilerType & ast_type)4114f1b3e8SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
4214f1b3e8SDimitry Andric llvm::StringRef name,
43f034231aSEd Maste const Address &address,
4414f1b3e8SDimitry Andric const CompilerType &ast_type) {
45cfca06d7SDimitry Andric auto manager_sp = ValueObjectManager::Create();
46cfca06d7SDimitry Andric return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
47cfca06d7SDimitry Andric ast_type))
48cfca06d7SDimitry Andric ->GetSP();
49f034231aSEd Maste }
50f034231aSEd Maste
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)51f034231aSEd Maste ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
52cfca06d7SDimitry Andric ValueObjectManager &manager,
5314f1b3e8SDimitry Andric llvm::StringRef name,
54f034231aSEd Maste const Address &address,
5514f1b3e8SDimitry Andric lldb::TypeSP &type_sp)
56cfca06d7SDimitry Andric : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
5714f1b3e8SDimitry Andric m_compiler_type() {
58f034231aSEd Maste // Do not attempt to construct one of these objects with no variable!
595f29bb8aSDimitry Andric assert(m_type_sp.get() != nullptr);
60f034231aSEd Maste SetName(ConstString(name));
61344a3780SDimitry Andric m_value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
62f034231aSEd Maste TargetSP target_sp(GetTargetSP());
63f034231aSEd Maste lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
6414f1b3e8SDimitry Andric if (load_address != LLDB_INVALID_ADDRESS) {
65344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::LoadAddress);
66f034231aSEd Maste m_value.GetScalar() = load_address;
6714f1b3e8SDimitry Andric } else {
68f034231aSEd Maste lldb::addr_t file_address = m_address.GetFileAddress();
6914f1b3e8SDimitry Andric if (file_address != LLDB_INVALID_ADDRESS) {
70344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::FileAddress);
71f034231aSEd Maste m_value.GetScalar() = file_address;
7214f1b3e8SDimitry Andric } else {
73f034231aSEd Maste m_value.GetScalar() = m_address.GetOffset();
74344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::Scalar);
75f034231aSEd Maste }
76f034231aSEd Maste }
77f034231aSEd Maste }
78f034231aSEd Maste
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,const CompilerType & ast_type)79f034231aSEd Maste ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
80cfca06d7SDimitry Andric ValueObjectManager &manager,
8114f1b3e8SDimitry Andric llvm::StringRef name,
82f034231aSEd Maste const Address &address,
8314f1b3e8SDimitry Andric const CompilerType &ast_type)
84cfca06d7SDimitry Andric : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
8514f1b3e8SDimitry Andric m_compiler_type(ast_type) {
86f034231aSEd Maste // Do not attempt to construct one of these objects with no variable!
87e3b55780SDimitry Andric assert(m_compiler_type.IsValid());
88f034231aSEd Maste
89f034231aSEd Maste TargetSP target_sp(GetTargetSP());
90f034231aSEd Maste
91f034231aSEd Maste SetName(ConstString(name));
92e81d9d49SDimitry Andric m_value.SetCompilerType(m_compiler_type);
93f034231aSEd Maste lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
9414f1b3e8SDimitry Andric if (load_address != LLDB_INVALID_ADDRESS) {
95344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::LoadAddress);
96f034231aSEd Maste m_value.GetScalar() = load_address;
9714f1b3e8SDimitry Andric } else {
98f034231aSEd Maste lldb::addr_t file_address = m_address.GetFileAddress();
9914f1b3e8SDimitry Andric if (file_address != LLDB_INVALID_ADDRESS) {
100344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::FileAddress);
101f034231aSEd Maste m_value.GetScalar() = file_address;
10214f1b3e8SDimitry Andric } else {
103f034231aSEd Maste m_value.GetScalar() = m_address.GetOffset();
104344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::Scalar);
105f034231aSEd Maste }
106f034231aSEd Maste }
107f034231aSEd Maste }
108f034231aSEd Maste
109344a3780SDimitry Andric ValueObjectMemory::~ValueObjectMemory() = default;
110f034231aSEd Maste
GetCompilerTypeImpl()11114f1b3e8SDimitry Andric CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
112f034231aSEd Maste if (m_type_sp)
113e81d9d49SDimitry Andric return m_type_sp->GetForwardCompilerType();
114e81d9d49SDimitry Andric return m_compiler_type;
115f034231aSEd Maste }
116f034231aSEd Maste
GetTypeName()11714f1b3e8SDimitry Andric ConstString ValueObjectMemory::GetTypeName() {
118f034231aSEd Maste if (m_type_sp)
119f034231aSEd Maste return m_type_sp->GetName();
120cfca06d7SDimitry Andric return m_compiler_type.GetTypeName();
121f034231aSEd Maste }
122f034231aSEd Maste
GetDisplayTypeName()12314f1b3e8SDimitry Andric ConstString ValueObjectMemory::GetDisplayTypeName() {
1240cac4ca3SEd Maste if (m_type_sp)
125e81d9d49SDimitry Andric return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
126e81d9d49SDimitry Andric return m_compiler_type.GetDisplayTypeName();
1270cac4ca3SEd Maste }
1280cac4ca3SEd Maste
CalculateNumChildren(uint32_t max)129ac9a064cSDimitry Andric llvm::Expected<uint32_t> ValueObjectMemory::CalculateNumChildren(uint32_t max) {
13014f1b3e8SDimitry Andric if (m_type_sp) {
131e81d9d49SDimitry Andric auto child_count = m_type_sp->GetNumChildren(true);
132ac9a064cSDimitry Andric if (!child_count)
133ac9a064cSDimitry Andric return child_count;
134ac9a064cSDimitry Andric return *child_count <= max ? *child_count : max;
135e81d9d49SDimitry Andric }
136e81d9d49SDimitry Andric
13794994d37SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
138f034231aSEd Maste const bool omit_empty_base_classes = true;
13994994d37SDimitry Andric auto child_count =
14094994d37SDimitry Andric m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
141ac9a064cSDimitry Andric if (!child_count)
142ac9a064cSDimitry Andric return child_count;
143ac9a064cSDimitry Andric return *child_count <= max ? *child_count : max;
144f034231aSEd Maste }
145f034231aSEd Maste
GetByteSize()146e3b55780SDimitry Andric std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
147b60736ecSDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
148f034231aSEd Maste if (m_type_sp)
149b60736ecSDimitry Andric return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
150b60736ecSDimitry Andric return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
151f034231aSEd Maste }
152f034231aSEd Maste
GetValueType() const15314f1b3e8SDimitry Andric lldb::ValueType ValueObjectMemory::GetValueType() const {
154f034231aSEd Maste // RETHINK: Should this be inherited from somewhere?
155f034231aSEd Maste return lldb::eValueTypeVariableGlobal;
156f034231aSEd Maste }
157f034231aSEd Maste
UpdateValue()15814f1b3e8SDimitry Andric bool ValueObjectMemory::UpdateValue() {
159f034231aSEd Maste SetValueIsValid(false);
160f034231aSEd Maste m_error.Clear();
161f034231aSEd Maste
162f034231aSEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
163f034231aSEd Maste
164f034231aSEd Maste Target *target = exe_ctx.GetTargetPtr();
16514f1b3e8SDimitry Andric if (target) {
166f034231aSEd Maste m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
167f034231aSEd Maste m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
168f034231aSEd Maste }
169f034231aSEd Maste
170f034231aSEd Maste Value old_value(m_value);
17114f1b3e8SDimitry Andric if (m_address.IsValid()) {
172f034231aSEd Maste Value::ValueType value_type = m_value.GetValueType();
173f034231aSEd Maste
17414f1b3e8SDimitry Andric switch (value_type) {
175344a3780SDimitry Andric case Value::ValueType::Invalid:
176344a3780SDimitry Andric m_error.SetErrorString("Invalid value");
177344a3780SDimitry Andric return false;
178344a3780SDimitry Andric case Value::ValueType::Scalar:
179f73363f1SDimitry Andric // The variable value is in the Scalar value inside the m_value. We can
180f73363f1SDimitry Andric // point our m_data right to it.
181ead24645SDimitry Andric m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
182f034231aSEd Maste break;
183f034231aSEd Maste
184344a3780SDimitry Andric case Value::ValueType::FileAddress:
185344a3780SDimitry Andric case Value::ValueType::LoadAddress:
186344a3780SDimitry Andric case Value::ValueType::HostAddress:
187f73363f1SDimitry Andric // The DWARF expression result was an address in the inferior process. If
188f73363f1SDimitry Andric // this variable is an aggregate type, we just need the address as the
189f73363f1SDimitry Andric // main value as all child variable objects will rely upon this location
190f73363f1SDimitry Andric // and add an offset and then read their own values as needed. If this
191f73363f1SDimitry Andric // variable is a simple type, we read all data for it into m_data. Make
192f73363f1SDimitry Andric // sure this type has a value before we try and read it
193f034231aSEd Maste
194f034231aSEd Maste // If we have a file address, convert it to a load address if we can.
195344a3780SDimitry Andric if (value_type == Value::ValueType::FileAddress &&
19614f1b3e8SDimitry Andric exe_ctx.GetProcessPtr()) {
197f034231aSEd Maste lldb::addr_t load_addr = m_address.GetLoadAddress(target);
19814f1b3e8SDimitry Andric if (load_addr != LLDB_INVALID_ADDRESS) {
199344a3780SDimitry Andric m_value.SetValueType(Value::ValueType::LoadAddress);
200f034231aSEd Maste m_value.GetScalar() = load_addr;
201f034231aSEd Maste }
202f034231aSEd Maste }
203f034231aSEd Maste
20414f1b3e8SDimitry Andric if (!CanProvideValue()) {
205f73363f1SDimitry Andric // this value object represents an aggregate type whose children have
206f73363f1SDimitry Andric // values, but this object does not. So we say we are changed if our
207f73363f1SDimitry Andric // location has changed.
20814f1b3e8SDimitry Andric SetValueDidChange(value_type != old_value.GetValueType() ||
20914f1b3e8SDimitry Andric m_value.GetScalar() != old_value.GetScalar());
21014f1b3e8SDimitry Andric } else {
211f73363f1SDimitry Andric // Copy the Value and set the context to use our Variable so it can
212f73363f1SDimitry Andric // extract read its value into m_data appropriately
213f034231aSEd Maste Value value(m_value);
214f034231aSEd Maste if (m_type_sp)
215344a3780SDimitry Andric value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
21614f1b3e8SDimitry Andric else {
217e81d9d49SDimitry Andric value.SetCompilerType(m_compiler_type);
218f034231aSEd Maste }
219f034231aSEd Maste
220ead24645SDimitry Andric m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
221f034231aSEd Maste }
222f034231aSEd Maste break;
223f034231aSEd Maste }
224f034231aSEd Maste
225f034231aSEd Maste SetValueIsValid(m_error.Success());
226f034231aSEd Maste }
227f034231aSEd Maste return m_error.Success();
228f034231aSEd Maste }
229f034231aSEd Maste
IsInScope()23014f1b3e8SDimitry Andric bool ValueObjectMemory::IsInScope() {
231f034231aSEd Maste // FIXME: Maybe try to read the memory address, and if that works, then
232f034231aSEd Maste // we are in scope?
233f034231aSEd Maste return true;
234f034231aSEd Maste }
235f034231aSEd Maste
GetModule()23614f1b3e8SDimitry Andric lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
237