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/Core/ValueObjectConstResult.h"
11 #include "lldb/Core/ValueObjectDynamicValue.h"
12 #include "lldb/Symbol/CompilerType.h"
13 #include "lldb/Target/ExecutionContext.h"
14 #include "lldb/Target/ExecutionContextScope.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/DataBuffer.h"
17 #include "lldb/Utility/DataBufferHeap.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Scalar.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
,
103 auto manager_sp
= ValueObjectManager::Create();
104 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, value
, name
,
109 ValueObjectConstResult::ValueObjectConstResult(
110 ExecutionContextScope
*exe_scope
, ValueObjectManager
&manager
,
111 const CompilerType
&compiler_type
, ConstString name
,
112 const lldb::DataBufferSP
&data_sp
, lldb::ByteOrder data_byte_order
,
113 uint32_t data_addr_size
, lldb::addr_t address
)
114 : ValueObject(exe_scope
, manager
), m_impl(this, address
) {
115 m_data
.SetByteOrder(data_byte_order
);
116 m_data
.SetAddressByteSize(data_addr_size
);
117 m_data
.SetData(data_sp
);
118 m_value
.GetScalar() = (uintptr_t)data_sp
->GetBytes();
119 m_value
.SetValueType(Value::ValueType::HostAddress
);
120 m_value
.SetCompilerType(compiler_type
);
123 SetValueIsValid(true);
124 SetAddressTypeOfChildren(eAddressTypeLoad
);
127 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
128 const CompilerType
&compiler_type
,
130 lldb::addr_t address
,
131 AddressType address_type
,
132 uint32_t addr_byte_size
) {
133 auto manager_sp
= ValueObjectManager::Create();
134 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, compiler_type
,
135 name
, address
, address_type
,
140 ValueObjectConstResult::ValueObjectConstResult(
141 ExecutionContextScope
*exe_scope
, ValueObjectManager
&manager
,
142 const CompilerType
&compiler_type
, ConstString name
, lldb::addr_t address
,
143 AddressType address_type
, uint32_t addr_byte_size
)
144 : ValueObject(exe_scope
, manager
), m_type_name(),
145 m_impl(this, address
) {
146 m_value
.GetScalar() = address
;
147 m_data
.SetAddressByteSize(addr_byte_size
);
148 m_value
.GetScalar().GetData(m_data
, addr_byte_size
);
149 // m_value.SetValueType(Value::ValueType::HostAddress);
150 switch (address_type
) {
151 case eAddressTypeInvalid
:
152 m_value
.SetValueType(Value::ValueType::Scalar
);
154 case eAddressTypeFile
:
155 m_value
.SetValueType(Value::ValueType::FileAddress
);
157 case eAddressTypeLoad
:
158 m_value
.SetValueType(Value::ValueType::LoadAddress
);
160 case eAddressTypeHost
:
161 m_value
.SetValueType(Value::ValueType::HostAddress
);
164 m_value
.SetCompilerType(compiler_type
);
167 SetValueIsValid(true);
168 SetAddressTypeOfChildren(eAddressTypeLoad
);
171 ValueObjectSP
ValueObjectConstResult::Create(ExecutionContextScope
*exe_scope
,
172 const Status
&error
) {
173 auto manager_sp
= ValueObjectManager::Create();
174 return (new ValueObjectConstResult(exe_scope
, *manager_sp
, error
))->GetSP();
177 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope
*exe_scope
,
178 ValueObjectManager
&manager
,
180 : ValueObject(exe_scope
, manager
), m_impl(this) {
185 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope
*exe_scope
,
186 ValueObjectManager
&manager
,
188 ConstString name
, Module
*module
)
189 : ValueObject(exe_scope
, manager
), m_impl(this) {
192 ExecutionContext exe_ctx
;
193 exe_scope
->CalculateExecutionContext(exe_ctx
);
194 m_error
= m_value
.GetValueAsData(&exe_ctx
, m_data
, module
);
197 ValueObjectConstResult::~ValueObjectConstResult() = default;
199 CompilerType
ValueObjectConstResult::GetCompilerTypeImpl() {
200 return m_value
.GetCompilerType();
203 lldb::ValueType
ValueObjectConstResult::GetValueType() const {
204 return eValueTypeConstResult
;
207 std::optional
<uint64_t> ValueObjectConstResult::GetByteSize() {
208 ExecutionContext
exe_ctx(GetExecutionContextRef());
211 GetCompilerType().GetByteSize(exe_ctx
.GetBestExecutionContextScope()))
217 void ValueObjectConstResult::SetByteSize(size_t size
) { m_byte_size
= size
; }
219 size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max
) {
220 ExecutionContext
exe_ctx(GetExecutionContextRef());
221 auto children_count
= GetCompilerType().GetNumChildren(true, &exe_ctx
);
222 return children_count
<= max
? children_count
: max
;
225 ConstString
ValueObjectConstResult::GetTypeName() {
226 if (m_type_name
.IsEmpty())
227 m_type_name
= GetCompilerType().GetTypeName();
231 ConstString
ValueObjectConstResult::GetDisplayTypeName() {
232 return GetCompilerType().GetDisplayTypeName();
235 bool ValueObjectConstResult::UpdateValue() {
236 // Const value is always valid
237 SetValueIsValid(true);
241 bool ValueObjectConstResult::IsInScope() {
242 // A const result value is always in scope since it serializes all
243 // information needed to contain the constant value.
247 lldb::ValueObjectSP
ValueObjectConstResult::Dereference(Status
&error
) {
248 return m_impl
.Dereference(error
);
251 lldb::ValueObjectSP
ValueObjectConstResult::GetSyntheticChildAtOffset(
252 uint32_t offset
, const CompilerType
&type
, bool can_create
,
253 ConstString name_const_str
) {
254 return m_impl
.GetSyntheticChildAtOffset(offset
, type
, can_create
,
258 lldb::ValueObjectSP
ValueObjectConstResult::AddressOf(Status
&error
) {
259 return m_impl
.AddressOf(error
);
262 lldb::addr_t
ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address
,
263 AddressType
*address_type
) {
264 return m_impl
.GetAddressOf(scalar_is_load_address
, address_type
);
267 ValueObject
*ValueObjectConstResult::CreateChildAtIndex(
268 size_t idx
, bool synthetic_array_member
, int32_t synthetic_index
) {
269 return m_impl
.CreateChildAtIndex(idx
, synthetic_array_member
,
273 size_t ValueObjectConstResult::GetPointeeData(DataExtractor
&data
,
275 uint32_t item_count
) {
276 return m_impl
.GetPointeeData(data
, item_idx
, item_count
);
280 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic
) {
281 // Always recalculate dynamic values for const results as the memory that
282 // they might point to might have changed at any time.
283 if (use_dynamic
!= eNoDynamicValues
) {
285 ExecutionContext
exe_ctx(GetExecutionContextRef());
286 Process
*process
= exe_ctx
.GetProcessPtr();
287 if (process
&& process
->IsPossibleDynamicValue(*this))
288 m_dynamic_value
= new ValueObjectDynamicValue(*this, use_dynamic
);
290 if (m_dynamic_value
&& m_dynamic_value
->GetError().Success())
291 return m_dynamic_value
->GetSP();
293 return ValueObjectSP();
297 ValueObjectConstResult::DoCast(const CompilerType
&compiler_type
) {
298 return m_impl
.Cast(compiler_type
);
301 lldb::LanguageType
ValueObjectConstResult::GetPreferredDisplayLanguage() {
302 if (m_preferred_display_language
!= lldb::eLanguageTypeUnknown
)
303 return m_preferred_display_language
;
304 return GetCompilerTypeImpl().GetMinimumLanguage();