xref: /freebsd-src/contrib/llvm-project/lldb/source/Core/ValueObjectMemory.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
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 
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 
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 
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 
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 
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 
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 
129*0fca6ea1SDimitry Andric llvm::Expected<uint32_t> ValueObjectMemory::CalculateNumChildren(uint32_t max) {
1300b57cec5SDimitry Andric   if (m_type_sp) {
1310b57cec5SDimitry Andric     auto child_count = m_type_sp->GetNumChildren(true);
132*0fca6ea1SDimitry Andric     if (!child_count)
133*0fca6ea1SDimitry Andric       return child_count;
134*0fca6ea1SDimitry Andric     return *child_count <= max ? *child_count : max;
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1380b57cec5SDimitry Andric   const bool omit_empty_base_classes = true;
1390b57cec5SDimitry Andric   auto child_count =
1400b57cec5SDimitry Andric       m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
141*0fca6ea1SDimitry Andric   if (!child_count)
142*0fca6ea1SDimitry Andric     return child_count;
143*0fca6ea1SDimitry Andric   return *child_count <= max ? *child_count : max;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
146bdd1243dSDimitry Andric std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
147e8d8bef9SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1480b57cec5SDimitry Andric   if (m_type_sp)
149e8d8bef9SDimitry Andric     return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
150e8d8bef9SDimitry Andric   return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric lldb::ValueType ValueObjectMemory::GetValueType() const {
1540b57cec5SDimitry Andric   // RETHINK: Should this be inherited from somewhere?
1550b57cec5SDimitry Andric   return lldb::eValueTypeVariableGlobal;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric bool ValueObjectMemory::UpdateValue() {
1590b57cec5SDimitry Andric   SetValueIsValid(false);
1600b57cec5SDimitry Andric   m_error.Clear();
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   Target *target = exe_ctx.GetTargetPtr();
1650b57cec5SDimitry Andric   if (target) {
1660b57cec5SDimitry Andric     m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
1670b57cec5SDimitry Andric     m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   Value old_value(m_value);
1710b57cec5SDimitry Andric   if (m_address.IsValid()) {
1720b57cec5SDimitry Andric     Value::ValueType value_type = m_value.GetValueType();
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     switch (value_type) {
175fe6060f1SDimitry Andric     case Value::ValueType::Invalid:
176fe6060f1SDimitry Andric       m_error.SetErrorString("Invalid value");
177fe6060f1SDimitry Andric       return false;
178fe6060f1SDimitry Andric     case Value::ValueType::Scalar:
1790b57cec5SDimitry Andric       // The variable value is in the Scalar value inside the m_value. We can
1800b57cec5SDimitry Andric       // point our m_data right to it.
1819dba64beSDimitry Andric       m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
1820b57cec5SDimitry Andric       break;
1830b57cec5SDimitry Andric 
184fe6060f1SDimitry Andric     case Value::ValueType::FileAddress:
185fe6060f1SDimitry Andric     case Value::ValueType::LoadAddress:
186fe6060f1SDimitry Andric     case Value::ValueType::HostAddress:
1870b57cec5SDimitry Andric       // The DWARF expression result was an address in the inferior process. If
1880b57cec5SDimitry Andric       // this variable is an aggregate type, we just need the address as the
1890b57cec5SDimitry Andric       // main value as all child variable objects will rely upon this location
1900b57cec5SDimitry Andric       // and add an offset and then read their own values as needed. If this
1910b57cec5SDimitry Andric       // variable is a simple type, we read all data for it into m_data. Make
1920b57cec5SDimitry Andric       // sure this type has a value before we try and read it
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric       // If we have a file address, convert it to a load address if we can.
195fe6060f1SDimitry Andric       if (value_type == Value::ValueType::FileAddress &&
1960b57cec5SDimitry Andric           exe_ctx.GetProcessPtr()) {
1970b57cec5SDimitry Andric         lldb::addr_t load_addr = m_address.GetLoadAddress(target);
1980b57cec5SDimitry Andric         if (load_addr != LLDB_INVALID_ADDRESS) {
199fe6060f1SDimitry Andric           m_value.SetValueType(Value::ValueType::LoadAddress);
2000b57cec5SDimitry Andric           m_value.GetScalar() = load_addr;
2010b57cec5SDimitry Andric         }
2020b57cec5SDimitry Andric       }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric       if (!CanProvideValue()) {
2050b57cec5SDimitry Andric         // this value object represents an aggregate type whose children have
2060b57cec5SDimitry Andric         // values, but this object does not. So we say we are changed if our
2070b57cec5SDimitry Andric         // location has changed.
2080b57cec5SDimitry Andric         SetValueDidChange(value_type != old_value.GetValueType() ||
2090b57cec5SDimitry Andric                           m_value.GetScalar() != old_value.GetScalar());
2100b57cec5SDimitry Andric       } else {
2110b57cec5SDimitry Andric         // Copy the Value and set the context to use our Variable so it can
2120b57cec5SDimitry Andric         // extract read its value into m_data appropriately
2130b57cec5SDimitry Andric         Value value(m_value);
2140b57cec5SDimitry Andric         if (m_type_sp)
215fe6060f1SDimitry Andric           value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
2160b57cec5SDimitry Andric         else {
2170b57cec5SDimitry Andric           value.SetCompilerType(m_compiler_type);
2180b57cec5SDimitry Andric         }
2190b57cec5SDimitry Andric 
2209dba64beSDimitry Andric         m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
2210b57cec5SDimitry Andric       }
2220b57cec5SDimitry Andric       break;
2230b57cec5SDimitry Andric     }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     SetValueIsValid(m_error.Success());
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric   return m_error.Success();
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric bool ValueObjectMemory::IsInScope() {
2310b57cec5SDimitry Andric   // FIXME: Maybe try to read the memory address, and if that works, then
2320b57cec5SDimitry Andric   // we are in scope?
2330b57cec5SDimitry Andric   return true;
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
237