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_type_name(), m_byte_size(0),
44       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_type_name(), m_byte_size(0),
68       m_impl(this, address) {
69   m_data = data;
70 
71   if (!m_data.GetSharedDataBuffer()) {
72     DataBufferSP shared_data_buffer(
73         new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
74     m_data.SetData(shared_data_buffer);
75   }
76 
77   m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
78   m_value.SetValueType(Value::eValueTypeHostAddress);
79   m_value.SetCompilerType(compiler_type);
80   m_name = name;
81   SetIsConstant();
82   SetValueIsValid(true);
83   SetAddressTypeOfChildren(eAddressTypeLoad);
84 }
85 
86 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
87                                              const CompilerType &compiler_type,
88                                              ConstString name,
89                                              const lldb::DataBufferSP &data_sp,
90                                              lldb::ByteOrder data_byte_order,
91                                              uint32_t data_addr_size,
92                                              lldb::addr_t address) {
93   auto manager_sp = ValueObjectManager::Create();
94   return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
95                                      name, data_sp, data_byte_order,
96                                      data_addr_size, address))
97       ->GetSP();
98 }
99 
100 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
101                                              Value &value,
102                                              ConstString name,
103                                              Module *module) {
104   auto manager_sp = ValueObjectManager::Create();
105   return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
106                                      module))
107       ->GetSP();
108 }
109 
110 ValueObjectConstResult::ValueObjectConstResult(
111     ExecutionContextScope *exe_scope, ValueObjectManager &manager,
112     const CompilerType &compiler_type, ConstString name,
113     const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,
114     uint32_t data_addr_size, lldb::addr_t address)
115     : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0),
116       m_impl(this, address) {
117   m_data.SetByteOrder(data_byte_order);
118   m_data.SetAddressByteSize(data_addr_size);
119   m_data.SetData(data_sp);
120   m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
121   m_value.SetValueType(Value::eValueTypeHostAddress);
122   m_value.SetCompilerType(compiler_type);
123   m_name = name;
124   SetIsConstant();
125   SetValueIsValid(true);
126   SetAddressTypeOfChildren(eAddressTypeLoad);
127 }
128 
129 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
130                                              const CompilerType &compiler_type,
131                                              ConstString name,
132                                              lldb::addr_t address,
133                                              AddressType address_type,
134                                              uint32_t addr_byte_size) {
135   auto manager_sp = ValueObjectManager::Create();
136   return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
137                                      name, address, address_type,
138                                      addr_byte_size))
139       ->GetSP();
140 }
141 
142 ValueObjectConstResult::ValueObjectConstResult(
143     ExecutionContextScope *exe_scope, ValueObjectManager &manager,
144     const CompilerType &compiler_type, ConstString name, lldb::addr_t address,
145     AddressType address_type, uint32_t addr_byte_size)
146     : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0),
147       m_impl(this, address) {
148   m_value.GetScalar() = address;
149   m_data.SetAddressByteSize(addr_byte_size);
150   m_value.GetScalar().GetData(m_data, addr_byte_size);
151   // m_value.SetValueType(Value::eValueTypeHostAddress);
152   switch (address_type) {
153   case eAddressTypeInvalid:
154     m_value.SetValueType(Value::eValueTypeScalar);
155     break;
156   case eAddressTypeFile:
157     m_value.SetValueType(Value::eValueTypeFileAddress);
158     break;
159   case eAddressTypeLoad:
160     m_value.SetValueType(Value::eValueTypeLoadAddress);
161     break;
162   case eAddressTypeHost:
163     m_value.SetValueType(Value::eValueTypeHostAddress);
164     break;
165   }
166   m_value.SetCompilerType(compiler_type);
167   m_name = name;
168   SetIsConstant();
169   SetValueIsValid(true);
170   SetAddressTypeOfChildren(eAddressTypeLoad);
171 }
172 
173 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
174                                              const Status &error) {
175   auto manager_sp = ValueObjectManager::Create();
176   return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();
177 }
178 
179 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
180                                                ValueObjectManager &manager,
181                                                const Status &error)
182     : ValueObject(exe_scope, manager), m_impl(this) {
183   m_error = error;
184   SetIsConstant();
185 }
186 
187 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
188                                                ValueObjectManager &manager,
189                                                const Value &value,
190                                                ConstString name, Module *module)
191     : ValueObject(exe_scope, manager), m_impl(this) {
192   m_value = value;
193   m_name = name;
194   ExecutionContext exe_ctx;
195   exe_scope->CalculateExecutionContext(exe_ctx);
196   m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
197 }
198 
199 ValueObjectConstResult::~ValueObjectConstResult() {}
200 
201 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
202   return m_value.GetCompilerType();
203 }
204 
205 lldb::ValueType ValueObjectConstResult::GetValueType() const {
206   return eValueTypeConstResult;
207 }
208 
209 llvm::Optional<uint64_t> ValueObjectConstResult::GetByteSize() {
210   ExecutionContext exe_ctx(GetExecutionContextRef());
211   if (!m_byte_size) {
212     if (auto size =
213         GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
214       SetByteSize(*size);
215   }
216   return m_byte_size;
217 }
218 
219 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
220 
221 size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
222   ExecutionContext exe_ctx(GetExecutionContextRef());
223   auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
224   return children_count <= max ? children_count : max;
225 }
226 
227 ConstString ValueObjectConstResult::GetTypeName() {
228   if (m_type_name.IsEmpty())
229     m_type_name = GetCompilerType().GetTypeName();
230   return m_type_name;
231 }
232 
233 ConstString ValueObjectConstResult::GetDisplayTypeName() {
234   return GetCompilerType().GetDisplayTypeName();
235 }
236 
237 bool ValueObjectConstResult::UpdateValue() {
238   // Const value is always valid
239   SetValueIsValid(true);
240   return true;
241 }
242 
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.
246   return true;
247 }
248 
249 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
250   return m_impl.Dereference(error);
251 }
252 
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,
257                                           name_const_str);
258 }
259 
260 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
261   return m_impl.AddressOf(error);
262 }
263 
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);
267 }
268 
269 ValueObject *ValueObjectConstResult::CreateChildAtIndex(
270     size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
271   return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
272                                    synthetic_index);
273 }
274 
275 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
276                                               uint32_t item_idx,
277                                               uint32_t item_count) {
278   return m_impl.GetPointeeData(data, item_idx, item_count);
279 }
280 
281 lldb::ValueObjectSP
282 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
283   // Always recalculate dynamic values for const results as the memory that
284   // they might point to might have changed at any time.
285   if (use_dynamic != eNoDynamicValues) {
286     if (!IsDynamic()) {
287       ExecutionContext exe_ctx(GetExecutionContextRef());
288       Process *process = exe_ctx.GetProcessPtr();
289       if (process && process->IsPossibleDynamicValue(*this))
290         m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
291     }
292     if (m_dynamic_value)
293       return m_dynamic_value->GetSP();
294   }
295   return ValueObjectSP();
296 }
297 
298 lldb::ValueObjectSP
299 ValueObjectConstResult::Cast(const CompilerType &compiler_type) {
300   return m_impl.Cast(compiler_type);
301 }
302 
303 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
304   if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
305     return m_preferred_display_language;
306   return GetCompilerTypeImpl().GetMinimumLanguage();
307 }
308