xref: /openbsd-src/gnu/llvm/lldb/source/Core/ValueObjectConstResult.cpp (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
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_type_name(), m_byte_size(0),
183       m_impl(this) {
184   m_error = error;
185   SetIsConstant();
186 }
187 
188 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
189                                                ValueObjectManager &manager,
190                                                const Value &value,
191                                                ConstString name, Module *module)
192     : ValueObject(exe_scope, manager), m_type_name(), m_byte_size(0),
193       m_impl(this) {
194   m_value = value;
195   m_name = name;
196   ExecutionContext exe_ctx;
197   exe_scope->CalculateExecutionContext(exe_ctx);
198   m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
199 }
200 
201 ValueObjectConstResult::~ValueObjectConstResult() {}
202 
203 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
204   return m_value.GetCompilerType();
205 }
206 
207 lldb::ValueType ValueObjectConstResult::GetValueType() const {
208   return eValueTypeConstResult;
209 }
210 
211 uint64_t ValueObjectConstResult::GetByteSize() {
212   ExecutionContext exe_ctx(GetExecutionContextRef());
213   if (m_byte_size == 0) {
214     if (auto size =
215         GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
216       SetByteSize(*size);
217   }
218   return m_byte_size;
219 }
220 
221 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
222 
223 size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
224   ExecutionContext exe_ctx(GetExecutionContextRef());
225   auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
226   return children_count <= max ? children_count : max;
227 }
228 
229 ConstString ValueObjectConstResult::GetTypeName() {
230   if (m_type_name.IsEmpty())
231     m_type_name = GetCompilerType().GetTypeName();
232   return m_type_name;
233 }
234 
235 ConstString ValueObjectConstResult::GetDisplayTypeName() {
236   return GetCompilerType().GetDisplayTypeName();
237 }
238 
239 bool ValueObjectConstResult::UpdateValue() {
240   // Const value is always valid
241   SetValueIsValid(true);
242   return true;
243 }
244 
245 bool ValueObjectConstResult::IsInScope() {
246   // A const result value is always in scope since it serializes all
247   // information needed to contain the constant value.
248   return true;
249 }
250 
251 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
252   return m_impl.Dereference(error);
253 }
254 
255 lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
256     uint32_t offset, const CompilerType &type, bool can_create,
257     ConstString name_const_str) {
258   return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
259                                           name_const_str);
260 }
261 
262 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
263   return m_impl.AddressOf(error);
264 }
265 
266 lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
267                                                   AddressType *address_type) {
268   return m_impl.GetAddressOf(scalar_is_load_address, address_type);
269 }
270 
271 ValueObject *ValueObjectConstResult::CreateChildAtIndex(
272     size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
273   return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
274                                    synthetic_index);
275 }
276 
277 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
278                                               uint32_t item_idx,
279                                               uint32_t item_count) {
280   return m_impl.GetPointeeData(data, item_idx, item_count);
281 }
282 
283 lldb::ValueObjectSP
284 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
285   // Always recalculate dynamic values for const results as the memory that
286   // they might point to might have changed at any time.
287   if (use_dynamic != eNoDynamicValues) {
288     if (!IsDynamic()) {
289       ExecutionContext exe_ctx(GetExecutionContextRef());
290       Process *process = exe_ctx.GetProcessPtr();
291       if (process && process->IsPossibleDynamicValue(*this))
292         m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
293     }
294     if (m_dynamic_value)
295       return m_dynamic_value->GetSP();
296   }
297   return ValueObjectSP();
298 }
299 
300 lldb::ValueObjectSP
301 ValueObjectConstResult::Cast(const CompilerType &compiler_type) {
302   return m_impl.Cast(compiler_type);
303 }
304 
305 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
306   if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
307     return m_preferred_display_language;
308   return GetCompilerTypeImpl().GetMinimumLanguage();
309 }
310