LLDB mainline
ValueObjectChild.cpp
Go to the documentation of this file.
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
10
11#include "lldb/Core/Value.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"
20
21#include <functional>
22#include <memory>
23#include <vector>
24
25#include <cstdio>
26#include <cstring>
27
28using namespace lldb_private;
29
31 ValueObject &parent, const CompilerType &compiler_type, ConstString name,
32 uint64_t byte_size, int32_t byte_offset, uint32_t bitfield_bit_size,
33 uint32_t bitfield_bit_offset, bool is_base_class, bool is_deref_of_parent,
34 AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
35 : ValueObject(parent), m_compiler_type(compiler_type),
36 m_byte_size(byte_size), m_byte_offset(byte_offset),
37 m_bitfield_bit_size(bitfield_bit_size),
38 m_bitfield_bit_offset(bitfield_bit_offset),
39 m_is_base_class(is_base_class), m_is_deref_of_parent(is_deref_of_parent),
41 m_name = name;
42 SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
43 SetLanguageFlags(language_flags);
44}
45
47
49 return m_parent->GetValueType();
50}
51
52llvm::Expected<uint32_t> ValueObjectChild::CalculateNumChildren(uint32_t max) {
54 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55 if (!children_count)
56 return children_count;
57 return *children_count <= max ? *children_count : max;
58}
59
61 uint8_t bitfield_bit_size) {
62 if (name && bitfield_bit_size)
63 name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());
64}
65
73
75 ConstString qualified_name = GetCompilerType().GetTypeName();
77 return qualified_name;
78}
79
85
89 if (m_parent) {
90 ValueObject *opinionated_parent =
91 m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
92 return (valobj->CanUpdateWithInvalidExecutionContext() ==
94 });
95 if (opinionated_parent)
97 opinionated_parent->CanUpdateWithInvalidExecutionContext());
98 }
101}
102
104 m_error.Clear();
105 SetValueIsValid(false);
106 ValueObject *parent = m_parent;
107 if (parent) {
108 if (parent->UpdateValueIfNeeded(false)) {
109 m_value.SetCompilerType(GetCompilerType());
110
111 CompilerType parent_type(parent->GetCompilerType());
112 // Copy the parent scalar value and the scalar value type
113 m_value.GetScalar() = parent->GetValue().GetScalar();
114 m_value.SetValueType(parent->GetValue().GetValueType());
115
116 Flags parent_type_flags(parent_type.GetTypeInfo());
117 // If instances of both the parent's type and the base class are pointers,
118 // as with a Swift class and its Swift base class, the base class child's
119 // value is the parent's pointer. An Objective-C base class of a Swift
120 // class is instead stored at the address that the pointer points to.
121 const bool is_instance_ptr_base =
123 parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer) &&
125 .AnySet(lldb::eTypeInstanceIsPointer);
126
128 lldb::addr_t addr = parent->GetPointerValue().address;
129 // The language runtime knows how a pointer that is an instance of the
130 // parent's type refers to the instance.
131 if (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer))
132 if (lldb::ProcessSP process_sp = GetProcessSP())
133 if (LanguageRuntime *runtime = process_sp->GetLanguageRuntime(
134 parent_type.GetMinimumLanguage())) {
135 if (llvm::Expected<lldb::addr_t> instance_addr =
136 runtime->FixupPointerValue(addr, parent_type)) {
137 addr = *instance_addr;
138 // The runtime returns an address in the target.
140 } else {
141 m_error = Status::FromError(instance_addr.takeError());
142 }
143 }
144 m_value.GetScalar() = addr;
145
146 switch (parent->GetAddressTypeOfChildren()) {
147 case eAddressTypeFile: {
148 lldb::ProcessSP process_sp(GetProcessSP());
149 if (process_sp && process_sp->IsAlive())
151 else
153 } break;
154 case eAddressTypeLoad:
155 m_value.SetValueType(is_instance_ptr_base
158 break;
159 case eAddressTypeHost:
161 break;
163 // TODO: does this make sense?
164 m_value.SetValueType(Value::ValueType::Scalar);
165 break;
166 }
167 }
168 switch (m_value.GetValueType()) {
170 break;
174 lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
175 if (addr == LLDB_INVALID_ADDRESS) {
176 m_error = Status::FromErrorString("parent address is invalid.");
177 } else if (addr == 0) {
178 m_error = Status::FromErrorString("parent is NULL");
179 } else {
180 // If a bitfield doesn't fit into the child_byte_size'd window at
181 // child_byte_offset, move the window forward until it fits. The
182 // problem here is that Value has no notion of bitfields and thus the
183 // Value's DataExtractor is sized like the bitfields CompilerType; a
184 // sequence of bitfields, however, can be larger than their underlying
185 // type.
187 const bool thread_and_frame_only_if_stopped = true;
189 thread_and_frame_only_if_stopped));
190 if (auto type_bit_size =
191 llvm::expectedToOptional(GetCompilerType().GetBitSize(
192 exe_ctx.GetBestExecutionContextScope()))) {
193 uint64_t bitfield_end =
195 if (bitfield_end > *type_bit_size) {
196 uint64_t overhang_bytes =
197 (bitfield_end - *type_bit_size + 7) / 8;
198 m_byte_offset += overhang_bytes;
199 m_bitfield_bit_offset -= overhang_bytes * 8;
200 }
201 }
202 }
203
204 // Set this object's scalar value to the address of its value by
205 // adding its byte offset to the parent address
206 m_value.GetScalar() += m_byte_offset;
207 }
208 } break;
209
211 // try to extract the child value from the parent's scalar value
212 {
213 Scalar scalar(m_value.GetScalar());
215 m_value.GetScalar() = scalar;
216 }
217 break;
218 }
219
220 if (m_error.Success()) {
221 const bool thread_and_frame_only_if_stopped = true;
222 ExecutionContext exe_ctx(
223 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
224 if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
225 Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
226 m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
227 } else {
228 m_error.Clear(); // No value so nothing to read...
229 }
230 }
231
232 } else {
234 "parent failed to evaluate: %s", parent->GetError().AsCString());
235 }
236 } else {
238 "ValueObjectChild has a NULL parent ValueObject.");
239 }
240
241 return m_error.Success();
242}
243
245 ValueObject *root(GetRoot());
246 if (root)
247 return root->IsInScope();
248 return false;
249}
static void AdjustForBitfieldness(ConstString &name, uint8_t bitfield_bit_size)
Generic representation of a type in a programming language.
lldb::LanguageType GetMinimumLanguage()
ConstString GetDisplayTypeName() const
bool ShouldTreatScalarValueAsAddress() const
ConstString GetTypeName(bool BaseOnly=false) const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
A uniqued constant string class.
Definition ConstString.h:40
void SetString(llvm::StringRef s)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
A class to manage flags.
Definition Flags.h:22
bool AnySet(ValueType mask) const
Test one or more flags.
Definition Flags.h:90
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition Scalar.cpp:816
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition Status.cpp:194
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:136
ConstString GetTypeName() override
ConstString GetQualifiedTypeName() override
ValueObjectChild(ValueObject &parent, const CompilerType &compiler_type, ConstString name, uint64_t byte_size, int32_t byte_offset, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool is_base_class, bool is_deref_of_parent, AddressType child_ptr_or_ref_addr_type, uint64_t language_flags)
lldb::ValueType GetValueType() const override
ConstString GetDisplayTypeName() override
LazyBool CanUpdateWithInvalidExecutionContext() override
std::optional< LazyBool > m_can_update_with_invalid_exe_ctx
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
void SetValueIsValid(bool valid)
virtual void SetLanguageFlags(uint64_t flags)
Status m_error
An error object that can describe any errors that occur when updating values.
lldb::ProcessSP GetProcessSP() const
DataExtractor m_data
A data extractor that can be used to extract the value.
virtual LazyBool CanUpdateWithInvalidExecutionContext()
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr)
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
bool UpdateValueIfNeeded(bool update_format=true)
AddressType GetAddressTypeOfChildren()
const Status & GetError()
CompilerType GetCompilerType()
ConstString m_name
The name of this object.
const ExecutionContextRef & GetExecutionContextRef() const
const Value & GetValue() const
void SetAddressTypeOfChildren(AddressType at)
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:114
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition Value.cpp:324
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:53
@ FileAddress
A file address value.
Definition Value.h:48
@ LoadAddress
A load address value.
Definition Value.h:50
@ Scalar
A raw scalar value.
Definition Value.h:46
ValueType GetValueType() const
Definition Value.cpp:112
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
@ eAddressTypeFile
Address is an address as found in an object or symbol file.
@ eAddressTypeLoad
Address is an address as in the current target inferior process.
@ eAddressTypeHost
Address is an address in the process that is running this code.
std::shared_ptr< lldb_private::Process > ProcessSP
uint64_t addr_t
Definition lldb-types.h:80