Path: blob/main/contrib/llvm-project/lldb/source/Core/ValueObjectConstResult.cpp
39587 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/Core/ValueObjectConstResult.h"910#include "lldb/Core/ValueObjectDynamicValue.h"11#include "lldb/Symbol/CompilerType.h"12#include "lldb/Target/ExecutionContext.h"13#include "lldb/Target/ExecutionContextScope.h"14#include "lldb/Target/Process.h"15#include "lldb/Utility/DataBuffer.h"16#include "lldb/Utility/DataBufferHeap.h"17#include "lldb/Utility/DataExtractor.h"18#include "lldb/Utility/Scalar.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,100ConstString name,101Module *module) {102auto manager_sp = ValueObjectManager::Create();103return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,104module))105->GetSP();106}107108ValueObjectConstResult::ValueObjectConstResult(109ExecutionContextScope *exe_scope, ValueObjectManager &manager,110const CompilerType &compiler_type, ConstString name,111const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,112uint32_t data_addr_size, lldb::addr_t address)113: ValueObject(exe_scope, manager), m_impl(this, address) {114m_data.SetByteOrder(data_byte_order);115m_data.SetAddressByteSize(data_addr_size);116m_data.SetData(data_sp);117m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();118m_value.SetValueType(Value::ValueType::HostAddress);119m_value.SetCompilerType(compiler_type);120m_name = name;121SetIsConstant();122SetValueIsValid(true);123SetAddressTypeOfChildren(eAddressTypeLoad);124}125126ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,127const CompilerType &compiler_type,128ConstString name,129lldb::addr_t address,130AddressType address_type,131uint32_t addr_byte_size) {132auto manager_sp = ValueObjectManager::Create();133return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,134name, address, address_type,135addr_byte_size))136->GetSP();137}138139ValueObjectConstResult::ValueObjectConstResult(140ExecutionContextScope *exe_scope, ValueObjectManager &manager,141const CompilerType &compiler_type, ConstString name, lldb::addr_t address,142AddressType address_type, uint32_t addr_byte_size)143: ValueObject(exe_scope, manager), m_type_name(),144m_impl(this, address) {145m_value.GetScalar() = address;146m_data.SetAddressByteSize(addr_byte_size);147m_value.GetScalar().GetData(m_data, addr_byte_size);148// m_value.SetValueType(Value::ValueType::HostAddress);149switch (address_type) {150case eAddressTypeInvalid:151m_value.SetValueType(Value::ValueType::Scalar);152break;153case eAddressTypeFile:154m_value.SetValueType(Value::ValueType::FileAddress);155break;156case eAddressTypeLoad:157m_value.SetValueType(Value::ValueType::LoadAddress);158break;159case eAddressTypeHost:160m_value.SetValueType(Value::ValueType::HostAddress);161break;162}163m_value.SetCompilerType(compiler_type);164m_name = name;165SetIsConstant();166SetValueIsValid(true);167SetAddressTypeOfChildren(eAddressTypeLoad);168}169170ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,171const Status &error) {172auto manager_sp = ValueObjectManager::Create();173return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();174}175176ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,177ValueObjectManager &manager,178const Status &error)179: ValueObject(exe_scope, manager), m_impl(this) {180m_error = error;181SetIsConstant();182}183184ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,185ValueObjectManager &manager,186const Value &value,187ConstString name, Module *module)188: ValueObject(exe_scope, manager), m_impl(this) {189m_value = value;190m_name = name;191ExecutionContext exe_ctx;192exe_scope->CalculateExecutionContext(exe_ctx);193m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);194}195196ValueObjectConstResult::~ValueObjectConstResult() = default;197198CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {199return m_value.GetCompilerType();200}201202lldb::ValueType ValueObjectConstResult::GetValueType() const {203return eValueTypeConstResult;204}205206std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {207ExecutionContext exe_ctx(GetExecutionContextRef());208if (!m_byte_size) {209if (auto size =210GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))211SetByteSize(*size);212}213return m_byte_size;214}215216void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }217218llvm::Expected<uint32_t>219ValueObjectConstResult::CalculateNumChildren(uint32_t max) {220ExecutionContext exe_ctx(GetExecutionContextRef());221auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);222if (!children_count)223return children_count;224return *children_count <= max ? *children_count : max;225}226227ConstString ValueObjectConstResult::GetTypeName() {228if (m_type_name.IsEmpty())229m_type_name = GetCompilerType().GetTypeName();230return m_type_name;231}232233ConstString ValueObjectConstResult::GetDisplayTypeName() {234return GetCompilerType().GetDisplayTypeName();235}236237bool ValueObjectConstResult::UpdateValue() {238// Const value is always valid239SetValueIsValid(true);240return true;241}242243bool ValueObjectConstResult::IsInScope() {244// A const result value is always in scope since it serializes all245// information needed to contain the constant value.246return true;247}248249lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {250return m_impl.Dereference(error);251}252253lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(254uint32_t offset, const CompilerType &type, bool can_create,255ConstString name_const_str) {256return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,257name_const_str);258}259260lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {261return m_impl.AddressOf(error);262}263264lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,265AddressType *address_type) {266return m_impl.GetAddressOf(scalar_is_load_address, address_type);267}268269size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,270uint32_t item_idx,271uint32_t item_count) {272return m_impl.GetPointeeData(data, item_idx, item_count);273}274275lldb::ValueObjectSP276ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {277// Always recalculate dynamic values for const results as the memory that278// they might point to might have changed at any time.279if (use_dynamic != eNoDynamicValues) {280if (!IsDynamic()) {281ExecutionContext exe_ctx(GetExecutionContextRef());282Process *process = exe_ctx.GetProcessPtr();283if (process && process->IsPossibleDynamicValue(*this))284m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);285}286if (m_dynamic_value && m_dynamic_value->GetError().Success())287return m_dynamic_value->GetSP();288}289return ValueObjectSP();290}291292lldb::ValueObjectSP293ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {294return m_impl.Cast(compiler_type);295}296297lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {298if (m_preferred_display_language != lldb::eLanguageTypeUnknown)299return m_preferred_display_language;300return GetCompilerTypeImpl().GetMinimumLanguage();301}302303304