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