15ffd83dbSDimitry Andric //===-- ValueObjectMemory.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Core/ValueObjectMemory.h"
100b57cec5SDimitry Andric #include "lldb/Core/Value.h"
110b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
120b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
130b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
140b57cec5SDimitry Andric #include "lldb/Target/Target.h"
150b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
170b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
180b57cec5SDimitry Andric #include "lldb/lldb-types.h"
190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
200b57cec5SDimitry Andric 
21fe6060f1SDimitry Andric #include <cassert>
220b57cec5SDimitry Andric #include <memory>
23bdd1243dSDimitry Andric #include <optional>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace lldb_private {
260b57cec5SDimitry Andric class ExecutionContextScope;
270b57cec5SDimitry Andric }
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric using namespace lldb;
300b57cec5SDimitry Andric using namespace lldb_private;
310b57cec5SDimitry Andric 
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)320b57cec5SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
330b57cec5SDimitry Andric                                         llvm::StringRef name,
340b57cec5SDimitry Andric                                         const Address &address,
350b57cec5SDimitry Andric                                         lldb::TypeSP &type_sp) {
365ffd83dbSDimitry Andric   auto manager_sp = ValueObjectManager::Create();
375ffd83dbSDimitry Andric   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
385ffd83dbSDimitry Andric       ->GetSP();
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,const CompilerType & ast_type)410b57cec5SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
420b57cec5SDimitry Andric                                         llvm::StringRef name,
430b57cec5SDimitry Andric                                         const Address &address,
440b57cec5SDimitry Andric                                         const CompilerType &ast_type) {
455ffd83dbSDimitry Andric   auto manager_sp = ValueObjectManager::Create();
465ffd83dbSDimitry Andric   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
475ffd83dbSDimitry Andric                                 ast_type))
485ffd83dbSDimitry Andric       ->GetSP();
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)510b57cec5SDimitry Andric ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
525ffd83dbSDimitry Andric                                      ValueObjectManager &manager,
530b57cec5SDimitry Andric                                      llvm::StringRef name,
540b57cec5SDimitry Andric                                      const Address &address,
550b57cec5SDimitry Andric                                      lldb::TypeSP &type_sp)
565ffd83dbSDimitry Andric     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
570b57cec5SDimitry Andric       m_compiler_type() {
580b57cec5SDimitry Andric   // Do not attempt to construct one of these objects with no variable!
590b57cec5SDimitry Andric   assert(m_type_sp.get() != nullptr);
600b57cec5SDimitry Andric   SetName(ConstString(name));
61fe6060f1SDimitry Andric   m_value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
620b57cec5SDimitry Andric   TargetSP target_sp(GetTargetSP());
630b57cec5SDimitry Andric   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
640b57cec5SDimitry Andric   if (load_address != LLDB_INVALID_ADDRESS) {
65fe6060f1SDimitry Andric     m_value.SetValueType(Value::ValueType::LoadAddress);
660b57cec5SDimitry Andric     m_value.GetScalar() = load_address;
670b57cec5SDimitry Andric   } else {
680b57cec5SDimitry Andric     lldb::addr_t file_address = m_address.GetFileAddress();
690b57cec5SDimitry Andric     if (file_address != LLDB_INVALID_ADDRESS) {
70fe6060f1SDimitry Andric       m_value.SetValueType(Value::ValueType::FileAddress);
710b57cec5SDimitry Andric       m_value.GetScalar() = file_address;
720b57cec5SDimitry Andric     } else {
730b57cec5SDimitry Andric       m_value.GetScalar() = m_address.GetOffset();
74fe6060f1SDimitry Andric       m_value.SetValueType(Value::ValueType::Scalar);
750b57cec5SDimitry Andric     }
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,const CompilerType & ast_type)790b57cec5SDimitry Andric ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
805ffd83dbSDimitry Andric                                      ValueObjectManager &manager,
810b57cec5SDimitry Andric                                      llvm::StringRef name,
820b57cec5SDimitry Andric                                      const Address &address,
830b57cec5SDimitry Andric                                      const CompilerType &ast_type)
845ffd83dbSDimitry Andric     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
850b57cec5SDimitry Andric       m_compiler_type(ast_type) {
860b57cec5SDimitry Andric   // Do not attempt to construct one of these objects with no variable!
87bdd1243dSDimitry Andric   assert(m_compiler_type.IsValid());
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   TargetSP target_sp(GetTargetSP());
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   SetName(ConstString(name));
920b57cec5SDimitry Andric   m_value.SetCompilerType(m_compiler_type);
930b57cec5SDimitry Andric   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
940b57cec5SDimitry Andric   if (load_address != LLDB_INVALID_ADDRESS) {
95fe6060f1SDimitry Andric     m_value.SetValueType(Value::ValueType::LoadAddress);
960b57cec5SDimitry Andric     m_value.GetScalar() = load_address;
970b57cec5SDimitry Andric   } else {
980b57cec5SDimitry Andric     lldb::addr_t file_address = m_address.GetFileAddress();
990b57cec5SDimitry Andric     if (file_address != LLDB_INVALID_ADDRESS) {
100fe6060f1SDimitry Andric       m_value.SetValueType(Value::ValueType::FileAddress);
1010b57cec5SDimitry Andric       m_value.GetScalar() = file_address;
1020b57cec5SDimitry Andric     } else {
1030b57cec5SDimitry Andric       m_value.GetScalar() = m_address.GetOffset();
104fe6060f1SDimitry Andric       m_value.SetValueType(Value::ValueType::Scalar);
1050b57cec5SDimitry Andric     }
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
109fe6060f1SDimitry Andric ValueObjectMemory::~ValueObjectMemory() = default;
1100b57cec5SDimitry Andric 
GetCompilerTypeImpl()1110b57cec5SDimitry Andric CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
1120b57cec5SDimitry Andric   if (m_type_sp)
1130b57cec5SDimitry Andric     return m_type_sp->GetForwardCompilerType();
1140b57cec5SDimitry Andric   return m_compiler_type;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
GetTypeName()1170b57cec5SDimitry Andric ConstString ValueObjectMemory::GetTypeName() {
1180b57cec5SDimitry Andric   if (m_type_sp)
1190b57cec5SDimitry Andric     return m_type_sp->GetName();
1205ffd83dbSDimitry Andric   return m_compiler_type.GetTypeName();
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
GetDisplayTypeName()1230b57cec5SDimitry Andric ConstString ValueObjectMemory::GetDisplayTypeName() {
1240b57cec5SDimitry Andric   if (m_type_sp)
1250b57cec5SDimitry Andric     return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
1260b57cec5SDimitry Andric   return m_compiler_type.GetDisplayTypeName();
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
CalculateNumChildren(uint32_t max)1290b57cec5SDimitry Andric size_t ValueObjectMemory::CalculateNumChildren(uint32_t max) {
1300b57cec5SDimitry Andric   if (m_type_sp) {
1310b57cec5SDimitry Andric     auto child_count = m_type_sp->GetNumChildren(true);
1320b57cec5SDimitry Andric     return child_count <= max ? child_count : max;
1330b57cec5SDimitry Andric   }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1360b57cec5SDimitry Andric   const bool omit_empty_base_classes = true;
1370b57cec5SDimitry Andric   auto child_count =
1380b57cec5SDimitry Andric       m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
1390b57cec5SDimitry Andric   return child_count <= max ? child_count : max;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
GetByteSize()142bdd1243dSDimitry Andric std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
143e8d8bef9SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1440b57cec5SDimitry Andric   if (m_type_sp)
145e8d8bef9SDimitry Andric     return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
146e8d8bef9SDimitry Andric   return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
GetValueType() const1490b57cec5SDimitry Andric lldb::ValueType ValueObjectMemory::GetValueType() const {
1500b57cec5SDimitry Andric   // RETHINK: Should this be inherited from somewhere?
1510b57cec5SDimitry Andric   return lldb::eValueTypeVariableGlobal;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
UpdateValue()1540b57cec5SDimitry Andric bool ValueObjectMemory::UpdateValue() {
1550b57cec5SDimitry Andric   SetValueIsValid(false);
1560b57cec5SDimitry Andric   m_error.Clear();
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   Target *target = exe_ctx.GetTargetPtr();
1610b57cec5SDimitry Andric   if (target) {
1620b57cec5SDimitry Andric     m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
1630b57cec5SDimitry Andric     m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   Value old_value(m_value);
1670b57cec5SDimitry Andric   if (m_address.IsValid()) {
1680b57cec5SDimitry Andric     Value::ValueType value_type = m_value.GetValueType();
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric     switch (value_type) {
171fe6060f1SDimitry Andric     case Value::ValueType::Invalid:
172fe6060f1SDimitry Andric       m_error.SetErrorString("Invalid value");
173fe6060f1SDimitry Andric       return false;
174fe6060f1SDimitry Andric     case Value::ValueType::Scalar:
1750b57cec5SDimitry Andric       // The variable value is in the Scalar value inside the m_value. We can
1760b57cec5SDimitry Andric       // point our m_data right to it.
1779dba64beSDimitry Andric       m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
1780b57cec5SDimitry Andric       break;
1790b57cec5SDimitry Andric 
180fe6060f1SDimitry Andric     case Value::ValueType::FileAddress:
181fe6060f1SDimitry Andric     case Value::ValueType::LoadAddress:
182fe6060f1SDimitry Andric     case Value::ValueType::HostAddress:
1830b57cec5SDimitry Andric       // The DWARF expression result was an address in the inferior process. If
1840b57cec5SDimitry Andric       // this variable is an aggregate type, we just need the address as the
1850b57cec5SDimitry Andric       // main value as all child variable objects will rely upon this location
1860b57cec5SDimitry Andric       // and add an offset and then read their own values as needed. If this
1870b57cec5SDimitry Andric       // variable is a simple type, we read all data for it into m_data. Make
1880b57cec5SDimitry Andric       // sure this type has a value before we try and read it
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric       // If we have a file address, convert it to a load address if we can.
191fe6060f1SDimitry Andric       if (value_type == Value::ValueType::FileAddress &&
1920b57cec5SDimitry Andric           exe_ctx.GetProcessPtr()) {
1930b57cec5SDimitry Andric         lldb::addr_t load_addr = m_address.GetLoadAddress(target);
1940b57cec5SDimitry Andric         if (load_addr != LLDB_INVALID_ADDRESS) {
195fe6060f1SDimitry Andric           m_value.SetValueType(Value::ValueType::LoadAddress);
1960b57cec5SDimitry Andric           m_value.GetScalar() = load_addr;
1970b57cec5SDimitry Andric         }
1980b57cec5SDimitry Andric       }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric       if (!CanProvideValue()) {
2010b57cec5SDimitry Andric         // this value object represents an aggregate type whose children have
2020b57cec5SDimitry Andric         // values, but this object does not. So we say we are changed if our
2030b57cec5SDimitry Andric         // location has changed.
2040b57cec5SDimitry Andric         SetValueDidChange(value_type != old_value.GetValueType() ||
2050b57cec5SDimitry Andric                           m_value.GetScalar() != old_value.GetScalar());
2060b57cec5SDimitry Andric       } else {
2070b57cec5SDimitry Andric         // Copy the Value and set the context to use our Variable so it can
2080b57cec5SDimitry Andric         // extract read its value into m_data appropriately
2090b57cec5SDimitry Andric         Value value(m_value);
2100b57cec5SDimitry Andric         if (m_type_sp)
211fe6060f1SDimitry Andric           value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
2120b57cec5SDimitry Andric         else {
2130b57cec5SDimitry Andric           value.SetCompilerType(m_compiler_type);
2140b57cec5SDimitry Andric         }
2150b57cec5SDimitry Andric 
2169dba64beSDimitry Andric         m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
2170b57cec5SDimitry Andric       }
2180b57cec5SDimitry Andric       break;
2190b57cec5SDimitry Andric     }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     SetValueIsValid(m_error.Success());
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric   return m_error.Success();
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
IsInScope()2260b57cec5SDimitry Andric bool ValueObjectMemory::IsInScope() {
2270b57cec5SDimitry Andric   // FIXME: Maybe try to read the memory address, and if that works, then
2280b57cec5SDimitry Andric   // we are in scope?
2290b57cec5SDimitry Andric   return true;
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric 
GetModule()2320b57cec5SDimitry Andric lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
233