180814287SRaphael Isemann //===-- ValueObjectConstResult.cpp ----------------------------------------===//
21d3afba3SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61d3afba3SGreg Clayton //
71d3afba3SGreg Clayton //===----------------------------------------------------------------------===//
81d3afba3SGreg Clayton
91d3afba3SGreg Clayton #include "lldb/Core/ValueObjectConstResult.h"
101d3afba3SGreg Clayton
119407302dSGreg Clayton #include "lldb/Core/ValueObjectDynamicValue.h"
122f3df613SZachary Turner #include "lldb/Symbol/CompilerType.h"
132f3df613SZachary Turner #include "lldb/Target/ExecutionContext.h"
14672d2c12SJonas Devlieghere #include "lldb/Target/ExecutionContextScope.h"
152f3df613SZachary Turner #include "lldb/Target/Process.h"
16672d2c12SJonas Devlieghere #include "lldb/Utility/DataBuffer.h"
17672d2c12SJonas Devlieghere #include "lldb/Utility/DataBufferHeap.h"
18666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
19672d2c12SJonas Devlieghere #include "lldb/Utility/Scalar.h"
201d3afba3SGreg Clayton
212f3df613SZachary Turner namespace lldb_private {
222f3df613SZachary Turner class Module;
232f3df613SZachary Turner }
241d3afba3SGreg Clayton
251d3afba3SGreg Clayton using namespace lldb;
261d3afba3SGreg Clayton using namespace lldb_private;
271d3afba3SGreg Clayton
Create(ExecutionContextScope * exe_scope,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)28b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
2958b59f95SJim Ingham ByteOrder byte_order,
309128ee2fSEnrico Granata uint32_t addr_byte_size,
31b9c1b51eSKate Stone lldb::addr_t address) {
32363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
33363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,
34363f05b8SPavel Labath addr_byte_size, address))
35b9c1b51eSKate Stone ->GetSP();
3658b59f95SJim Ingham }
3758b59f95SJim Ingham
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,ByteOrder byte_order,uint32_t addr_byte_size,lldb::addr_t address)3857ee3067SGreg Clayton ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
39363f05b8SPavel Labath ValueObjectManager &manager,
408b2fe6dcSGreg Clayton ByteOrder byte_order,
419128ee2fSEnrico Granata uint32_t addr_byte_size,
42b9c1b51eSKate Stone lldb::addr_t address)
43113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_impl(this, address) {
448b2fe6dcSGreg Clayton SetIsConstant();
458b2fe6dcSGreg Clayton SetValueIsValid(true);
468b2fe6dcSGreg Clayton m_data.SetByteOrder(byte_order);
478b2fe6dcSGreg Clayton m_data.SetAddressByteSize(addr_byte_size);
489128ee2fSEnrico Granata SetAddressTypeOfChildren(eAddressTypeLoad);
498b2fe6dcSGreg Clayton }
508b2fe6dcSGreg Clayton
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)51b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
523ad353f3SBruce Mitchener const CompilerType &compiler_type,
530e4c4821SAdrian Prantl ConstString name,
549128ee2fSEnrico Granata const DataExtractor &data,
55b9c1b51eSKate Stone lldb::addr_t address) {
56363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
57363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
58363f05b8SPavel Labath name, data, address))
59b9c1b51eSKate Stone ->GetSP();
6058b59f95SJim Ingham }
6158b59f95SJim Ingham
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,const DataExtractor & data,lldb::addr_t address)62b9c1b51eSKate Stone ValueObjectConstResult::ValueObjectConstResult(
63363f05b8SPavel Labath ExecutionContextScope *exe_scope, ValueObjectManager &manager,
64363f05b8SPavel Labath const CompilerType &compiler_type, ConstString name,
65363f05b8SPavel Labath const DataExtractor &data, lldb::addr_t address)
66113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_impl(this, address) {
678b2fe6dcSGreg Clayton m_data = data;
6831a8d051SSean Callanan
69b9c1b51eSKate Stone if (!m_data.GetSharedDataBuffer()) {
70b9c1b51eSKate Stone DataBufferSP shared_data_buffer(
71b9c1b51eSKate Stone new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
7231a8d051SSean Callanan m_data.SetData(shared_data_buffer);
7331a8d051SSean Callanan }
7431a8d051SSean Callanan
758b2fe6dcSGreg Clayton m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
76057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::HostAddress);
773ad353f3SBruce Mitchener m_value.SetCompilerType(compiler_type);
788b2fe6dcSGreg Clayton m_name = name;
798b2fe6dcSGreg Clayton SetIsConstant();
808b2fe6dcSGreg Clayton SetValueIsValid(true);
819128ee2fSEnrico Granata SetAddressTypeOfChildren(eAddressTypeLoad);
828b2fe6dcSGreg Clayton }
838b2fe6dcSGreg Clayton
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)84b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
853ad353f3SBruce Mitchener const CompilerType &compiler_type,
860e4c4821SAdrian Prantl ConstString name,
8758b59f95SJim Ingham const lldb::DataBufferSP &data_sp,
8858b59f95SJim Ingham lldb::ByteOrder data_byte_order,
89c7bece56SGreg Clayton uint32_t data_addr_size,
90b9c1b51eSKate Stone lldb::addr_t address) {
91363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
92363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
93363f05b8SPavel Labath name, data_sp, data_byte_order,
94363f05b8SPavel Labath data_addr_size, address))
95b9c1b51eSKate Stone ->GetSP();
9658b59f95SJim Ingham }
9758b59f95SJim Ingham
Create(ExecutionContextScope * exe_scope,Value & value,ConstString name,Module * module)98b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
9973ca05a2SJim Ingham Value &value,
1000e4c4821SAdrian Prantl ConstString name,
101b9c1b51eSKate Stone Module *module) {
102363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
103363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
104363f05b8SPavel Labath module))
105363f05b8SPavel Labath ->GetSP();
10673ca05a2SJim Ingham }
10773ca05a2SJim Ingham
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)108b9c1b51eSKate Stone ValueObjectConstResult::ValueObjectConstResult(
109363f05b8SPavel Labath ExecutionContextScope *exe_scope, ValueObjectManager &manager,
110363f05b8SPavel Labath const CompilerType &compiler_type, ConstString name,
111363f05b8SPavel Labath const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,
112363f05b8SPavel Labath uint32_t data_addr_size, lldb::addr_t address)
113113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_impl(this, address) {
1141d3afba3SGreg Clayton m_data.SetByteOrder(data_byte_order);
1151d3afba3SGreg Clayton m_data.SetAddressByteSize(data_addr_size);
1161d3afba3SGreg Clayton m_data.SetData(data_sp);
1171d3afba3SGreg Clayton m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
118057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::HostAddress);
1193ad353f3SBruce Mitchener m_value.SetCompilerType(compiler_type);
1201d3afba3SGreg Clayton m_name = name;
121b71f3844SGreg Clayton SetIsConstant();
122e2f8841dSJim Ingham SetValueIsValid(true);
1239128ee2fSEnrico Granata SetAddressTypeOfChildren(eAddressTypeLoad);
1248b2fe6dcSGreg Clayton }
1258b2fe6dcSGreg Clayton
Create(ExecutionContextScope * exe_scope,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)126b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
1273ad353f3SBruce Mitchener const CompilerType &compiler_type,
1280e4c4821SAdrian Prantl ConstString name,
12958b59f95SJim Ingham lldb::addr_t address,
13058b59f95SJim Ingham AddressType address_type,
131b9c1b51eSKate Stone uint32_t addr_byte_size) {
132363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
133363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
134363f05b8SPavel Labath name, address, address_type,
135363f05b8SPavel Labath addr_byte_size))
136b9c1b51eSKate Stone ->GetSP();
13758b59f95SJim Ingham }
13858b59f95SJim Ingham
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const CompilerType & compiler_type,ConstString name,lldb::addr_t address,AddressType address_type,uint32_t addr_byte_size)139b9c1b51eSKate Stone ValueObjectConstResult::ValueObjectConstResult(
140363f05b8SPavel Labath ExecutionContextScope *exe_scope, ValueObjectManager &manager,
141363f05b8SPavel Labath const CompilerType &compiler_type, ConstString name, lldb::addr_t address,
142363f05b8SPavel Labath AddressType address_type, uint32_t addr_byte_size)
143113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_type_name(),
144b9c1b51eSKate Stone m_impl(this, address) {
1458b2fe6dcSGreg Clayton m_value.GetScalar() = address;
1468b2fe6dcSGreg Clayton m_data.SetAddressByteSize(addr_byte_size);
1478b2fe6dcSGreg Clayton m_value.GetScalar().GetData(m_data, addr_byte_size);
148057efa99SAdrian Prantl // m_value.SetValueType(Value::ValueType::HostAddress);
149b9c1b51eSKate Stone switch (address_type) {
150b9c1b51eSKate Stone case eAddressTypeInvalid:
151057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::Scalar);
152b9c1b51eSKate Stone break;
153b9c1b51eSKate Stone case eAddressTypeFile:
154057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::FileAddress);
155b9c1b51eSKate Stone break;
156b9c1b51eSKate Stone case eAddressTypeLoad:
157057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::LoadAddress);
158b9c1b51eSKate Stone break;
159b9c1b51eSKate Stone case eAddressTypeHost:
160057efa99SAdrian Prantl m_value.SetValueType(Value::ValueType::HostAddress);
161b9c1b51eSKate Stone break;
1628b2fe6dcSGreg Clayton }
1633ad353f3SBruce Mitchener m_value.SetCompilerType(compiler_type);
1648b2fe6dcSGreg Clayton m_name = name;
1658b2fe6dcSGreg Clayton SetIsConstant();
1668b2fe6dcSGreg Clayton SetValueIsValid(true);
1679128ee2fSEnrico Granata SetAddressTypeOfChildren(eAddressTypeLoad);
168b71f3844SGreg Clayton }
169b71f3844SGreg Clayton
Create(ExecutionContextScope * exe_scope,const Status & error)170b9c1b51eSKate Stone ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
17197206d57SZachary Turner const Status &error) {
172363f05b8SPavel Labath auto manager_sp = ValueObjectManager::Create();
173363f05b8SPavel Labath return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();
17458b59f95SJim Ingham }
17558b59f95SJim Ingham
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Status & error)17657ee3067SGreg Clayton ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
177363f05b8SPavel Labath ValueObjectManager &manager,
17897206d57SZachary Turner const Status &error)
179113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_impl(this) {
180b71f3844SGreg Clayton m_error = error;
181b71f3844SGreg Clayton SetIsConstant();
1821d3afba3SGreg Clayton }
1831d3afba3SGreg Clayton
ValueObjectConstResult(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const Value & value,ConstString name,Module * module)18457ee3067SGreg Clayton ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
185363f05b8SPavel Labath ValueObjectManager &manager,
18673ca05a2SJim Ingham const Value &value,
187363f05b8SPavel Labath ConstString name, Module *module)
188113f56fbSAdrian Prantl : ValueObject(exe_scope, manager), m_impl(this) {
18973ca05a2SJim Ingham m_value = value;
1905510a576SEnrico Granata m_name = name;
1910c10a850SEnrico Granata ExecutionContext exe_ctx;
1920c10a850SEnrico Granata exe_scope->CalculateExecutionContext(exe_ctx);
193d9cbd2acSAdrian Prantl m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
19473ca05a2SJim Ingham }
19573ca05a2SJim Ingham
196*fd2433e1SJonas Devlieghere ValueObjectConstResult::~ValueObjectConstResult() = default;
1971d3afba3SGreg Clayton
GetCompilerTypeImpl()198b9c1b51eSKate Stone CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
19999558cc4SGreg Clayton return m_value.GetCompilerType();
2001d3afba3SGreg Clayton }
2011d3afba3SGreg Clayton
GetValueType() const202b9c1b51eSKate Stone lldb::ValueType ValueObjectConstResult::GetValueType() const {
2031d3afba3SGreg Clayton return eValueTypeConstResult;
2041d3afba3SGreg Clayton }
2051d3afba3SGreg Clayton
GetByteSize()206113f56fbSAdrian Prantl llvm::Optional<uint64_t> ValueObjectConstResult::GetByteSize() {
207951bdd5fSEnrico Granata ExecutionContext exe_ctx(GetExecutionContextRef());
208113f56fbSAdrian Prantl if (!m_byte_size) {
209d963a7c3SAdrian Prantl if (auto size =
210d963a7c3SAdrian Prantl GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
211d963a7c3SAdrian Prantl SetByteSize(*size);
212d963a7c3SAdrian Prantl }
2138b2fe6dcSGreg Clayton return m_byte_size;
2148b2fe6dcSGreg Clayton }
2158b2fe6dcSGreg Clayton
SetByteSize(size_t size)216b9c1b51eSKate Stone void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
2171d3afba3SGreg Clayton
CalculateNumChildren(uint32_t max)218b9c1b51eSKate Stone size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
219eca07c59SAdrian Prantl ExecutionContext exe_ctx(GetExecutionContextRef());
220eca07c59SAdrian Prantl auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
2219ac7a6c5SSiva Chandra return children_count <= max ? children_count : max;
2221d3afba3SGreg Clayton }
2231d3afba3SGreg Clayton
GetTypeName()224b9c1b51eSKate Stone ConstString ValueObjectConstResult::GetTypeName() {
2251d3afba3SGreg Clayton if (m_type_name.IsEmpty())
22630ce956aSRaphael Isemann m_type_name = GetCompilerType().GetTypeName();
2271d3afba3SGreg Clayton return m_type_name;
2281d3afba3SGreg Clayton }
2291d3afba3SGreg Clayton
GetDisplayTypeName()230b9c1b51eSKate Stone ConstString ValueObjectConstResult::GetDisplayTypeName() {
23199558cc4SGreg Clayton return GetCompilerType().GetDisplayTypeName();
232e8daa2f8SEnrico Granata }
233e8daa2f8SEnrico Granata
UpdateValue()234b9c1b51eSKate Stone bool ValueObjectConstResult::UpdateValue() {
2351d3afba3SGreg Clayton // Const value is always valid
2361d3afba3SGreg Clayton SetValueIsValid(true);
2376035b67dSJim Ingham return true;
2381d3afba3SGreg Clayton }
2391d3afba3SGreg Clayton
IsInScope()240b9c1b51eSKate Stone bool ValueObjectConstResult::IsInScope() {
2411d3afba3SGreg Clayton // A const result value is always in scope since it serializes all
2421d3afba3SGreg Clayton // information needed to contain the constant value.
2431d3afba3SGreg Clayton return true;
2441d3afba3SGreg Clayton }
2459128ee2fSEnrico Granata
Dereference(Status & error)24697206d57SZachary Turner lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
2479128ee2fSEnrico Granata return m_impl.Dereference(error);
2489128ee2fSEnrico Granata }
2499128ee2fSEnrico Granata
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)250b9c1b51eSKate Stone lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
251b9c1b51eSKate Stone uint32_t offset, const CompilerType &type, bool can_create,
252b9c1b51eSKate Stone ConstString name_const_str) {
253b9c1b51eSKate Stone return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
254b9c1b51eSKate Stone name_const_str);
2559128ee2fSEnrico Granata }
2569128ee2fSEnrico Granata
AddressOf(Status & error)25797206d57SZachary Turner lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
2589128ee2fSEnrico Granata return m_impl.AddressOf(error);
2599128ee2fSEnrico Granata }
2609128ee2fSEnrico Granata
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)261b9c1b51eSKate Stone lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
262b9c1b51eSKate Stone AddressType *address_type) {
263b456b792SJohnny Chen return m_impl.GetAddressOf(scalar_is_load_address, address_type);
264b456b792SJohnny Chen }
265b456b792SJohnny Chen
CreateChildAtIndex(size_t idx,bool synthetic_array_member,int32_t synthetic_index)266b9c1b51eSKate Stone ValueObject *ValueObjectConstResult::CreateChildAtIndex(
267b9c1b51eSKate Stone size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
268b9c1b51eSKate Stone return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
269b9c1b51eSKate Stone synthetic_index);
2709128ee2fSEnrico Granata }
2719128ee2fSEnrico Granata
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)272b9c1b51eSKate Stone size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
2739128ee2fSEnrico Granata uint32_t item_idx,
274b9c1b51eSKate Stone uint32_t item_count) {
2759128ee2fSEnrico Granata return m_impl.GetPointeeData(data, item_idx, item_count);
2769128ee2fSEnrico Granata }
2779407302dSGreg Clayton
2789407302dSGreg Clayton lldb::ValueObjectSP
GetDynamicValue(lldb::DynamicValueType use_dynamic)279b9c1b51eSKate Stone ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
2809407302dSGreg Clayton // Always recalculate dynamic values for const results as the memory that
2819407302dSGreg Clayton // they might point to might have changed at any time.
282b9c1b51eSKate Stone if (use_dynamic != eNoDynamicValues) {
283b9c1b51eSKate Stone if (!IsDynamic()) {
2849407302dSGreg Clayton ExecutionContext exe_ctx(GetExecutionContextRef());
2859407302dSGreg Clayton Process *process = exe_ctx.GetProcessPtr();
2869407302dSGreg Clayton if (process && process->IsPossibleDynamicValue(*this))
2879407302dSGreg Clayton m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
2889407302dSGreg Clayton }
2899407302dSGreg Clayton if (m_dynamic_value)
2909407302dSGreg Clayton return m_dynamic_value->GetSP();
2919407302dSGreg Clayton }
2929407302dSGreg Clayton return ValueObjectSP();
2939407302dSGreg Clayton }
2949407302dSGreg Clayton
295f8877efcSSiva Chandra lldb::ValueObjectSP
Cast(const CompilerType & compiler_type)296b9c1b51eSKate Stone ValueObjectConstResult::Cast(const CompilerType &compiler_type) {
2973ad353f3SBruce Mitchener return m_impl.Cast(compiler_type);
298f8877efcSSiva Chandra }
299f8877efcSSiva Chandra
GetPreferredDisplayLanguage()300b9c1b51eSKate Stone lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
30173e8c4d0SEnrico Granata if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
30273e8c4d0SEnrico Granata return m_preferred_display_language;
30373e8c4d0SEnrico Granata return GetCompilerTypeImpl().GetMinimumLanguage();
304c1247f55SEnrico Granata }
305