Path: blob/main/contrib/llvm-project/lldb/source/ValueObject/ValueObjectConstResult.cpp
213764 views
//===-- ValueObjectConstResult.cpp ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/ValueObject/ValueObjectConstResult.h"910#include "lldb/Symbol/CompilerType.h"11#include "lldb/Target/ExecutionContext.h"12#include "lldb/Target/ExecutionContextScope.h"13#include "lldb/Target/Process.h"14#include "lldb/Utility/DataBuffer.h"15#include "lldb/Utility/DataBufferHeap.h"16#include "lldb/Utility/DataExtractor.h"17#include "lldb/Utility/Scalar.h"18#include "lldb/ValueObject/ValueObjectDynamicValue.h"19#include <optional>2021namespace lldb_private {22class Module;23}2425using namespace lldb;26using namespace lldb_private;2728ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,29ByteOrder byte_order,30uint32_t addr_byte_size,31lldb::addr_t address) {32auto manager_sp = ValueObjectManager::Create();33return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,34addr_byte_size, address))35->GetSP();36}3738ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,39ValueObjectManager &manager,40ByteOrder byte_order,41uint32_t addr_byte_size,42lldb::addr_t address)43: ValueObject(exe_scope, manager), m_impl(this, address) {44SetIsConstant();45SetValueIsValid(true);46m_data.SetByteOrder(byte_order);47m_data.SetAddressByteSize(addr_byte_size);48SetAddressTypeOfChildren(eAddressTypeLoad);49}5051ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,52const CompilerType &compiler_type,53ConstString name,54const DataExtractor &data,55lldb::addr_t address) {56auto manager_sp = ValueObjectManager::Create();57return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,58name, data, address))59->GetSP();60}6162ValueObjectConstResult::ValueObjectConstResult(63ExecutionContextScope *exe_scope, ValueObjectManager &manager,64const CompilerType &compiler_type, ConstString name,65const DataExtractor &data, lldb::addr_t address)66: ValueObject(exe_scope, manager), m_impl(this, address) {67m_data = data;6869if (!m_data.GetSharedDataBuffer()) {70DataBufferSP shared_data_buffer(71new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));72m_data.SetData(shared_data_buffer);73}7475m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();76m_value.SetValueType(Value::ValueType::HostAddress);77m_value.SetCompilerType(compiler_type);78m_name = name;79SetIsConstant();80SetValueIsValid(true);81SetAddressTypeOfChildren(eAddressTypeLoad);82}8384ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,85const CompilerType &compiler_type,86ConstString name,87const lldb::DataBufferSP &data_sp,88lldb::ByteOrder data_byte_order,89uint32_t data_addr_size,90lldb::addr_t address) {91auto manager_sp = ValueObjectManager::Create();92return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,93name, data_sp, data_byte_order,94data_addr_size, address))95->GetSP();96}9798ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,99Value &value, ConstString name,100Module *module) {101auto manager_sp = ValueObjectManager::Create();102return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,103module))104->GetSP();105}106107ValueObjectConstResult::ValueObjectConstResult(108ExecutionContextScope *exe_scope, ValueObjectManager &manager,109const CompilerType &compiler_type, ConstString name,110const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,111uint32_t data_addr_size, lldb::addr_t address)112: ValueObject(exe_scope, manager), m_impl(this, address) {113m_data.SetByteOrder(data_byte_order);114m_data.SetAddressByteSize(data_addr_size);115m_data.SetData(data_sp);116m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();117m_value.SetValueType(Value::ValueType::HostAddress);118m_value.SetCompilerType(compiler_type);119m_name = name;120SetIsConstant();121SetValueIsValid(true);122SetAddressTypeOfChildren(eAddressTypeLoad);123}124125ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,126const CompilerType &compiler_type,127ConstString name,128lldb::addr_t address,129AddressType address_type,130uint32_t addr_byte_size) {131auto manager_sp = ValueObjectManager::Create();132return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,133name, address, address_type,134addr_byte_size))135->GetSP();136}137138ValueObjectConstResult::ValueObjectConstResult(139ExecutionContextScope *exe_scope, ValueObjectManager &manager,140const CompilerType &compiler_type, ConstString name, lldb::addr_t address,141AddressType address_type, uint32_t addr_byte_size)142: ValueObject(exe_scope, manager), m_type_name(), m_impl(this, address) {143m_value.GetScalar() = address;144m_data.SetAddressByteSize(addr_byte_size);145m_value.GetScalar().GetData(m_data, addr_byte_size);146// m_value.SetValueType(Value::ValueType::HostAddress);147switch (address_type) {148case eAddressTypeInvalid:149m_value.SetValueType(Value::ValueType::Scalar);150break;151case eAddressTypeFile:152m_value.SetValueType(Value::ValueType::FileAddress);153break;154case eAddressTypeLoad:155m_value.SetValueType(Value::ValueType::LoadAddress);156break;157case eAddressTypeHost:158m_value.SetValueType(Value::ValueType::HostAddress);159break;160}161m_value.SetCompilerType(compiler_type);162m_name = name;163SetIsConstant();164SetValueIsValid(true);165SetAddressTypeOfChildren(eAddressTypeLoad);166}167168ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,169Status &&error) {170auto manager_sp = ValueObjectManager::Create();171return (new ValueObjectConstResult(exe_scope, *manager_sp, std::move(error)))172->GetSP();173}174175ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,176ValueObjectManager &manager,177Status &&error)178: ValueObject(exe_scope, manager), m_impl(this) {179m_error = std::move(error);180SetIsConstant();181}182183ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,184ValueObjectManager &manager,185const Value &value,186ConstString name, Module *module)187: ValueObject(exe_scope, manager), m_impl(this) {188m_value = value;189m_name = name;190ExecutionContext exe_ctx;191exe_scope->CalculateExecutionContext(exe_ctx);192m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);193}194195ValueObjectConstResult::~ValueObjectConstResult() = default;196197CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {198return m_value.GetCompilerType();199}200201lldb::ValueType ValueObjectConstResult::GetValueType() const {202return eValueTypeConstResult;203}204205llvm::Expected<uint64_t> ValueObjectConstResult::GetByteSize() {206ExecutionContext exe_ctx(GetExecutionContextRef());207if (!m_byte_size) {208auto size_or_err =209GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());210if (!size_or_err)211return size_or_err;212SetByteSize(*size_or_err);213}214if (m_byte_size)215return *m_byte_size;216return llvm::createStringError("unknown size of const result");217}218219void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }220221llvm::Expected<uint32_t>222ValueObjectConstResult::CalculateNumChildren(uint32_t max) {223ExecutionContext exe_ctx(GetExecutionContextRef());224auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);225if (!children_count)226return children_count;227return *children_count <= max ? *children_count : max;228}229230ConstString ValueObjectConstResult::GetTypeName() {231if (m_type_name.IsEmpty())232m_type_name = GetCompilerType().GetTypeName();233return m_type_name;234}235236ConstString ValueObjectConstResult::GetDisplayTypeName() {237return GetCompilerType().GetDisplayTypeName();238}239240bool ValueObjectConstResult::UpdateValue() {241// Const value is always valid242SetValueIsValid(true);243return true;244}245246bool ValueObjectConstResult::IsInScope() {247// A const result value is always in scope since it serializes all248// information needed to contain the constant value.249return true;250}251252lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {253return m_impl.Dereference(error);254}255256lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(257uint32_t offset, const CompilerType &type, bool can_create,258ConstString name_const_str) {259return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,260name_const_str);261}262263lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {264return m_impl.AddressOf(error);265}266267ValueObject::AddrAndType268ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address) {269return m_impl.GetAddressOf(scalar_is_load_address);270}271272size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,273uint32_t item_idx,274uint32_t item_count) {275return m_impl.GetPointeeData(data, item_idx, item_count);276}277278lldb::ValueObjectSP279ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {280// Always recalculate dynamic values for const results as the memory that281// they might point to might have changed at any time.282if (use_dynamic != eNoDynamicValues) {283if (!IsDynamic()) {284ExecutionContext exe_ctx(GetExecutionContextRef());285Process *process = exe_ctx.GetProcessPtr();286if (process && process->IsPossibleDynamicValue(*this))287m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);288}289if (m_dynamic_value && m_dynamic_value->GetError().Success())290return m_dynamic_value->GetSP();291}292return ValueObjectSP();293}294295lldb::ValueObjectSP296ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {297return m_impl.Cast(compiler_type);298}299300lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {301if (m_preferred_display_language != lldb::eLanguageTypeUnknown)302return m_preferred_display_language;303return GetCompilerTypeImpl().GetMinimumLanguage();304}305306307