LLDB mainline
ValueObjectCast.cpp
Go to the documentation of this file.
1//===-- ValueObjectCast.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/Utility/Scalar.h"
15#include "lldb/Utility/Status.h"
17#include <optional>
18
19using namespace lldb_private;
20
22 llvm::StringRef name,
23 const CompilerType &cast_type) {
24 ValueObjectCast *cast_valobj_ptr =
25 new ValueObjectCast(parent, name, cast_type);
26 return cast_valobj_ptr->GetSP();
27}
28
29ValueObjectCast::ValueObjectCast(ValueObject &parent, llvm::StringRef name,
30 const CompilerType &cast_type)
31 : ValueObject(parent), m_cast_type(cast_type) {
32 SetName(name);
33 m_value.SetCompilerType(cast_type);
34}
35
37
39
40llvm::Expected<uint32_t> ValueObjectCast::CalculateNumChildren(uint32_t max) {
42 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
43 if (!children_count)
44 return children_count;
45 return *children_count <= max ? *children_count : max;
46}
47
48llvm::Expected<uint64_t> ValueObjectCast::GetByteSize() {
50 return m_value.GetValueByteSize(nullptr, &exe_ctx);
51}
52
54 // Let our parent answer global, local, argument, etc...
55 return m_parent->GetValueType();
56}
57
59 SetValueIsValid(false);
60 m_error.Clear();
61
62 if (m_parent->UpdateValueIfNeeded(false)) {
63 Value old_value(m_value);
64 m_update_point.SetUpdated();
65 m_value = m_parent->GetValue();
66 CompilerType compiler_type(GetCompilerType());
67 m_value.SetCompilerType(compiler_type);
68 SetAddressTypeOfChildren(m_parent->GetAddressTypeOfChildren());
69 // A cast that drops __ptrauth strips the signature bits from the value.
70 if (m_parent->GetCompilerType().HasPointerAuthQualifier() &&
71 !compiler_type.HasPointerAuthQualifier()) {
72 if (auto stripped = m_parent->GetStrippedPointerValue(
73 m_parent->GetPointerValue().address)) {
74 m_value.GetScalar() = *stripped;
76 }
77 }
78 if (!CanProvideValue()) {
79 // this value object represents an aggregate type whose children have
80 // values, but this object does not. So we say we are changed if our
81 // location has changed.
82 SetValueDidChange(m_value.GetValueType() != old_value.GetValueType() ||
83 m_value.GetScalar() != old_value.GetScalar());
84 }
86 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
87 SetValueDidChange(m_parent->GetValueDidChange());
88 return true;
89 }
90
91 // The dynamic value failed to get an error, pass the error along
92 if (m_error.Success() && m_parent->GetError().Fail())
93 m_error = m_parent->GetError().Clone();
94 SetValueIsValid(false);
95 return false;
96}
97
98bool ValueObjectCast::IsInScope() { return m_parent->IsInScope(); }
99
101 // A parent without storage, such as a register variable, keeps the base path.
102 return UpdateValueIfNeeded(false) &&
103 m_value.GetValueType() == Value::ValueType::Scalar &&
104 m_parent->GetAddressOf(/*scalar_is_load_address=*/false).type !=
106}
107
109ValueObjectCast::GetAddressOf(bool scalar_is_load_address) {
111 return m_parent->GetAddressOf(scalar_is_load_address);
112 return ValueObject::GetAddressOf(scalar_is_load_address);
113}
114
117 return m_parent->GetLocationAsCString();
119}
120
122 // UpdateValue rebuilds a stripped pointer from the parent; a write is lost.
123 if (m_parent->GetCompilerType().HasPointerAuthQualifier() &&
124 !GetCompilerType().HasPointerAuthQualifier())
125 return llvm::createStringError(
126 "cannot modify a value through a cast that drops __ptrauth");
128}
Generic representation of a type in a programming language.
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
CompilerType GetCompilerTypeImpl() override
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
llvm::Expected< uint64_t > GetByteSize() override
static lldb::ValueObjectSP Create(ValueObject &parent, llvm::StringRef name, const CompilerType &cast_type)
bool LivesInParentStorage()
True if the value is a scalar but the cast lives in the parent's storage.
llvm::Error CanSetValue() override
Check if the value may be writable.
ValueObjectCast(ValueObject &parent, llvm::StringRef name, const CompilerType &cast_type)
AddrAndType GetAddressOf(bool scalar_is_load_address=true) override
const char * GetLocationAsCString() override
lldb::ValueType GetValueType() const override
void SetValueIsValid(bool valid)
EvaluationPoint m_update_point
Stores both the stop id and the full context at which this value was last updated.
ValueObject(ExecutionContextScope *exe_scope, ValueObjectManager &manager, AddressType child_ptr_or_ref_addr_type=eAddressTypeLoad)
Use this constructor to create a "root variable object".
lldb::ValueObjectSP GetSP()
virtual llvm::Error CanSetValue()
Check if the value may be writable.
Status m_error
An error object that can describe any errors that occur when updating values.
DataExtractor m_data
A data extractor that can be used to extract the value.
void SetValueDidChange(bool value_changed)
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.
void SetName(llvm::StringRef name)
Change the name of the current ValueObject.
bool UpdateValueIfNeeded(bool update_format=true)
CompilerType GetCompilerType()
virtual const char * GetLocationAsCString()
const ExecutionContextRef & GetExecutionContextRef() const
virtual AddrAndType GetAddressOf(bool scalar_is_load_address=true)
void SetAddressTypeOfChildren(AddressType at)
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:114
@ Scalar
A raw scalar value.
Definition Value.h:46
ValueType GetValueType() const
Definition Value.cpp:112
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP