Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame^] | 1 | //===-- ValueObjectConstResult.cpp ----------------------------------------===// |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Core/ValueObjectConstResult.h" |
| 10 | |
Greg Clayton | 9407302 | 2012-07-07 01:22:45 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ValueObjectDynamicValue.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 12 | #include "lldb/Symbol/CompilerType.h" |
| 13 | #include "lldb/Target/ExecutionContext.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 14 | #include "lldb/Target/ExecutionContextScope.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Process.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/DataBuffer.h" |
| 17 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/DataExtractor.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Scalar.h" |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 20 | |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 21 | namespace lldb_private { |
| 22 | class Module; |
| 23 | } |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 29 | ByteOrder byte_order, |
| 30 | uint32_t addr_byte_size, |
| 31 | lldb::addr_t address) { |
| 32 | return (new ValueObjectConstResult(exe_scope, byte_order, addr_byte_size, |
| 33 | address)) |
| 34 | ->GetSP(); |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, |
| 38 | ByteOrder byte_order, |
| 39 | uint32_t addr_byte_size, |
| 40 | lldb::addr_t address) |
| 41 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), |
| 42 | m_impl(this, address) { |
| 43 | SetIsConstant(); |
| 44 | SetValueIsValid(true); |
| 45 | m_data.SetByteOrder(byte_order); |
| 46 | m_data.SetAddressByteSize(addr_byte_size); |
| 47 | SetAddressTypeOfChildren(eAddressTypeLoad); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 51 | const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 52 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | const DataExtractor &data, |
| 54 | lldb::addr_t address) { |
| 55 | return (new ValueObjectConstResult(exe_scope, compiler_type, name, data, |
| 56 | address)) |
| 57 | ->GetSP(); |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | ValueObjectConstResult::ValueObjectConstResult( |
| 61 | ExecutionContextScope *exe_scope, const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 62 | ConstString name, const DataExtractor &data, lldb::addr_t address) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), |
| 64 | m_impl(this, address) { |
| 65 | m_data = data; |
| 66 | |
| 67 | if (!m_data.GetSharedDataBuffer()) { |
| 68 | DataBufferSP shared_data_buffer( |
| 69 | new DataBufferHeap(data.GetDataStart(), data.GetByteSize())); |
| 70 | m_data.SetData(shared_data_buffer); |
| 71 | } |
| 72 | |
| 73 | m_value.GetScalar() = (uintptr_t)m_data.GetDataStart(); |
| 74 | m_value.SetValueType(Value::eValueTypeHostAddress); |
| 75 | m_value.SetCompilerType(compiler_type); |
| 76 | m_name = name; |
| 77 | SetIsConstant(); |
| 78 | SetValueIsValid(true); |
| 79 | SetAddressTypeOfChildren(eAddressTypeLoad); |
| 80 | } |
| 81 | |
| 82 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 83 | const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 84 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | const lldb::DataBufferSP &data_sp, |
| 86 | lldb::ByteOrder data_byte_order, |
| 87 | uint32_t data_addr_size, |
| 88 | lldb::addr_t address) { |
| 89 | return (new ValueObjectConstResult(exe_scope, compiler_type, name, data_sp, |
| 90 | data_byte_order, data_addr_size, address)) |
| 91 | ->GetSP(); |
| 92 | } |
| 93 | |
| 94 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 95 | Value &value, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 96 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | Module *module) { |
| 98 | return (new ValueObjectConstResult(exe_scope, value, name, module))->GetSP(); |
| 99 | } |
| 100 | |
| 101 | ValueObjectConstResult::ValueObjectConstResult( |
| 102 | ExecutionContextScope *exe_scope, const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 103 | ConstString name, const lldb::DataBufferSP &data_sp, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | lldb::ByteOrder data_byte_order, uint32_t data_addr_size, |
| 105 | lldb::addr_t address) |
| 106 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), |
| 107 | m_impl(this, address) { |
| 108 | m_data.SetByteOrder(data_byte_order); |
| 109 | m_data.SetAddressByteSize(data_addr_size); |
| 110 | m_data.SetData(data_sp); |
| 111 | m_value.GetScalar() = (uintptr_t)data_sp->GetBytes(); |
| 112 | m_value.SetValueType(Value::eValueTypeHostAddress); |
| 113 | // m_value.SetContext(Value::eContextTypeClangType, compiler_type); |
| 114 | m_value.SetCompilerType(compiler_type); |
| 115 | m_name = name; |
| 116 | SetIsConstant(); |
| 117 | SetValueIsValid(true); |
| 118 | SetAddressTypeOfChildren(eAddressTypeLoad); |
| 119 | } |
| 120 | |
| 121 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
| 122 | const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 123 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | lldb::addr_t address, |
| 125 | AddressType address_type, |
| 126 | uint32_t addr_byte_size) { |
| 127 | return (new ValueObjectConstResult(exe_scope, compiler_type, name, address, |
| 128 | address_type, addr_byte_size)) |
| 129 | ->GetSP(); |
| 130 | } |
| 131 | |
| 132 | ValueObjectConstResult::ValueObjectConstResult( |
| 133 | ExecutionContextScope *exe_scope, const CompilerType &compiler_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 134 | ConstString name, lldb::addr_t address, AddressType address_type, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | uint32_t addr_byte_size) |
| 136 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), |
| 137 | m_impl(this, address) { |
| 138 | m_value.GetScalar() = address; |
| 139 | m_data.SetAddressByteSize(addr_byte_size); |
| 140 | m_value.GetScalar().GetData(m_data, addr_byte_size); |
| 141 | // m_value.SetValueType(Value::eValueTypeHostAddress); |
| 142 | switch (address_type) { |
| 143 | case eAddressTypeInvalid: |
| 144 | m_value.SetValueType(Value::eValueTypeScalar); |
| 145 | break; |
| 146 | case eAddressTypeFile: |
| 147 | m_value.SetValueType(Value::eValueTypeFileAddress); |
| 148 | break; |
| 149 | case eAddressTypeLoad: |
| 150 | m_value.SetValueType(Value::eValueTypeLoadAddress); |
| 151 | break; |
| 152 | case eAddressTypeHost: |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 153 | m_value.SetValueType(Value::eValueTypeHostAddress); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | // m_value.SetContext(Value::eContextTypeClangType, compiler_type); |
| 157 | m_value.SetCompilerType(compiler_type); |
| 158 | m_name = name; |
| 159 | SetIsConstant(); |
| 160 | SetValueIsValid(true); |
| 161 | SetAddressTypeOfChildren(eAddressTypeLoad); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 165 | const Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | return (new ValueObjectConstResult(exe_scope, error))->GetSP(); |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 170 | const Status &error) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) { |
| 172 | m_error = error; |
| 173 | SetIsConstant(); |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope, |
| 177 | const Value &value, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 178 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | Module *module) |
| 180 | : ValueObject(exe_scope), m_type_name(), m_byte_size(0), m_impl(this) { |
| 181 | m_value = value; |
| 182 | m_name = name; |
| 183 | ExecutionContext exe_ctx; |
| 184 | exe_scope->CalculateExecutionContext(exe_ctx); |
Adrian Prantl | d9cbd2a | 2019-08-08 19:22:32 +0000 | [diff] [blame] | 185 | m_error = m_value.GetValueAsData(&exe_ctx, m_data, module); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | ValueObjectConstResult::~ValueObjectConstResult() {} |
| 189 | |
| 190 | CompilerType ValueObjectConstResult::GetCompilerTypeImpl() { |
| 191 | return m_value.GetCompilerType(); |
Jim Ingham | 58b59f9 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | lldb::ValueType ValueObjectConstResult::GetValueType() const { |
| 195 | return eValueTypeConstResult; |
| 196 | } |
| 197 | |
| 198 | uint64_t ValueObjectConstResult::GetByteSize() { |
| 199 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 200 | if (m_byte_size == 0) { |
| 201 | if (auto size = |
| 202 | GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope())) |
| 203 | SetByteSize(*size); |
| 204 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | return m_byte_size; |
| 206 | } |
| 207 | |
| 208 | void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; } |
| 209 | |
| 210 | size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) { |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 211 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
| 212 | auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | return children_count <= max ? children_count : max; |
| 214 | } |
| 215 | |
| 216 | ConstString ValueObjectConstResult::GetTypeName() { |
| 217 | if (m_type_name.IsEmpty()) |
| 218 | m_type_name = GetCompilerType().GetConstTypeName(); |
| 219 | return m_type_name; |
| 220 | } |
| 221 | |
| 222 | ConstString ValueObjectConstResult::GetDisplayTypeName() { |
| 223 | return GetCompilerType().GetDisplayTypeName(); |
| 224 | } |
| 225 | |
| 226 | bool ValueObjectConstResult::UpdateValue() { |
| 227 | // Const value is always valid |
| 228 | SetValueIsValid(true); |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool ValueObjectConstResult::IsInScope() { |
| 233 | // A const result value is always in scope since it serializes all |
| 234 | // information needed to contain the constant value. |
| 235 | return true; |
| 236 | } |
| 237 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 238 | lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | return m_impl.Dereference(error); |
| 240 | } |
| 241 | |
| 242 | lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset( |
| 243 | uint32_t offset, const CompilerType &type, bool can_create, |
| 244 | ConstString name_const_str) { |
| 245 | return m_impl.GetSyntheticChildAtOffset(offset, type, can_create, |
| 246 | name_const_str); |
| 247 | } |
| 248 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 249 | lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | return m_impl.AddressOf(error); |
| 251 | } |
| 252 | |
| 253 | lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address, |
| 254 | AddressType *address_type) { |
| 255 | return m_impl.GetAddressOf(scalar_is_load_address, address_type); |
| 256 | } |
| 257 | |
| 258 | ValueObject *ValueObjectConstResult::CreateChildAtIndex( |
| 259 | size_t idx, bool synthetic_array_member, int32_t synthetic_index) { |
| 260 | return m_impl.CreateChildAtIndex(idx, synthetic_array_member, |
| 261 | synthetic_index); |
| 262 | } |
| 263 | |
| 264 | size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data, |
| 265 | uint32_t item_idx, |
| 266 | uint32_t item_count) { |
| 267 | return m_impl.GetPointeeData(data, item_idx, item_count); |
| 268 | } |
| 269 | |
| 270 | lldb::ValueObjectSP |
| 271 | ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) { |
| 272 | // Always recalculate dynamic values for const results as the memory that |
| 273 | // they might point to might have changed at any time. |
| 274 | if (use_dynamic != eNoDynamicValues) { |
| 275 | if (!IsDynamic()) { |
| 276 | ExecutionContext exe_ctx(GetExecutionContextRef()); |
| 277 | Process *process = exe_ctx.GetProcessPtr(); |
| 278 | if (process && process->IsPossibleDynamicValue(*this)) |
| 279 | m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 280 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | if (m_dynamic_value) |
| 282 | return m_dynamic_value->GetSP(); |
| 283 | } |
| 284 | return ValueObjectSP(); |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 285 | } |
Enrico Granata | 9128ee2 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 286 | |
| 287 | lldb::ValueObjectSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | ValueObjectConstResult::Cast(const CompilerType &compiler_type) { |
| 289 | return m_impl.Cast(compiler_type); |
Enrico Granata | 9128ee2 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 292 | lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() { |
| 293 | if (m_preferred_display_language != lldb::eLanguageTypeUnknown) |
| 294 | return m_preferred_display_language; |
| 295 | return GetCompilerTypeImpl().GetMinimumLanguage(); |
Enrico Granata | c1247f5 | 2014-11-06 21:23:20 +0000 | [diff] [blame] | 296 | } |