llvm-project
226 строк · 8.3 Кб
1//===-- ValueObjectChild.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/ValueObjectChild.h"10
11#include "lldb/Core/Value.h"12#include "lldb/Symbol/CompilerType.h"13#include "lldb/Target/ExecutionContext.h"14#include "lldb/Target/Process.h"15#include "lldb/Utility/Flags.h"16#include "lldb/Utility/Scalar.h"17#include "lldb/Utility/Status.h"18#include "lldb/lldb-forward.h"19
20#include <functional>21#include <memory>22#include <vector>23
24#include <cstdio>25#include <cstring>26
27using namespace lldb_private;28
29ValueObjectChild::ValueObjectChild(30ValueObject &parent, const CompilerType &compiler_type,31ConstString name, uint64_t byte_size, int32_t byte_offset,32uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,33bool is_base_class, bool is_deref_of_parent,34AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)35: ValueObject(parent), m_compiler_type(compiler_type),36m_byte_size(byte_size), m_byte_offset(byte_offset),37m_bitfield_bit_size(bitfield_bit_size),38m_bitfield_bit_offset(bitfield_bit_offset),39m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),40m_can_update_with_invalid_exe_ctx() {41m_name = name;42SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);43SetLanguageFlags(language_flags);44}
45
46ValueObjectChild::~ValueObjectChild() = default;47
48lldb::ValueType ValueObjectChild::GetValueType() const {49return m_parent->GetValueType();50}
51
52llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {53ExecutionContext exe_ctx(GetExecutionContextRef());54auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);55if (!children_count)56return children_count;57return *children_count <= max ? *children_count : max;58}
59
60static void AdjustForBitfieldness(ConstString &name,61uint8_t bitfield_bit_size) {62if (name && bitfield_bit_size)63name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());64}
65
66ConstString ValueObjectChild::GetTypeName() {67if (m_type_name.IsEmpty()) {68m_type_name = GetCompilerType().GetTypeName();69AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);70}71return m_type_name;72}
73
74ConstString ValueObjectChild::GetQualifiedTypeName() {75ConstString qualified_name = GetCompilerType().GetTypeName();76AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);77return qualified_name;78}
79
80ConstString ValueObjectChild::GetDisplayTypeName() {81ConstString display_name = GetCompilerType().GetDisplayTypeName();82AdjustForBitfieldness(display_name, m_bitfield_bit_size);83return display_name;84}
85
86LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {87if (m_can_update_with_invalid_exe_ctx)88return *m_can_update_with_invalid_exe_ctx;89if (m_parent) {90ValueObject *opinionated_parent =91m_parent->FollowParentChain([](ValueObject *valobj) -> bool {92return (valobj->CanUpdateWithInvalidExecutionContext() ==93eLazyBoolCalculate);94});95if (opinionated_parent)96return *(m_can_update_with_invalid_exe_ctx =97opinionated_parent->CanUpdateWithInvalidExecutionContext());98}99return *(m_can_update_with_invalid_exe_ctx =100this->ValueObject::CanUpdateWithInvalidExecutionContext());101}
102
103bool ValueObjectChild::UpdateValue() {104m_error.Clear();105SetValueIsValid(false);106ValueObject *parent = m_parent;107if (parent) {108if (parent->UpdateValueIfNeeded(false)) {109m_value.SetCompilerType(GetCompilerType());110
111CompilerType parent_type(parent->GetCompilerType());112// Copy the parent scalar value and the scalar value type113m_value.GetScalar() = parent->GetValue().GetScalar();114m_value.SetValueType(parent->GetValue().GetValueType());115
116Flags parent_type_flags(parent_type.GetTypeInfo());117const bool is_instance_ptr_base =118((m_is_base_class) &&119(parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));120
121if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {122m_value.GetScalar() = parent->GetPointerValue();123
124switch (parent->GetAddressTypeOfChildren()) {125case eAddressTypeFile: {126lldb::ProcessSP process_sp(GetProcessSP());127if (process_sp && process_sp->IsAlive())128m_value.SetValueType(Value::ValueType::LoadAddress);129else130m_value.SetValueType(Value::ValueType::FileAddress);131} break;132case eAddressTypeLoad:133m_value.SetValueType(is_instance_ptr_base134? Value::ValueType::Scalar135: Value::ValueType::LoadAddress);136break;137case eAddressTypeHost:138m_value.SetValueType(Value::ValueType::HostAddress);139break;140case eAddressTypeInvalid:141// TODO: does this make sense?142m_value.SetValueType(Value::ValueType::Scalar);143break;144}145}146switch (m_value.GetValueType()) {147case Value::ValueType::Invalid:148break;149case Value::ValueType::LoadAddress:150case Value::ValueType::FileAddress:151case Value::ValueType::HostAddress: {152lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);153if (addr == LLDB_INVALID_ADDRESS) {154m_error.SetErrorString("parent address is invalid.");155} else if (addr == 0) {156m_error.SetErrorString("parent is NULL");157} else {158// If a bitfield doesn't fit into the child_byte_size'd window at159// child_byte_offset, move the window forward until it fits. The160// problem here is that Value has no notion of bitfields and thus the161// Value's DataExtractor is sized like the bitfields CompilerType; a162// sequence of bitfields, however, can be larger than their underlying163// type.164if (m_bitfield_bit_offset) {165const bool thread_and_frame_only_if_stopped = true;166ExecutionContext exe_ctx(GetExecutionContextRef().Lock(167thread_and_frame_only_if_stopped));168if (auto type_bit_size = GetCompilerType().GetBitSize(169exe_ctx.GetBestExecutionContextScope())) {170uint64_t bitfield_end =171m_bitfield_bit_size + m_bitfield_bit_offset;172if (bitfield_end > *type_bit_size) {173uint64_t overhang_bytes =174(bitfield_end - *type_bit_size + 7) / 8;175m_byte_offset += overhang_bytes;176m_bitfield_bit_offset -= overhang_bytes * 8;177}178}179}180
181// Set this object's scalar value to the address of its value by182// adding its byte offset to the parent address183m_value.GetScalar() += m_byte_offset;184}185} break;186
187case Value::ValueType::Scalar:188// try to extract the child value from the parent's scalar value189{190Scalar scalar(m_value.GetScalar());191scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);192m_value.GetScalar() = scalar;193}194break;195}196
197if (m_error.Success()) {198const bool thread_and_frame_only_if_stopped = true;199ExecutionContext exe_ctx(200GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));201if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {202Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;203m_error =204value.GetValueAsData(&exe_ctx, m_data, GetModule().get());205} else {206m_error.Clear(); // No value so nothing to read...207}208}209
210} else {211m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",212parent->GetError().AsCString());213}214} else {215m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");216}217
218return m_error.Success();219}
220
221bool ValueObjectChild::IsInScope() {222ValueObject *root(GetRoot());223if (root)224return root->IsInScope();225return false;226}
227