| 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/ValueObject/ValueObjectConstResult.h" |
| 10 | |
| 11 | #include "lldb/Symbol/CompilerType.h" |
| 12 | #include "lldb/Target/ExecutionContext.h" |
| 13 | #include "lldb/Target/ExecutionContextScope.h" |
| 14 | #include "lldb/Target/Process.h" |
| 15 | #include "lldb/Utility/DataBuffer.h" |
| 16 | #include "lldb/Utility/DataBufferHeap.h" |
| 17 | #include "lldb/Utility/DataExtractor.h" |
| 18 | #include "lldb/Utility/Scalar.h" |
| 19 | #include "lldb/ValueObject/ValueObjectDynamicValue.h" |
| 20 | #include <optional> |
| 21 | |
| 22 | namespace lldb_private { |
| 23 | class Module; |
| 24 | } |
| 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 30 | ByteOrder byte_order, |
| 31 | uint32_t addr_byte_size, |
| 32 | lldb::addr_t address) { |
| 33 | auto manager_sp = ValueObjectManager::Create(); |
| 34 | return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order, |
| 35 | addr_byte_size, address)) |
| 36 | ->GetSP(); |
| 37 | } |
| 38 | |
| 39 | ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, |
| 40 | ValueObjectManager &manager, |
| 41 | ByteOrder byte_order, |
| 42 | uint32_t addr_byte_size, |
| 43 | lldb::addr_t address) |
| 44 | : ValueObject(exe_scope, manager), 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::(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_impl(this, address) { |
| 68 | m_data = data; |
| 69 | |
| 70 | if (!m_data.GetSharedDataBuffer()) { |
| 71 | DataBufferSP shared_data_buffer( |
| 72 | new DataBufferHeap(data.GetDataStart(), data.GetByteSize())); |
| 73 | m_data.SetData(data_sp: shared_data_buffer); |
| 74 | } |
| 75 | |
| 76 | m_value.GetScalar() = (uintptr_t)m_data.GetDataStart(); |
| 77 | m_value.SetValueType(Value::ValueType::HostAddress); |
| 78 | m_value.SetCompilerType(compiler_type); |
| 79 | m_name = name; |
| 80 | SetIsConstant(); |
| 81 | SetValueIsValid(true); |
| 82 | SetAddressTypeOfChildren(eAddressTypeLoad); |
| 83 | } |
| 84 | |
| 85 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 86 | const CompilerType &compiler_type, |
| 87 | ConstString name, |
| 88 | const lldb::DataBufferSP &data_sp, |
| 89 | lldb::ByteOrder data_byte_order, |
| 90 | uint32_t data_addr_size, |
| 91 | lldb::addr_t address) { |
| 92 | auto manager_sp = ValueObjectManager::Create(); |
| 93 | return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type, |
| 94 | name, data_sp, data_byte_order, |
| 95 | data_addr_size, address)) |
| 96 | ->GetSP(); |
| 97 | } |
| 98 | |
| 99 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 100 | Value &value, 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(), m_impl(this, address) { |
| 144 | m_value.GetScalar() = address; |
| 145 | m_data.SetAddressByteSize(addr_byte_size); |
| 146 | m_value.GetScalar().GetData(data&: m_data, limit_byte_size: addr_byte_size); |
| 147 | // m_value.SetValueType(Value::ValueType::HostAddress); |
| 148 | switch (address_type) { |
| 149 | case eAddressTypeInvalid: |
| 150 | m_value.SetValueType(Value::ValueType::Scalar); |
| 151 | break; |
| 152 | case eAddressTypeFile: |
| 153 | m_value.SetValueType(Value::ValueType::FileAddress); |
| 154 | break; |
| 155 | case eAddressTypeLoad: |
| 156 | m_value.SetValueType(Value::ValueType::LoadAddress); |
| 157 | break; |
| 158 | case eAddressTypeHost: |
| 159 | m_value.SetValueType(Value::ValueType::HostAddress); |
| 160 | break; |
| 161 | } |
| 162 | m_value.SetCompilerType(compiler_type); |
| 163 | m_name = name; |
| 164 | SetIsConstant(); |
| 165 | SetValueIsValid(true); |
| 166 | SetAddressTypeOfChildren(eAddressTypeLoad); |
| 167 | } |
| 168 | |
| 169 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 170 | Status &&error) { |
| 171 | auto manager_sp = ValueObjectManager::Create(); |
| 172 | return (new ValueObjectConstResult(exe_scope, *manager_sp, std::move(error))) |
| 173 | ->GetSP(); |
| 174 | } |
| 175 | |
| 176 | ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, |
| 177 | ValueObjectManager &manager, |
| 178 | Status &&error) |
| 179 | : ValueObject(exe_scope, manager), m_impl(this) { |
| 180 | m_error = std::move(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: &exe_ctx, data&: 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::Expected<uint64_t> ValueObjectConstResult::GetByteSize() { |
| 207 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
| 208 | if (!m_byte_size) { |
| 209 | auto size_or_err = |
| 210 | GetCompilerType().GetByteSize(exe_scope: exe_ctx.GetBestExecutionContextScope()); |
| 211 | if (!size_or_err) |
| 212 | return size_or_err; |
| 213 | SetByteSize(*size_or_err); |
| 214 | } |
| 215 | if (m_byte_size) |
| 216 | return *m_byte_size; |
| 217 | return llvm::createStringError(Fmt: "unknown size of const result" ); |
| 218 | } |
| 219 | |
| 220 | void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; } |
| 221 | |
| 222 | llvm::Expected<uint32_t> |
| 223 | ValueObjectConstResult::CalculateNumChildren(uint32_t max) { |
| 224 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
| 225 | auto children_count = GetCompilerType().GetNumChildren(omit_empty_base_classes: true, exe_ctx: &exe_ctx); |
| 226 | if (!children_count) |
| 227 | return children_count; |
| 228 | return *children_count <= max ? *children_count : max; |
| 229 | } |
| 230 | |
| 231 | ConstString ValueObjectConstResult::GetTypeName() { |
| 232 | if (m_type_name.IsEmpty()) |
| 233 | m_type_name = GetCompilerType().GetTypeName(); |
| 234 | return m_type_name; |
| 235 | } |
| 236 | |
| 237 | ConstString ValueObjectConstResult::GetDisplayTypeName() { |
| 238 | return GetCompilerType().GetDisplayTypeName(); |
| 239 | } |
| 240 | |
| 241 | bool ValueObjectConstResult::UpdateValue() { |
| 242 | // Const value is always valid |
| 243 | SetValueIsValid(true); |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | bool ValueObjectConstResult::IsInScope() { |
| 248 | // A const result value is always in scope since it serializes all |
| 249 | // information needed to contain the constant value. |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) { |
| 254 | return m_impl.Dereference(error); |
| 255 | } |
| 256 | |
| 257 | lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset( |
| 258 | uint32_t offset, const CompilerType &type, bool can_create, |
| 259 | ConstString name_const_str) { |
| 260 | return m_impl.GetSyntheticChildAtOffset(offset, type, can_create, |
| 261 | name_const_str); |
| 262 | } |
| 263 | |
| 264 | lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) { |
| 265 | return m_impl.AddressOf(error); |
| 266 | } |
| 267 | |
| 268 | ValueObject::AddrAndType |
| 269 | ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address) { |
| 270 | return m_impl.GetAddressOf(scalar_is_load_address); |
| 271 | } |
| 272 | |
| 273 | size_t ValueObjectConstResult::(DataExtractor &data, |
| 274 | uint32_t item_idx, |
| 275 | uint32_t item_count) { |
| 276 | return m_impl.GetPointeeData(data, item_idx, item_count); |
| 277 | } |
| 278 | |
| 279 | lldb::ValueObjectSP |
| 280 | ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) { |
| 281 | // Always recalculate dynamic values for const results as the memory that |
| 282 | // they might point to might have changed at any time. |
| 283 | if (use_dynamic != eNoDynamicValues) { |
| 284 | if (!IsDynamic()) { |
| 285 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
| 286 | Process *process = exe_ctx.GetProcessPtr(); |
| 287 | if (process && process->IsPossibleDynamicValue(in_value&: *this)) |
| 288 | m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic); |
| 289 | } |
| 290 | if (m_dynamic_value && m_dynamic_value->GetError().Success()) |
| 291 | return m_dynamic_value->GetSP(); |
| 292 | } |
| 293 | return ValueObjectSP(); |
| 294 | } |
| 295 | |
| 296 | lldb::ValueObjectSP |
| 297 | ValueObjectConstResult::DoCast(const CompilerType &compiler_type) { |
| 298 | return m_impl.Cast(compiler_type); |
| 299 | } |
| 300 | |
| 301 | lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() { |
| 302 | if (m_preferred_display_language != lldb::eLanguageTypeUnknown) |
| 303 | return m_preferred_display_language; |
| 304 | return GetCompilerTypeImpl().GetMinimumLanguage(); |
| 305 | } |
| 306 | |