Path: blob/main/contrib/llvm-project/lldb/source/ValueObject/ValueObjectChild.cpp
213764 views
//===-- ValueObjectChild.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/ValueObjectChild.h"910#include "lldb/Core/Value.h"11#include "lldb/Symbol/CompilerType.h"12#include "lldb/Target/ExecutionContext.h"13#include "lldb/Target/Process.h"14#include "lldb/Utility/Flags.h"15#include "lldb/Utility/Scalar.h"16#include "lldb/Utility/Status.h"17#include "lldb/lldb-forward.h"1819#include <functional>20#include <memory>21#include <vector>2223#include <cstdio>24#include <cstring>2526using namespace lldb_private;2728ValueObjectChild::ValueObjectChild(29ValueObject &parent, const CompilerType &compiler_type, ConstString name,30uint64_t byte_size, int32_t byte_offset, uint32_t bitfield_bit_size,31uint32_t bitfield_bit_offset, bool is_base_class, bool is_deref_of_parent,32AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)33: ValueObject(parent), m_compiler_type(compiler_type),34m_byte_size(byte_size), m_byte_offset(byte_offset),35m_bitfield_bit_size(bitfield_bit_size),36m_bitfield_bit_offset(bitfield_bit_offset),37m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),38m_can_update_with_invalid_exe_ctx() {39m_name = name;40SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);41SetLanguageFlags(language_flags);42}4344ValueObjectChild::~ValueObjectChild() = default;4546lldb::ValueType ValueObjectChild::GetValueType() const {47return m_parent->GetValueType();48}4950llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {51ExecutionContext exe_ctx(GetExecutionContextRef());52auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);53if (!children_count)54return children_count;55return *children_count <= max ? *children_count : max;56}5758static void AdjustForBitfieldness(ConstString &name,59uint8_t bitfield_bit_size) {60if (name && bitfield_bit_size)61name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());62}6364ConstString ValueObjectChild::GetTypeName() {65if (m_type_name.IsEmpty()) {66m_type_name = GetCompilerType().GetTypeName();67AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);68}69return m_type_name;70}7172ConstString ValueObjectChild::GetQualifiedTypeName() {73ConstString qualified_name = GetCompilerType().GetTypeName();74AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);75return qualified_name;76}7778ConstString ValueObjectChild::GetDisplayTypeName() {79ConstString display_name = GetCompilerType().GetDisplayTypeName();80AdjustForBitfieldness(display_name, m_bitfield_bit_size);81return display_name;82}8384LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {85if (m_can_update_with_invalid_exe_ctx)86return *m_can_update_with_invalid_exe_ctx;87if (m_parent) {88ValueObject *opinionated_parent =89m_parent->FollowParentChain([](ValueObject *valobj) -> bool {90return (valobj->CanUpdateWithInvalidExecutionContext() ==91eLazyBoolCalculate);92});93if (opinionated_parent)94return *(m_can_update_with_invalid_exe_ctx =95opinionated_parent->CanUpdateWithInvalidExecutionContext());96}97return *(m_can_update_with_invalid_exe_ctx =98this->ValueObject::CanUpdateWithInvalidExecutionContext());99}100101bool ValueObjectChild::UpdateValue() {102m_error.Clear();103SetValueIsValid(false);104ValueObject *parent = m_parent;105if (parent) {106if (parent->UpdateValueIfNeeded(false)) {107m_value.SetCompilerType(GetCompilerType());108109CompilerType parent_type(parent->GetCompilerType());110// Copy the parent scalar value and the scalar value type111m_value.GetScalar() = parent->GetValue().GetScalar();112m_value.SetValueType(parent->GetValue().GetValueType());113114Flags parent_type_flags(parent_type.GetTypeInfo());115const bool is_instance_ptr_base =116((m_is_base_class) &&117(parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));118119if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {120m_value.GetScalar() = parent->GetPointerValue().address;121122switch (parent->GetAddressTypeOfChildren()) {123case eAddressTypeFile: {124lldb::ProcessSP process_sp(GetProcessSP());125if (process_sp && process_sp->IsAlive())126m_value.SetValueType(Value::ValueType::LoadAddress);127else128m_value.SetValueType(Value::ValueType::FileAddress);129} break;130case eAddressTypeLoad:131m_value.SetValueType(is_instance_ptr_base132? Value::ValueType::Scalar133: Value::ValueType::LoadAddress);134break;135case eAddressTypeHost:136m_value.SetValueType(Value::ValueType::HostAddress);137break;138case eAddressTypeInvalid:139// TODO: does this make sense?140m_value.SetValueType(Value::ValueType::Scalar);141break;142}143}144switch (m_value.GetValueType()) {145case Value::ValueType::Invalid:146break;147case Value::ValueType::LoadAddress:148case Value::ValueType::FileAddress:149case Value::ValueType::HostAddress: {150lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);151if (addr == LLDB_INVALID_ADDRESS) {152m_error = Status::FromErrorString("parent address is invalid.");153} else if (addr == 0) {154m_error = Status::FromErrorString("parent is NULL");155} else {156// If a bitfield doesn't fit into the child_byte_size'd window at157// child_byte_offset, move the window forward until it fits. The158// problem here is that Value has no notion of bitfields and thus the159// Value's DataExtractor is sized like the bitfields CompilerType; a160// sequence of bitfields, however, can be larger than their underlying161// type.162if (m_bitfield_bit_offset) {163const bool thread_and_frame_only_if_stopped = true;164ExecutionContext exe_ctx(GetExecutionContextRef().Lock(165thread_and_frame_only_if_stopped));166if (auto type_bit_size =167llvm::expectedToOptional(GetCompilerType().GetBitSize(168exe_ctx.GetBestExecutionContextScope()))) {169uint64_t bitfield_end =170m_bitfield_bit_size + m_bitfield_bit_offset;171if (bitfield_end > *type_bit_size) {172uint64_t overhang_bytes =173(bitfield_end - *type_bit_size + 7) / 8;174m_byte_offset += overhang_bytes;175m_bitfield_bit_offset -= overhang_bytes * 8;176}177}178}179180// Set this object's scalar value to the address of its value by181// adding its byte offset to the parent address182m_value.GetScalar() += m_byte_offset;183}184} break;185186case Value::ValueType::Scalar:187// try to extract the child value from the parent's scalar value188{189Scalar scalar(m_value.GetScalar());190scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);191m_value.GetScalar() = scalar;192}193break;194}195196if (m_error.Success()) {197const bool thread_and_frame_only_if_stopped = true;198ExecutionContext exe_ctx(199GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));200if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {201Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;202m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());203} else {204m_error.Clear(); // No value so nothing to read...205}206}207208} else {209m_error = Status::FromErrorStringWithFormat(210"parent failed to evaluate: %s", parent->GetError().AsCString());211}212} else {213m_error = Status::FromErrorString(214"ValueObjectChild has a NULL parent ValueObject.");215}216217return m_error.Success();218}219220bool ValueObjectChild::IsInScope() {221ValueObject *root(GetRoot());222if (root)223return root->IsInScope();224return false;225}226227228