1 //===-- ValueObjectConstResult.cpp ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/ValueObject/ValueObjectConstResult.h"
11 #include "lldb/Symbol/CompilerType.h"
12 #include "lldb/Target/ExecutionContext.h"
13 #include "lldb/Target/ExecutionContextScope.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/DataBuffer.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Scalar.h"
19 #include "lldb/ValueObject/ValueObjectDynamicValue.h"
22 namespace lldb_private
{
27 using namespace lldb_private
;
29 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
31 uint32_t addr_byte_size
,
32 lldb::addr_t address
) {
33 auto manager_sp
= ValueObjectManager::Create();
34 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, byte_order
,
35 addr_byte_size
, address
))
39 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope
*exe_scope
,
40 ValueObjectManager
&manager
,
42 uint32_t addr_byte_size
,
44 : ValueObject(exe_scope
, manager
), m_impl(this, address
) {
46 SetValueIsValid(true);
47 m_data
.SetByteOrder(byte_order
);
48 m_data
.SetAddressByteSize(addr_byte_size
);
49 SetAddressTypeOfChildren(eAddressTypeLoad
);
52 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
53 const CompilerType
&compiler_type
,
55 const DataExtractor
&data
,
56 lldb::addr_t address
) {
57 auto manager_sp
= ValueObjectManager::Create();
58 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, compiler_type
,
63 ValueObjectConstResult::ValueObjectConstResult(
64 ExecutionContextScope
*exe_scope
, ValueObjectManager
&manager
,
65 const CompilerType
&compiler_type
, ConstString name
,
66 const DataExtractor
&data
, lldb::addr_t address
)
67 : ValueObject(exe_scope
, manager
), m_impl(this, address
) {
70 if (!m_data
.GetSharedDataBuffer()) {
71 DataBufferSP
shared_data_buffer(
72 new DataBufferHeap(data
.GetDataStart(), data
.GetByteSize()));
73 m_data
.SetData(shared_data_buffer
);
76 m_value
.GetScalar() = (uintptr_t)m_data
.GetDataStart();
77 m_value
.SetValueType(Value::ValueType::HostAddress
);
78 m_value
.SetCompilerType(compiler_type
);
81 SetValueIsValid(true);
82 SetAddressTypeOfChildren(eAddressTypeLoad
);
85 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
86 const CompilerType
&compiler_type
,
88 const lldb::DataBufferSP
&data_sp
,
89 lldb::ByteOrder data_byte_order
,
90 uint32_t data_addr_size
,
91 lldb::addr_t address
) {
92 auto manager_sp
= ValueObjectManager::Create();
93 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, compiler_type
,
94 name
, data_sp
, data_byte_order
,
95 data_addr_size
, address
))
99 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
100 Value
&value
, ConstString name
,
102 auto manager_sp
= ValueObjectManager::Create();
103 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, value
, name
,
108 ValueObjectConstResult::ValueObjectConstResult(
109 ExecutionContextScope
*exe_scope
, ValueObjectManager
&manager
,
110 const CompilerType
&compiler_type
, ConstString name
,
111 const lldb::DataBufferSP
&data_sp
, lldb::ByteOrder data_byte_order
,
112 uint32_t data_addr_size
, lldb::addr_t address
)
113 : ValueObject(exe_scope
, manager
), m_impl(this, address
) {
114 m_data
.SetByteOrder(data_byte_order
);
115 m_data
.SetAddressByteSize(data_addr_size
);
116 m_data
.SetData(data_sp
);
117 m_value
.GetScalar() = (uintptr_t)data_sp
->GetBytes();
118 m_value
.SetValueType(Value::ValueType::HostAddress
);
119 m_value
.SetCompilerType(compiler_type
);
122 SetValueIsValid(true);
123 SetAddressTypeOfChildren(eAddressTypeLoad
);
126 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
127 const CompilerType
&compiler_type
,
129 lldb::addr_t address
,
130 AddressType address_type
,
131 uint32_t addr_byte_size
) {
132 auto manager_sp
= ValueObjectManager::Create();
133 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, compiler_type
,
134 name
, address
, address_type
,
139 ValueObjectConstResult::ValueObjectConstResult(
140 ExecutionContextScope
*exe_scope
, ValueObjectManager
&manager
,
141 const CompilerType
&compiler_type
, ConstString name
, lldb::addr_t address
,
142 AddressType address_type
, uint32_t addr_byte_size
)
143 : ValueObject(exe_scope
, manager
), m_type_name(), m_impl(this, address
) {
144 m_value
.GetScalar() = address
;
145 m_data
.SetAddressByteSize(addr_byte_size
);
146 m_value
.GetScalar().GetData(m_data
, addr_byte_size
);
147 // m_value.SetValueType(Value::ValueType::HostAddress);
148 switch (address_type
) {
149 case eAddressTypeInvalid
:
150 m_value
.SetValueType(Value::ValueType::Scalar
);
152 case eAddressTypeFile
:
153 m_value
.SetValueType(Value::ValueType::FileAddress
);
155 case eAddressTypeLoad
:
156 m_value
.SetValueType(Value::ValueType::LoadAddress
);
158 case eAddressTypeHost
:
159 m_value
.SetValueType(Value::ValueType::HostAddress
);
162 m_value
.SetCompilerType(compiler_type
);
165 SetValueIsValid(true);
166 SetAddressTypeOfChildren(eAddressTypeLoad
);
169 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
171 auto manager_sp
= ValueObjectManager::Create();
172 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, std::move(error
)))
176 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope
*exe_scope
,
177 ValueObjectManager
&manager
,
179 : ValueObject(exe_scope
, manager
), m_impl(this) {
180 m_error
= std::move(error
);
184 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope
*exe_scope
,
185 ValueObjectManager
&manager
,
187 ConstString name
, Module
*module
)
188 : ValueObject(exe_scope
, manager
), m_impl(this) {
191 ExecutionContext exe_ctx
;
192 exe_scope
->CalculateExecutionContext(exe_ctx
);
193 m_error
= m_value
.GetValueAsData(&exe_ctx
, m_data
, module
);
196 ValueObjectConstResult::~ValueObjectConstResult() = default;
198 CompilerType
ValueObjectConstResult::GetCompilerTypeImpl() {
199 return m_value
.GetCompilerType();
202 lldb::ValueType
ValueObjectConstResult::GetValueType() const {
203 return eValueTypeConstResult
;
206 std::optional
<uint64_t> ValueObjectConstResult::GetByteSize() {
207 ExecutionContext
exe_ctx(GetExecutionContextRef());
209 if (auto size
= GetCompilerType().GetByteSize(
210 exe_ctx
.GetBestExecutionContextScope()))
216 void ValueObjectConstResult::SetByteSize(size_t size
) { m_byte_size
= size
; }
218 llvm::Expected
<uint32_t>
219 ValueObjectConstResult::CalculateNumChildren(uint32_t max
) {
220 ExecutionContext
exe_ctx(GetExecutionContextRef());
221 auto children_count
= GetCompilerType().GetNumChildren(true, &exe_ctx
);
223 return children_count
;
224 return *children_count
<= max
? *children_count
: max
;
227 ConstString
ValueObjectConstResult::GetTypeName() {
228 if (m_type_name
.IsEmpty())
229 m_type_name
= GetCompilerType().GetTypeName();
233 ConstString
ValueObjectConstResult::GetDisplayTypeName() {
234 return GetCompilerType().GetDisplayTypeName();
237 bool ValueObjectConstResult::UpdateValue() {
238 // Const value is always valid
239 SetValueIsValid(true);
243 bool ValueObjectConstResult::IsInScope() {
244 // A const result value is always in scope since it serializes all
245 // information needed to contain the constant value.
249 lldb::ValueObjectSP
ValueObjectConstResult::Dereference(Status
&error
) {
250 return m_impl
.Dereference(error
);
253 lldb::ValueObjectSP
ValueObjectConstResult::GetSyntheticChildAtOffset(
254 uint32_t offset
, const CompilerType
&type
, bool can_create
,
255 ConstString name_const_str
) {
256 return m_impl
.GetSyntheticChildAtOffset(offset
, type
, can_create
,
260 lldb::ValueObjectSP
ValueObjectConstResult::AddressOf(Status
&error
) {
261 return m_impl
.AddressOf(error
);
264 lldb::addr_t
ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address
,
265 AddressType
*address_type
) {
266 return m_impl
.GetAddressOf(scalar_is_load_address
, address_type
);
269 size_t ValueObjectConstResult::GetPointeeData(DataExtractor
&data
,
271 uint32_t item_count
) {
272 return m_impl
.GetPointeeData(data
, item_idx
, item_count
);
276 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic
) {
277 // Always recalculate dynamic values for const results as the memory that
278 // they might point to might have changed at any time.
279 if (use_dynamic
!= eNoDynamicValues
) {
281 ExecutionContext
exe_ctx(GetExecutionContextRef());
282 Process
*process
= exe_ctx
.GetProcessPtr();
283 if (process
&& process
->IsPossibleDynamicValue(*this))
284 m_dynamic_value
= new ValueObjectDynamicValue(*this, use_dynamic
);
286 if (m_dynamic_value
&& m_dynamic_value
->GetError().Success())
287 return m_dynamic_value
->GetSP();
289 return ValueObjectSP();
293 ValueObjectConstResult::DoCast(const CompilerType
&compiler_type
) {
294 return m_impl
.Cast(compiler_type
);
297 lldb::LanguageType
ValueObjectConstResult::GetPreferredDisplayLanguage() {
298 if (m_preferred_display_language
!= lldb::eLanguageTypeUnknown
)
299 return m_preferred_display_language
;
300 return GetCompilerTypeImpl().GetMinimumLanguage();