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"
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
30 ValueObject &parent, const CompilerType &compiler_type,
31 ConstString name, uint64_t byte_size, int32_t byte_offset,
32 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
33 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),
40 m_can_update_with_invalid_exe_ctx() {
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
54 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
55 return children_count <= max ? children_count : max;
56}
57
59 uint8_t bitfield_bit_size) {
60 if (name && bitfield_bit_size)
61 name.SetString(llvm::formatv("{0}:{1}", name, bitfield_bit_size).str());
62}
63
65 if (m_type_name.IsEmpty()) {
68 }
69 return m_type_name;
70}
71
73 ConstString qualified_name = GetCompilerType().GetTypeName();
75 return qualified_name;
76}
77
81 return display_name;
82}
83
87 if (m_parent) {
88 ValueObject *opinionated_parent =
89 m_parent->FollowParentChain([](ValueObject *valobj) -> bool {
90 return (valobj->CanUpdateWithInvalidExecutionContext() ==
92 });
93 if (opinionated_parent)
95 opinionated_parent->CanUpdateWithInvalidExecutionContext());
96 }
99}
100
102 m_error.Clear();
103 SetValueIsValid(false);
104 ValueObject *parent = m_parent;
105 if (parent) {
106 if (parent->UpdateValueIfNeeded(false)) {
108
109 CompilerType parent_type(parent->GetCompilerType());
110 // Copy the parent scalar value and the scalar value type
111 m_value.GetScalar() = parent->GetValue().GetScalar();
113
114 Flags parent_type_flags(parent_type.GetTypeInfo());
115 const bool is_instance_ptr_base =
116 ((m_is_base_class) &&
117 (parent_type_flags.AnySet(lldb::eTypeInstanceIsPointer)));
118
120 m_value.GetScalar() = parent->GetPointerValue();
121
122 switch (parent->GetAddressTypeOfChildren()) {
123 case eAddressTypeFile: {
124 lldb::ProcessSP process_sp(GetProcessSP());
125 if (process_sp && process_sp->IsAlive())
127 else
129 } break;
130 case eAddressTypeLoad:
131 m_value.SetValueType(is_instance_ptr_base
134 break;
135 case eAddressTypeHost:
137 break;
139 // TODO: does this make sense?
141 break;
142 }
143 }
144 switch (m_value.GetValueType()) {
146 break;
151 if (addr == LLDB_INVALID_ADDRESS) {
152 m_error.SetErrorString("parent address is invalid.");
153 } else if (addr == 0) {
154 m_error.SetErrorString("parent is NULL");
155 } else {
156 // If a bitfield doesn't fit into the child_byte_size'd window at
157 // child_byte_offset, move the window forward until it fits. The
158 // problem here is that Value has no notion of bitfields and thus the
159 // Value's DataExtractor is sized like the bitfields CompilerType; a
160 // sequence of bitfields, however, can be larger than their underlying
161 // type.
163 const bool thread_and_frame_only_if_stopped = true;
165 thread_and_frame_only_if_stopped));
166 if (auto type_bit_size = GetCompilerType().GetBitSize(
167 exe_ctx.GetBestExecutionContextScope())) {
168 uint64_t bitfield_end =
170 if (bitfield_end > *type_bit_size) {
171 uint64_t overhang_bytes =
172 (bitfield_end - *type_bit_size + 7) / 8;
173 m_byte_offset += overhang_bytes;
174 m_bitfield_bit_offset -= overhang_bytes * 8;
175 }
176 }
177 }
178
179 // Set this object's scalar value to the address of its value by
180 // adding its byte offset to the parent address
182 }
183 } break;
184
186 // try to extract the child value from the parent's scalar value
187 {
188 Scalar scalar(m_value.GetScalar());
190 m_value.GetScalar() = scalar;
191 }
192 break;
193 }
194
195 if (m_error.Success()) {
196 const bool thread_and_frame_only_if_stopped = true;
197 ExecutionContext exe_ctx(
198 GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
199 if (GetCompilerType().GetTypeInfo() & lldb::eTypeHasValue) {
200 Value &value = is_instance_ptr_base ? m_parent->GetValue() : m_value;
201 m_error =
202 value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
203 } else {
204 m_error.Clear(); // No value so nothing to read...
205 }
206 }
207
208 } else {
209 m_error.SetErrorStringWithFormat("parent failed to evaluate: %s",
210 parent->GetError().AsCString());
211 }
212 } else {
213 m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
214 }
215
216 return m_error.Success();
217}
218
220 ValueObject *root(GetRoot());
221 if (root)
222 return root->IsInScope();
223 return false;
224}
static void AdjustForBitfieldness(ConstString &name, uint8_t bitfield_bit_size)
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
ConstString GetDisplayTypeName() const
bool ShouldTreatScalarValueAsAddress() const
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
ConstString GetTypeName(bool BaseOnly=false) const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:39
void SetString(const llvm::StringRef &s)
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:303
"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
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:334
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition: Scalar.cpp:798
void Clear()
Clear the object state.
Definition: Status.cpp:167
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:255
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
bool Success() const
Test for success condition.
Definition: Status.cpp:287
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
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
ValueObject * FollowParentChain(std::function< bool(ValueObject *)>)
Given a ValueObject, loop over itself and its parent, and its parent's parent, .
virtual bool IsInScope()
Definition: ValueObject.h:420
lldb::addr_t GetPointerValue(AddressType *address_type=nullptr)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:791
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
lldb::ProcessSP GetProcessSP() const
Definition: ValueObject.h:338
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
virtual lldb::ValueType GetValueType() const =0
virtual LazyBool CanUpdateWithInvalidExecutionContext()
Definition: ValueObject.h:950
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:840
bool UpdateValueIfNeeded(bool update_format=true)
AddressType GetAddressTypeOfChildren()
const Status & GetError()
virtual uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr)
Definition: ValueObject.h:378
ConstString m_name
The name of this object.
Definition: ValueObject.h:849
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
const Value & GetValue() const
Definition: ValueObject.h:497
void SetAddressTypeOfChildren(AddressType at)
Definition: ValueObject.h:759
const Scalar & GetScalar() const
Definition: Value.h:112
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:299
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
@ FileAddress
A file address value.
@ LoadAddress
A load address value.
@ Scalar
A raw scalar value.
ValueType GetValueType() const
Definition: Value.cpp:107
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:252
void SetValueType(ValueType value_type)
Definition: Value.h:89
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
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.
uint64_t addr_t
Definition: lldb-types.h:83