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 
22 namespace lldb_private {
23 class Module;
24 }
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 ValueObjectSP 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 
39 ValueObjectConstResult::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 
52 ValueObjectSP 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 
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) {
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(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 
85 ValueObjectSP 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 
99 ValueObjectSP 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 
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);
121   m_name = name;
122   SetIsConstant();
123   SetValueIsValid(true);
124   SetAddressTypeOfChildren(eAddressTypeLoad);
125 }
126 
127 ValueObjectSP 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 
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);
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 
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();
175 }
176 
177 ValueObjectConstResult::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 
185 ValueObjectConstResult::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, m_data, module);
195 }
196 
197 ValueObjectConstResult::~ValueObjectConstResult() = default;
198 
199 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
200   return m_value.GetCompilerType();
201 }
202 
203 lldb::ValueType ValueObjectConstResult::GetValueType() const {
204   return eValueTypeConstResult;
205 }
206 
207 std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
208   ExecutionContext exe_ctx(GetExecutionContextRef());
209   if (!m_byte_size) {
210     if (auto size =
211         GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
212       SetByteSize(*size);
213   }
214   return m_byte_size;
215 }
216 
217 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
218 
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;
223 }
224 
225 ConstString ValueObjectConstResult::GetTypeName() {
226   if (m_type_name.IsEmpty())
227     m_type_name = GetCompilerType().GetTypeName();
228   return m_type_name;
229 }
230 
231 ConstString ValueObjectConstResult::GetDisplayTypeName() {
232   return GetCompilerType().GetDisplayTypeName();
233 }
234 
235 bool ValueObjectConstResult::UpdateValue() {
236   // Const value is always valid
237   SetValueIsValid(true);
238   return true;
239 }
240 
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.
244   return true;
245 }
246 
247 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
248   return m_impl.Dereference(error);
249 }
250 
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,
255                                           name_const_str);
256 }
257 
258 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
259   return m_impl.AddressOf(error);
260 }
261 
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);
265 }
266 
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,
270                                    synthetic_index);
271 }
272 
273 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
274                                               uint32_t item_idx,
275                                               uint32_t item_count) {
276   return m_impl.GetPointeeData(data, item_idx, item_count);
277 }
278 
279 lldb::ValueObjectSP
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) {
284     if (!IsDynamic()) {
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);
289     }
290     if (m_dynamic_value && m_dynamic_value->GetError().Success())
291       return m_dynamic_value->GetSP();
292   }
293   return ValueObjectSP();
294 }
295 
296 lldb::ValueObjectSP
297 ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {
298   return m_impl.Cast(compiler_type);
299 }
300 
301 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
302   if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
303     return m_preferred_display_language;
304   return GetCompilerTypeImpl().GetMinimumLanguage();
305 }
306