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 #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::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_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(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, 101 ConstString name, 102 Module *module) { 103 auto manager_sp = ValueObjectManager::Create(); 104 return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name, 105 module)) 106 ->GetSP(); 107 } 108 109 ValueObjectConstResult::ValueObjectConstResult( 110 ExecutionContextScope *exe_scope, ValueObjectManager &manager, 111 const CompilerType &compiler_type, ConstString name, 112 const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order, 113 uint32_t data_addr_size, lldb::addr_t address) 114 : ValueObject(exe_scope, manager), m_impl(this, address) { 115 m_data.SetByteOrder(data_byte_order); 116 m_data.SetAddressByteSize(data_addr_size); 117 m_data.SetData(data_sp); 118 m_value.GetScalar() = (uintptr_t)data_sp->GetBytes(); 119 m_value.SetValueType(Value::ValueType::HostAddress); 120 m_value.SetCompilerType(compiler_type); 121 m_name = name; 122 SetIsConstant(); 123 SetValueIsValid(true); 124 SetAddressTypeOfChildren(eAddressTypeLoad); 125 } 126 127 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, 128 const CompilerType &compiler_type, 129 ConstString name, 130 lldb::addr_t address, 131 AddressType address_type, 132 uint32_t addr_byte_size) { 133 auto manager_sp = ValueObjectManager::Create(); 134 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type, 135 name, address, address_type, 136 addr_byte_size)) 137 ->GetSP(); 138 } 139 140 ValueObjectConstResult::ValueObjectConstResult( 141 ExecutionContextScope *exe_scope, ValueObjectManager &manager, 142 const CompilerType &compiler_type, ConstString name, lldb::addr_t address, 143 AddressType address_type, uint32_t addr_byte_size) 144 : ValueObject(exe_scope, manager), m_type_name(), 145 m_impl(this, address) { 146 m_value.GetScalar() = address; 147 m_data.SetAddressByteSize(addr_byte_size); 148 m_value.GetScalar().GetData(m_data, addr_byte_size); 149 // m_value.SetValueType(Value::ValueType::HostAddress); 150 switch (address_type) { 151 case eAddressTypeInvalid: 152 m_value.SetValueType(Value::ValueType::Scalar); 153 break; 154 case eAddressTypeFile: 155 m_value.SetValueType(Value::ValueType::FileAddress); 156 break; 157 case eAddressTypeLoad: 158 m_value.SetValueType(Value::ValueType::LoadAddress); 159 break; 160 case eAddressTypeHost: 161 m_value.SetValueType(Value::ValueType::HostAddress); 162 break; 163 } 164 m_value.SetCompilerType(compiler_type); 165 m_name = name; 166 SetIsConstant(); 167 SetValueIsValid(true); 168 SetAddressTypeOfChildren(eAddressTypeLoad); 169 } 170 171 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, 172 const Status &error) { 173 auto manager_sp = ValueObjectManager::Create(); 174 return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP(); 175 } 176 177 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, 178 ValueObjectManager &manager, 179 const Status &error) 180 : ValueObject(exe_scope, manager), m_impl(this) { 181 m_error = error; 182 SetIsConstant(); 183 } 184 185 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, 186 ValueObjectManager &manager, 187 const Value &value, 188 ConstString name, Module *module) 189 : ValueObject(exe_scope, manager), m_impl(this) { 190 m_value = value; 191 m_name = name; 192 ExecutionContext exe_ctx; 193 exe_scope->CalculateExecutionContext(exe_ctx); 194 m_error = m_value.GetValueAsData(&exe_ctx, m_data, module); 195 } 196 197 ValueObjectConstResult::~ValueObjectConstResult() = default; 198 199 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() { 200 return m_value.GetCompilerType(); 201 } 202 203 lldb::ValueType ValueObjectConstResult::GetValueType() const { 204 return eValueTypeConstResult; 205 } 206 207 std::optional<uint64_t> ValueObjectConstResult::GetByteSize() { 208 ExecutionContext exe_ctx(GetExecutionContextRef()); 209 if (!m_byte_size) { 210 if (auto size = 211 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope())) 212 SetByteSize(*size); 213 } 214 return m_byte_size; 215 } 216 217 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; } 218 219 llvm::Expected<uint32_t> 220 ValueObjectConstResult::CalculateNumChildren(uint32_t max) { 221 ExecutionContext exe_ctx(GetExecutionContextRef()); 222 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx); 223 if (!children_count) 224 return children_count; 225 return *children_count <= max ? *children_count : max; 226 } 227 228 ConstString ValueObjectConstResult::GetTypeName() { 229 if (m_type_name.IsEmpty()) 230 m_type_name = GetCompilerType().GetTypeName(); 231 return m_type_name; 232 } 233 234 ConstString ValueObjectConstResult::GetDisplayTypeName() { 235 return GetCompilerType().GetDisplayTypeName(); 236 } 237 238 bool ValueObjectConstResult::UpdateValue() { 239 // Const value is always valid 240 SetValueIsValid(true); 241 return true; 242 } 243 244 bool ValueObjectConstResult::IsInScope() { 245 // A const result value is always in scope since it serializes all 246 // information needed to contain the constant value. 247 return true; 248 } 249 250 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) { 251 return m_impl.Dereference(error); 252 } 253 254 lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset( 255 uint32_t offset, const CompilerType &type, bool can_create, 256 ConstString name_const_str) { 257 return m_impl.GetSyntheticChildAtOffset(offset, type, can_create, 258 name_const_str); 259 } 260 261 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) { 262 return m_impl.AddressOf(error); 263 } 264 265 lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address, 266 AddressType *address_type) { 267 return m_impl.GetAddressOf(scalar_is_load_address, address_type); 268 } 269 270 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data, 271 uint32_t item_idx, 272 uint32_t item_count) { 273 return m_impl.GetPointeeData(data, item_idx, item_count); 274 } 275 276 lldb::ValueObjectSP 277 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) { 278 // Always recalculate dynamic values for const results as the memory that 279 // they might point to might have changed at any time. 280 if (use_dynamic != eNoDynamicValues) { 281 if (!IsDynamic()) { 282 ExecutionContext exe_ctx(GetExecutionContextRef()); 283 Process *process = exe_ctx.GetProcessPtr(); 284 if (process && process->IsPossibleDynamicValue(*this)) 285 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic); 286 } 287 if (m_dynamic_value && m_dynamic_value->GetError().Success()) 288 return m_dynamic_value->GetSP(); 289 } 290 return ValueObjectSP(); 291 } 292 293 lldb::ValueObjectSP 294 ValueObjectConstResult::DoCast(const CompilerType &compiler_type) { 295 return m_impl.Cast(compiler_type); 296 } 297 298 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() { 299 if (m_preferred_display_language != lldb::eLanguageTypeUnknown) 300 return m_preferred_display_language; 301 return GetCompilerTypeImpl().GetMinimumLanguage(); 302 } 303