1//===-- ValueObjectConstResult.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
9#include "lldb/Core/ValueObjectConstResult.h"
10
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"
20#include <optional>
21
22namespace lldb_private {
23class Module;
24}
25
26using namespace lldb;
27using namespace lldb_private;
28
29ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
30 ByteOrder byte_order,
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))
36 ->GetSP();
37}
38
39ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
40 ValueObjectManager &manager,
41 ByteOrder byte_order,
42 uint32_t addr_byte_size,
43 lldb::addr_t address)
44 : ValueObject(exe_scope, manager), m_impl(this, address) {
45 SetIsConstant();
46 SetValueIsValid(true);
47 m_data.SetByteOrder(byte_order);
48 m_data.SetAddressByteSize(addr_byte_size);
49 SetAddressTypeOfChildren(eAddressTypeLoad);
50}
51
52ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
53 const CompilerType &compiler_type,
54 ConstString name,
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,
59 name, data, address))
60 ->GetSP();
61}
62
63ValueObjectConstResult::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) {
68 m_data = data;
69
70 if (!m_data.GetSharedDataBuffer()) {
71 DataBufferSP shared_data_buffer(
72 new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
73 m_data.SetData(data_sp: shared_data_buffer);
74 }
75
76 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
77 m_value.SetValueType(Value::ValueType::HostAddress);
78 m_value.SetCompilerType(compiler_type);
79 m_name = name;
80 SetIsConstant();
81 SetValueIsValid(true);
82 SetAddressTypeOfChildren(eAddressTypeLoad);
83}
84
85ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
86 const CompilerType &compiler_type,
87 ConstString name,
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))
96 ->GetSP();
97}
98
99ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
100 Value &value,
101 ConstString name,
102 Module *module) {
103 auto manager_sp = ValueObjectManager::Create();
104 return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
105 module))
106 ->GetSP();
107}
108
109ValueObjectConstResult::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);
121 m_name = name;
122 SetIsConstant();
123 SetValueIsValid(true);
124 SetAddressTypeOfChildren(eAddressTypeLoad);
125}
126
127ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
128 const CompilerType &compiler_type,
129 ConstString name,
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,
136 addr_byte_size))
137 ->GetSP();
138}
139
140ValueObjectConstResult::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(data&: m_data, limit_byte_size: addr_byte_size);
149 // m_value.SetValueType(Value::ValueType::HostAddress);
150 switch (address_type) {
151 case eAddressTypeInvalid:
152 m_value.SetValueType(Value::ValueType::Scalar);
153 break;
154 case eAddressTypeFile:
155 m_value.SetValueType(Value::ValueType::FileAddress);
156 break;
157 case eAddressTypeLoad:
158 m_value.SetValueType(Value::ValueType::LoadAddress);
159 break;
160 case eAddressTypeHost:
161 m_value.SetValueType(Value::ValueType::HostAddress);
162 break;
163 }
164 m_value.SetCompilerType(compiler_type);
165 m_name = name;
166 SetIsConstant();
167 SetValueIsValid(true);
168 SetAddressTypeOfChildren(eAddressTypeLoad);
169}
170
171ValueObjectSP 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();
175}
176
177ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
178 ValueObjectManager &manager,
179 const Status &error)
180 : ValueObject(exe_scope, manager), m_impl(this) {
181 m_error = error;
182 SetIsConstant();
183}
184
185ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
186 ValueObjectManager &manager,
187 const Value &value,
188 ConstString name, Module *module)
189 : ValueObject(exe_scope, manager), m_impl(this) {
190 m_value = value;
191 m_name = name;
192 ExecutionContext exe_ctx;
193 exe_scope->CalculateExecutionContext(exe_ctx);
194 m_error = m_value.GetValueAsData(exe_ctx: &exe_ctx, data&: m_data, module);
195}
196
197ValueObjectConstResult::~ValueObjectConstResult() = default;
198
199CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
200 return m_value.GetCompilerType();
201}
202
203lldb::ValueType ValueObjectConstResult::GetValueType() const {
204 return eValueTypeConstResult;
205}
206
207std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
208 ExecutionContext exe_ctx(GetExecutionContextRef());
209 if (!m_byte_size) {
210 if (auto size =
211 GetCompilerType().GetByteSize(exe_scope: exe_ctx.GetBestExecutionContextScope()))
212 SetByteSize(*size);
213 }
214 return m_byte_size;
215}
216
217void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
218
219llvm::Expected<uint32_t>
220ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
221 ExecutionContext exe_ctx(GetExecutionContextRef());
222 auto children_count = GetCompilerType().GetNumChildren(omit_empty_base_classes: true, exe_ctx: &exe_ctx);
223 if (!children_count)
224 return children_count;
225 return *children_count <= max ? *children_count : max;
226}
227
228ConstString ValueObjectConstResult::GetTypeName() {
229 if (m_type_name.IsEmpty())
230 m_type_name = GetCompilerType().GetTypeName();
231 return m_type_name;
232}
233
234ConstString ValueObjectConstResult::GetDisplayTypeName() {
235 return GetCompilerType().GetDisplayTypeName();
236}
237
238bool ValueObjectConstResult::UpdateValue() {
239 // Const value is always valid
240 SetValueIsValid(true);
241 return true;
242}
243
244bool ValueObjectConstResult::IsInScope() {
245 // A const result value is always in scope since it serializes all
246 // information needed to contain the constant value.
247 return true;
248}
249
250lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
251 return m_impl.Dereference(error);
252}
253
254lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
255 uint32_t offset, const CompilerType &type, bool can_create,
256 ConstString name_const_str) {
257 return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
258 name_const_str);
259}
260
261lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
262 return m_impl.AddressOf(error);
263}
264
265lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
266 AddressType *address_type) {
267 return m_impl.GetAddressOf(scalar_is_load_address, address_type);
268}
269
270ValueObject *ValueObjectConstResult::CreateChildAtIndex(
271 size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
272 return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
273 synthetic_index);
274}
275
276size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
277 uint32_t item_idx,
278 uint32_t item_count) {
279 return m_impl.GetPointeeData(data, item_idx, item_count);
280}
281
282lldb::ValueObjectSP
283ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
284 // Always recalculate dynamic values for const results as the memory that
285 // they might point to might have changed at any time.
286 if (use_dynamic != eNoDynamicValues) {
287 if (!IsDynamic()) {
288 ExecutionContext exe_ctx(GetExecutionContextRef());
289 Process *process = exe_ctx.GetProcessPtr();
290 if (process && process->IsPossibleDynamicValue(in_value&: *this))
291 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
292 }
293 if (m_dynamic_value && m_dynamic_value->GetError().Success())
294 return m_dynamic_value->GetSP();
295 }
296 return ValueObjectSP();
297}
298
299lldb::ValueObjectSP
300ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {
301 return m_impl.Cast(compiler_type);
302}
303
304lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
305 if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
306 return m_preferred_display_language;
307 return GetCompilerTypeImpl().GetMinimumLanguage();
308}
309

source code of lldb/source/Core/ValueObjectConstResult.cpp