xref: /src/contrib/llvm-project/lldb/source/Core/ValueObjectConstResult.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- ValueObjectConstResult.cpp ----------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Core/ValueObjectConstResult.h"
10f034231aSEd Maste 
11f034231aSEd Maste #include "lldb/Core/ValueObjectDynamicValue.h"
12e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerType.h"
13f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
1494994d37SDimitry Andric #include "lldb/Target/ExecutionContextScope.h"
15f034231aSEd Maste #include "lldb/Target/Process.h"
1694994d37SDimitry Andric #include "lldb/Utility/DataBuffer.h"
1794994d37SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
1874a628f7SDimitry Andric #include "lldb/Utility/DataExtractor.h"
1994994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
20e3b55780SDimitry Andric #include <optional>
2174a628f7SDimitry Andric 
2274a628f7SDimitry Andric namespace lldb_private {
2374a628f7SDimitry Andric class Module;
2474a628f7SDimitry Andric }
25f034231aSEd Maste 
26f034231aSEd Maste using namespace lldb;
27f034231aSEd Maste using namespace lldb_private;
28f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)2914f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
30f034231aSEd Maste                                              ByteOrder byte_order,
31f034231aSEd Maste                                              uint32_t addr_byte_size,
3214f1b3e8SDimitry Andric                                              lldb::addr_t address) {
33cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
34cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,
35cfca06d7SDimitry Andric                                      addr_byte_size, address))
3614f1b3e8SDimitry Andric       ->GetSP();
37f034231aSEd Maste }
38f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)39f034231aSEd Maste ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
40cfca06d7SDimitry Andric                                                ValueObjectManager &manager,
41f034231aSEd Maste                                                ByteOrder byte_order,
42f034231aSEd Maste                                                uint32_t addr_byte_size,
4314f1b3e8SDimitry Andric                                                lldb::addr_t address)
44b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_impl(this, address) {
45f034231aSEd Maste   SetIsConstant();
46f034231aSEd Maste   SetValueIsValid(true);
47f034231aSEd Maste   m_data.SetByteOrder(byte_order);
48f034231aSEd Maste   m_data.SetAddressByteSize(addr_byte_size);
49f034231aSEd Maste   SetAddressTypeOfChildren(eAddressTypeLoad);
50f034231aSEd Maste }
51f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)5214f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
53e81d9d49SDimitry Andric                                              const CompilerType &compiler_type,
545f29bb8aSDimitry Andric                                              ConstString name,
55f034231aSEd Maste                                              const DataExtractor &data,
5614f1b3e8SDimitry Andric                                              lldb::addr_t address) {
57cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
58cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
59cfca06d7SDimitry Andric                                      name, data, address))
6014f1b3e8SDimitry Andric       ->GetSP();
61f034231aSEd Maste }
62f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)6314f1b3e8SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
64cfca06d7SDimitry Andric     ExecutionContextScope *exe_scope, ValueObjectManager &manager,
65cfca06d7SDimitry Andric     const CompilerType &compiler_type, ConstString name,
66cfca06d7SDimitry Andric     const DataExtractor &data, lldb::addr_t address)
67b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_impl(this, address) {
68f034231aSEd Maste   m_data = data;
69f034231aSEd Maste 
7014f1b3e8SDimitry Andric   if (!m_data.GetSharedDataBuffer()) {
7114f1b3e8SDimitry Andric     DataBufferSP shared_data_buffer(
7214f1b3e8SDimitry Andric         new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
73f034231aSEd Maste     m_data.SetData(shared_data_buffer);
74f034231aSEd Maste   }
75f034231aSEd Maste 
76f034231aSEd Maste   m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
77344a3780SDimitry Andric   m_value.SetValueType(Value::ValueType::HostAddress);
78e81d9d49SDimitry Andric   m_value.SetCompilerType(compiler_type);
79f034231aSEd Maste   m_name = name;
80f034231aSEd Maste   SetIsConstant();
81f034231aSEd Maste   SetValueIsValid(true);
82f034231aSEd Maste   SetAddressTypeOfChildren(eAddressTypeLoad);
83f034231aSEd Maste }
84f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,const lldb::DataBufferSP & data_sp,lldb::ByteOrder data_byte_order,uint32_t data_addr_size,lldb::addr_t address)8514f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
86e81d9d49SDimitry Andric                                              const CompilerType &compiler_type,
875f29bb8aSDimitry Andric                                              ConstString name,
88f034231aSEd Maste                                              const lldb::DataBufferSP &data_sp,
89f034231aSEd Maste                                              lldb::ByteOrder data_byte_order,
90f034231aSEd Maste                                              uint32_t data_addr_size,
9114f1b3e8SDimitry Andric                                              lldb::addr_t address) {
92cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
93cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
94cfca06d7SDimitry Andric                                      name, data_sp, data_byte_order,
95cfca06d7SDimitry Andric                                      data_addr_size, address))
9614f1b3e8SDimitry Andric       ->GetSP();
97f034231aSEd Maste }
98f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,Value & value,ConstString name,Module * module)9914f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
100f034231aSEd Maste                                              Value &value,
1015f29bb8aSDimitry Andric                                              ConstString name,
10214f1b3e8SDimitry Andric                                              Module *module) {
103cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
104cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
105cfca06d7SDimitry Andric                                      module))
106cfca06d7SDimitry Andric       ->GetSP();
107f034231aSEd Maste }
108f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,const lldb::DataBufferSP & data_sp,lldb::ByteOrder data_byte_order,uint32_t data_addr_size,lldb::addr_t address)10914f1b3e8SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
110cfca06d7SDimitry Andric     ExecutionContextScope *exe_scope, ValueObjectManager &manager,
111cfca06d7SDimitry Andric     const CompilerType &compiler_type, ConstString name,
112cfca06d7SDimitry Andric     const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,
113cfca06d7SDimitry Andric     uint32_t data_addr_size, lldb::addr_t address)
114b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_impl(this, address) {
115f034231aSEd Maste   m_data.SetByteOrder(data_byte_order);
116f034231aSEd Maste   m_data.SetAddressByteSize(data_addr_size);
117f034231aSEd Maste   m_data.SetData(data_sp);
118f034231aSEd Maste   m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
119344a3780SDimitry Andric   m_value.SetValueType(Value::ValueType::HostAddress);
120e81d9d49SDimitry Andric   m_value.SetCompilerType(compiler_type);
121f034231aSEd Maste   m_name = name;
122f034231aSEd Maste   SetIsConstant();
123f034231aSEd Maste   SetValueIsValid(true);
124f034231aSEd Maste   SetAddressTypeOfChildren(eAddressTypeLoad);
125f034231aSEd Maste }
126f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)12714f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
128e81d9d49SDimitry Andric                                              const CompilerType &compiler_type,
1295f29bb8aSDimitry Andric                                              ConstString name,
130f034231aSEd Maste                                              lldb::addr_t address,
131f034231aSEd Maste                                              AddressType address_type,
13214f1b3e8SDimitry Andric                                              uint32_t addr_byte_size) {
133cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
134cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
135cfca06d7SDimitry Andric                                      name, address, address_type,
136cfca06d7SDimitry Andric                                      addr_byte_size))
13714f1b3e8SDimitry Andric       ->GetSP();
138f034231aSEd Maste }
139f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)14014f1b3e8SDimitry Andric ValueObjectConstResult::ValueObjectConstResult(
141cfca06d7SDimitry Andric     ExecutionContextScope *exe_scope, ValueObjectManager &manager,
142cfca06d7SDimitry Andric     const CompilerType &compiler_type, ConstString name, lldb::addr_t address,
143cfca06d7SDimitry Andric     AddressType address_type, uint32_t addr_byte_size)
144b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_type_name(),
14514f1b3e8SDimitry Andric       m_impl(this, address) {
146f034231aSEd Maste   m_value.GetScalar() = address;
147f034231aSEd Maste   m_data.SetAddressByteSize(addr_byte_size);
148f034231aSEd Maste   m_value.GetScalar().GetData(m_data, addr_byte_size);
149344a3780SDimitry Andric   // m_value.SetValueType(Value::ValueType::HostAddress);
15014f1b3e8SDimitry Andric   switch (address_type) {
15114f1b3e8SDimitry Andric   case eAddressTypeInvalid:
152344a3780SDimitry Andric     m_value.SetValueType(Value::ValueType::Scalar);
15314f1b3e8SDimitry Andric     break;
15414f1b3e8SDimitry Andric   case eAddressTypeFile:
155344a3780SDimitry Andric     m_value.SetValueType(Value::ValueType::FileAddress);
15614f1b3e8SDimitry Andric     break;
15714f1b3e8SDimitry Andric   case eAddressTypeLoad:
158344a3780SDimitry Andric     m_value.SetValueType(Value::ValueType::LoadAddress);
15914f1b3e8SDimitry Andric     break;
16014f1b3e8SDimitry Andric   case eAddressTypeHost:
161344a3780SDimitry Andric     m_value.SetValueType(Value::ValueType::HostAddress);
16214f1b3e8SDimitry Andric     break;
163f034231aSEd Maste   }
164e81d9d49SDimitry Andric   m_value.SetCompilerType(compiler_type);
165f034231aSEd Maste   m_name = name;
166f034231aSEd Maste   SetIsConstant();
167f034231aSEd Maste   SetValueIsValid(true);
168f034231aSEd Maste   SetAddressTypeOfChildren(eAddressTypeLoad);
169f034231aSEd Maste }
170f034231aSEd Maste 
Create(ExecutionContextScope * exe_scope,const Status & error)17114f1b3e8SDimitry Andric ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
172b76161e4SDimitry Andric                                              const Status &error) {
173cfca06d7SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
174cfca06d7SDimitry Andric   return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();
175f034231aSEd Maste }
176f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Status & error)177f034231aSEd Maste ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
178cfca06d7SDimitry Andric                                                ValueObjectManager &manager,
179b76161e4SDimitry Andric                                                const Status &error)
180b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_impl(this) {
181f034231aSEd Maste   m_error = error;
182f034231aSEd Maste   SetIsConstant();
183f034231aSEd Maste }
184f034231aSEd Maste 
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Value & value,ConstString name,Module * module)185f034231aSEd Maste ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
186cfca06d7SDimitry Andric                                                ValueObjectManager &manager,
187f034231aSEd Maste                                                const Value &value,
188cfca06d7SDimitry Andric                                                ConstString name, Module *module)
189b60736ecSDimitry Andric     : ValueObject(exe_scope, manager), m_impl(this) {
190f034231aSEd Maste   m_value = value;
191205afe67SEd Maste   m_name = name;
192205afe67SEd Maste   ExecutionContext exe_ctx;
193205afe67SEd Maste   exe_scope->CalculateExecutionContext(exe_ctx);
194ead24645SDimitry Andric   m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
195f034231aSEd Maste }
196f034231aSEd Maste 
197344a3780SDimitry Andric ValueObjectConstResult::~ValueObjectConstResult() = default;
198f034231aSEd Maste 
GetCompilerTypeImpl()19914f1b3e8SDimitry Andric CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
200e81d9d49SDimitry Andric   return m_value.GetCompilerType();
201f034231aSEd Maste }
202f034231aSEd Maste 
GetValueType() const20314f1b3e8SDimitry Andric lldb::ValueType ValueObjectConstResult::GetValueType() const {
204f034231aSEd Maste   return eValueTypeConstResult;
205f034231aSEd Maste }
206f034231aSEd Maste 
GetByteSize()207e3b55780SDimitry Andric std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
20812bd4897SEd Maste   ExecutionContext exe_ctx(GetExecutionContextRef());
209b60736ecSDimitry Andric   if (!m_byte_size) {
21094994d37SDimitry Andric     if (auto size =
21194994d37SDimitry Andric         GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
21294994d37SDimitry Andric       SetByteSize(*size);
21394994d37SDimitry Andric   }
214f034231aSEd Maste   return m_byte_size;
215f034231aSEd Maste }
216f034231aSEd Maste 
SetByteSize(size_t size)21714f1b3e8SDimitry Andric void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
218f034231aSEd Maste 
219ac9a064cSDimitry Andric llvm::Expected<uint32_t>
CalculateNumChildren(uint32_t max)220ac9a064cSDimitry Andric ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
22194994d37SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
22294994d37SDimitry Andric   auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
223ac9a064cSDimitry Andric   if (!children_count)
224ac9a064cSDimitry Andric     return children_count;
225ac9a064cSDimitry Andric   return *children_count <= max ? *children_count : max;
226f034231aSEd Maste }
227f034231aSEd Maste 
GetTypeName()22814f1b3e8SDimitry Andric ConstString ValueObjectConstResult::GetTypeName() {
229f034231aSEd Maste   if (m_type_name.IsEmpty())
230cfca06d7SDimitry Andric     m_type_name = GetCompilerType().GetTypeName();
231f034231aSEd Maste   return m_type_name;
232f034231aSEd Maste }
233f034231aSEd Maste 
GetDisplayTypeName()23414f1b3e8SDimitry Andric ConstString ValueObjectConstResult::GetDisplayTypeName() {
235e81d9d49SDimitry Andric   return GetCompilerType().GetDisplayTypeName();
2360cac4ca3SEd Maste }
2370cac4ca3SEd Maste 
UpdateValue()23814f1b3e8SDimitry Andric bool ValueObjectConstResult::UpdateValue() {
239f034231aSEd Maste   // Const value is always valid
240f034231aSEd Maste   SetValueIsValid(true);
241f034231aSEd Maste   return true;
242f034231aSEd Maste }
243f034231aSEd Maste 
IsInScope()24414f1b3e8SDimitry Andric bool ValueObjectConstResult::IsInScope() {
245f034231aSEd Maste   // A const result value is always in scope since it serializes all
246f034231aSEd Maste   // information needed to contain the constant value.
247f034231aSEd Maste   return true;
248f034231aSEd Maste }
249f034231aSEd Maste 
Dereference(Status & error)250b76161e4SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
251f034231aSEd Maste   return m_impl.Dereference(error);
252f034231aSEd Maste }
253f034231aSEd Maste 
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)25414f1b3e8SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
25514f1b3e8SDimitry Andric     uint32_t offset, const CompilerType &type, bool can_create,
25614f1b3e8SDimitry Andric     ConstString name_const_str) {
25714f1b3e8SDimitry Andric   return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
25814f1b3e8SDimitry Andric                                           name_const_str);
259f034231aSEd Maste }
260f034231aSEd Maste 
AddressOf(Status & error)261b76161e4SDimitry Andric lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
262f034231aSEd Maste   return m_impl.AddressOf(error);
263f034231aSEd Maste }
264f034231aSEd Maste 
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)26514f1b3e8SDimitry Andric lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
26614f1b3e8SDimitry Andric                                                   AddressType *address_type) {
267f034231aSEd Maste   return m_impl.GetAddressOf(scalar_is_load_address, address_type);
268f034231aSEd Maste }
269f034231aSEd Maste 
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)27014f1b3e8SDimitry Andric size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
271f034231aSEd Maste                                               uint32_t item_idx,
27214f1b3e8SDimitry Andric                                               uint32_t item_count) {
273f034231aSEd Maste   return m_impl.GetPointeeData(data, item_idx, item_count);
274f034231aSEd Maste }
275f034231aSEd Maste 
276f034231aSEd Maste lldb::ValueObjectSP
GetDynamicValue(lldb::DynamicValueType use_dynamic)27714f1b3e8SDimitry Andric ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
278f034231aSEd Maste   // Always recalculate dynamic values for const results as the memory that
279f034231aSEd Maste   // they might point to might have changed at any time.
28014f1b3e8SDimitry Andric   if (use_dynamic != eNoDynamicValues) {
28114f1b3e8SDimitry Andric     if (!IsDynamic()) {
282f034231aSEd Maste       ExecutionContext exe_ctx(GetExecutionContextRef());
283f034231aSEd Maste       Process *process = exe_ctx.GetProcessPtr();
284f034231aSEd Maste       if (process && process->IsPossibleDynamicValue(*this))
285f034231aSEd Maste         m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
286f034231aSEd Maste     }
2877fa27ce4SDimitry Andric     if (m_dynamic_value && m_dynamic_value->GetError().Success())
288f034231aSEd Maste       return m_dynamic_value->GetSP();
289f034231aSEd Maste   }
290f034231aSEd Maste   return ValueObjectSP();
291f034231aSEd Maste }
292f034231aSEd Maste 
293e81d9d49SDimitry Andric lldb::ValueObjectSP
DoCast(const CompilerType & compiler_type)2947fa27ce4SDimitry Andric ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {
295e81d9d49SDimitry Andric   return m_impl.Cast(compiler_type);
296e81d9d49SDimitry Andric }
297e81d9d49SDimitry Andric 
GetPreferredDisplayLanguage()29814f1b3e8SDimitry Andric lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
299e81d9d49SDimitry Andric   if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
300e81d9d49SDimitry Andric     return m_preferred_display_language;
301e81d9d49SDimitry Andric   return GetCompilerTypeImpl().GetMinimumLanguage();
302205afe67SEd Maste }
303