blob: 1965d708284a341d62ec666a7052432bcb983032 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ValueObject.cpp -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ValueObject.h"
11
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000012#include "lldb/Core/Address.h"
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Core/Module.h"
Enrico Granata21fd13f2012-10-27 02:05:48 +000014#include "lldb/Core/ValueObjectCast.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000016#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000017#include "lldb/Core/ValueObjectDynamicValue.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000018#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000019#include "lldb/Core/ValueObjectSyntheticFilter.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000020#include "lldb/DataFormatters/DataVisualization.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000021#include "lldb/DataFormatters/DumpValueObjectOptions.h"
22#include "lldb/DataFormatters/FormatManager.h"
Enrico Granata2206b482014-10-30 18:27:31 +000023#include "lldb/DataFormatters/StringPrinter.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000024#include "lldb/DataFormatters/TypeFormat.h"
25#include "lldb/DataFormatters/TypeSummary.h"
26#include "lldb/DataFormatters/TypeValidator.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000027#include "lldb/DataFormatters/ValueObjectPrinter.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000028#include "lldb/Expression/ExpressionVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/ClangASTContext.h"
Enrico Granatac1247f52014-11-06 21:23:20 +000030#include "lldb/Symbol/CompileUnit.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000031#include "lldb/Symbol/CompilerType.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000032#include "lldb/Symbol/Declaration.h"
33#include "lldb/Symbol/SymbolContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/Type.h"
Jim Ingham53c47f12010-09-10 23:12:17 +000035#include "lldb/Target/ExecutionContext.h"
Enrico Granata407b5c62015-11-02 21:52:05 +000036#include "lldb/Target/Language.h"
Jim Ingham5a369122010-09-28 01:25:32 +000037#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000038#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/Process.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000040#include "lldb/Target/StackFrame.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000041#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/Thread.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000043#include "lldb/Target/ThreadList.h"
44#include "lldb/Utility/DataBuffer.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000045#include "lldb/Utility/DataBufferHeap.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000046#include "lldb/Utility/Flags.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000047#include "lldb/Utility/Log.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000048#include "lldb/Utility/Logging.h"
49#include "lldb/Utility/Scalar.h"
50#include "lldb/Utility/SharingPtr.h"
51#include "lldb/Utility/Stream.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000052#include "lldb/Utility/StreamString.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000053#include "lldb/lldb-private-types.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000054
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000055#include "llvm/Support/Compiler.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000056
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000057#include <algorithm>
58#include <cstdint>
59#include <cstdlib>
60#include <memory>
61#include <tuple>
Zachary Turner2f3df612017-04-06 21:28:29 +000062
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000063#include <assert.h>
64#include <inttypes.h>
65#include <stdio.h>
66#include <string.h>
Zachary Turner2f3df612017-04-06 21:28:29 +000067
68namespace lldb_private {
69class ExecutionContextScope;
70}
71namespace lldb_private {
72class SymbolContextScope;
73}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
75using namespace lldb;
76using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000077using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Greg Claytonafacd142011-09-02 01:15:17 +000079static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080
81//----------------------------------------------------------------------
82// ValueObject constructor
83//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000084ValueObject::ValueObject(ValueObject &parent)
85 : UserID(++g_value_obj_uid), // Unique identifier for every value object
86 m_parent(&parent), m_root(NULL), m_update_point(parent.GetUpdatePoint()),
87 m_name(), m_data(), m_value(), m_error(), m_value_str(),
88 m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(),
89 m_validation_result(), m_manager(parent.GetManager()), m_children(),
90 m_synthetic_children(), m_dynamic_value(NULL), m_synthetic_value(NULL),
91 m_deref_valobj(NULL), m_format(eFormatDefault),
92 m_last_format(eFormatDefault), m_last_format_mgr_revision(0),
93 m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(),
94 m_type_validator_sp(), m_user_id_of_forced_summary(),
95 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
96 m_value_checksum(),
97 m_preferred_display_language(lldb::eLanguageTypeUnknown),
98 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
99 m_children_count_valid(false), m_old_value_valid(false),
100 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
101 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
102 m_is_getting_summary(false),
103 m_did_calculate_complete_objc_class_type(false),
104 m_is_synthetic_children_generated(
105 parent.m_is_synthetic_children_generated) {
106 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000107}
108
109//----------------------------------------------------------------------
110// ValueObject constructor
111//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112ValueObject::ValueObject(ExecutionContextScope *exe_scope,
113 AddressType child_ptr_or_ref_addr_type)
114 : UserID(++g_value_obj_uid), // Unique identifier for every value object
115 m_parent(NULL), m_root(NULL), m_update_point(exe_scope), m_name(),
116 m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(),
117 m_location_str(), m_summary_str(), m_object_desc_str(),
118 m_validation_result(), m_manager(), m_children(), m_synthetic_children(),
119 m_dynamic_value(NULL), m_synthetic_value(NULL), m_deref_valobj(NULL),
120 m_format(eFormatDefault), m_last_format(eFormatDefault),
121 m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(),
122 m_synthetic_children_sp(), m_type_validator_sp(),
123 m_user_id_of_forced_summary(),
124 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
125 m_value_checksum(),
126 m_preferred_display_language(lldb::eLanguageTypeUnknown),
127 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
128 m_children_count_valid(false), m_old_value_valid(false),
129 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
130 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
131 m_is_getting_summary(false),
132 m_did_calculate_complete_objc_class_type(false),
133 m_is_synthetic_children_generated(false) {
134 m_manager = new ValueObjectManager();
135 m_manager->ManageObject(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
138//----------------------------------------------------------------------
139// Destructor
140//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141ValueObject::~ValueObject() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143bool ValueObject::UpdateValueIfNeeded(bool update_format) {
Greg Claytonb71f3842010-10-05 03:13:51 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 bool did_change_formats = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 if (update_format)
148 did_change_formats = UpdateFormatsIfNeeded();
Greg Claytonefbc7d22012-03-09 04:23:44 +0000149
Adrian Prantl05097242018-04-30 16:49:04 +0000150 // If this is a constant value, then our success is predicated on whether we
151 // have an error or not
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 if (GetIsConstant()) {
153 // if you are constant, things might still have changed behind your back
154 // (e.g. you are a frozen object and things have changed deeper than you
Adrian Prantl05097242018-04-30 16:49:04 +0000155 // cared to freeze-dry yourself) in this case, your value has not changed,
156 // but "computed" entries might have, so you might now have a different
157 // summary, or a different object description. clear these so we will
158 // recompute them
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 if (update_format && !did_change_formats)
160 ClearUserVisibleData(eClearUserVisibleDataItemsSummary |
161 eClearUserVisibleDataItemsDescription);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 return m_error.Success();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164
165 bool first_update = IsChecksumEmpty();
166
167 if (NeedsUpdating()) {
168 m_update_point.SetUpdated();
169
Adrian Prantl05097242018-04-30 16:49:04 +0000170 // Save the old value using swap to avoid a string copy which also will
171 // clear our m_value_str
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 if (m_value_str.empty()) {
173 m_old_value_valid = false;
174 } else {
175 m_old_value_valid = true;
176 m_old_value_str.swap(m_value_str);
177 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
178 }
179
180 ClearUserVisibleData();
181
182 if (IsInScope()) {
183 const bool value_was_valid = GetValueIsValid();
184 SetValueDidChange(false);
185
186 m_error.Clear();
187
188 // Call the pure virtual function to update the value
189
190 bool need_compare_checksums = false;
191 llvm::SmallVector<uint8_t, 16> old_checksum;
192
193 if (!first_update && CanProvideValue()) {
194 need_compare_checksums = true;
195 old_checksum.resize(m_value_checksum.size());
196 std::copy(m_value_checksum.begin(), m_value_checksum.end(),
197 old_checksum.begin());
198 }
199
200 bool success = UpdateValue();
201
202 SetValueIsValid(success);
203
204 if (success) {
205 const uint64_t max_checksum_size = 128;
206 m_data.Checksum(m_value_checksum, max_checksum_size);
207 } else {
208 need_compare_checksums = false;
209 m_value_checksum.clear();
210 }
211
212 assert(!need_compare_checksums ||
213 (!old_checksum.empty() && !m_value_checksum.empty()));
214
215 if (first_update)
216 SetValueDidChange(false);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000217 else if (!m_value_did_change && !success) {
Adrian Prantl05097242018-04-30 16:49:04 +0000218 // The value wasn't gotten successfully, so we mark this as changed if
219 // the value used to be valid and now isn't
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 SetValueDidChange(value_was_valid);
221 } else if (need_compare_checksums) {
222 SetValueDidChange(memcmp(&old_checksum[0], &m_value_checksum[0],
223 m_value_checksum.size()));
224 }
225
226 } else {
227 m_error.SetErrorString("out of scope");
228 }
229 }
230 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233bool ValueObject::UpdateFormatsIfNeeded() {
234 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
235 if (log)
236 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject "
237 "rev: %d - Global rev: %d",
238 GetName().GetCString(), static_cast<void *>(this),
239 m_last_format_mgr_revision,
240 DataVisualization::GetCurrentRevision());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 bool any_change = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if ((m_last_format_mgr_revision != DataVisualization::GetCurrentRevision())) {
245 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
246 any_change = true;
247
248 SetValueFormat(DataVisualization::GetFormat(*this, eNoDynamicValues));
249 SetSummaryFormat(
250 DataVisualization::GetSummaryFormat(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000251#ifndef LLDB_DISABLE_PYTHON
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 SetSyntheticChildren(
253 DataVisualization::GetSyntheticChildren(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000254#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
256 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 return any_change;
Enrico Granata4becb372011-06-29 22:27:15 +0000259}
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261void ValueObject::SetNeedsUpdate() {
262 m_update_point.SetNeedsUpdate();
263 // We have to clear the value string here so ConstResult children will notice
Adrian Prantl05097242018-04-30 16:49:04 +0000264 // if their values are changed by hand (i.e. with SetValueAsCString).
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000266}
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268void ValueObject::ClearDynamicTypeInformation() {
269 m_children_count_valid = false;
270 m_did_calculate_complete_objc_class_type = false;
271 m_last_format_mgr_revision = 0;
272 m_override_type = CompilerType();
273 SetValueFormat(lldb::TypeFormatImplSP());
274 SetSummaryFormat(lldb::TypeSummaryImplSP());
275 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000276}
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278CompilerType ValueObject::MaybeCalculateCompleteType() {
279 CompilerType compiler_type(GetCompilerTypeImpl());
280
281 if (m_did_calculate_complete_objc_class_type) {
282 if (m_override_type.IsValid())
283 return m_override_type;
Sean Callanan72772842012-02-22 23:57:45 +0000284 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 return compiler_type;
286 }
287
288 CompilerType class_type;
289 bool is_pointer_type = false;
290
291 if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type)) {
292 is_pointer_type = true;
293 } else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type)) {
294 class_type = compiler_type;
295 } else {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000296 return compiler_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 }
298
299 m_did_calculate_complete_objc_class_type = true;
300
301 if (class_type) {
302 ConstString class_name(class_type.GetConstTypeName());
303
304 if (class_name) {
305 ProcessSP process_sp(
306 GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
307
308 if (process_sp) {
309 ObjCLanguageRuntime *objc_language_runtime(
310 process_sp->GetObjCLanguageRuntime());
311
312 if (objc_language_runtime) {
313 TypeSP complete_objc_class_type_sp =
314 objc_language_runtime->LookupInCompleteClassCache(class_name);
315
316 if (complete_objc_class_type_sp) {
317 CompilerType complete_class(
318 complete_objc_class_type_sp->GetFullCompilerType());
319
320 if (complete_class.GetCompleteType()) {
321 if (is_pointer_type) {
322 m_override_type = complete_class.GetPointerType();
323 } else {
324 m_override_type = complete_class;
325 }
326
327 if (m_override_type.IsValid())
328 return m_override_type;
329 }
330 }
331 }
332 }
333 }
334 }
335 return compiler_type;
Sean Callanan72772842012-02-22 23:57:45 +0000336}
337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338CompilerType ValueObject::GetCompilerType() {
339 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342TypeImpl ValueObject::GetTypeImpl() { return TypeImpl(GetCompilerType()); }
343
344DataExtractor &ValueObject::GetDataExtractor() {
345 UpdateValueIfNeeded(false);
346 return m_data;
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000347}
348
Zachary Turner97206d52017-05-12 04:51:55 +0000349const Status &ValueObject::GetError() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 UpdateValueIfNeeded(false);
351 return m_error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354const ConstString &ValueObject::GetName() const { return m_name; }
355
356const char *ValueObject::GetLocationAsCString() {
357 return GetLocationAsCStringImpl(m_value, m_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
361 const DataExtractor &data) {
362 if (UpdateValueIfNeeded(false)) {
363 if (m_location_str.empty()) {
364 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 Value::ValueType value_type = value.GetValueType();
Enrico Granata82fabf82013-04-30 20:45:04 +0000367
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 switch (value_type) {
369 case Value::eValueTypeScalar:
370 case Value::eValueTypeVector:
371 if (value.GetContextType() == Value::eContextTypeRegisterInfo) {
372 RegisterInfo *reg_info = value.GetRegisterInfo();
373 if (reg_info) {
374 if (reg_info->name)
375 m_location_str = reg_info->name;
376 else if (reg_info->alt_name)
377 m_location_str = reg_info->alt_name;
378 if (m_location_str.empty())
379 m_location_str = (reg_info->encoding == lldb::eEncodingVector)
380 ? "vector"
381 : "scalar";
382 }
383 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 if (m_location_str.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 m_location_str =
386 (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
387 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 case Value::eValueTypeLoadAddress:
390 case Value::eValueTypeFileAddress:
391 case Value::eValueTypeHostAddress: {
392 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
393 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size,
394 value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
Zachary Turnerc1564272016-11-16 21:15:24 +0000395 m_location_str = sstr.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 } break;
397 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 }
400 return m_location_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403Value &ValueObject::GetValue() { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405const Value &ValueObject::GetValue() const { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407bool ValueObject::ResolveValue(Scalar &scalar) {
408 if (UpdateValueIfNeeded(
409 false)) // make sure that you are up to date before returning anything
410 {
411 ExecutionContext exe_ctx(GetExecutionContextRef());
412 Value tmp_value(m_value);
413 scalar = tmp_value.ResolveValue(&exe_ctx);
414 if (scalar.IsValid()) {
415 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
416 if (bitfield_bit_size)
417 return scalar.ExtractBitfield(bitfield_bit_size,
418 GetBitfieldBitOffset());
419 return true;
Enrico Granata6fd87d52011-08-04 01:41:02 +0000420 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 }
422 return false;
423}
424
Zachary Turner97206d52017-05-12 04:51:55 +0000425bool ValueObject::IsLogicalTrue(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
427 LazyBool is_logical_true = language->IsLogicalTrue(*this, error);
428 switch (is_logical_true) {
429 case eLazyBoolYes:
430 case eLazyBoolNo:
431 return (is_logical_true == true);
432 case eLazyBoolCalculate:
433 break;
434 }
435 }
436
437 Scalar scalar_value;
438
439 if (!ResolveValue(scalar_value)) {
440 error.SetErrorString("failed to get a scalar result");
Greg Claytondcad5022011-12-29 01:26:56 +0000441 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 }
443
444 bool ret;
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000445 ret = scalar_value.ULongLong(1) != 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 error.Clear();
447 return ret;
Greg Clayton8f343b02010-11-04 01:54:29 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450bool ValueObject::GetValueIsValid() const { return m_value_is_valid; }
451
452void ValueObject::SetValueIsValid(bool b) { m_value_is_valid = b; }
453
454bool ValueObject::GetValueDidChange() { return m_value_did_change; }
455
456void ValueObject::SetValueDidChange(bool value_changed) {
457 m_value_did_change = value_changed;
458}
459
460ValueObjectSP ValueObject::GetChildAtIndex(size_t idx, bool can_create) {
461 ValueObjectSP child_sp;
462 // We may need to update our value if we are dynamic
463 if (IsPossibleDynamicType())
464 UpdateValueIfNeeded(false);
465 if (idx < GetNumChildren()) {
466 // Check if we have already made the child value object?
467 if (can_create && !m_children.HasChildAtIndex(idx)) {
468 // No we haven't created the child at this index, so lets have our
469 // subclass do it and cache the result for quick future access.
470 m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
Enrico Granata407b5c62015-11-02 21:52:05 +0000471 }
Jim Ingham98e6daf2015-10-31 00:02:18 +0000472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 ValueObject *child = m_children.GetChildAtIndex(idx);
474 if (child != NULL)
475 return child->GetSP();
476 }
477 return child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478}
479
Enrico Granata3309d882013-01-12 01:00:22 +0000480lldb::ValueObjectSP
Lang Hames088d0012017-04-26 18:15:40 +0000481ValueObject::GetChildAtIndexPath(llvm::ArrayRef<size_t> idxs,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 size_t *index_of_error) {
483 if (idxs.size() == 0)
484 return GetSP();
485 ValueObjectSP root(GetSP());
486 for (size_t idx : idxs) {
487 root = root->GetChildAtIndex(idx, true);
488 if (!root) {
489 if (index_of_error)
490 *index_of_error = idx;
491 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000492 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 }
494 return root;
495}
496
497lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
Lang Hames088d0012017-04-26 18:15:40 +0000498 llvm::ArrayRef<std::pair<size_t, bool>> idxs, size_t *index_of_error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499 if (idxs.size() == 0)
500 return GetSP();
501 ValueObjectSP root(GetSP());
502 for (std::pair<size_t, bool> idx : idxs) {
503 root = root->GetChildAtIndex(idx.first, idx.second);
504 if (!root) {
505 if (index_of_error)
506 *index_of_error = idx.first;
507 return root;
508 }
509 }
510 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000511}
512
513lldb::ValueObjectSP
Lang Hames088d0012017-04-26 18:15:40 +0000514ValueObject::GetChildAtNamePath(llvm::ArrayRef<ConstString> names,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 ConstString *name_of_error) {
516 if (names.size() == 0)
517 return GetSP();
518 ValueObjectSP root(GetSP());
519 for (ConstString name : names) {
520 root = root->GetChildMemberWithName(name, true);
521 if (!root) {
522 if (name_of_error)
523 *name_of_error = name;
524 return root;
525 }
526 }
527 return root;
Enrico Granataef8dde62015-12-21 23:10:17 +0000528}
529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
Lang Hames088d0012017-04-26 18:15:40 +0000531 llvm::ArrayRef<std::pair<ConstString, bool>> names,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532 ConstString *name_of_error) {
533 if (names.size() == 0)
534 return GetSP();
535 ValueObjectSP root(GetSP());
536 for (std::pair<ConstString, bool> name : names) {
537 root = root->GetChildMemberWithName(name.first, name.second);
538 if (!root) {
539 if (name_of_error)
540 *name_of_error = name.first;
541 return root;
542 }
543 }
544 return root;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000545}
546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547size_t ValueObject::GetIndexOfChildWithName(const ConstString &name) {
548 bool omit_empty_base_classes = true;
549 return GetCompilerType().GetIndexOfChildWithName(name.GetCString(),
550 omit_empty_base_classes);
Enrico Granatae2e220a2013-09-12 00:48:47 +0000551}
552
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553ValueObjectSP ValueObject::GetChildMemberWithName(const ConstString &name,
554 bool can_create) {
Adrian Prantl05097242018-04-30 16:49:04 +0000555 // when getting a child by name, it could be buried inside some base classes
556 // (which really aren't part of the expression path), so we need a vector of
557 // indexes that can get us down to the correct child
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 ValueObjectSP child_sp;
559
560 // We may need to update our value if we are dynamic
561 if (IsPossibleDynamicType())
562 UpdateValueIfNeeded(false);
563
564 std::vector<uint32_t> child_indexes;
565 bool omit_empty_base_classes = true;
566 const size_t num_child_indexes =
567 GetCompilerType().GetIndexOfChildMemberWithName(
568 name.GetCString(), omit_empty_base_classes, child_indexes);
569 if (num_child_indexes > 0) {
570 std::vector<uint32_t>::const_iterator pos = child_indexes.begin();
571 std::vector<uint32_t>::const_iterator end = child_indexes.end();
572
573 child_sp = GetChildAtIndex(*pos, can_create);
574 for (++pos; pos != end; ++pos) {
575 if (child_sp) {
576 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex(*pos, can_create));
577 child_sp = new_child_sp;
578 } else {
579 child_sp.reset();
580 }
Enrico Granatae2e220a2013-09-12 00:48:47 +0000581 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000582 }
583 return child_sp;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000584}
585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586size_t ValueObject::GetNumChildren(uint32_t max) {
587 UpdateValueIfNeeded();
588
589 if (max < UINT32_MAX) {
590 if (m_children_count_valid) {
591 size_t children_count = m_children.GetChildrenCount();
592 return children_count <= max ? children_count : max;
593 } else
594 return CalculateNumChildren(max);
595 }
596
597 if (!m_children_count_valid) {
598 SetNumChildren(CalculateNumChildren());
599 }
600 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
602
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603bool ValueObject::MightHaveChildren() {
604 bool has_children = false;
605 const uint32_t type_info = GetTypeInfo();
606 if (type_info) {
607 if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
608 has_children = true;
609 } else {
610 has_children = GetNumChildren() > 0;
611 }
612 return has_children;
Greg Clayton4a792072012-10-23 01:50:10 +0000613}
614
615// Should only be called by ValueObject::GetNumChildren()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616void ValueObject::SetNumChildren(size_t num_children) {
617 m_children_count_valid = true;
618 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619}
620
Kate Stoneb9c1b512016-09-06 20:57:50 +0000621void ValueObject::SetName(const ConstString &name) { m_name = name; }
622
623ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
624 bool synthetic_array_member,
625 int32_t synthetic_index) {
626 ValueObject *valobj = NULL;
627
628 bool omit_empty_base_classes = true;
629 bool ignore_array_bounds = synthetic_array_member;
630 std::string child_name_str;
631 uint32_t child_byte_size = 0;
632 int32_t child_byte_offset = 0;
633 uint32_t child_bitfield_bit_size = 0;
634 uint32_t child_bitfield_bit_offset = 0;
635 bool child_is_base_class = false;
636 bool child_is_deref_of_parent = false;
637 uint64_t language_flags = 0;
638
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000639 const bool transparent_pointers = !synthetic_array_member;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 CompilerType child_compiler_type;
641
642 ExecutionContext exe_ctx(GetExecutionContextRef());
643
644 child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
645 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
646 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
647 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
648 child_is_deref_of_parent, this, language_flags);
649 if (child_compiler_type) {
650 if (synthetic_index)
651 child_byte_offset += child_byte_size * synthetic_index;
652
653 ConstString child_name;
654 if (!child_name_str.empty())
655 child_name.SetCString(child_name_str.c_str());
656
657 valobj = new ValueObjectChild(
658 *this, child_compiler_type, child_name, child_byte_size,
659 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
660 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
661 language_flags);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 }
663
664 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665}
666
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
668 std::string &destination,
669 lldb::LanguageType lang) {
670 return GetSummaryAsCString(summary_ptr, destination,
671 TypeSummaryOptions().SetLanguage(lang));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
675 std::string &destination,
676 const TypeSummaryOptions &options) {
677 destination.clear();
678
Adrian Prantl05097242018-04-30 16:49:04 +0000679 // ideally we would like to bail out if passing NULL, but if we do so we end
680 // up not providing the summary for function pointers anymore
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
682 return false;
683
684 m_is_getting_summary = true;
685
686 TypeSummaryOptions actual_options(options);
687
688 if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
689 actual_options.SetLanguage(GetPreferredDisplayLanguage());
690
691 // this is a hot path in code and we prefer to avoid setting this string all
Adrian Prantl05097242018-04-30 16:49:04 +0000692 // too often also clearing out other information that we might care to see in
693 // a crash log. might be useful in very specific situations though.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s.
695 Summary provider's description is %s",
696 GetTypeName().GetCString(),
697 GetName().GetCString(),
698 summary_ptr->GetDescription().c_str());*/
699
700 if (UpdateValueIfNeeded(false) && summary_ptr) {
701 if (HasSyntheticValue())
702 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on
703 // the synthetic children being
704 // up-to-date (e.g. ${svar%#})
705 summary_ptr->FormatObject(this, destination, actual_options);
706 }
707 m_is_getting_summary = false;
708 return !destination.empty();
Enrico Granatac1247f52014-11-06 21:23:20 +0000709}
710
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711const char *ValueObject::GetSummaryAsCString(lldb::LanguageType lang) {
712 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) {
713 TypeSummaryOptions summary_options;
714 summary_options.SetLanguage(lang);
715 GetSummaryAsCString(GetSummaryFormat().get(), m_summary_str,
716 summary_options);
717 }
718 if (m_summary_str.empty())
719 return NULL;
720 return m_summary_str.c_str();
721}
722
723bool ValueObject::GetSummaryAsCString(std::string &destination,
724 const TypeSummaryOptions &options) {
725 return GetSummaryAsCString(GetSummaryFormat().get(), destination, options);
726}
727
728bool ValueObject::IsCStringContainer(bool check_pointer) {
729 CompilerType pointee_or_element_compiler_type;
730 const Flags type_flags(GetTypeInfo(&pointee_or_element_compiler_type));
731 bool is_char_arr_ptr(type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
732 pointee_or_element_compiler_type.IsCharType());
733 if (!is_char_arr_ptr)
734 return false;
735 if (!check_pointer)
736 return true;
737 if (type_flags.Test(eTypeIsArray))
738 return true;
739 addr_t cstr_address = LLDB_INVALID_ADDRESS;
740 AddressType cstr_address_type = eAddressTypeInvalid;
741 cstr_address = GetAddressOf(true, &cstr_address_type);
742 return (cstr_address != LLDB_INVALID_ADDRESS);
743}
744
745size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
746 uint32_t item_count) {
747 CompilerType pointee_or_element_compiler_type;
748 const uint32_t type_info = GetTypeInfo(&pointee_or_element_compiler_type);
749 const bool is_pointer_type = type_info & eTypeIsPointer;
750 const bool is_array_type = type_info & eTypeIsArray;
751 if (!(is_pointer_type || is_array_type))
752 return 0;
753
754 if (item_count == 0)
755 return 0;
756
757 ExecutionContext exe_ctx(GetExecutionContextRef());
758
Adrian Prantld963a7c2019-01-15 18:07:52 +0000759 auto item_type_size = pointee_or_element_compiler_type.GetByteSize(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 exe_ctx.GetBestExecutionContextScope());
Adrian Prantld963a7c2019-01-15 18:07:52 +0000761 if (!item_type_size)
762 return 0;
763 const uint64_t bytes = item_count * *item_type_size;
764 const uint64_t offset = item_idx * *item_type_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765
766 if (item_idx == 0 && item_count == 1) // simply a deref
767 {
768 if (is_pointer_type) {
Zachary Turner97206d52017-05-12 04:51:55 +0000769 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 ValueObjectSP pointee_sp = Dereference(error);
771 if (error.Fail() || pointee_sp.get() == NULL)
772 return 0;
773 return pointee_sp->GetData(data, error);
774 } else {
775 ValueObjectSP child_sp = GetChildAtIndex(0, true);
776 if (child_sp.get() == NULL)
777 return 0;
Zachary Turner97206d52017-05-12 04:51:55 +0000778 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 return child_sp->GetData(data, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 return true;
782 } else /* (items > 1) */
783 {
Zachary Turner97206d52017-05-12 04:51:55 +0000784 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 lldb_private::DataBufferHeap *heap_buf_ptr = NULL;
786 lldb::DataBufferSP data_sp(heap_buf_ptr =
787 new lldb_private::DataBufferHeap());
Enrico Granata0c489f52012-03-01 04:24:26 +0000788
Kate Stoneb9c1b512016-09-06 20:57:50 +0000789 AddressType addr_type;
790 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type)
791 : GetAddressOf(true, &addr_type);
792
793 switch (addr_type) {
794 case eAddressTypeFile: {
795 ModuleSP module_sp(GetModule());
796 if (module_sp) {
797 addr = addr + offset;
798 Address so_addr;
799 module_sp->ResolveFileAddress(addr, so_addr);
800 ExecutionContext exe_ctx(GetExecutionContextRef());
801 Target *target = exe_ctx.GetTargetPtr();
802 if (target) {
803 heap_buf_ptr->SetByteSize(bytes);
804 size_t bytes_read = target->ReadMemory(
805 so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
806 if (error.Success()) {
807 data.SetData(data_sp);
808 return bytes_read;
809 }
810 }
811 }
812 } break;
813 case eAddressTypeLoad: {
814 ExecutionContext exe_ctx(GetExecutionContextRef());
815 Process *process = exe_ctx.GetProcessPtr();
816 if (process) {
817 heap_buf_ptr->SetByteSize(bytes);
818 size_t bytes_read = process->ReadMemory(
819 addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
820 if (error.Success() || bytes_read > 0) {
821 data.SetData(data_sp);
822 return bytes_read;
823 }
824 }
825 } break;
826 case eAddressTypeHost: {
Adrian Prantld963a7c2019-01-15 18:07:52 +0000827 auto max_bytes =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
Adrian Prantld963a7c2019-01-15 18:07:52 +0000829 if (max_bytes && *max_bytes > offset) {
830 size_t bytes_read = std::min<uint64_t>(*max_bytes - offset, bytes);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831 addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
832 if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
833 break;
834 heap_buf_ptr->CopyData((uint8_t *)(addr + offset), bytes_read);
835 data.SetData(data_sp);
836 return bytes_read;
837 }
838 } break;
839 case eAddressTypeInvalid:
840 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000841 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 }
843 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844}
845
Zachary Turner97206d52017-05-12 04:51:55 +0000846uint64_t ValueObject::GetData(DataExtractor &data, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 UpdateValueIfNeeded(false);
848 ExecutionContext exe_ctx(GetExecutionContextRef());
849 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
850 if (error.Fail()) {
851 if (m_data.GetByteSize()) {
852 data = m_data;
853 error.Clear();
854 return data.GetByteSize();
855 } else {
856 return 0;
857 }
858 }
859 data.SetAddressByteSize(m_data.GetAddressByteSize());
860 data.SetByteOrder(m_data.GetByteOrder());
861 return data.GetByteSize();
Enrico Granata49bfafb2014-11-18 23:36:25 +0000862}
863
Zachary Turner97206d52017-05-12 04:51:55 +0000864bool ValueObject::SetData(DataExtractor &data, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 error.Clear();
866 // Make sure our value is up to date first so that our location and location
867 // type is valid.
868 if (!UpdateValueIfNeeded(false)) {
869 error.SetErrorString("unable to read value");
870 return false;
871 }
872
873 uint64_t count = 0;
874 const Encoding encoding = GetCompilerType().GetEncoding(count);
875
876 const size_t byte_size = GetByteSize();
877
878 Value::ValueType value_type = m_value.GetValueType();
879
880 switch (value_type) {
881 case Value::eValueTypeScalar: {
Zachary Turner97206d52017-05-12 04:51:55 +0000882 Status set_error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883 m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
884
885 if (!set_error.Success()) {
886 error.SetErrorStringWithFormat("unable to set scalar value: %s",
887 set_error.AsCString());
888 return false;
889 }
890 } break;
891 case Value::eValueTypeLoadAddress: {
892 // If it is a load address, then the scalar value is the storage location
893 // of the data, and we have to shove this value down to that load location.
894 ExecutionContext exe_ctx(GetExecutionContextRef());
895 Process *process = exe_ctx.GetProcessPtr();
896 if (process) {
897 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
898 size_t bytes_written = process->WriteMemory(
899 target_addr, data.GetDataStart(), byte_size, error);
900 if (!error.Success())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000901 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000902 if (bytes_written != byte_size) {
903 error.SetErrorString("unable to write value to memory");
904 return false;
905 }
906 }
907 } break;
908 case Value::eValueTypeHostAddress: {
909 // If it is a host address, then we stuff the scalar as a DataBuffer into
910 // the Value's data.
911 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
912 m_data.SetData(buffer_sp, 0);
913 data.CopyByteOrderedData(0, byte_size,
914 const_cast<uint8_t *>(m_data.GetDataStart()),
915 byte_size, m_data.GetByteOrder());
916 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
917 } break;
918 case Value::eValueTypeFileAddress:
919 case Value::eValueTypeVector:
920 break;
921 }
922
Adrian Prantl05097242018-04-30 16:49:04 +0000923 // If we have reached this point, then we have successfully changed the
924 // value.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 SetNeedsUpdate();
926 return true;
927}
928
929static bool CopyStringDataToBufferSP(const StreamString &source,
930 lldb::DataBufferSP &destination) {
931 destination.reset(new DataBufferHeap(source.GetSize() + 1, 0));
Zachary Turnerc1564272016-11-16 21:15:24 +0000932 memcpy(destination->GetBytes(), source.GetString().data(), source.GetSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 return true;
934}
935
936std::pair<size_t, bool>
Zachary Turner97206d52017-05-12 04:51:55 +0000937ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938 uint32_t max_length, bool honor_array,
939 Format item_format) {
940 bool was_capped = false;
941 StreamString s;
942 ExecutionContext exe_ctx(GetExecutionContextRef());
943 Target *target = exe_ctx.GetTargetPtr();
944
945 if (!target) {
946 s << "<no target to read from>";
947 error.SetErrorString("no target to read from");
948 CopyStringDataToBufferSP(s, buffer_sp);
949 return {0, was_capped};
950 }
951
952 if (max_length == 0)
953 max_length = target->GetMaximumSizeOfStringSummary();
954
955 size_t bytes_read = 0;
956 size_t total_bytes_read = 0;
957
958 CompilerType compiler_type = GetCompilerType();
959 CompilerType elem_or_pointee_compiler_type;
960 const Flags type_flags(GetTypeInfo(&elem_or_pointee_compiler_type));
961 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
962 elem_or_pointee_compiler_type.IsCharType()) {
Greg Claytonafacd142011-09-02 01:15:17 +0000963 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000964 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000965
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 size_t cstr_len = 0;
967 bool capped_data = false;
968 const bool is_array = type_flags.Test(eTypeIsArray);
969 if (is_array) {
970 // We have an array
971 uint64_t array_size = 0;
972 if (compiler_type.IsArrayType(NULL, &array_size, NULL)) {
973 cstr_len = array_size;
974 if (cstr_len > max_length) {
975 capped_data = true;
976 cstr_len = max_length;
Enrico Granata9128ee22011-09-06 19:20:51 +0000977 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 }
979 cstr_address = GetAddressOf(true, &cstr_address_type);
980 } else {
981 // We have a pointer
982 cstr_address = GetPointerValue(&cstr_address_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000983 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) {
986 if (cstr_address_type == eAddressTypeHost && is_array) {
987 const char *cstr = GetDataExtractor().PeekCStr(0);
988 if (cstr == nullptr) {
989 s << "<invalid address>";
990 error.SetErrorString("invalid address");
991 CopyStringDataToBufferSP(s, buffer_sp);
992 return {0, was_capped};
Sean Callananed185ab2013-04-19 19:47:32 +0000993 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 buffer_sp.reset(new DataBufferHeap(cstr_len, 0));
995 memcpy(buffer_sp->GetBytes(), cstr, cstr_len);
996 return {cstr_len, was_capped};
997 } else {
998 s << "<invalid address>";
999 error.SetErrorString("invalid address");
Enrico Granata2206b482014-10-30 18:27:31 +00001000 CopyStringDataToBufferSP(s, buffer_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 return {0, was_capped};
1002 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001003 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004
1005 Address cstr_so_addr(cstr_address);
1006 DataExtractor data;
1007 if (cstr_len > 0 && honor_array) {
1008 // I am using GetPointeeData() here to abstract the fact that some
Adrian Prantl05097242018-04-30 16:49:04 +00001009 // ValueObjects are actually frozen pointers in the host but the pointed-
1010 // to data lives in the debuggee, and GetPointeeData() automatically
1011 // takes care of this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 GetPointeeData(data, 0, cstr_len);
1013
1014 if ((bytes_read = data.GetByteSize()) > 0) {
1015 total_bytes_read = bytes_read;
1016 for (size_t offset = 0; offset < bytes_read; offset++)
1017 s.Printf("%c", *data.PeekData(offset, 1));
1018 if (capped_data)
1019 was_capped = true;
1020 }
1021 } else {
1022 cstr_len = max_length;
1023 const size_t k_max_buf_size = 64;
1024
1025 size_t offset = 0;
1026
1027 int cstr_len_displayed = -1;
1028 bool capped_cstr = false;
1029 // I am using GetPointeeData() here to abstract the fact that some
Adrian Prantl05097242018-04-30 16:49:04 +00001030 // ValueObjects are actually frozen pointers in the host but the pointed-
1031 // to data lives in the debuggee, and GetPointeeData() automatically
1032 // takes care of this
Kate Stoneb9c1b512016-09-06 20:57:50 +00001033 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) {
1034 total_bytes_read += bytes_read;
1035 const char *cstr = data.PeekCStr(0);
1036 size_t len = strnlen(cstr, k_max_buf_size);
1037 if (cstr_len_displayed < 0)
1038 cstr_len_displayed = len;
1039
1040 if (len == 0)
1041 break;
1042 cstr_len_displayed += len;
1043 if (len > bytes_read)
1044 len = bytes_read;
1045 if (len > cstr_len)
1046 len = cstr_len;
1047
1048 for (size_t offset = 0; offset < bytes_read; offset++)
1049 s.Printf("%c", *data.PeekData(offset, 1));
1050
1051 if (len < k_max_buf_size)
1052 break;
1053
1054 if (len >= cstr_len) {
1055 capped_cstr = true;
1056 break;
1057 }
1058
1059 cstr_len -= len;
1060 offset += len;
1061 }
1062
1063 if (cstr_len_displayed >= 0) {
1064 if (capped_cstr)
1065 was_capped = true;
1066 }
1067 }
1068 } else {
1069 error.SetErrorString("not a string object");
1070 s << "<not a string object>";
1071 }
1072 CopyStringDataToBufferSP(s, buffer_sp);
1073 return {total_bytes_read, was_capped};
1074}
1075
1076std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() {
1077 if (!UpdateValueIfNeeded(true))
1078 return {TypeValidatorResult::Success,
1079 ""}; // not the validator's job to discuss update problems
1080
1081 if (m_validation_result.hasValue())
1082 return m_validation_result.getValue();
1083
1084 if (!m_type_validator_sp)
1085 return {TypeValidatorResult::Success, ""}; // no validator no failure
1086
1087 auto outcome = m_type_validator_sp->FormatObject(this);
1088
1089 return (m_validation_result = {outcome.m_result, outcome.m_message})
1090 .getValue();
1091}
1092
1093const char *ValueObject::GetObjectDescription() {
1094
1095 if (!UpdateValueIfNeeded(true))
1096 return NULL;
1097
1098 if (!m_object_desc_str.empty())
1099 return m_object_desc_str.c_str();
1100
1101 ExecutionContext exe_ctx(GetExecutionContextRef());
1102 Process *process = exe_ctx.GetProcessPtr();
1103 if (process == NULL)
1104 return NULL;
1105
1106 StreamString s;
1107
1108 LanguageType language = GetObjectRuntimeLanguage();
1109 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1110
1111 if (runtime == NULL) {
1112 // Aw, hell, if the things a pointer, or even just an integer, let's try
1113 // ObjC anyway...
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001114 CompilerType compiler_type = GetCompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001115 if (compiler_type) {
1116 bool is_signed;
1117 if (compiler_type.IsIntegerType(is_signed) ||
1118 compiler_type.IsPointerType()) {
1119 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1120 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001121 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 }
1123
1124 if (runtime && runtime->GetObjectDescription(s, *this)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001125 m_object_desc_str.append(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 }
1127
1128 if (m_object_desc_str.empty())
1129 return NULL;
1130 else
1131 return m_object_desc_str.c_str();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001132}
1133
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134bool ValueObject::GetValueAsCString(const lldb_private::TypeFormatImpl &format,
1135 std::string &destination) {
1136 if (UpdateValueIfNeeded(false))
1137 return format.FormatObject(this, destination);
1138 else
1139 return false;
Enrico Granata744794a2014-09-05 21:46:22 +00001140}
1141
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142bool ValueObject::GetValueAsCString(lldb::Format format,
1143 std::string &destination) {
1144 return GetValueAsCString(TypeFormatImpl_Format(format), destination);
1145}
Enrico Granata0a3958e2011-07-02 00:25:22 +00001146
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147const char *ValueObject::GetValueAsCString() {
1148 if (UpdateValueIfNeeded(true)) {
1149 lldb::TypeFormatImplSP format_sp;
1150 lldb::Format my_format = GetFormat();
1151 if (my_format == lldb::eFormatDefault) {
1152 if (m_type_format_sp)
1153 format_sp = m_type_format_sp;
1154 else {
1155 if (m_is_bitfield_for_scalar)
1156 my_format = eFormatUnsigned;
1157 else {
1158 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) {
1159 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1160 if (reg_info)
1161 my_format = reg_info->format;
1162 } else {
1163 my_format = GetValue().GetCompilerType().GetFormat();
1164 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001165 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001167 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 if (my_format != m_last_format || m_value_str.empty()) {
1169 m_last_format = my_format;
1170 if (!format_sp)
1171 format_sp.reset(new TypeFormatImpl_Format(my_format));
1172 if (GetValueAsCString(*format_sp.get(), m_value_str)) {
1173 if (!m_value_did_change && m_old_value_valid) {
Adrian Prantl05097242018-04-30 16:49:04 +00001174 // The value was gotten successfully, so we consider the value as
1175 // changed if the value string differs
Kate Stoneb9c1b512016-09-06 20:57:50 +00001176 SetValueDidChange(m_old_value_str != m_value_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 }
1181 if (m_value_str.empty())
1182 return NULL;
1183 return m_value_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184}
1185
Adrian Prantl05097242018-04-30 16:49:04 +00001186// if > 8bytes, 0 is returned. this method should mostly be used to read
1187// address values out of pointers
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188uint64_t ValueObject::GetValueAsUnsigned(uint64_t fail_value, bool *success) {
1189 // If our byte size is zero this is an aggregate type that has children
1190 if (CanProvideValue()) {
1191 Scalar scalar;
1192 if (ResolveValue(scalar)) {
1193 if (success)
1194 *success = true;
1195 return scalar.ULongLong(fail_value);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001196 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001197 // fallthrough, otherwise...
1198 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001199
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 if (success)
1201 *success = false;
1202 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001203}
1204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205int64_t ValueObject::GetValueAsSigned(int64_t fail_value, bool *success) {
1206 // If our byte size is zero this is an aggregate type that has children
1207 if (CanProvideValue()) {
1208 Scalar scalar;
1209 if (ResolveValue(scalar)) {
1210 if (success)
1211 *success = true;
1212 return scalar.SLongLong(fail_value);
Enrico Granatad7373f62013-10-31 18:57:50 +00001213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 // fallthrough, otherwise...
1215 }
1216
1217 if (success)
1218 *success = false;
1219 return fail_value;
Enrico Granatad7373f62013-10-31 18:57:50 +00001220}
1221
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222// if any more "special cases" are added to
Adrian Prantl05097242018-04-30 16:49:04 +00001223// ValueObject::DumpPrintableRepresentation() please keep this call up to date
1224// by returning true for your new special cases. We will eventually move to
1225// checking this call result before trying to display special cases
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226bool ValueObject::HasSpecialPrintableRepresentation(
1227 ValueObjectRepresentationStyle val_obj_display, Format custom_format) {
1228 Flags flags(GetTypeInfo());
1229 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1230 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1231 if (IsCStringContainer(true) &&
1232 (custom_format == eFormatCString || custom_format == eFormatCharArray ||
1233 custom_format == eFormatChar || custom_format == eFormatVectorOfChar))
1234 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001235
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 if (flags.Test(eTypeIsArray)) {
1237 if ((custom_format == eFormatBytes) ||
1238 (custom_format == eFormatBytesWithASCII))
1239 return true;
1240
1241 if ((custom_format == eFormatVectorOfChar) ||
1242 (custom_format == eFormatVectorOfFloat32) ||
1243 (custom_format == eFormatVectorOfFloat64) ||
1244 (custom_format == eFormatVectorOfSInt16) ||
1245 (custom_format == eFormatVectorOfSInt32) ||
1246 (custom_format == eFormatVectorOfSInt64) ||
1247 (custom_format == eFormatVectorOfSInt8) ||
1248 (custom_format == eFormatVectorOfUInt128) ||
1249 (custom_format == eFormatVectorOfUInt16) ||
1250 (custom_format == eFormatVectorOfUInt32) ||
1251 (custom_format == eFormatVectorOfUInt64) ||
1252 (custom_format == eFormatVectorOfUInt8))
1253 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 }
1256 return false;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001257}
1258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259bool ValueObject::DumpPrintableRepresentation(
1260 Stream &s, ValueObjectRepresentationStyle val_obj_display,
1261 Format custom_format, PrintableRepresentationSpecialCases special,
1262 bool do_dump_error) {
Enrico Granataf4efecd2011-07-12 22:56:10 +00001263
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264 Flags flags(GetTypeInfo());
Enrico Granata86cc9822012-03-19 22:58:49 +00001265
Enrico Granata65d86e42016-11-07 23:32:20 +00001266 bool allow_special =
1267 (special == ValueObject::PrintableRepresentationSpecialCases::eAllow);
1268 const bool only_special = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269
1270 if (allow_special) {
1271 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1272 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1273 // when being asked to get a printable display an array or pointer type
Adrian Prantl05097242018-04-30 16:49:04 +00001274 // directly, try to "do the right thing"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275
1276 if (IsCStringContainer(true) &&
1277 (custom_format == eFormatCString ||
1278 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1279 custom_format ==
1280 eFormatVectorOfChar)) // print char[] & char* directly
1281 {
Zachary Turner97206d52017-05-12 04:51:55 +00001282 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283 lldb::DataBufferSP buffer_sp;
1284 std::pair<size_t, bool> read_string = ReadPointedString(
1285 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1286 (custom_format == eFormatCharArray));
1287 lldb_private::formatters::StringPrinter::
1288 ReadBufferAndDumpToStreamOptions options(*this);
1289 options.SetData(DataExtractor(
1290 buffer_sp, lldb::eByteOrderInvalid,
1291 8)); // none of this matters for a string - pass some defaults
1292 options.SetStream(&s);
1293 options.SetPrefixToken(0);
1294 options.SetQuote('"');
1295 options.SetSourceSize(buffer_sp->GetByteSize());
1296 options.SetIsTruncated(read_string.second);
1297 formatters::StringPrinter::ReadBufferAndDumpToStream<
1298 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1299 options);
1300 return !error.Fail();
1301 }
1302
1303 if (custom_format == eFormatEnum)
Enrico Granata85933ed2011-08-18 16:38:26 +00001304 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001305
Adrian Prantl05097242018-04-30 16:49:04 +00001306 // this only works for arrays, because I have no way to know when the
1307 // pointed memory ends, and no special \0 end of data marker
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 if (flags.Test(eTypeIsArray)) {
1309 if ((custom_format == eFormatBytes) ||
1310 (custom_format == eFormatBytesWithASCII)) {
1311 const size_t count = GetNumChildren();
1312
1313 s << '[';
1314 for (size_t low = 0; low < count; low++) {
1315
1316 if (low)
1317 s << ',';
1318
1319 ValueObjectSP child = GetChildAtIndex(low, true);
1320 if (!child.get()) {
1321 s << "<invalid child>";
1322 continue;
Enrico Granata86cc9822012-03-19 22:58:49 +00001323 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 child->DumpPrintableRepresentation(
1325 s, ValueObject::eValueObjectRepresentationStyleValue,
1326 custom_format);
1327 }
1328
1329 s << ']';
1330
1331 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001332 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333
1334 if ((custom_format == eFormatVectorOfChar) ||
1335 (custom_format == eFormatVectorOfFloat32) ||
1336 (custom_format == eFormatVectorOfFloat64) ||
1337 (custom_format == eFormatVectorOfSInt16) ||
1338 (custom_format == eFormatVectorOfSInt32) ||
1339 (custom_format == eFormatVectorOfSInt64) ||
1340 (custom_format == eFormatVectorOfSInt8) ||
1341 (custom_format == eFormatVectorOfUInt128) ||
1342 (custom_format == eFormatVectorOfUInt16) ||
1343 (custom_format == eFormatVectorOfUInt32) ||
1344 (custom_format == eFormatVectorOfUInt64) ||
1345 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1346 // with ASCII or any vector
1347 // format should be printed
1348 // directly
Enrico Granata86cc9822012-03-19 22:58:49 +00001349 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350 const size_t count = GetNumChildren();
1351
1352 Format format = FormatManager::GetSingleItemFormat(custom_format);
1353
1354 s << '[';
1355 for (size_t low = 0; low < count; low++) {
1356
1357 if (low)
1358 s << ',';
1359
1360 ValueObjectSP child = GetChildAtIndex(low, true);
1361 if (!child.get()) {
1362 s << "<invalid child>";
1363 continue;
Enrico Granata0dba9b32014-01-08 01:36:59 +00001364 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 child->DumpPrintableRepresentation(
1366 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1367 }
1368
1369 s << ']';
1370
1371 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001372 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 }
1374
1375 if ((custom_format == eFormatBoolean) ||
1376 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1377 (custom_format == eFormatCharPrintable) ||
1378 (custom_format == eFormatComplexFloat) ||
1379 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1380 (custom_format == eFormatHexUppercase) ||
1381 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1382 (custom_format == eFormatOSType) ||
1383 (custom_format == eFormatUnicode16) ||
1384 (custom_format == eFormatUnicode32) ||
1385 (custom_format == eFormatUnsigned) ||
1386 (custom_format == eFormatPointer) ||
1387 (custom_format == eFormatComplexInteger) ||
1388 (custom_format == eFormatComplex) ||
1389 (custom_format == eFormatDefault)) // use the [] operator
1390 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001391 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 }
1393
1394 if (only_special)
1395 return false;
1396
1397 bool var_success = false;
1398
1399 {
Zachary Turnerc1564272016-11-16 21:15:24 +00001400 llvm::StringRef str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401
1402 // this is a local stream that we are using to ensure that the data pointed
Adrian Prantl05097242018-04-30 16:49:04 +00001403 // to by cstr survives long enough for us to copy it to its destination -
1404 // it is necessary to have this temporary storage area for cases where our
Zachary Turnerc1564272016-11-16 21:15:24 +00001405 // desired output is not backed by some other longer-term storage
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406 StreamString strm;
1407
1408 if (custom_format != eFormatInvalid)
1409 SetFormat(custom_format);
1410
1411 switch (val_obj_display) {
1412 case eValueObjectRepresentationStyleValue:
Zachary Turnerc1564272016-11-16 21:15:24 +00001413 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001414 break;
1415
1416 case eValueObjectRepresentationStyleSummary:
Zachary Turnerc1564272016-11-16 21:15:24 +00001417 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418 break;
1419
1420 case eValueObjectRepresentationStyleLanguageSpecific:
Zachary Turnerc1564272016-11-16 21:15:24 +00001421 str = GetObjectDescription();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 break;
1423
1424 case eValueObjectRepresentationStyleLocation:
Zachary Turnerc1564272016-11-16 21:15:24 +00001425 str = GetLocationAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 break;
1427
1428 case eValueObjectRepresentationStyleChildrenCount:
1429 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
Zachary Turnerc1564272016-11-16 21:15:24 +00001430 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 break;
1432
1433 case eValueObjectRepresentationStyleType:
Zachary Turnerc1564272016-11-16 21:15:24 +00001434 str = GetTypeName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 break;
1436
1437 case eValueObjectRepresentationStyleName:
Zachary Turnerc1564272016-11-16 21:15:24 +00001438 str = GetName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 break;
1440
1441 case eValueObjectRepresentationStyleExpressionPath:
1442 GetExpressionPath(strm, false);
Zachary Turnerc1564272016-11-16 21:15:24 +00001443 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 break;
1445 }
1446
Zachary Turnerc1564272016-11-16 21:15:24 +00001447 if (str.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 if (val_obj_display == eValueObjectRepresentationStyleValue)
Zachary Turnerc1564272016-11-16 21:15:24 +00001449 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1451 if (!CanProvideValue()) {
1452 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1453 GetLocationAsCString());
Zachary Turnerc1564272016-11-16 21:15:24 +00001454 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001455 } else
Zachary Turnerc1564272016-11-16 21:15:24 +00001456 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001457 }
1458 }
1459
Zachary Turnerc1564272016-11-16 21:15:24 +00001460 if (!str.empty())
1461 s << str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 else {
1463 if (m_error.Fail()) {
1464 if (do_dump_error)
1465 s.Printf("<%s>", m_error.AsCString());
1466 else
1467 return false;
1468 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1469 s.PutCString("<no summary available>");
1470 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1471 s.PutCString("<no value available>");
1472 else if (val_obj_display ==
1473 eValueObjectRepresentationStyleLanguageSpecific)
1474 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1475 // have other runtimes
1476 // that support a
1477 // description
1478 else
1479 s.PutCString("<no printable representation>");
1480 }
1481
Adrian Prantl05097242018-04-30 16:49:04 +00001482 // we should only return false here if we could not do *anything* even if
1483 // we have an error message as output, that's a success from our callers'
1484 // perspective, so return true
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 var_success = true;
1486
1487 if (custom_format != eFormatInvalid)
1488 SetFormat(eFormatDefault);
1489 }
1490
1491 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001492}
1493
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1495 AddressType *address_type) {
1496 // Can't take address of a bitfield
1497 if (IsBitfield())
Greg Clayton73b472d2010-10-27 03:32:59 +00001498 return LLDB_INVALID_ADDRESS;
Greg Clayton73b472d2010-10-27 03:32:59 +00001499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 if (!UpdateValueIfNeeded(false))
1501 return LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +00001502
Kate Stoneb9c1b512016-09-06 20:57:50 +00001503 switch (m_value.GetValueType()) {
1504 case Value::eValueTypeScalar:
1505 case Value::eValueTypeVector:
1506 if (scalar_is_load_address) {
1507 if (address_type)
1508 *address_type = eAddressTypeLoad;
1509 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001510 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001512
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 case Value::eValueTypeLoadAddress:
1514 case Value::eValueTypeFileAddress: {
Enrico Granata9128ee22011-09-06 19:20:51 +00001515 if (address_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001516 *address_type = m_value.GetValueAddressType();
1517 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1518 } break;
1519 case Value::eValueTypeHostAddress: {
1520 if (address_type)
1521 *address_type = m_value.GetValueAddressType();
1522 return LLDB_INVALID_ADDRESS;
1523 } break;
1524 }
1525 if (address_type)
1526 *address_type = eAddressTypeInvalid;
1527 return LLDB_INVALID_ADDRESS;
1528}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1531 addr_t address = LLDB_INVALID_ADDRESS;
1532 if (address_type)
1533 *address_type = eAddressTypeInvalid;
1534
1535 if (!UpdateValueIfNeeded(false))
Greg Clayton737b9322010-09-13 03:32:57 +00001536 return address;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537
1538 switch (m_value.GetValueType()) {
1539 case Value::eValueTypeScalar:
1540 case Value::eValueTypeVector:
1541 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1542 break;
1543
1544 case Value::eValueTypeHostAddress:
1545 case Value::eValueTypeLoadAddress:
1546 case Value::eValueTypeFileAddress: {
1547 lldb::offset_t data_offset = 0;
1548 address = m_data.GetPointer(&data_offset);
1549 } break;
1550 }
1551
1552 if (address_type)
1553 *address_type = GetAddressTypeOfChildren();
1554
1555 return address;
Greg Clayton737b9322010-09-13 03:32:57 +00001556}
1557
Zachary Turner97206d52017-05-12 04:51:55 +00001558bool ValueObject::SetValueFromCString(const char *value_str, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001559 error.Clear();
1560 // Make sure our value is up to date first so that our location and location
1561 // type is valid.
1562 if (!UpdateValueIfNeeded(false)) {
1563 error.SetErrorString("unable to read value");
Greg Clayton81e871e2012-02-04 02:27:34 +00001564 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 }
1566
1567 uint64_t count = 0;
1568 const Encoding encoding = GetCompilerType().GetEncoding(count);
1569
1570 const size_t byte_size = GetByteSize();
1571
1572 Value::ValueType value_type = m_value.GetValueType();
1573
1574 if (value_type == Value::eValueTypeScalar) {
1575 // If the value is already a scalar, then let the scalar change itself:
1576 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1577 } else if (byte_size <= 16) {
1578 // If the value fits in a scalar, then make a new scalar and again let the
1579 // scalar code do the conversion, then figure out where to put the new
1580 // value.
1581 Scalar new_scalar;
1582 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1583 if (error.Success()) {
1584 switch (value_type) {
1585 case Value::eValueTypeLoadAddress: {
1586 // If it is a load address, then the scalar value is the storage
Adrian Prantl05097242018-04-30 16:49:04 +00001587 // location of the data, and we have to shove this value down to that
1588 // load location.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 ExecutionContext exe_ctx(GetExecutionContextRef());
1590 Process *process = exe_ctx.GetProcessPtr();
1591 if (process) {
1592 addr_t target_addr =
1593 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1594 size_t bytes_written = process->WriteScalarToMemory(
1595 target_addr, new_scalar, byte_size, error);
1596 if (!error.Success())
1597 return false;
1598 if (bytes_written != byte_size) {
1599 error.SetErrorString("unable to write value to memory");
1600 return false;
1601 }
1602 }
1603 } break;
1604 case Value::eValueTypeHostAddress: {
1605 // If it is a host address, then we stuff the scalar as a DataBuffer
1606 // into the Value's data.
1607 DataExtractor new_data;
1608 new_data.SetByteOrder(m_data.GetByteOrder());
1609
1610 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1611 m_data.SetData(buffer_sp, 0);
1612 bool success = new_scalar.GetData(new_data);
1613 if (success) {
1614 new_data.CopyByteOrderedData(
1615 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1616 byte_size, m_data.GetByteOrder());
1617 }
1618 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1619
1620 } break;
1621 case Value::eValueTypeFileAddress:
1622 case Value::eValueTypeScalar:
1623 case Value::eValueTypeVector:
1624 break;
1625 }
1626 } else {
1627 return false;
1628 }
1629 } else {
1630 // We don't support setting things bigger than a scalar at present.
1631 error.SetErrorString("unable to write aggregate data type");
1632 return false;
1633 }
1634
Adrian Prantl05097242018-04-30 16:49:04 +00001635 // If we have reached this point, then we have successfully changed the
1636 // value.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 SetNeedsUpdate();
1638 return true;
Greg Clayton81e871e2012-02-04 02:27:34 +00001639}
1640
Kate Stoneb9c1b512016-09-06 20:57:50 +00001641bool ValueObject::GetDeclaration(Declaration &decl) {
1642 decl.Clear();
1643 return false;
Greg Clayton84db9102012-03-26 23:03:23 +00001644}
1645
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646ConstString ValueObject::GetTypeName() {
1647 return GetCompilerType().GetConstTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001648}
1649
Kate Stoneb9c1b512016-09-06 20:57:50 +00001650ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1651
1652ConstString ValueObject::GetQualifiedTypeName() {
1653 return GetCompilerType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001654}
1655
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656LanguageType ValueObject::GetObjectRuntimeLanguage() {
1657 return GetCompilerType().GetMinimumLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001658}
1659
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660void ValueObject::AddSyntheticChild(const ConstString &key,
1661 ValueObject *valobj) {
1662 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663}
1664
Kate Stoneb9c1b512016-09-06 20:57:50 +00001665ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1666 ValueObjectSP synthetic_child_sp;
1667 std::map<ConstString, ValueObject *>::const_iterator pos =
1668 m_synthetic_children.find(key);
1669 if (pos != m_synthetic_children.end())
1670 synthetic_child_sp = pos->second->GetSP();
1671 return synthetic_child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001672}
1673
Greg Clayton2452ab72013-02-08 22:02:02 +00001674uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1676 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00001677}
1678
Kate Stoneb9c1b512016-09-06 20:57:50 +00001679bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1680
1681bool ValueObject::IsArrayType() {
1682 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683}
1684
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1686
1687bool ValueObject::IsIntegerType(bool &is_signed) {
1688 return GetCompilerType().IsIntegerType(is_signed);
Greg Claytondaf515f2011-07-09 20:12:33 +00001689}
1690
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691bool ValueObject::IsPointerOrReferenceType() {
1692 return GetCompilerType().IsPointerOrReferenceType();
Enrico Granata9fc19442011-07-06 02:13:41 +00001693}
1694
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695bool ValueObject::IsPossibleDynamicType() {
1696 ExecutionContext exe_ctx(GetExecutionContextRef());
1697 Process *process = exe_ctx.GetProcessPtr();
1698 if (process)
1699 return process->IsPossibleDynamicValue(*this);
1700 else
1701 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001702}
Greg Clayton73b472d2010-10-27 03:32:59 +00001703
Kate Stoneb9c1b512016-09-06 20:57:50 +00001704bool ValueObject::IsRuntimeSupportValue() {
1705 Process *process(GetProcessSP().get());
1706 if (process) {
1707 LanguageRuntime *runtime =
1708 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1709 if (!runtime)
1710 runtime = process->GetObjCLanguageRuntime();
1711 if (runtime)
1712 return runtime->IsRuntimeSupportValue(*this);
1713 }
1714 return false;
Greg Clayton007d5be2011-05-30 00:49:24 +00001715}
1716
Kate Stoneb9c1b512016-09-06 20:57:50 +00001717bool ValueObject::IsNilReference() {
1718 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1719 return language->IsNilReference(*this);
1720 }
1721 return false;
Greg Claytondea8cb42011-06-29 22:09:02 +00001722}
1723
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724bool ValueObject::IsUninitializedReference() {
1725 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1726 return language->IsUninitializedReference(*this);
1727 }
1728 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00001729}
1730
Adrian Prantl05097242018-04-30 16:49:04 +00001731// This allows you to create an array member using and index that doesn't not
1732// fall in the normal bounds of the array. Many times structure can be defined
1733// as: struct Collection {
Greg Claytondaf515f2011-07-09 20:12:33 +00001734// uint32_t item_count;
1735// Item item_array[0];
1736// };
Adrian Prantl05097242018-04-30 16:49:04 +00001737// The size of the "item_array" is 1, but many times in practice there are more
1738// items in "item_array".
Greg Claytondaf515f2011-07-09 20:12:33 +00001739
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1741 bool can_create) {
1742 ValueObjectSP synthetic_child_sp;
1743 if (IsPointerType() || IsArrayType()) {
1744 char index_str[64];
1745 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1746 ConstString index_const_str(index_str);
Adrian Prantl05097242018-04-30 16:49:04 +00001747 // Check if we have already created a synthetic array member in this valid
1748 // object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001749 synthetic_child_sp = GetSyntheticChild(index_const_str);
1750 if (!synthetic_child_sp) {
1751 ValueObject *synthetic_child;
Adrian Prantl05097242018-04-30 16:49:04 +00001752 // We haven't made a synthetic array member for INDEX yet, so lets make
1753 // one and cache it for any future reference.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001754 synthetic_child = CreateChildAtIndex(0, true, index);
1755
1756 // Cache the value if we got one back...
1757 if (synthetic_child) {
1758 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001759 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760 synthetic_child_sp->SetName(ConstString(index_str));
1761 synthetic_child_sp->m_is_array_item_for_pointer = true;
1762 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001763 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 }
1765 return synthetic_child_sp;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001766}
1767
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1769 bool can_create) {
1770 ValueObjectSP synthetic_child_sp;
1771 if (IsScalarType()) {
1772 char index_str[64];
1773 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1774 ConstString index_const_str(index_str);
Adrian Prantl05097242018-04-30 16:49:04 +00001775 // Check if we have already created a synthetic array member in this valid
1776 // object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 synthetic_child_sp = GetSyntheticChild(index_const_str);
1778 if (!synthetic_child_sp) {
1779 uint32_t bit_field_size = to - from + 1;
1780 uint32_t bit_field_offset = from;
1781 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1782 bit_field_offset =
1783 GetByteSize() * 8 - bit_field_size - bit_field_offset;
Adrian Prantl05097242018-04-30 16:49:04 +00001784 // We haven't made a synthetic array member for INDEX yet, so lets make
1785 // one and cache it for any future reference.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 ValueObjectChild *synthetic_child = new ValueObjectChild(
1787 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1788 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1789 0);
1790
1791 // Cache the value if we got one back...
1792 if (synthetic_child) {
1793 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata32556cd2014-08-26 20:54:04 +00001794 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795 synthetic_child_sp->SetName(ConstString(index_str));
1796 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1797 }
Enrico Granata32556cd2014-08-26 20:54:04 +00001798 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 }
1800 return synthetic_child_sp;
Enrico Granata32556cd2014-08-26 20:54:04 +00001801}
1802
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1804 uint32_t offset, const CompilerType &type, bool can_create,
1805 ConstString name_const_str) {
1806
1807 ValueObjectSP synthetic_child_sp;
1808
1809 if (name_const_str.IsEmpty()) {
1810 char name_str[64];
1811 snprintf(name_str, sizeof(name_str), "@%i", offset);
1812 name_const_str.SetCString(name_str);
1813 }
1814
Adrian Prantl05097242018-04-30 16:49:04 +00001815 // Check if we have already created a synthetic array member in this valid
1816 // object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 synthetic_child_sp = GetSyntheticChild(name_const_str);
1818
1819 if (synthetic_child_sp.get())
1820 return synthetic_child_sp;
1821
1822 if (!can_create)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001823 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001824
1825 ExecutionContext exe_ctx(GetExecutionContextRef());
Adrian Prantld963a7c2019-01-15 18:07:52 +00001826 auto size = type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
1827 if (!size)
1828 return {};
1829 ValueObjectChild *synthetic_child =
1830 new ValueObjectChild(*this, type, name_const_str, *size, offset, 0, 0,
1831 false, false, eAddressTypeInvalid, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001832 if (synthetic_child) {
1833 AddSyntheticChild(name_const_str, synthetic_child);
1834 synthetic_child_sp = synthetic_child->GetSP();
1835 synthetic_child_sp->SetName(name_const_str);
1836 synthetic_child_sp->m_is_child_at_offset = true;
1837 }
1838 return synthetic_child_sp;
1839}
1840
1841ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1842 const CompilerType &type,
1843 bool can_create,
1844 ConstString name_const_str) {
1845 ValueObjectSP synthetic_child_sp;
1846
1847 if (name_const_str.IsEmpty()) {
1848 char name_str[128];
1849 snprintf(name_str, sizeof(name_str), "base%s@%i",
1850 type.GetTypeName().AsCString("<unknown>"), offset);
1851 name_const_str.SetCString(name_str);
1852 }
1853
Adrian Prantl05097242018-04-30 16:49:04 +00001854 // Check if we have already created a synthetic array member in this valid
1855 // object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001856 synthetic_child_sp = GetSyntheticChild(name_const_str);
1857
1858 if (synthetic_child_sp.get())
1859 return synthetic_child_sp;
1860
1861 if (!can_create)
Adrian Prantld963a7c2019-01-15 18:07:52 +00001862 return {};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001863
1864 const bool is_base_class = true;
1865
1866 ExecutionContext exe_ctx(GetExecutionContextRef());
Adrian Prantld963a7c2019-01-15 18:07:52 +00001867 auto size = type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
1868 if (!size)
1869 return {};
1870 ValueObjectChild *synthetic_child =
1871 new ValueObjectChild(*this, type, name_const_str, *size, offset, 0, 0,
1872 is_base_class, false, eAddressTypeInvalid, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001873 if (synthetic_child) {
1874 AddSyntheticChild(name_const_str, synthetic_child);
1875 synthetic_child_sp = synthetic_child->GetSP();
1876 synthetic_child_sp->SetName(name_const_str);
1877 }
1878 return synthetic_child_sp;
1879}
Enrico Granata32556cd2014-08-26 20:54:04 +00001880
Adrian Prantl05097242018-04-30 16:49:04 +00001881// your expression path needs to have a leading . or -> (unless it somehow
1882// "looks like" an array, in which case it has a leading [ symbol). while the [
1883// is meaningful and should be shown to the user, . and -> are just parser
1884// design, but by no means added information for the user.. strip them off
Kate Stoneb9c1b512016-09-06 20:57:50 +00001885static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1886 if (!expression || !expression[0])
Enrico Granatad55546b2011-07-22 00:16:08 +00001887 return expression;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001888 if (expression[0] == '.')
1889 return expression + 1;
1890 if (expression[0] == '-' && expression[1] == '>')
1891 return expression + 2;
1892 return expression;
Enrico Granatad55546b2011-07-22 00:16:08 +00001893}
1894
Greg Claytonafacd142011-09-02 01:15:17 +00001895ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1897 bool can_create) {
1898 ValueObjectSP synthetic_child_sp;
1899 ConstString name_const_string(expression);
Adrian Prantl05097242018-04-30 16:49:04 +00001900 // Check if we have already created a synthetic array member in this valid
1901 // object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902 synthetic_child_sp = GetSyntheticChild(name_const_string);
1903 if (!synthetic_child_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +00001904 // We haven't made a synthetic array member for expression yet, so lets
1905 // make one and cache it for any future reference.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 synthetic_child_sp = GetValueForExpressionPath(
Zachary Turnerd2daca72016-11-18 17:55:04 +00001907 expression, NULL, NULL,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001908 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1909 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1910 None));
1911
1912 // Cache the value if we got one back...
1913 if (synthetic_child_sp.get()) {
1914 // FIXME: this causes a "real" child to end up with its name changed to
1915 // the contents of expression
1916 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1917 synthetic_child_sp->SetName(
1918 ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001919 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001920 }
1921 return synthetic_child_sp;
Enrico Granatad55546b2011-07-22 00:16:08 +00001922}
1923
Kate Stoneb9c1b512016-09-06 20:57:50 +00001924void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001925 if (!use_synthetic)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001926 return;
1927
1928 TargetSP target_sp(GetTargetSP());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001929 if (target_sp && !target_sp->GetEnableSyntheticValue()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001930 m_synthetic_value = NULL;
1931 return;
1932 }
1933
1934 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1935
1936 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1937 return;
1938
1939 if (m_synthetic_children_sp.get() == NULL)
1940 return;
1941
1942 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1943 return;
1944
1945 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1946}
1947
1948void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1949 if (use_dynamic == eNoDynamicValues)
1950 return;
1951
1952 if (!m_dynamic_value && !IsDynamic()) {
1953 ExecutionContext exe_ctx(GetExecutionContextRef());
1954 Process *process = exe_ctx.GetProcessPtr();
1955 if (process && process->IsPossibleDynamicValue(*this)) {
1956 ClearDynamicTypeInformation();
1957 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
Enrico Granatac5bc4122012-03-27 02:35:13 +00001958 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001959 }
Enrico Granatad55546b2011-07-22 00:16:08 +00001960}
1961
Kate Stoneb9c1b512016-09-06 20:57:50 +00001962ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1963 if (use_dynamic == eNoDynamicValues)
1964 return ValueObjectSP();
1965
1966 if (!IsDynamic() && m_dynamic_value == NULL) {
1967 CalculateDynamicValue(use_dynamic);
1968 }
1969 if (m_dynamic_value)
1970 return m_dynamic_value->GetSP();
1971 else
1972 return ValueObjectSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001973}
1974
Kate Stoneb9c1b512016-09-06 20:57:50 +00001975ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1976
1977lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1978
1979ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001980 if (!use_synthetic)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001981 return ValueObjectSP();
1982
1983 CalculateSyntheticValue(use_synthetic);
1984
1985 if (m_synthetic_value)
1986 return m_synthetic_value->GetSP();
1987 else
1988 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001989}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001990
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991bool ValueObject::HasSyntheticValue() {
1992 UpdateFormatsIfNeeded();
Jim Ingham60dbabb2011-12-08 19:44:08 +00001993
Kate Stoneb9c1b512016-09-06 20:57:50 +00001994 if (m_synthetic_children_sp.get() == NULL)
1995 return false;
Enrico Granata886147f2012-05-08 18:47:08 +00001996
Kate Stoneb9c1b512016-09-06 20:57:50 +00001997 CalculateSyntheticValue(true);
Enrico Granata86cc9822012-03-19 22:58:49 +00001998
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001999 return m_synthetic_value != nullptr;
Greg Claytone221f822011-01-21 01:59:00 +00002000}
2001
Kate Stoneb9c1b512016-09-06 20:57:50 +00002002bool ValueObject::GetBaseClassPath(Stream &s) {
2003 if (IsBaseClass()) {
2004 bool parent_had_base_class =
2005 GetParent() && GetParent()->GetBaseClassPath(s);
2006 CompilerType compiler_type = GetCompilerType();
2007 std::string cxx_class_name;
2008 bool this_had_base_class =
2009 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2010 if (this_had_base_class) {
2011 if (parent_had_base_class)
2012 s.PutCString("::");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00002013 s.PutCString(cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002014 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 return parent_had_base_class || this_had_base_class;
2016 }
2017 return false;
Greg Claytone221f822011-01-21 01:59:00 +00002018}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002019
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020ValueObject *ValueObject::GetNonBaseClassParent() {
2021 if (GetParent()) {
2022 if (GetParent()->IsBaseClass())
2023 return GetParent()->GetNonBaseClassParent();
2024 else
2025 return GetParent();
2026 }
2027 return NULL;
2028}
Enrico Granataa3c8f042014-08-19 22:29:08 +00002029
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030bool ValueObject::IsBaseClass(uint32_t &depth) {
2031 if (!IsBaseClass()) {
2032 depth = 0;
2033 return false;
2034 }
2035 if (GetParent()) {
2036 GetParent()->IsBaseClass(depth);
2037 depth = depth + 1;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002038 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 }
2040 // TODO: a base of no parent? weird..
2041 depth = 1;
2042 return true;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002043}
2044
Kate Stoneb9c1b512016-09-06 20:57:50 +00002045void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2046 GetExpressionPathFormat epformat) {
2047 // synthetic children do not actually "exist" as part of the hierarchy, and
Adrian Prantl05097242018-04-30 16:49:04 +00002048 // sometimes they are consed up in ways that don't make sense from an
2049 // underlying language/API standpoint. So, use a special code path here to
2050 // return something that can hopefully be used in expression
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051 if (m_is_synthetic_children_generated) {
2052 UpdateValueIfNeeded();
2053
2054 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2055 if (IsPointerOrReferenceType()) {
2056 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2057 GetValueAsUnsigned(0));
Enrico Granata986fa5f2014-12-09 21:41:16 +00002058 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002059 } else {
2060 uint64_t load_addr =
2061 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2062 if (load_addr != LLDB_INVALID_ADDRESS) {
2063 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2064 load_addr);
2065 return;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002066 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002067 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002068 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002069
2070 if (CanProvideValue()) {
2071 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2072 GetValueAsCString());
2073 return;
Enrico Granata4becb372011-06-29 22:27:15 +00002074 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002075
2076 return;
2077 }
2078
2079 const bool is_deref_of_parent = IsDereferenceOfParent();
2080
2081 if (is_deref_of_parent &&
2082 epformat == eGetExpressionPathFormatDereferencePointers) {
2083 // this is the original format of GetExpressionPath() producing code like
Adrian Prantl05097242018-04-30 16:49:04 +00002084 // *(a_ptr).memberName, which is entirely fine, until you put this into
Kate Stoneb9c1b512016-09-06 20:57:50 +00002085 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
Adrian Prantl05097242018-04-30 16:49:04 +00002086 // a_ptr->memberName. the eHonorPointers mode is meant to produce strings
2087 // in this latter format
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088 s.PutCString("*(");
2089 }
2090
2091 ValueObject *parent = GetParent();
2092
2093 if (parent)
2094 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2095
Adrian Prantl05097242018-04-30 16:49:04 +00002096 // if we are a deref_of_parent just because we are synthetic array members
2097 // made up to allow ptr[%d] syntax to work in variable printing, then add our
2098 // name ([%d]) to the expression path
Kate Stoneb9c1b512016-09-06 20:57:50 +00002099 if (m_is_array_item_for_pointer &&
2100 epformat == eGetExpressionPathFormatHonorPointers)
2101 s.PutCString(m_name.AsCString());
2102
2103 if (!IsBaseClass()) {
2104 if (!is_deref_of_parent) {
2105 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2106 if (non_base_class_parent &&
2107 !non_base_class_parent->GetName().IsEmpty()) {
2108 CompilerType non_base_class_parent_compiler_type =
2109 non_base_class_parent->GetCompilerType();
2110 if (non_base_class_parent_compiler_type) {
2111 if (parent && parent->IsDereferenceOfParent() &&
2112 epformat == eGetExpressionPathFormatHonorPointers) {
2113 s.PutCString("->");
2114 } else {
2115 const uint32_t non_base_class_parent_type_info =
2116 non_base_class_parent_compiler_type.GetTypeInfo();
2117
2118 if (non_base_class_parent_type_info & eTypeIsPointer) {
2119 s.PutCString("->");
2120 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2121 !(non_base_class_parent_type_info & eTypeIsArray)) {
2122 s.PutChar('.');
2123 }
2124 }
2125 }
2126 }
2127
2128 const char *name = GetName().GetCString();
2129 if (name) {
2130 if (qualify_cxx_base_classes) {
2131 if (GetBaseClassPath(s))
2132 s.PutCString("::");
2133 }
2134 s.PutCString(name);
2135 }
2136 }
2137 }
2138
2139 if (is_deref_of_parent &&
2140 epformat == eGetExpressionPathFormatDereferencePointers) {
2141 s.PutChar(')');
2142 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002143}
2144
Kate Stoneb9c1b512016-09-06 20:57:50 +00002145ValueObjectSP ValueObject::GetValueForExpressionPath(
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002146 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002147 ExpressionPathEndResultType *final_value_type,
2148 const GetValueForExpressionPathOptions &options,
2149 ExpressionPathAftermath *final_task_on_target) {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002150
Kate Stoneb9c1b512016-09-06 20:57:50 +00002151 ExpressionPathScanEndReason dummy_reason_to_stop =
2152 ValueObject::eExpressionPathScanEndReasonUnknown;
2153 ExpressionPathEndResultType dummy_final_value_type =
2154 ValueObject::eExpressionPathEndResultTypeInvalid;
2155 ExpressionPathAftermath dummy_final_task_on_target =
2156 ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002157
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
Zachary Turnerd2daca72016-11-18 17:55:04 +00002159 expression, reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002160 final_value_type ? final_value_type : &dummy_final_value_type, options,
2161 final_task_on_target ? final_task_on_target
2162 : &dummy_final_task_on_target);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002163
Kate Stoneb9c1b512016-09-06 20:57:50 +00002164 if (!final_task_on_target ||
2165 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2166 return ret_val;
2167
2168 if (ret_val.get() &&
2169 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2170 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2171 // of plain objects
2172 {
2173 if ((final_task_on_target ? *final_task_on_target
2174 : dummy_final_task_on_target) ==
2175 ValueObject::eExpressionPathAftermathDereference) {
Zachary Turner97206d52017-05-12 04:51:55 +00002176 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 ValueObjectSP final_value = ret_val->Dereference(error);
2178 if (error.Fail() || !final_value.get()) {
2179 if (reason_to_stop)
2180 *reason_to_stop =
2181 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2182 if (final_value_type)
2183 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002184 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002185 } else {
2186 if (final_task_on_target)
2187 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2188 return final_value;
2189 }
2190 }
2191 if (*final_task_on_target ==
2192 ValueObject::eExpressionPathAftermathTakeAddress) {
Zachary Turner97206d52017-05-12 04:51:55 +00002193 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002194 ValueObjectSP final_value = ret_val->AddressOf(error);
2195 if (error.Fail() || !final_value.get()) {
2196 if (reason_to_stop)
2197 *reason_to_stop =
2198 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2199 if (final_value_type)
2200 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2201 return ValueObjectSP();
2202 } else {
2203 if (final_task_on_target)
2204 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2205 return final_value;
2206 }
2207 }
2208 }
2209 return ret_val; // final_task_on_target will still have its original value, so
2210 // you know I did not do it
2211}
2212
Kate Stoneb9c1b512016-09-06 20:57:50 +00002213ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002214 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002215 ExpressionPathEndResultType *final_result,
2216 const GetValueForExpressionPathOptions &options,
2217 ExpressionPathAftermath *what_next) {
2218 ValueObjectSP root = GetSP();
2219
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002220 if (!root)
2221 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002222
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002223 llvm::StringRef remainder = expression;
Zachary Turner655c4522016-11-18 06:34:45 +00002224
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225 while (true) {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002226 llvm::StringRef temp_expression = remainder;
Zachary Turner655c4522016-11-18 06:34:45 +00002227
Kate Stoneb9c1b512016-09-06 20:57:50 +00002228 CompilerType root_compiler_type = root->GetCompilerType();
2229 CompilerType pointee_compiler_type;
2230 Flags pointee_compiler_type_info;
2231
2232 Flags root_compiler_type_info(
2233 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2234 if (pointee_compiler_type)
2235 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2236
Zachary Turnerd2daca72016-11-18 17:55:04 +00002237 if (temp_expression.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002238 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2239 return root;
2240 }
2241
Zachary Turnerd2daca72016-11-18 17:55:04 +00002242 switch (temp_expression.front()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 case '-': {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002244 temp_expression = temp_expression.drop_front();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002245 if (options.m_check_dot_vs_arrow_syntax &&
2246 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2247 // use -> on a
2248 // non-pointer and I
2249 // must catch the error
2250 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002251 *reason_to_stop =
2252 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2253 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2254 return ValueObjectSP();
2255 }
2256 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2257 // extract an ObjC IVar
2258 // when this is forbidden
2259 root_compiler_type_info.Test(eTypeIsPointer) &&
2260 options.m_no_fragile_ivar) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002261 *reason_to_stop =
2262 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2263 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2264 return ValueObjectSP();
2265 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002266 if (!temp_expression.startswith(">")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002267 *reason_to_stop =
2268 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2269 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2270 return ValueObjectSP();
2271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272 }
2273 LLVM_FALLTHROUGH;
2274 case '.': // or fallthrough from ->
2275 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002276 if (options.m_check_dot_vs_arrow_syntax &&
2277 temp_expression.front() == '.' &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00002278 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2279 // use . on a pointer
2280 // and I must catch the
2281 // error
2282 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002283 *reason_to_stop =
2284 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2285 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002286 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002287 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002288 temp_expression = temp_expression.drop_front(); // skip . or >
2289
2290 size_t next_sep_pos = temp_expression.find_first_of("-.[", 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002291 ConstString child_name;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002292 if (next_sep_pos == llvm::StringRef::npos) // if no other separator just
2293 // expand this last layer
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002295 child_name.SetString(temp_expression);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002296 ValueObjectSP child_valobj_sp =
2297 root->GetChildMemberWithName(child_name, true);
2298
2299 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002300 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002301 *reason_to_stop =
2302 ValueObject::eExpressionPathScanEndReasonEndOfString;
2303 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2304 return child_valobj_sp;
2305 } else {
2306 switch (options.m_synthetic_children_traversal) {
2307 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2308 None:
2309 break;
2310 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2311 FromSynthetic:
2312 if (root->IsSynthetic()) {
2313 child_valobj_sp = root->GetNonSyntheticValue();
2314 if (child_valobj_sp.get())
2315 child_valobj_sp =
2316 child_valobj_sp->GetChildMemberWithName(child_name, true);
2317 }
2318 break;
2319 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2320 ToSynthetic:
2321 if (!root->IsSynthetic()) {
2322 child_valobj_sp = root->GetSyntheticValue();
2323 if (child_valobj_sp.get())
2324 child_valobj_sp =
2325 child_valobj_sp->GetChildMemberWithName(child_name, true);
2326 }
2327 break;
2328 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2329 Both:
2330 if (root->IsSynthetic()) {
2331 child_valobj_sp = root->GetNonSyntheticValue();
2332 if (child_valobj_sp.get())
2333 child_valobj_sp =
2334 child_valobj_sp->GetChildMemberWithName(child_name, true);
2335 } else {
2336 child_valobj_sp = root->GetSyntheticValue();
2337 if (child_valobj_sp.get())
2338 child_valobj_sp =
2339 child_valobj_sp->GetChildMemberWithName(child_name, true);
2340 }
2341 break;
2342 }
2343 }
2344
2345 // if we are here and options.m_no_synthetic_children is true,
Adrian Prantl05097242018-04-30 16:49:04 +00002346 // child_valobj_sp is going to be a NULL SP, so we hit the "else"
2347 // branch, and return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002348 if (child_valobj_sp.get()) // if it worked, just return
2349 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002350 *reason_to_stop =
2351 ValueObject::eExpressionPathScanEndReasonEndOfString;
2352 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2353 return child_valobj_sp;
2354 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002355 *reason_to_stop =
2356 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2357 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002358 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002359 }
2360 } else // other layers do expand
2361 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002362 llvm::StringRef next_separator = temp_expression.substr(next_sep_pos);
2363
2364 child_name.SetString(temp_expression.slice(0, next_sep_pos));
2365
Kate Stoneb9c1b512016-09-06 20:57:50 +00002366 ValueObjectSP child_valobj_sp =
2367 root->GetChildMemberWithName(child_name, true);
2368 if (child_valobj_sp.get()) // store the new root and move on
2369 {
2370 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002371 remainder = next_separator;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002372 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2373 continue;
2374 } else {
2375 switch (options.m_synthetic_children_traversal) {
2376 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2377 None:
2378 break;
2379 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2380 FromSynthetic:
2381 if (root->IsSynthetic()) {
2382 child_valobj_sp = root->GetNonSyntheticValue();
2383 if (child_valobj_sp.get())
2384 child_valobj_sp =
2385 child_valobj_sp->GetChildMemberWithName(child_name, true);
2386 }
2387 break;
2388 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2389 ToSynthetic:
2390 if (!root->IsSynthetic()) {
2391 child_valobj_sp = root->GetSyntheticValue();
2392 if (child_valobj_sp.get())
2393 child_valobj_sp =
2394 child_valobj_sp->GetChildMemberWithName(child_name, true);
2395 }
2396 break;
2397 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2398 Both:
2399 if (root->IsSynthetic()) {
2400 child_valobj_sp = root->GetNonSyntheticValue();
2401 if (child_valobj_sp.get())
2402 child_valobj_sp =
2403 child_valobj_sp->GetChildMemberWithName(child_name, true);
2404 } else {
2405 child_valobj_sp = root->GetSyntheticValue();
2406 if (child_valobj_sp.get())
2407 child_valobj_sp =
2408 child_valobj_sp->GetChildMemberWithName(child_name, true);
2409 }
2410 break;
2411 }
2412 }
2413
2414 // if we are here and options.m_no_synthetic_children is true,
Adrian Prantl05097242018-04-30 16:49:04 +00002415 // child_valobj_sp is going to be a NULL SP, so we hit the "else"
2416 // branch, and return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 if (child_valobj_sp.get()) // if it worked, move on
2418 {
2419 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002420 remainder = next_separator;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002421 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2422 continue;
2423 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 *reason_to_stop =
2425 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2426 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002427 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002428 }
2429 }
2430 break;
2431 }
2432 case '[': {
2433 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2434 !root_compiler_type_info.Test(eTypeIsPointer) &&
2435 !root_compiler_type_info.Test(
2436 eTypeIsVector)) // if this is not a T[] nor a T*
2437 {
2438 if (!root_compiler_type_info.Test(
2439 eTypeIsScalar)) // if this is not even a scalar...
2440 {
2441 if (options.m_synthetic_children_traversal ==
2442 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2443 None) // ...only chance left is synthetic
2444 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445 *reason_to_stop =
2446 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2447 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2448 return ValueObjectSP();
2449 }
2450 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2451 // check that we can
2452 // expand bitfields
2453 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002454 *reason_to_stop =
2455 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2456 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2457 return ValueObjectSP();
2458 }
2459 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002460 if (temp_expression[1] ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461 ']') // if this is an unbounded range it only works for arrays
2462 {
2463 if (!root_compiler_type_info.Test(eTypeIsArray)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002464 *reason_to_stop =
2465 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2466 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002467 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002468 } else // even if something follows, we cannot expand unbounded ranges,
2469 // just let the caller do it
2470 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002471 *reason_to_stop =
2472 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2473 *final_result =
2474 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2475 return root;
2476 }
2477 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002478
2479 size_t close_bracket_position = temp_expression.find(']', 1);
2480 if (close_bracket_position ==
2481 llvm::StringRef::npos) // if there is no ], this is a syntax error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002482 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483 *reason_to_stop =
2484 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2485 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002486 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002487 }
Zachary Turnerc2d55582016-11-18 03:51:19 +00002488
Zachary Turnerd2daca72016-11-18 17:55:04 +00002489 llvm::StringRef bracket_expr =
2490 temp_expression.slice(1, close_bracket_position);
2491
2492 // If this was an empty expression it would have been caught by the if
2493 // above.
2494 assert(!bracket_expr.empty());
2495
2496 if (!bracket_expr.contains('-')) {
Adrian Prantl05097242018-04-30 16:49:04 +00002497 // if no separator, this is of the form [N]. Note that this cannot be
2498 // an unbounded range of the form [], because that case was handled
Zachary Turnerc2d55582016-11-18 03:51:19 +00002499 // above with an unconditional return.
Zachary Turnerd2daca72016-11-18 17:55:04 +00002500 unsigned long index = 0;
2501 if (bracket_expr.getAsInteger(0, index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002502 *reason_to_stop =
2503 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2504 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002505 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002506 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002507
Kate Stoneb9c1b512016-09-06 20:57:50 +00002508 // from here on we do have a valid index
2509 if (root_compiler_type_info.Test(eTypeIsArray)) {
2510 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2511 if (!child_valobj_sp)
2512 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2513 if (!child_valobj_sp)
2514 if (root->HasSyntheticValue() &&
2515 root->GetSyntheticValue()->GetNumChildren() > index)
2516 child_valobj_sp =
2517 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2518 if (child_valobj_sp) {
2519 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002520 remainder =
2521 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002522 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2523 continue;
2524 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002525 *reason_to_stop =
2526 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2527 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002528 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002529 }
2530 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2531 if (*what_next ==
2532 ValueObject::
2533 eExpressionPathAftermathDereference && // if this is a
2534 // ptr-to-scalar, I
2535 // am accessing it
2536 // by index and I
2537 // would have
2538 // deref'ed anyway,
2539 // then do it now
2540 // and use this as
2541 // a bitfield
2542 pointee_compiler_type_info.Test(eTypeIsScalar)) {
Zachary Turner97206d52017-05-12 04:51:55 +00002543 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 root = root->Dereference(error);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002545 if (error.Fail() || !root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002546 *reason_to_stop =
2547 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2548 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002549 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002550 } else {
2551 *what_next = eExpressionPathAftermathNothing;
2552 continue;
2553 }
2554 } else {
2555 if (root->GetCompilerType().GetMinimumLanguage() ==
2556 eLanguageTypeObjC &&
2557 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2558 root->HasSyntheticValue() &&
2559 (options.m_synthetic_children_traversal ==
2560 GetValueForExpressionPathOptions::
2561 SyntheticChildrenTraversal::ToSynthetic ||
2562 options.m_synthetic_children_traversal ==
2563 GetValueForExpressionPathOptions::
2564 SyntheticChildrenTraversal::Both)) {
2565 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2566 } else
2567 root = root->GetSyntheticArrayMember(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002568 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002569 *reason_to_stop =
2570 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2571 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002572 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002573 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002574 remainder =
2575 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002576 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2577 continue;
2578 }
2579 }
2580 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2581 root = root->GetSyntheticBitFieldChild(index, index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002582 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002583 *reason_to_stop =
2584 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2585 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002586 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002587 } else // we do not know how to expand members of bitfields, so we
2588 // just return and let the caller do any further processing
2589 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 *reason_to_stop = ValueObject::
2591 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2592 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2593 return root;
2594 }
2595 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2596 root = root->GetChildAtIndex(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002597 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002598 *reason_to_stop =
2599 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2600 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2601 return ValueObjectSP();
2602 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002603 remainder =
2604 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002605 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2606 continue;
2607 }
2608 } else if (options.m_synthetic_children_traversal ==
2609 GetValueForExpressionPathOptions::
2610 SyntheticChildrenTraversal::ToSynthetic ||
2611 options.m_synthetic_children_traversal ==
2612 GetValueForExpressionPathOptions::
2613 SyntheticChildrenTraversal::Both) {
2614 if (root->HasSyntheticValue())
2615 root = root->GetSyntheticValue();
2616 else if (!root->IsSynthetic()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002617 *reason_to_stop =
2618 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2619 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002620 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002621 }
Adrian Prantl05097242018-04-30 16:49:04 +00002622 // if we are here, then root itself is a synthetic VO.. should be
2623 // good to go
Kate Stoneb9c1b512016-09-06 20:57:50 +00002624
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002625 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 *reason_to_stop =
2627 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2628 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002629 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002630 }
2631 root = root->GetChildAtIndex(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002632 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002633 *reason_to_stop =
2634 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2635 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002636 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002637 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002638 remainder =
2639 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002640 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2641 continue;
2642 }
2643 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002644 *reason_to_stop =
2645 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2646 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002647 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002648 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002649 } else {
2650 // we have a low and a high index
2651 llvm::StringRef sleft, sright;
2652 unsigned long low_index, high_index;
2653 std::tie(sleft, sright) = bracket_expr.split('-');
2654 if (sleft.getAsInteger(0, low_index) ||
2655 sright.getAsInteger(0, high_index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002656 *reason_to_stop =
2657 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2658 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002659 return nullptr;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002660 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002661
2662 if (low_index > high_index) // swap indices if required
2663 std::swap(low_index, high_index);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002664
Kate Stoneb9c1b512016-09-06 20:57:50 +00002665 if (root_compiler_type_info.Test(
2666 eTypeIsScalar)) // expansion only works for scalars
2667 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002668 root = root->GetSyntheticBitFieldChild(low_index, high_index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002669 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002670 *reason_to_stop =
2671 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2672 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002673 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002674 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002675 *reason_to_stop = ValueObject::
2676 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2677 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2678 return root;
2679 }
2680 } else if (root_compiler_type_info.Test(
2681 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2682 // accessing it by index and I would
2683 // have deref'ed anyway, then do it
2684 // now and use this as a bitfield
2685 *what_next ==
2686 ValueObject::eExpressionPathAftermathDereference &&
2687 pointee_compiler_type_info.Test(eTypeIsScalar)) {
Zachary Turner97206d52017-05-12 04:51:55 +00002688 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002689 root = root->Dereference(error);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002690 if (error.Fail() || !root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002691 *reason_to_stop =
2692 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2693 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002694 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002695 } else {
2696 *what_next = ValueObject::eExpressionPathAftermathNothing;
2697 continue;
2698 }
2699 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700 *reason_to_stop =
2701 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2702 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2703 return root;
2704 }
2705 }
2706 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002707 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002708 default: // some non-separator is in the way
2709 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002710 *reason_to_stop =
2711 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2712 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002713 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002714 }
2715 }
2716 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002717}
2718
Kate Stoneb9c1b512016-09-06 20:57:50 +00002719void ValueObject::LogValueObject(Log *log) {
2720 if (log)
2721 return LogValueObject(log, DumpValueObjectOptions(*this));
Enrico Granata538a88a2014-10-09 18:24:30 +00002722}
2723
Kate Stoneb9c1b512016-09-06 20:57:50 +00002724void ValueObject::LogValueObject(Log *log,
2725 const DumpValueObjectOptions &options) {
2726 if (log) {
2727 StreamString s;
2728 Dump(s, options);
2729 if (s.GetSize())
2730 log->PutCString(s.GetData());
2731 }
Greg Clayton759e7442014-07-19 00:12:57 +00002732}
2733
Kate Stoneb9c1b512016-09-06 20:57:50 +00002734void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
Chaoren Lind7bdc272015-07-31 00:35:40 +00002735
Kate Stoneb9c1b512016-09-06 20:57:50 +00002736void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
2737 ValueObjectPrinter printer(this, &s, options);
2738 printer.PrintValueObject();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002739}
2740
Kate Stoneb9c1b512016-09-06 20:57:50 +00002741ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
2742 ValueObjectSP valobj_sp;
2743
2744 if (UpdateValueIfNeeded(false) && m_error.Success()) {
2745 ExecutionContext exe_ctx(GetExecutionContextRef());
2746
2747 DataExtractor data;
2748 data.SetByteOrder(m_data.GetByteOrder());
2749 data.SetAddressByteSize(m_data.GetAddressByteSize());
2750
2751 if (IsBitfield()) {
2752 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
2753 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2754 } else
2755 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2756
2757 valobj_sp = ValueObjectConstResult::Create(
2758 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
2759 GetAddressOf());
2760 }
2761
2762 if (!valobj_sp) {
2763 ExecutionContext exe_ctx(GetExecutionContextRef());
2764 valobj_sp = ValueObjectConstResult::Create(
2765 exe_ctx.GetBestExecutionContextScope(), m_error);
2766 }
2767 return valobj_sp;
2768}
2769
2770ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
2771 lldb::DynamicValueType dynValue, bool synthValue) {
2772 ValueObjectSP result_sp(GetSP());
2773
2774 switch (dynValue) {
2775 case lldb::eDynamicCanRunTarget:
2776 case lldb::eDynamicDontRunTarget: {
2777 if (!result_sp->IsDynamic()) {
2778 if (result_sp->GetDynamicValue(dynValue))
2779 result_sp = result_sp->GetDynamicValue(dynValue);
2780 }
2781 } break;
2782 case lldb::eNoDynamicValues: {
2783 if (result_sp->IsDynamic()) {
2784 if (result_sp->GetStaticValue())
2785 result_sp = result_sp->GetStaticValue();
2786 }
2787 } break;
2788 }
2789
2790 if (synthValue) {
2791 if (!result_sp->IsSynthetic()) {
2792 if (result_sp->GetSyntheticValue())
2793 result_sp = result_sp->GetSyntheticValue();
2794 }
2795 } else {
2796 if (result_sp->IsSynthetic()) {
2797 if (result_sp->GetNonSyntheticValue())
2798 result_sp = result_sp->GetNonSyntheticValue();
2799 }
2800 }
2801
2802 return result_sp;
2803}
2804
Zachary Turner97206d52017-05-12 04:51:55 +00002805ValueObjectSP ValueObject::Dereference(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 if (m_deref_valobj)
2807 return m_deref_valobj->GetSP();
2808
2809 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
2810 if (is_pointer_or_reference_type) {
2811 bool omit_empty_base_classes = true;
2812 bool ignore_array_bounds = false;
2813
2814 std::string child_name_str;
2815 uint32_t child_byte_size = 0;
2816 int32_t child_byte_offset = 0;
2817 uint32_t child_bitfield_bit_size = 0;
2818 uint32_t child_bitfield_bit_offset = 0;
2819 bool child_is_base_class = false;
2820 bool child_is_deref_of_parent = false;
2821 const bool transparent_pointers = false;
2822 CompilerType compiler_type = GetCompilerType();
2823 CompilerType child_compiler_type;
2824 uint64_t language_flags;
2825
2826 ExecutionContext exe_ctx(GetExecutionContextRef());
2827
2828 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
2829 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
2830 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
2831 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
2832 child_is_deref_of_parent, this, language_flags);
2833 if (child_compiler_type && child_byte_size) {
2834 ConstString child_name;
2835 if (!child_name_str.empty())
2836 child_name.SetCString(child_name_str.c_str());
2837
2838 m_deref_valobj = new ValueObjectChild(
2839 *this, child_compiler_type, child_name, child_byte_size,
2840 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
2841 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
2842 language_flags);
2843 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +00002844 } else if (HasSyntheticValue()) {
2845 m_deref_valobj =
2846 GetSyntheticValue()
2847 ->GetChildMemberWithName(ConstString("$$dereference$$"), true)
2848 .get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002849 }
2850
2851 if (m_deref_valobj) {
Greg Clayton54979cd2010-12-15 05:08:08 +00002852 error.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002853 return m_deref_valobj->GetSP();
2854 } else {
2855 StreamString strm;
2856 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002857
Kate Stoneb9c1b512016-09-06 20:57:50 +00002858 if (is_pointer_or_reference_type)
2859 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
2860 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00002861 strm.GetData());
Sean Callananed185ab2013-04-19 19:47:32 +00002862 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002863 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
2864 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00002865 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002866 return ValueObjectSP();
2867 }
2868}
2869
Zachary Turner97206d52017-05-12 04:51:55 +00002870ValueObjectSP ValueObject::AddressOf(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002871 if (m_addr_of_valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00002872 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002873
Kate Stoneb9c1b512016-09-06 20:57:50 +00002874 AddressType address_type = eAddressTypeInvalid;
2875 const bool scalar_is_load_address = false;
2876 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
2877 error.Clear();
2878 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
2879 switch (address_type) {
2880 case eAddressTypeInvalid: {
2881 StreamString expr_path_strm;
2882 GetExpressionPath(expr_path_strm, true);
2883 error.SetErrorStringWithFormat("'%s' is not in memory",
Zachary Turnerc1564272016-11-16 21:15:24 +00002884 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002885 } break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002886
Kate Stoneb9c1b512016-09-06 20:57:50 +00002887 case eAddressTypeFile:
2888 case eAddressTypeLoad: {
2889 CompilerType compiler_type = GetCompilerType();
2890 if (compiler_type) {
2891 std::string name(1, '&');
2892 name.append(m_name.AsCString(""));
2893 ExecutionContext exe_ctx(GetExecutionContextRef());
2894 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
2895 exe_ctx.GetBestExecutionContextScope(),
2896 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
2897 eAddressTypeInvalid, m_data.GetAddressByteSize());
2898 }
2899 } break;
2900 default:
2901 break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002902 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 } else {
2904 StreamString expr_path_strm;
2905 GetExpressionPath(expr_path_strm, true);
2906 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
Zachary Turnerc1564272016-11-16 21:15:24 +00002907 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002908 }
2909
2910 return m_addr_of_valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002911}
2912
Kate Stoneb9c1b512016-09-06 20:57:50 +00002913ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
2914 return ValueObjectCast::Create(*this, GetName(), compiler_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00002915}
2916
Tamas Berghammer4fbb55b2017-03-31 20:48:00 +00002917lldb::ValueObjectSP ValueObject::Clone(const ConstString &new_name) {
2918 return ValueObjectCast::Create(*this, new_name, GetCompilerType());
2919}
2920
Kate Stoneb9c1b512016-09-06 20:57:50 +00002921ValueObjectSP ValueObject::CastPointerType(const char *name,
2922 CompilerType &compiler_type) {
2923 ValueObjectSP valobj_sp;
2924 AddressType address_type;
2925 addr_t ptr_value = GetPointerValue(&address_type);
2926
2927 if (ptr_value != LLDB_INVALID_ADDRESS) {
2928 Address ptr_addr(ptr_value);
2929 ExecutionContext exe_ctx(GetExecutionContextRef());
2930 valobj_sp = ValueObjectMemory::Create(
2931 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
2932 }
2933 return valobj_sp;
Jim Ingham6035b672011-03-31 00:19:25 +00002934}
2935
Kate Stoneb9c1b512016-09-06 20:57:50 +00002936ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
2937 ValueObjectSP valobj_sp;
2938 AddressType address_type;
2939 addr_t ptr_value = GetPointerValue(&address_type);
2940
2941 if (ptr_value != LLDB_INVALID_ADDRESS) {
2942 Address ptr_addr(ptr_value);
2943 ExecutionContext exe_ctx(GetExecutionContextRef());
2944 valobj_sp = ValueObjectMemory::Create(
2945 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
2946 }
2947 return valobj_sp;
2948}
2949
2950ValueObject::EvaluationPoint::EvaluationPoint()
2951 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
2952
2953ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
2954 bool use_selected)
2955 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
2956 ExecutionContext exe_ctx(exe_scope);
2957 TargetSP target_sp(exe_ctx.GetTargetSP());
2958 if (target_sp) {
2959 m_exe_ctx_ref.SetTargetSP(target_sp);
2960 ProcessSP process_sp(exe_ctx.GetProcessSP());
2961 if (!process_sp)
2962 process_sp = target_sp->GetProcessSP();
2963
2964 if (process_sp) {
2965 m_mod_id = process_sp->GetModID();
2966 m_exe_ctx_ref.SetProcessSP(process_sp);
2967
2968 ThreadSP thread_sp(exe_ctx.GetThreadSP());
2969
2970 if (!thread_sp) {
2971 if (use_selected)
2972 thread_sp = process_sp->GetThreadList().GetSelectedThread();
2973 }
2974
2975 if (thread_sp) {
2976 m_exe_ctx_ref.SetThreadSP(thread_sp);
2977
2978 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
2979 if (!frame_sp) {
2980 if (use_selected)
2981 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00002982 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002983 if (frame_sp)
2984 m_exe_ctx_ref.SetFrameSP(frame_sp);
2985 }
Jim Ingham6035b672011-03-31 00:19:25 +00002986 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002987 }
Jim Ingham6035b672011-03-31 00:19:25 +00002988}
2989
Kate Stoneb9c1b512016-09-06 20:57:50 +00002990ValueObject::EvaluationPoint::EvaluationPoint(
2991 const ValueObject::EvaluationPoint &rhs)
2992 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
Jim Ingham6035b672011-03-31 00:19:25 +00002993
Kate Stoneb9c1b512016-09-06 20:57:50 +00002994ValueObject::EvaluationPoint::~EvaluationPoint() {}
Jim Ingham6035b672011-03-31 00:19:25 +00002995
Kate Stoneb9c1b512016-09-06 20:57:50 +00002996// This function checks the EvaluationPoint against the current process state.
Adrian Prantl05097242018-04-30 16:49:04 +00002997// If the current state matches the evaluation point, or the evaluation point
2998// is already invalid, then we return false, meaning "no change". If the
2999// current state is different, we update our state, and return true meaning
3000// "yes, change". If we did see a change, we also set m_needs_update to true,
3001// so future calls to NeedsUpdate will return true. exe_scope will be set to
3002// the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003003
Kate Stoneb9c1b512016-09-06 20:57:50 +00003004bool ValueObject::EvaluationPoint::SyncWithProcessState(
3005 bool accept_invalid_exe_ctx) {
Adrian Prantl05097242018-04-30 16:49:04 +00003006 // Start with the target, if it is NULL, then we're obviously not going to
3007 // get any further:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003008 const bool thread_and_frame_only_if_stopped = true;
3009 ExecutionContext exe_ctx(
3010 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3011
3012 if (exe_ctx.GetTargetPtr() == NULL)
3013 return false;
3014
3015 // If we don't have a process nothing can change.
3016 Process *process = exe_ctx.GetProcessPtr();
3017 if (process == NULL)
3018 return false;
3019
3020 // If our stop id is the current stop ID, nothing has changed:
3021 ProcessModID current_mod_id = process->GetModID();
3022
3023 // If the current stop id is 0, either we haven't run yet, or the process
Adrian Prantl05097242018-04-30 16:49:04 +00003024 // state has been cleared. In either case, we aren't going to be able to sync
3025 // with the process state.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003026 if (current_mod_id.GetStopID() == 0)
3027 return false;
3028
3029 bool changed = false;
3030 const bool was_valid = m_mod_id.IsValid();
3031 if (was_valid) {
3032 if (m_mod_id == current_mod_id) {
Adrian Prantl05097242018-04-30 16:49:04 +00003033 // Everything is already up to date in this object, no need to update the
3034 // execution context scope.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003035 changed = false;
3036 } else {
3037 m_mod_id = current_mod_id;
3038 m_needs_update = true;
3039 changed = true;
3040 }
3041 }
3042
3043 // Now re-look up the thread and frame in case the underlying objects have
Adrian Prantl05097242018-04-30 16:49:04 +00003044 // gone away & been recreated. That way we'll be sure to return a valid
3045 // exe_scope. If we used to have a thread or a frame but can't find it
3046 // anymore, then mark ourselves as invalid.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047
3048 if (!accept_invalid_exe_ctx) {
3049 if (m_exe_ctx_ref.HasThreadRef()) {
3050 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3051 if (thread_sp) {
3052 if (m_exe_ctx_ref.HasFrameRef()) {
3053 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3054 if (!frame_sp) {
3055 // We used to have a frame, but now it is gone
3056 SetInvalid();
3057 changed = was_valid;
3058 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003060 } else {
3061 // We used to have a thread, but now it is gone
3062 SetInvalid();
3063 changed = was_valid;
3064 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003065 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003066 }
Enrico Granatabb642e52015-05-16 01:27:00 +00003067
Kate Stoneb9c1b512016-09-06 20:57:50 +00003068 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003069}
3070
Kate Stoneb9c1b512016-09-06 20:57:50 +00003071void ValueObject::EvaluationPoint::SetUpdated() {
3072 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3073 if (process_sp)
3074 m_mod_id = process_sp->GetModID();
3075 m_needs_update = false;
Johnny Chen44805302011-07-19 19:48:13 +00003076}
Enrico Granata9128ee22011-09-06 19:20:51 +00003077
Kate Stoneb9c1b512016-09-06 20:57:50 +00003078void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3079 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3080 eClearUserVisibleDataItemsValue)
3081 m_value_str.clear();
3082
3083 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3084 eClearUserVisibleDataItemsLocation)
3085 m_location_str.clear();
3086
3087 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3088 eClearUserVisibleDataItemsSummary)
3089 m_summary_str.clear();
3090
3091 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3092 eClearUserVisibleDataItemsDescription)
3093 m_object_desc_str.clear();
3094
3095 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3096 eClearUserVisibleDataItemsSyntheticChildren) {
3097 if (m_synthetic_value)
3098 m_synthetic_value = NULL;
3099 }
3100
3101 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3102 eClearUserVisibleDataItemsValidator)
3103 m_validation_result.reset();
Enrico Granata9128ee22011-09-06 19:20:51 +00003104}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003105
Kate Stoneb9c1b512016-09-06 20:57:50 +00003106SymbolContextScope *ValueObject::GetSymbolContextScope() {
3107 if (m_parent) {
3108 if (!m_parent->IsPointerOrReferenceType())
3109 return m_parent->GetSymbolContextScope();
3110 }
3111 return NULL;
Enrico Granata972be532014-12-17 21:18:43 +00003112}
3113
Zachary Turneraa5611f2016-11-13 03:29:46 +00003114lldb::ValueObjectSP
3115ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3116 llvm::StringRef expression,
3117 const ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003118 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3119 EvaluateExpressionOptions());
3120}
Enrico Granata972be532014-12-17 21:18:43 +00003121
Kate Stoneb9c1b512016-09-06 20:57:50 +00003122lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003123 llvm::StringRef name, llvm::StringRef expression,
3124 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003125 lldb::ValueObjectSP retval_sp;
3126 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3127 if (!target_sp)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003128 return retval_sp;
Zachary Turneraa5611f2016-11-13 03:29:46 +00003129 if (expression.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003130 return retval_sp;
3131 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3132 retval_sp, options);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003133 if (retval_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003134 retval_sp->SetName(ConstString(name));
3135 return retval_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003136}
3137
Zachary Turneraa5611f2016-11-13 03:29:46 +00003138lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3139 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3140 CompilerType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003141 if (type) {
3142 CompilerType pointer_type(type.GetPointerType());
3143 if (pointer_type) {
3144 lldb::DataBufferSP buffer(
3145 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3146 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3147 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3148 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3149 exe_ctx.GetAddressByteSize()));
3150 if (ptr_result_valobj_sp) {
3151 ptr_result_valobj_sp->GetValue().SetValueType(
3152 Value::eValueTypeLoadAddress);
Zachary Turner97206d52017-05-12 04:51:55 +00003153 Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003154 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003155 if (ptr_result_valobj_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003156 ptr_result_valobj_sp->SetName(ConstString(name));
3157 }
3158 return ptr_result_valobj_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003160 }
3161 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003162}
3163
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003165 llvm::StringRef name, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166 const ExecutionContext &exe_ctx, CompilerType type) {
3167 lldb::ValueObjectSP new_value_sp;
3168 new_value_sp = ValueObjectConstResult::Create(
3169 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3170 LLDB_INVALID_ADDRESS);
3171 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003172 if (new_value_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173 new_value_sp->SetName(ConstString(name));
3174 return new_value_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003175}
Enrico Granata4873e522013-04-11 22:48:58 +00003176
Kate Stoneb9c1b512016-09-06 20:57:50 +00003177ModuleSP ValueObject::GetModule() {
3178 ValueObject *root(GetRoot());
3179 if (root != this)
3180 return root->GetModule();
3181 return lldb::ModuleSP();
3182}
3183
3184ValueObject *ValueObject::GetRoot() {
3185 if (m_root)
3186 return m_root;
3187 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3188 return (vo->m_parent != nullptr);
3189 }));
3190}
3191
3192ValueObject *
3193ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3194 ValueObject *vo = this;
3195 while (vo) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003196 if (!f(vo))
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197 break;
3198 vo = vo->m_parent;
3199 }
3200 return vo;
3201}
3202
3203AddressType ValueObject::GetAddressTypeOfChildren() {
3204 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3205 ValueObject *root(GetRoot());
Enrico Granata4873e522013-04-11 22:48:58 +00003206 if (root != this)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003207 return root->GetAddressTypeOfChildren();
3208 }
3209 return m_address_type_of_ptr_or_ref_children;
Enrico Granata4873e522013-04-11 22:48:58 +00003210}
3211
Kate Stoneb9c1b512016-09-06 20:57:50 +00003212lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3213 ValueObject *with_dv_info = this;
3214 while (with_dv_info) {
3215 if (with_dv_info->HasDynamicValueTypeInfo())
3216 return with_dv_info->GetDynamicValueTypeImpl();
3217 with_dv_info = with_dv_info->m_parent;
3218 }
3219 return lldb::eNoDynamicValues;
Enrico Granatade61eba2015-01-22 03:07:34 +00003220}
3221
Kate Stoneb9c1b512016-09-06 20:57:50 +00003222lldb::Format ValueObject::GetFormat() const {
3223 const ValueObject *with_fmt_info = this;
3224 while (with_fmt_info) {
3225 if (with_fmt_info->m_format != lldb::eFormatDefault)
3226 return with_fmt_info->m_format;
3227 with_fmt_info = with_fmt_info->m_parent;
3228 }
3229 return m_format;
Enrico Granata4873e522013-04-11 22:48:58 +00003230}
3231
Kate Stoneb9c1b512016-09-06 20:57:50 +00003232lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3233 lldb::LanguageType type = m_preferred_display_language;
3234 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3235 if (GetRoot()) {
3236 if (GetRoot() == this) {
3237 if (StackFrameSP frame_sp = GetFrameSP()) {
3238 const SymbolContext &sc(
3239 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3240 if (CompileUnit *cu = sc.comp_unit)
3241 type = cu->GetLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +00003242 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003243 } else {
3244 type = GetRoot()->GetPreferredDisplayLanguage();
3245 }
Enrico Granatac1247f52014-11-06 21:23:20 +00003246 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247 }
3248 return (m_preferred_display_language = type); // only compute it once
Enrico Granataed3228a2015-01-21 01:47:13 +00003249}
3250
Kate Stoneb9c1b512016-09-06 20:57:50 +00003251void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3252 m_preferred_display_language = lt;
Enrico Granatac1247f52014-11-06 21:23:20 +00003253}
3254
Kate Stoneb9c1b512016-09-06 20:57:50 +00003255void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3256 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3257 SetPreferredDisplayLanguage(lt);
Enrico Granata73e8c4d2015-10-07 02:36:35 +00003258}
3259
Kate Stoneb9c1b512016-09-06 20:57:50 +00003260bool ValueObject::CanProvideValue() {
Adrian Prantl05097242018-04-30 16:49:04 +00003261 // we need to support invalid types as providers of values because some bare-
3262 // board debugging scenarios have no notion of types, but still manage to
3263 // have raw numeric values for things like registers. sigh.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003264 const CompilerType &type(GetCompilerType());
Jonas Devliegherea6682a42018-12-15 00:15:33 +00003265 return (!type.IsValid()) || (0 != (type.GetTypeInfo() & eTypeHasValue));
Sean Callanan7375f3e2014-12-09 21:18:59 +00003266}
3267
Kate Stoneb9c1b512016-09-06 20:57:50 +00003268bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3269
3270ValueObjectSP ValueObject::Persist() {
3271 if (!UpdateValueIfNeeded())
3272 return nullptr;
3273
3274 TargetSP target_sp(GetTargetSP());
3275 if (!target_sp)
3276 return nullptr;
3277
3278 PersistentExpressionState *persistent_state =
3279 target_sp->GetPersistentExpressionStateForLanguage(
3280 GetPreferredDisplayLanguage());
3281
3282 if (!persistent_state)
3283 return nullptr;
3284
Adrian Prantl03219f72018-04-30 23:59:17 +00003285 auto prefix = persistent_state->GetPersistentVariablePrefix();
3286 ConstString name =
3287 persistent_state->GetNextPersistentVariableName(*target_sp, prefix);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003288
3289 ValueObjectSP const_result_sp =
3290 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3291
3292 ExpressionVariableSP clang_var_sp =
3293 persistent_state->CreatePersistentVariable(const_result_sp);
3294 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3295 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3296
3297 return clang_var_sp->GetValueObject();
Enrico Granatad07cfd32014-10-08 18:27:36 +00003298}
Enrico Granata0c10a852014-12-08 23:13:56 +00003299
Kate Stoneb9c1b512016-09-06 20:57:50 +00003300bool ValueObject::IsSyntheticChildrenGenerated() {
3301 return m_is_synthetic_children_generated;
Enrico Granata0c10a852014-12-08 23:13:56 +00003302}
Enrico Granatae29df232014-12-09 19:51:20 +00003303
Kate Stoneb9c1b512016-09-06 20:57:50 +00003304void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3305 m_is_synthetic_children_generated = b;
Enrico Granatae29df232014-12-09 19:51:20 +00003306}
3307
Kate Stoneb9c1b512016-09-06 20:57:50 +00003308uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
Enrico Granatadc62ffd2015-11-09 19:27:34 +00003309
Kate Stoneb9c1b512016-09-06 20:57:50 +00003310void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }
Greg Clayton8369b282016-12-28 21:22:37 +00003311
3312ValueObjectManager::ValueObjectManager(lldb::ValueObjectSP in_valobj_sp,
3313 lldb::DynamicValueType use_dynamic,
3314 bool use_synthetic) : m_root_valobj_sp(),
3315 m_user_valobj_sp(), m_use_dynamic(use_dynamic), m_stop_id(UINT32_MAX),
3316 m_use_synthetic(use_synthetic) {
3317 if (!in_valobj_sp)
3318 return;
3319 // If the user passes in a value object that is dynamic or synthetic, then
3320 // water it down to the static type.
3321 m_root_valobj_sp = in_valobj_sp->GetQualifiedRepresentationIfAvailable(lldb::eNoDynamicValues, false);
3322}
3323
3324bool ValueObjectManager::IsValid() const {
3325 if (!m_root_valobj_sp)
3326 return false;
3327 lldb::TargetSP target_sp = GetTargetSP();
3328 if (target_sp)
3329 return target_sp->IsValid();
3330 return false;
3331}
3332
3333lldb::ValueObjectSP ValueObjectManager::GetSP() {
3334 lldb::ProcessSP process_sp = GetProcessSP();
3335 if (!process_sp)
3336 return lldb::ValueObjectSP();
3337
3338 const uint32_t current_stop_id = process_sp->GetLastNaturalStopID();
3339 if (current_stop_id == m_stop_id)
3340 return m_user_valobj_sp;
3341
3342 m_stop_id = current_stop_id;
3343
3344 if (!m_root_valobj_sp) {
3345 m_user_valobj_sp.reset();
3346 return m_root_valobj_sp;
3347 }
3348
3349 m_user_valobj_sp = m_root_valobj_sp;
3350
3351 if (m_use_dynamic != lldb::eNoDynamicValues) {
3352 lldb::ValueObjectSP dynamic_sp = m_user_valobj_sp->GetDynamicValue(m_use_dynamic);
3353 if (dynamic_sp)
3354 m_user_valobj_sp = dynamic_sp;
3355 }
3356
3357 if (m_use_synthetic) {
3358 lldb::ValueObjectSP synthetic_sp = m_user_valobj_sp->GetSyntheticValue(m_use_synthetic);
3359 if (synthetic_sp)
3360 m_user_valobj_sp = synthetic_sp;
3361 }
3362
3363 return m_user_valobj_sp;
3364}
3365
3366void ValueObjectManager::SetUseDynamic(lldb::DynamicValueType use_dynamic) {
3367 if (use_dynamic != m_use_dynamic) {
3368 m_use_dynamic = use_dynamic;
3369 m_user_valobj_sp.reset();
3370 m_stop_id = UINT32_MAX;
3371 }
3372}
3373
3374void ValueObjectManager::SetUseSynthetic(bool use_synthetic) {
3375 if (m_use_synthetic != use_synthetic) {
3376 m_use_synthetic = use_synthetic;
3377 m_user_valobj_sp.reset();
3378 m_stop_id = UINT32_MAX;
3379 }
3380}
3381
3382lldb::TargetSP ValueObjectManager::GetTargetSP() const {
3383 if (!m_root_valobj_sp)
3384 return m_root_valobj_sp->GetTargetSP();
3385 return lldb::TargetSP();
3386}
3387
3388lldb::ProcessSP ValueObjectManager::GetProcessSP() const {
3389 if (m_root_valobj_sp)
3390 return m_root_valobj_sp->GetProcessSP();
3391 return lldb::ProcessSP();
3392}
3393
3394lldb::ThreadSP ValueObjectManager::GetThreadSP() const {
3395 if (m_root_valobj_sp)
3396 return m_root_valobj_sp->GetThreadSP();
3397 return lldb::ThreadSP();
3398}
3399
3400lldb::StackFrameSP ValueObjectManager::GetFrameSP() const {
3401 if (m_root_valobj_sp)
3402 return m_root_valobj_sp->GetFrameSP();
3403 return lldb::StackFrameSP();
3404}
3405