blob: ceb4491f86663e9331cebff62d1b7155a27aa961 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ValueObjectConstResult.cpp ----------------------------------------===//
Greg Clayton1d3afba2010-10-05 00:00:42 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Clayton1d3afba2010-10-05 00:00:42 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Core/ValueObjectConstResult.h"
10
Greg Clayton94073022012-07-07 01:22:45 +000011#include "lldb/Core/ValueObjectDynamicValue.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000012#include "lldb/Symbol/CompilerType.h"
13#include "lldb/Target/ExecutionContext.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000014#include "lldb/Target/ExecutionContextScope.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000015#include "lldb/Target/Process.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000016#include "lldb/Utility/DataBuffer.h"
17#include "lldb/Utility/DataBufferHeap.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000018#include "lldb/Utility/DataExtractor.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000019#include "lldb/Utility/Scalar.h"
Greg Clayton1d3afba2010-10-05 00:00:42 +000020
Zachary Turner2f3df612017-04-06 21:28:29 +000021namespace lldb_private {
22class Module;
23}
Greg Clayton1d3afba2010-10-05 00:00:42 +000024
25using namespace lldb;
26using namespace lldb_private;
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
29 ByteOrder byte_order,
30 uint32_t addr_byte_size,
31 lldb::addr_t address) {
Pavel Labath363f05b2020-02-03 18:05:21 -080032 auto manager_sp = ValueObjectManager::Create();
33 return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,
34 addr_byte_size, address))
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 ->GetSP();
Jim Ingham58b59f92011-04-22 23:53:53 +000036}
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
Pavel Labath363f05b2020-02-03 18:05:21 -080039 ValueObjectManager &manager,
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 ByteOrder byte_order,
41 uint32_t addr_byte_size,
42 lldb::addr_t address)
Adrian Prantl113f56f2020-07-25 08:27:21 -070043 : ValueObject(exe_scope, manager), m_impl(this, address) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 SetIsConstant();
45 SetValueIsValid(true);
46 m_data.SetByteOrder(byte_order);
47 m_data.SetAddressByteSize(addr_byte_size);
48 SetAddressTypeOfChildren(eAddressTypeLoad);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000049}
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
52 const CompilerType &compiler_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +000053 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 const DataExtractor &data,
55 lldb::addr_t address) {
Pavel Labath363f05b2020-02-03 18:05:21 -080056 auto manager_sp = ValueObjectManager::Create();
57 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
58 name, data, address))
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 ->GetSP();
Jim Ingham58b59f92011-04-22 23:53:53 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062ValueObjectConstResult::ValueObjectConstResult(
Pavel Labath363f05b2020-02-03 18:05:21 -080063 ExecutionContextScope *exe_scope, ValueObjectManager &manager,
64 const CompilerType &compiler_type, ConstString name,
65 const DataExtractor &data, lldb::addr_t address)
Adrian Prantl113f56f2020-07-25 08:27:21 -070066 : ValueObject(exe_scope, manager), m_impl(this, address) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 m_data = data;
68
69 if (!m_data.GetSharedDataBuffer()) {
70 DataBufferSP shared_data_buffer(
71 new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
72 m_data.SetData(shared_data_buffer);
73 }
74
75 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
76 m_value.SetValueType(Value::eValueTypeHostAddress);
77 m_value.SetCompilerType(compiler_type);
78 m_name = name;
79 SetIsConstant();
80 SetValueIsValid(true);
81 SetAddressTypeOfChildren(eAddressTypeLoad);
82}
83
84ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
85 const CompilerType &compiler_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +000086 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 const lldb::DataBufferSP &data_sp,
88 lldb::ByteOrder data_byte_order,
89 uint32_t data_addr_size,
90 lldb::addr_t address) {
Pavel Labath363f05b2020-02-03 18:05:21 -080091 auto manager_sp = ValueObjectManager::Create();
92 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
93 name, data_sp, data_byte_order,
94 data_addr_size, address))
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 ->GetSP();
96}
97
98ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
99 Value &value,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000100 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 Module *module) {
Pavel Labath363f05b2020-02-03 18:05:21 -0800102 auto manager_sp = ValueObjectManager::Create();
103 return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
104 module))
105 ->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106}
107
108ValueObjectConstResult::ValueObjectConstResult(
Pavel Labath363f05b2020-02-03 18:05:21 -0800109 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)
Adrian Prantl113f56f2020-07-25 08:27:21 -0700113 : ValueObject(exe_scope, manager), m_impl(this, address) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 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::eValueTypeHostAddress);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 m_value.SetCompilerType(compiler_type);
120 m_name = name;
121 SetIsConstant();
122 SetValueIsValid(true);
123 SetAddressTypeOfChildren(eAddressTypeLoad);
124}
125
126ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
127 const CompilerType &compiler_type,
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000128 ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 lldb::addr_t address,
130 AddressType address_type,
131 uint32_t addr_byte_size) {
Pavel Labath363f05b2020-02-03 18:05:21 -0800132 auto manager_sp = ValueObjectManager::Create();
133 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
134 name, address, address_type,
135 addr_byte_size))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 ->GetSP();
137}
138
139ValueObjectConstResult::ValueObjectConstResult(
Pavel Labath363f05b2020-02-03 18:05:21 -0800140 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)
Adrian Prantl113f56f2020-07-25 08:27:21 -0700143 : ValueObject(exe_scope, manager), m_type_name(),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 m_impl(this, address) {
145 m_value.GetScalar() = address;
146 m_data.SetAddressByteSize(addr_byte_size);
147 m_value.GetScalar().GetData(m_data, addr_byte_size);
148 // m_value.SetValueType(Value::eValueTypeHostAddress);
149 switch (address_type) {
150 case eAddressTypeInvalid:
151 m_value.SetValueType(Value::eValueTypeScalar);
152 break;
153 case eAddressTypeFile:
154 m_value.SetValueType(Value::eValueTypeFileAddress);
155 break;
156 case eAddressTypeLoad:
157 m_value.SetValueType(Value::eValueTypeLoadAddress);
158 break;
159 case eAddressTypeHost:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000160 m_value.SetValueType(Value::eValueTypeHostAddress);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 break;
162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 m_value.SetCompilerType(compiler_type);
164 m_name = name;
165 SetIsConstant();
166 SetValueIsValid(true);
167 SetAddressTypeOfChildren(eAddressTypeLoad);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000168}
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
Zachary Turner97206d52017-05-12 04:51:55 +0000171 const Status &error) {
Pavel Labath363f05b2020-02-03 18:05:21 -0800172 auto manager_sp = ValueObjectManager::Create();
173 return (new ValueObjectConstResult(exe_scope, *manager_sp, error))->GetSP();
Jim Ingham58b59f92011-04-22 23:53:53 +0000174}
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
Pavel Labath363f05b2020-02-03 18:05:21 -0800177 ValueObjectManager &manager,
Zachary Turner97206d52017-05-12 04:51:55 +0000178 const Status &error)
Adrian Prantl113f56f2020-07-25 08:27:21 -0700179 : ValueObject(exe_scope, manager), m_impl(this) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 m_error = error;
181 SetIsConstant();
Jim Ingham73ca05a2011-12-17 01:35:57 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
Pavel Labath363f05b2020-02-03 18:05:21 -0800185 ValueObjectManager &manager,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 const Value &value,
Pavel Labath363f05b2020-02-03 18:05:21 -0800187 ConstString name, Module *module)
Adrian Prantl113f56f2020-07-25 08:27:21 -0700188 : ValueObject(exe_scope, manager), m_impl(this) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 m_value = value;
190 m_name = name;
191 ExecutionContext exe_ctx;
192 exe_scope->CalculateExecutionContext(exe_ctx);
Adrian Prantld9cbd2a2019-08-08 19:22:32 +0000193 m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000194}
195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196ValueObjectConstResult::~ValueObjectConstResult() {}
197
198CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
199 return m_value.GetCompilerType();
Jim Ingham58b59f92011-04-22 23:53:53 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202lldb::ValueType ValueObjectConstResult::GetValueType() const {
203 return eValueTypeConstResult;
204}
205
Adrian Prantl113f56f2020-07-25 08:27:21 -0700206llvm::Optional<uint64_t> ValueObjectConstResult::GetByteSize() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 ExecutionContext exe_ctx(GetExecutionContextRef());
Adrian Prantl113f56f2020-07-25 08:27:21 -0700208 if (!m_byte_size) {
Adrian Prantld963a7c2019-01-15 18:07:52 +0000209 if (auto size =
210 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope()))
211 SetByteSize(*size);
212 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return m_byte_size;
214}
215
216void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
217
218size_t ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
Adrian Prantleca07c52018-11-05 20:49:07 +0000219 ExecutionContext exe_ctx(GetExecutionContextRef());
220 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 return children_count <= max ? children_count : max;
222}
223
224ConstString ValueObjectConstResult::GetTypeName() {
225 if (m_type_name.IsEmpty())
Raphael Isemann30ce9562020-02-12 09:35:19 +0100226 m_type_name = GetCompilerType().GetTypeName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 return m_type_name;
228}
229
230ConstString ValueObjectConstResult::GetDisplayTypeName() {
231 return GetCompilerType().GetDisplayTypeName();
232}
233
234bool ValueObjectConstResult::UpdateValue() {
235 // Const value is always valid
236 SetValueIsValid(true);
237 return true;
238}
239
240bool ValueObjectConstResult::IsInScope() {
241 // A const result value is always in scope since it serializes all
242 // information needed to contain the constant value.
243 return true;
244}
245
Zachary Turner97206d52017-05-12 04:51:55 +0000246lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 return m_impl.Dereference(error);
248}
249
250lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
251 uint32_t offset, const CompilerType &type, bool can_create,
252 ConstString name_const_str) {
253 return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
254 name_const_str);
255}
256
Zachary Turner97206d52017-05-12 04:51:55 +0000257lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 return m_impl.AddressOf(error);
259}
260
261lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
262 AddressType *address_type) {
263 return m_impl.GetAddressOf(scalar_is_load_address, address_type);
264}
265
266ValueObject *ValueObjectConstResult::CreateChildAtIndex(
267 size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
268 return m_impl.CreateChildAtIndex(idx, synthetic_array_member,
269 synthetic_index);
270}
271
272size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
273 uint32_t item_idx,
274 uint32_t item_count) {
275 return m_impl.GetPointeeData(data, item_idx, item_count);
276}
277
278lldb::ValueObjectSP
279ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
280 // Always recalculate dynamic values for const results as the memory that
281 // they might point to might have changed at any time.
282 if (use_dynamic != eNoDynamicValues) {
283 if (!IsDynamic()) {
284 ExecutionContext exe_ctx(GetExecutionContextRef());
285 Process *process = exe_ctx.GetProcessPtr();
286 if (process && process->IsPossibleDynamicValue(*this))
287 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000288 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (m_dynamic_value)
290 return m_dynamic_value->GetSP();
291 }
292 return ValueObjectSP();
Greg Clayton1d3afba2010-10-05 00:00:42 +0000293}
Enrico Granata9128ee22011-09-06 19:20:51 +0000294
295lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296ValueObjectConstResult::Cast(const CompilerType &compiler_type) {
297 return m_impl.Cast(compiler_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000298}
299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
301 if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
302 return m_preferred_display_language;
303 return GetCompilerTypeImpl().GetMinimumLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +0000304}