1 //===-- ValueObjectChild.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ValueObjectChild.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Symbol/CompilerType.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/Flags.h"
17 #include "lldb/Utility/Scalar.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/lldb-forward.h"
28 using namespace lldb_private;
30 ValueObjectChild::ValueObjectChild(
31 ValueObject &parent, const CompilerType &compiler_type,
32 const ConstString &name, uint64_t byte_size, int32_t byte_offset,
33 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
34 bool is_base_class, bool is_deref_of_parent,
35 AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
36 : ValueObject(parent), m_compiler_type(compiler_type),
37 m_byte_size(byte_size), m_byte_offset(byte_offset),
38 m_bitfield_bit_size(bitfield_bit_size),
39 m_bitfield_bit_offset(bitfield_bit_offset),
40 m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),
41 m_can_update_with_invalid_exe_ctx() {
43 SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
44 SetLanguageFlags(language_flags);
47 ValueObjectChild::~ValueObjectChild() {}
49 lldb::ValueType ValueObjectChild::GetValueType() const {
50 return m_parent->GetValueType();
53 size_t ValueObjectChild::CalculateNumChildren(uint32_t max) {
54 ExecutionContext exe_ctx(GetExecutionContextRef());
55 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
56 return children_count <= max ? children_count : max;
59 static void AdjustForBitfieldness(ConstString &name,
60 uint8_t bitfield_bit_size) {
61 if (name && bitfield_bit_size) {
62 const char *compiler_type_name = name.AsCString();
63 if (compiler_type_name) {
64 std::vector<char> bitfield_type_name(strlen(compiler_type_name) + 32, 0);
65 ::snprintf(&bitfield_type_name.front(), bitfield_type_name.size(),
66 "%s:%u", compiler_type_name, bitfield_bit_size);
67 name.SetCString(&bitfield_type_name.front());
72 ConstString ValueObjectChild::GetTypeName() {
73 if (m_type_name.IsEmpty()) {
74 m_type_name = GetCompilerType().GetConstTypeName();
75 AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);
80 ConstString ValueObjectChild::GetQualifiedTypeName() {
81 ConstString qualified_name = GetCompilerType().GetConstTypeName();
82 AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);
83 return qualified_name;
86 ConstString ValueObjectChild::GetDisplayTypeName() {
87 ConstString display_name = GetCompilerType().GetDisplayTypeName();
88 AdjustForBitfieldness(display_name, m_bitfield_bit_size);
92 LazyBool ValueObjectChild::CanUpdateWithInvalidExecutionContext() {
93 if (m_can_update_with_invalid_exe_ctx.hasValue())
94 return m_can_update_with_invalid_exe_ctx.getValue();
96 ValueObject *opinionated_parent =
97 m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
98 return (valobj->CanUpdateWithInvalidExecutionContext() ==
101 if (opinionated_parent)
102 return (m_can_update_with_invalid_exe_ctx =
103 opinionated_parent->CanUpdateWithInvalidExecutionContext())
106 return (m_can_update_with_invalid_exe_ctx =
107 this->ValueObject::CanUpdateWithInvalidExecutionContext())
111 bool ValueObjectChild::UpdateValue() {
113 SetValueIsValid(false);
114 ValueObject *parent = m_parent;
116 if (parent->UpdateValueIfNeeded(false)) {
117 m_value.SetCompilerType(GetCompilerType());
119 CompilerType parent_type(parent->GetCompilerType());
120 // Copy the parent scalar value and the scalar value type
121 m_value.GetScalar() = parent->GetValue().GetScalar();
122 Value::ValueType value_type = parent->GetValue().GetValueType();
123 m_value.SetValueType(value_type);
125 Flags parent_type_flags(parent_type.GetTypeInfo());
126 const bool is_instance_ptr_base =
127 ((m_is_base_class) &&
128 (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));
130 if (parent->GetCompilerType().ShouldTreatScalarValueAsAddress()) {
131 lldb::addr_t addr = parent->GetPointerValue();
132 m_value.GetScalar() = addr;
134 if (addr == LLDB_INVALID_ADDRESS) {
135 m_error.SetErrorString("parent address is invalid.");
136 } else if (addr == 0) {
137 m_error.SetErrorString("parent is NULL");
139 m_value.GetScalar() += m_byte_offset;
140 AddressType addr_type = parent->GetAddressTypeOfChildren();
143 case eAddressTypeFile: {
144 lldb::ProcessSP process_sp(GetProcessSP());
145 if (process_sp && process_sp->IsAlive())
146 m_value.SetValueType(Value::eValueTypeLoadAddress);
148 m_value.SetValueType(Value::eValueTypeFileAddress);
150 case eAddressTypeLoad:
151 m_value.SetValueType(is_instance_ptr_base
152 ? Value::eValueTypeScalar
153 : Value::eValueTypeLoadAddress);
155 case eAddressTypeHost:
156 m_value.SetValueType(Value::eValueTypeHostAddress);
158 case eAddressTypeInvalid:
159 // TODO: does this make sense?
160 m_value.SetValueType(Value::eValueTypeScalar);
165 switch (value_type) {
166 case Value::eValueTypeLoadAddress:
167 case Value::eValueTypeFileAddress:
168 case Value::eValueTypeHostAddress: {
170 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
171 if (addr == LLDB_INVALID_ADDRESS) {
172 m_error.SetErrorString("parent address is invalid.");
173 } else if (addr == 0) {
174 m_error.SetErrorString("parent is NULL");
176 // Set this object's scalar value to the address of its value by
177 // adding its byte offset to the parent address
178 m_value.GetScalar() += GetByteOffset();
182 case Value::eValueTypeScalar:
183 // try to extract the child value from the parent's scalar value
185 Scalar scalar(m_value.GetScalar());
186 if (m_bitfield_bit_size)
187 scalar.ExtractBitfield(m_bitfield_bit_size,
188 m_bitfield_bit_offset);
190 scalar.ExtractBitfield(8 * m_byte_size, 8 * m_byte_offset);
191 m_value.GetScalar() = scalar;
195 m_error.SetErrorString("parent has invalid value.");
200 if (m_error.Success()) {
201 const bool thread_and_frame_only_if_stopped = true;
202 ExecutionContext exe_ctx(
203 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
204 if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
205 Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
207 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
209 m_error.Clear(); // No value so nothing to read...
214 m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
215 parent->GetError().AsCString());
218 m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
221 return m_error.Success();
224 bool ValueObjectChild::IsInScope() {
225 ValueObject *root(GetRoot());
227 return root->IsInScope();