15ffd83dbSDimitry Andric //===-- ValueObjectConstResult.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/ValueObjectConstResult.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "lldb/Core/ValueObjectDynamicValue.h"
120b57cec5SDimitry Andric #include "lldb/Symbol/CompilerType.h"
130b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
140b57cec5SDimitry Andric #include "lldb/Target/ExecutionContextScope.h"
150b57cec5SDimitry Andric #include "lldb/Target/Process.h"
160b57cec5SDimitry Andric #include "lldb/Utility/DataBuffer.h"
170b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
180b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
190b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
20bdd1243dSDimitry Andric #include <optional>
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric namespace lldb_private {
230b57cec5SDimitry Andric class Module;
240b57cec5SDimitry Andric }
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric using namespace lldb;
270b57cec5SDimitry Andric using namespace lldb_private;
280b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)290b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
300b57cec5SDimitry Andric ByteOrder byte_order,
310b57cec5SDimitry Andric uint32_t addr_byte_size,
320b57cec5SDimitry Andric lldb::addr_t address) {
335ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
345ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,
355ffd83dbSDimitry Andric addr_byte_size, address))
360b57cec5SDimitry Andric ->GetSP();
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)390b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
405ffd83dbSDimitry Andric ValueObjectManager &manager,
410b57cec5SDimitry Andric ByteOrder byte_order,
420b57cec5SDimitry Andric uint32_t addr_byte_size,
430b57cec5SDimitry Andric lldb::addr_t address)
44e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_impl(this, address) {
450b57cec5SDimitry Andric SetIsConstant();
460b57cec5SDimitry Andric SetValueIsValid(true);
470b57cec5SDimitry Andric m_data.SetByteOrder(byte_order);
480b57cec5SDimitry Andric m_data.SetAddressByteSize(addr_byte_size);
490b57cec5SDimitry Andric SetAddressTypeOfChildren(eAddressTypeLoad);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)520b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
530b57cec5SDimitry Andric const CompilerType &compiler_type,
540b57cec5SDimitry Andric ConstString name,
550b57cec5SDimitry Andric const DataExtractor &data,
560b57cec5SDimitry Andric lldb::addr_t address) {
575ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
585ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
595ffd83dbSDimitry Andric name, data, address))
600b57cec5SDimitry Andric ->GetSP();
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)630b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
645ffd83dbSDimitry Andric ExecutionContextScope *exe_scope, ValueObjectManager &manager,
655ffd83dbSDimitry Andric const CompilerType &compiler_type, ConstString name,
665ffd83dbSDimitry Andric const DataExtractor &data, lldb::addr_t address)
67e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_impl(this, address) {
680b57cec5SDimitry Andric m_data = data;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric if (!m_data.GetSharedDataBuffer()) {
710b57cec5SDimitry Andric DataBufferSP shared_data_buffer(
720b57cec5SDimitry Andric new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
730b57cec5SDimitry Andric m_data.SetData(shared_data_buffer);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
77fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::HostAddress);
780b57cec5SDimitry Andric m_value.SetCompilerType(compiler_type);
790b57cec5SDimitry Andric m_name = name;
800b57cec5SDimitry Andric SetIsConstant();
810b57cec5SDimitry Andric SetValueIsValid(true);
820b57cec5SDimitry Andric SetAddressTypeOfChildren(eAddressTypeLoad);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,const lldb::DataBufferSP & data_sp,lldb::ByteOrder data_byte_order,uint32_t data_addr_size,lldb::addr_t address)850b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
860b57cec5SDimitry Andric const CompilerType &compiler_type,
870b57cec5SDimitry Andric ConstString name,
880b57cec5SDimitry Andric const lldb::DataBufferSP &data_sp,
890b57cec5SDimitry Andric lldb::ByteOrder data_byte_order,
900b57cec5SDimitry Andric uint32_t data_addr_size,
910b57cec5SDimitry Andric lldb::addr_t address) {
925ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
935ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
945ffd83dbSDimitry Andric name, data_sp, data_byte_order,
955ffd83dbSDimitry Andric data_addr_size, address))
960b57cec5SDimitry Andric ->GetSP();
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,Value & value,ConstString name,Module * module)990b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
1000b57cec5SDimitry Andric Value &value,
1010b57cec5SDimitry Andric ConstString name,
1020b57cec5SDimitry Andric Module *module) {
1035ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
1045ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
1055ffd83dbSDimitry Andric module))
1065ffd83dbSDimitry Andric ->GetSP();
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,const lldb::DataBufferSP & data_sp,lldb::ByteOrder data_byte_order,uint32_t data_addr_size,lldb::addr_t address)1090b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
1105ffd83dbSDimitry Andric ExecutionContextScope *exe_scope, ValueObjectManager &manager,
1115ffd83dbSDimitry Andric const CompilerType &compiler_type, ConstString name,
1125ffd83dbSDimitry Andric const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,
1135ffd83dbSDimitry Andric uint32_t data_addr_size, lldb::addr_t address)
114e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_impl(this, address) {
1150b57cec5SDimitry Andric m_data.SetByteOrder(data_byte_order);
1160b57cec5SDimitry Andric m_data.SetAddressByteSize(data_addr_size);
1170b57cec5SDimitry Andric m_data.SetData(data_sp);
1180b57cec5SDimitry Andric m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
119fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::HostAddress);
1200b57cec5SDimitry Andric m_value.SetCompilerType(compiler_type);
1210b57cec5SDimitry Andric m_name = name;
1220b57cec5SDimitry Andric SetIsConstant();
1230b57cec5SDimitry Andric SetValueIsValid(true);
1240b57cec5SDimitry Andric SetAddressTypeOfChildren(eAddressTypeLoad);
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)1270b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
1280b57cec5SDimitry Andric const CompilerType &compiler_type,
1290b57cec5SDimitry Andric ConstString name,
1300b57cec5SDimitry Andric lldb::addr_t address,
1310b57cec5SDimitry Andric AddressType address_type,
1320b57cec5SDimitry Andric uint32_t addr_byte_size) {
1335ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
1345ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
1355ffd83dbSDimitry Andric name, address, address_type,
1365ffd83dbSDimitry Andric addr_byte_size))
1370b57cec5SDimitry Andric ->GetSP();
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)1400b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
1415ffd83dbSDimitry Andric ExecutionContextScope *exe_scope, ValueObjectManager &manager,
1425ffd83dbSDimitry Andric const CompilerType &compiler_type, ConstString name, lldb::addr_t address,
1435ffd83dbSDimitry Andric AddressType address_type, uint32_t addr_byte_size)
144e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_type_name(),
1450b57cec5SDimitry Andric m_impl(this, address) {
1460b57cec5SDimitry Andric m_value.GetScalar() = address;
1470b57cec5SDimitry Andric m_data.SetAddressByteSize(addr_byte_size);
1480b57cec5SDimitry Andric m_value.GetScalar().GetData(m_data, addr_byte_size);
149fe6060f1SDimitry Andric // m_value.SetValueType(Value::ValueType::HostAddress);
1500b57cec5SDimitry Andric switch (address_type) {
1510b57cec5SDimitry Andric case eAddressTypeInvalid:
152fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::Scalar);
1530b57cec5SDimitry Andric break;
1540b57cec5SDimitry Andric case eAddressTypeFile:
155fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::FileAddress);
1560b57cec5SDimitry Andric break;
1570b57cec5SDimitry Andric case eAddressTypeLoad:
158fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::LoadAddress);
1590b57cec5SDimitry Andric break;
1600b57cec5SDimitry Andric case eAddressTypeHost:
161fe6060f1SDimitry Andric m_value.SetValueType(Value::ValueType::HostAddress);
1620b57cec5SDimitry Andric break;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric m_value.SetCompilerType(compiler_type);
1650b57cec5SDimitry Andric m_name = name;
1660b57cec5SDimitry Andric SetIsConstant();
1670b57cec5SDimitry Andric SetValueIsValid(true);
1680b57cec5SDimitry Andric SetAddressTypeOfChildren(eAddressTypeLoad);
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric
Create(ExecutionContextScope * exe_scope,const Status & error)1710b57cec5SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
1720b57cec5SDimitry Andric const Status &error) {
1735ffd83dbSDimitry Andric auto manager_sp = ValueObjectManager::Create();
1745ffd83dbSDimitry Andric return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Status & error)1770b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
1785ffd83dbSDimitry Andric ValueObjectManager &manager,
1790b57cec5SDimitry Andric const Status &error)
180e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_impl(this) {
1810b57cec5SDimitry Andric m_error = error;
1820b57cec5SDimitry Andric SetIsConstant();
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Value & value,ConstString name,Module * module)1850b57cec5SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
1865ffd83dbSDimitry Andric ValueObjectManager &manager,
1870b57cec5SDimitry Andric const Value &value,
1885ffd83dbSDimitry Andric ConstString name, Module *module)
189e8d8bef9SDimitry Andric : ValueObject(exe_scope, manager), m_impl(this) {
1900b57cec5SDimitry Andric m_value = value;
1910b57cec5SDimitry Andric m_name = name;
1920b57cec5SDimitry Andric ExecutionContext exe_ctx;
1930b57cec5SDimitry Andric exe_scope->CalculateExecutionContext(exe_ctx);
1949dba64beSDimitry Andric m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric
197fe6060f1SDimitry Andric ValueObjectConstResult::~ValueObjectConstResult() = default;
1980b57cec5SDimitry Andric
GetCompilerTypeImpl()1990b57cec5SDimitry Andric CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
2000b57cec5SDimitry Andric return m_value.GetCompilerType();
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric
GetValueType() const2030b57cec5SDimitry Andric lldb::ValueType ValueObjectConstResult::GetValueType() const {
2040b57cec5SDimitry Andric return eValueTypeConstResult;
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric
GetByteSize()207bdd1243dSDimitry Andric std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
2080b57cec5SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
209e8d8bef9SDimitry Andric if (!m_byte_size) {
2100b57cec5SDimitry Andric if (auto size =
2110b57cec5SDimitry Andric GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
2120b57cec5SDimitry Andric SetByteSize(*size);
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric return m_byte_size;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
SetByteSize(size_t size)2170b57cec5SDimitry Andric void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
2180b57cec5SDimitry Andric
CalculateNumChildren(uint32_t max)2190b57cec5SDimitry Andric size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
2200b57cec5SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
2210b57cec5SDimitry Andric auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
2220b57cec5SDimitry Andric return children_count <= max ? children_count : max;
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
GetTypeName()2250b57cec5SDimitry Andric ConstString ValueObjectConstResult::GetTypeName() {
2260b57cec5SDimitry Andric if (m_type_name.IsEmpty())
2275ffd83dbSDimitry Andric m_type_name = GetCompilerType().GetTypeName();
2280b57cec5SDimitry Andric return m_type_name;
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
GetDisplayTypeName()2310b57cec5SDimitry Andric ConstString ValueObjectConstResult::GetDisplayTypeName() {
2320b57cec5SDimitry Andric return GetCompilerType().GetDisplayTypeName();
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric
UpdateValue()2350b57cec5SDimitry Andric bool ValueObjectConstResult::UpdateValue() {
2360b57cec5SDimitry Andric // Const value is always valid
2370b57cec5SDimitry Andric SetValueIsValid(true);
2380b57cec5SDimitry Andric return true;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric
IsInScope()2410b57cec5SDimitry Andric bool ValueObjectConstResult::IsInScope() {
2420b57cec5SDimitry Andric // A const result value is always in scope since it serializes all
2430b57cec5SDimitry Andric // information needed to contain the constant value.
2440b57cec5SDimitry Andric return true;
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric
Dereference(Status & error)2470b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
2480b57cec5SDimitry Andric return m_impl.Dereference(error);
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)2510b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
2520b57cec5SDimitry Andric uint32_t offset, const CompilerType &type, bool can_create,
2530b57cec5SDimitry Andric ConstString name_const_str) {
2540b57cec5SDimitry Andric return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
2550b57cec5SDimitry Andric name_const_str);
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric
AddressOf(Status & error)2580b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
2590b57cec5SDimitry Andric return m_impl.AddressOf(error);
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)2620b57cec5SDimitry Andric lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
2630b57cec5SDimitry Andric AddressType *address_type) {
2640b57cec5SDimitry Andric return m_impl.GetAddressOf(scalar_is_load_address, address_type);
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric
CreateChildAtIndex(size_t idx,bool synthetic_array_member,int32_t synthetic_index)2670b57cec5SDimitry Andric ValueObject *ValueObjectConstResult::CreateChildAtIndex(
2680b57cec5SDimitry Andric size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
2690b57cec5SDimitry Andric return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
2700b57cec5SDimitry Andric synthetic_index);
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)2730b57cec5SDimitry Andric size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
2740b57cec5SDimitry Andric uint32_t item_idx,
2750b57cec5SDimitry Andric uint32_t item_count) {
2760b57cec5SDimitry Andric return m_impl.GetPointeeData(data, item_idx, item_count);
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric lldb::ValueObjectSP
GetDynamicValue(lldb::DynamicValueType use_dynamic)2800b57cec5SDimitry Andric ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
2810b57cec5SDimitry Andric // Always recalculate dynamic values for const results as the memory that
2820b57cec5SDimitry Andric // they might point to might have changed at any time.
2830b57cec5SDimitry Andric if (use_dynamic != eNoDynamicValues) {
2840b57cec5SDimitry Andric if (!IsDynamic()) {
2850b57cec5SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
2860b57cec5SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
2870b57cec5SDimitry Andric if (process && process->IsPossibleDynamicValue(*this))
2880b57cec5SDimitry Andric m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
2890b57cec5SDimitry Andric }
290*fe013be4SDimitry Andric if (m_dynamic_value && m_dynamic_value->GetError().Success())
2910b57cec5SDimitry Andric return m_dynamic_value->GetSP();
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric return ValueObjectSP();
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric lldb::ValueObjectSP
DoCast(const CompilerType & compiler_type)297*fe013be4SDimitry Andric ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {
2980b57cec5SDimitry Andric return m_impl.Cast(compiler_type);
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
GetPreferredDisplayLanguage()3010b57cec5SDimitry Andric lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
3020b57cec5SDimitry Andric if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
3030b57cec5SDimitry Andric return m_preferred_display_language;
3040b57cec5SDimitry Andric return GetCompilerTypeImpl().GetMinimumLanguage();
3050b57cec5SDimitry Andric }
306