Path: blob/main/contrib/llvm-project/lldb/source/Core/ValueObjectChild.cpp
39587 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/Core/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,30ConstString name, uint64_t byte_size, int32_t byte_offset,31uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,32bool is_base_class, bool is_deref_of_parent,33AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)34: ValueObject(parent), m_compiler_type(compiler_type),35m_byte_size(byte_size), m_byte_offset(byte_offset),36m_bitfield_bit_size(bitfield_bit_size),37m_bitfield_bit_offset(bitfield_bit_offset),38m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),39m_can_update_with_invalid_exe_ctx() {40m_name = name;41SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);42SetLanguageFlags(language_flags);43}4445ValueObjectChild::~ValueObjectChild() = default;4647lldb::ValueType ValueObjectChild::GetValueType() const {48return m_parent->GetValueType();49}5051llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {52ExecutionContext exe_ctx(GetExecutionContextRef());53auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);54if (!children_count)55return children_count;56return *children_count <= max ? *children_count : max;57}5859static void AdjustForBitfieldness(ConstString &name,60uint8_t bitfield_bit_size) {61if (name && bitfield_bit_size)62name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());63}6465ConstString ValueObjectChild::GetTypeName() {66if (m_type_name.IsEmpty()) {67m_type_name = GetCompilerType().GetTypeName();68AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);69}70return m_type_name;71}7273ConstString ValueObjectChild::GetQualifiedTypeName() {74ConstString qualified_name = GetCompilerType().GetTypeName();75AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);76return qualified_name;77}7879ConstString ValueObjectChild::GetDisplayTypeName() {80ConstString display_name = GetCompilerType().GetDisplayTypeName();81AdjustForBitfieldness(display_name, m_bitfield_bit_size);82return display_name;83}8485LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {86if (m_can_update_with_invalid_exe_ctx)87return *m_can_update_with_invalid_exe_ctx;88if (m_parent) {89ValueObject *opinionated_parent =90m_parent->FollowParentChain([](ValueObject *valobj) -> bool {91return (valobj->CanUpdateWithInvalidExecutionContext() ==92eLazyBoolCalculate);93});94if (opinionated_parent)95return *(m_can_update_with_invalid_exe_ctx =96opinionated_parent->CanUpdateWithInvalidExecutionContext());97}98return *(m_can_update_with_invalid_exe_ctx =99this->ValueObject::CanUpdateWithInvalidExecutionContext());100}101102bool ValueObjectChild::UpdateValue() {103m_error.Clear();104SetValueIsValid(false);105ValueObject *parent = m_parent;106if (parent) {107if (parent->UpdateValueIfNeeded(false)) {108m_value.SetCompilerType(GetCompilerType());109110CompilerType parent_type(parent->GetCompilerType());111// Copy the parent scalar value and the scalar value type112m_value.GetScalar() = parent->GetValue().GetScalar();113m_value.SetValueType(parent->GetValue().GetValueType());114115Flags parent_type_flags(parent_type.GetTypeInfo());116const bool is_instance_ptr_base =117((m_is_base_class) &&118(parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));119120if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {121m_value.GetScalar() = parent->GetPointerValue();122123switch (parent->GetAddressTypeOfChildren()) {124case eAddressTypeFile: {125lldb::ProcessSP process_sp(GetProcessSP());126if (process_sp && process_sp->IsAlive())127m_value.SetValueType(Value::ValueType::LoadAddress);128else129m_value.SetValueType(Value::ValueType::FileAddress);130} break;131case eAddressTypeLoad:132m_value.SetValueType(is_instance_ptr_base133? Value::ValueType::Scalar134: Value::ValueType::LoadAddress);135break;136case eAddressTypeHost:137m_value.SetValueType(Value::ValueType::HostAddress);138break;139case eAddressTypeInvalid:140// TODO: does this make sense?141m_value.SetValueType(Value::ValueType::Scalar);142break;143}144}145switch (m_value.GetValueType()) {146case Value::ValueType::Invalid:147break;148case Value::ValueType::LoadAddress:149case Value::ValueType::FileAddress:150case Value::ValueType::HostAddress: {151lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);152if (addr == LLDB_INVALID_ADDRESS) {153m_error.SetErrorString("parent address is invalid.");154} else if (addr == 0) {155m_error.SetErrorString("parent is NULL");156} else {157// If a bitfield doesn't fit into the child_byte_size'd window at158// child_byte_offset, move the window forward until it fits. The159// problem here is that Value has no notion of bitfields and thus the160// Value's DataExtractor is sized like the bitfields CompilerType; a161// sequence of bitfields, however, can be larger than their underlying162// type.163if (m_bitfield_bit_offset) {164const bool thread_and_frame_only_if_stopped = true;165ExecutionContext exe_ctx(GetExecutionContextRef().Lock(166thread_and_frame_only_if_stopped));167if (auto type_bit_size = 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 =203value.GetValueAsData(&exe_ctx, m_data, GetModule().get());204} else {205m_error.Clear(); // No value so nothing to read...206}207}208209} else {210m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",211parent->GetError().AsCString());212}213} else {214m_error.SetErrorString("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