blob: 235f30595b5a95d4c7127adc3ed95121eee3f0ec [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
12// C Includes
Greg Claytonf5e56de2010-09-14 23:36:40 +000013#include <stdlib.h>
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// C++ Includes
16// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "llvm/Support/raw_ostream.h"
18
19// Project includes
20#include "lldb/Core/DataBufferHeap.h"
Enrico Granata4becb372011-06-29 22:27:15 +000021#include "lldb/Core/Debugger.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000022#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
Enrico Granata21fd13f2012-10-27 02:05:48 +000025#include "lldb/Core/ValueObjectCast.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000027#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000028#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/ValueObjectList.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000030#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000031#include "lldb/Core/ValueObjectSyntheticFilter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Enrico Granata5548cb52013-01-28 23:47:25 +000033#include "lldb/DataFormatters/DataVisualization.h"
Enrico Granata2206b482014-10-30 18:27:31 +000034#include "lldb/DataFormatters/StringPrinter.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000035#include "lldb/DataFormatters/ValueObjectPrinter.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000036
Sean Callanan30e33972015-09-03 00:48:23 +000037#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000038#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Enrico Granata0c10a852014-12-08 23:13:56 +000039
Greg Clayton7fb56d02011-02-01 01:31:41 +000040#include "lldb/Host/Endian.h"
41
Enrico Granata61a80ba2011-08-12 16:42:31 +000042#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granataf2bbf712011-07-15 02:26:42 +000043
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Symbol/ClangASTContext.h"
Enrico Granatac1247f52014-11-06 21:23:20 +000045#include "lldb/Symbol/CompileUnit.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000046#include "lldb/Symbol/CompilerType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Symbol/Type.h"
48
Jim Ingham53c47f12010-09-10 23:12:17 +000049#include "lldb/Target/ExecutionContext.h"
Enrico Granata407b5c62015-11-02 21:52:05 +000050#include "lldb/Target/Language.h"
Jim Ingham5a369122010-09-28 01:25:32 +000051#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000052#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
54#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000055#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000056#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000061using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Greg Claytonafacd142011-09-02 01:15:17 +000063static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65//----------------------------------------------------------------------
66// ValueObject constructor
67//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000068ValueObject::ValueObject(ValueObject &parent)
69 : UserID(++g_value_obj_uid), // Unique identifier for every value object
70 m_parent(&parent), m_root(NULL), m_update_point(parent.GetUpdatePoint()),
71 m_name(), m_data(), m_value(), m_error(), m_value_str(),
72 m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(),
73 m_validation_result(), m_manager(parent.GetManager()), m_children(),
74 m_synthetic_children(), m_dynamic_value(NULL), m_synthetic_value(NULL),
75 m_deref_valobj(NULL), m_format(eFormatDefault),
76 m_last_format(eFormatDefault), m_last_format_mgr_revision(0),
77 m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(),
78 m_type_validator_sp(), m_user_id_of_forced_summary(),
79 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
80 m_value_checksum(),
81 m_preferred_display_language(lldb::eLanguageTypeUnknown),
82 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
83 m_children_count_valid(false), m_old_value_valid(false),
84 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
85 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
86 m_is_getting_summary(false),
87 m_did_calculate_complete_objc_class_type(false),
88 m_is_synthetic_children_generated(
89 parent.m_is_synthetic_children_generated) {
90 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +000091}
92
93//----------------------------------------------------------------------
94// ValueObject constructor
95//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000096ValueObject::ValueObject(ExecutionContextScope *exe_scope,
97 AddressType child_ptr_or_ref_addr_type)
98 : UserID(++g_value_obj_uid), // Unique identifier for every value object
99 m_parent(NULL), m_root(NULL), m_update_point(exe_scope), m_name(),
100 m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(),
101 m_location_str(), m_summary_str(), m_object_desc_str(),
102 m_validation_result(), m_manager(), m_children(), m_synthetic_children(),
103 m_dynamic_value(NULL), m_synthetic_value(NULL), m_deref_valobj(NULL),
104 m_format(eFormatDefault), m_last_format(eFormatDefault),
105 m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(),
106 m_synthetic_children_sp(), m_type_validator_sp(),
107 m_user_id_of_forced_summary(),
108 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
109 m_value_checksum(),
110 m_preferred_display_language(lldb::eLanguageTypeUnknown),
111 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
112 m_children_count_valid(false), m_old_value_valid(false),
113 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
114 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
115 m_is_getting_summary(false),
116 m_did_calculate_complete_objc_class_type(false),
117 m_is_synthetic_children_generated(false) {
118 m_manager = new ValueObjectManager();
119 m_manager->ManageObject(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120}
121
122//----------------------------------------------------------------------
123// Destructor
124//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125ValueObject::~ValueObject() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127bool ValueObject::UpdateValueIfNeeded(bool update_format) {
Greg Claytonb71f3842010-10-05 03:13:51 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 bool did_change_formats = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 if (update_format)
132 did_change_formats = UpdateFormatsIfNeeded();
Greg Claytonefbc7d22012-03-09 04:23:44 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 // If this is a constant value, then our success is predicated on whether
135 // we have an error or not
136 if (GetIsConstant()) {
137 // if you are constant, things might still have changed behind your back
138 // (e.g. you are a frozen object and things have changed deeper than you
139 // cared to freeze-dry yourself)
140 // in this case, your value has not changed, but "computed" entries might
141 // have, so you might now have
142 // a different summary, or a different object description. clear these so we
143 // will recompute them
144 if (update_format && !did_change_formats)
145 ClearUserVisibleData(eClearUserVisibleDataItemsSummary |
146 eClearUserVisibleDataItemsDescription);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 return m_error.Success();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 }
149
150 bool first_update = IsChecksumEmpty();
151
152 if (NeedsUpdating()) {
153 m_update_point.SetUpdated();
154
155 // Save the old value using swap to avoid a string copy which
156 // also will clear our m_value_str
157 if (m_value_str.empty()) {
158 m_old_value_valid = false;
159 } else {
160 m_old_value_valid = true;
161 m_old_value_str.swap(m_value_str);
162 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
163 }
164
165 ClearUserVisibleData();
166
167 if (IsInScope()) {
168 const bool value_was_valid = GetValueIsValid();
169 SetValueDidChange(false);
170
171 m_error.Clear();
172
173 // Call the pure virtual function to update the value
174
175 bool need_compare_checksums = false;
176 llvm::SmallVector<uint8_t, 16> old_checksum;
177
178 if (!first_update && CanProvideValue()) {
179 need_compare_checksums = true;
180 old_checksum.resize(m_value_checksum.size());
181 std::copy(m_value_checksum.begin(), m_value_checksum.end(),
182 old_checksum.begin());
183 }
184
185 bool success = UpdateValue();
186
187 SetValueIsValid(success);
188
189 if (success) {
190 const uint64_t max_checksum_size = 128;
191 m_data.Checksum(m_value_checksum, max_checksum_size);
192 } else {
193 need_compare_checksums = false;
194 m_value_checksum.clear();
195 }
196
197 assert(!need_compare_checksums ||
198 (!old_checksum.empty() && !m_value_checksum.empty()));
199
200 if (first_update)
201 SetValueDidChange(false);
202 else if (!m_value_did_change && success == false) {
203 // The value wasn't gotten successfully, so we mark this
204 // as changed if the value used to be valid and now isn't
205 SetValueDidChange(value_was_valid);
206 } else if (need_compare_checksums) {
207 SetValueDidChange(memcmp(&old_checksum[0], &m_value_checksum[0],
208 m_value_checksum.size()));
209 }
210
211 } else {
212 m_error.SetErrorString("out of scope");
213 }
214 }
215 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216}
217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218bool ValueObject::UpdateFormatsIfNeeded() {
219 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
220 if (log)
221 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject "
222 "rev: %d - Global rev: %d",
223 GetName().GetCString(), static_cast<void *>(this),
224 m_last_format_mgr_revision,
225 DataVisualization::GetCurrentRevision());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 bool any_change = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 if ((m_last_format_mgr_revision != DataVisualization::GetCurrentRevision())) {
230 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
231 any_change = true;
232
233 SetValueFormat(DataVisualization::GetFormat(*this, eNoDynamicValues));
234 SetSummaryFormat(
235 DataVisualization::GetSummaryFormat(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000236#ifndef LLDB_DISABLE_PYTHON
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 SetSyntheticChildren(
238 DataVisualization::GetSyntheticChildren(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000239#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
241 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 return any_change;
Enrico Granata4becb372011-06-29 22:27:15 +0000244}
245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246void ValueObject::SetNeedsUpdate() {
247 m_update_point.SetNeedsUpdate();
248 // We have to clear the value string here so ConstResult children will notice
249 // if their values are
250 // changed by hand (i.e. with SetValueAsCString).
251 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254void ValueObject::ClearDynamicTypeInformation() {
255 m_children_count_valid = false;
256 m_did_calculate_complete_objc_class_type = false;
257 m_last_format_mgr_revision = 0;
258 m_override_type = CompilerType();
259 SetValueFormat(lldb::TypeFormatImplSP());
260 SetSummaryFormat(lldb::TypeSummaryImplSP());
261 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000262}
263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264CompilerType ValueObject::MaybeCalculateCompleteType() {
265 CompilerType compiler_type(GetCompilerTypeImpl());
266
267 if (m_did_calculate_complete_objc_class_type) {
268 if (m_override_type.IsValid())
269 return m_override_type;
Sean Callanan72772842012-02-22 23:57:45 +0000270 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 return compiler_type;
272 }
273
274 CompilerType class_type;
275 bool is_pointer_type = false;
276
277 if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type)) {
278 is_pointer_type = true;
279 } else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type)) {
280 class_type = compiler_type;
281 } else {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000282 return compiler_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 }
284
285 m_did_calculate_complete_objc_class_type = true;
286
287 if (class_type) {
288 ConstString class_name(class_type.GetConstTypeName());
289
290 if (class_name) {
291 ProcessSP process_sp(
292 GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
293
294 if (process_sp) {
295 ObjCLanguageRuntime *objc_language_runtime(
296 process_sp->GetObjCLanguageRuntime());
297
298 if (objc_language_runtime) {
299 TypeSP complete_objc_class_type_sp =
300 objc_language_runtime->LookupInCompleteClassCache(class_name);
301
302 if (complete_objc_class_type_sp) {
303 CompilerType complete_class(
304 complete_objc_class_type_sp->GetFullCompilerType());
305
306 if (complete_class.GetCompleteType()) {
307 if (is_pointer_type) {
308 m_override_type = complete_class.GetPointerType();
309 } else {
310 m_override_type = complete_class;
311 }
312
313 if (m_override_type.IsValid())
314 return m_override_type;
315 }
316 }
317 }
318 }
319 }
320 }
321 return compiler_type;
Sean Callanan72772842012-02-22 23:57:45 +0000322}
323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324CompilerType ValueObject::GetCompilerType() {
325 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328TypeImpl ValueObject::GetTypeImpl() { return TypeImpl(GetCompilerType()); }
329
330DataExtractor &ValueObject::GetDataExtractor() {
331 UpdateValueIfNeeded(false);
332 return m_data;
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000333}
334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335const Error &ValueObject::GetError() {
336 UpdateValueIfNeeded(false);
337 return m_error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338}
339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340const ConstString &ValueObject::GetName() const { return m_name; }
341
342const char *ValueObject::GetLocationAsCString() {
343 return GetLocationAsCStringImpl(m_value, m_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
347 const DataExtractor &data) {
348 if (UpdateValueIfNeeded(false)) {
349 if (m_location_str.empty()) {
350 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 Value::ValueType value_type = value.GetValueType();
Enrico Granata82fabf82013-04-30 20:45:04 +0000353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 switch (value_type) {
355 case Value::eValueTypeScalar:
356 case Value::eValueTypeVector:
357 if (value.GetContextType() == Value::eContextTypeRegisterInfo) {
358 RegisterInfo *reg_info = value.GetRegisterInfo();
359 if (reg_info) {
360 if (reg_info->name)
361 m_location_str = reg_info->name;
362 else if (reg_info->alt_name)
363 m_location_str = reg_info->alt_name;
364 if (m_location_str.empty())
365 m_location_str = (reg_info->encoding == lldb::eEncodingVector)
366 ? "vector"
367 : "scalar";
368 }
369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 if (m_location_str.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 m_location_str =
372 (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
373 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 case Value::eValueTypeLoadAddress:
376 case Value::eValueTypeFileAddress:
377 case Value::eValueTypeHostAddress: {
378 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
379 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size,
380 value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
381 m_location_str.swap(sstr.GetString());
382 } break;
383 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 }
386 return m_location_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389Value &ValueObject::GetValue() { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391const Value &ValueObject::GetValue() const { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393bool ValueObject::ResolveValue(Scalar &scalar) {
394 if (UpdateValueIfNeeded(
395 false)) // make sure that you are up to date before returning anything
396 {
397 ExecutionContext exe_ctx(GetExecutionContextRef());
398 Value tmp_value(m_value);
399 scalar = tmp_value.ResolveValue(&exe_ctx);
400 if (scalar.IsValid()) {
401 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
402 if (bitfield_bit_size)
403 return scalar.ExtractBitfield(bitfield_bit_size,
404 GetBitfieldBitOffset());
405 return true;
Enrico Granata6fd87d52011-08-04 01:41:02 +0000406 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 }
408 return false;
409}
410
411bool ValueObject::IsLogicalTrue(Error &error) {
412 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
413 LazyBool is_logical_true = language->IsLogicalTrue(*this, error);
414 switch (is_logical_true) {
415 case eLazyBoolYes:
416 case eLazyBoolNo:
417 return (is_logical_true == true);
418 case eLazyBoolCalculate:
419 break;
420 }
421 }
422
423 Scalar scalar_value;
424
425 if (!ResolveValue(scalar_value)) {
426 error.SetErrorString("failed to get a scalar result");
Greg Claytondcad5022011-12-29 01:26:56 +0000427 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 }
429
430 bool ret;
431 if (scalar_value.ULongLong(1) == 0)
432 ret = false;
433 else
434 ret = true;
435 error.Clear();
436 return ret;
Greg Clayton8f343b02010-11-04 01:54:29 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439bool ValueObject::GetValueIsValid() const { return m_value_is_valid; }
440
441void ValueObject::SetValueIsValid(bool b) { m_value_is_valid = b; }
442
443bool ValueObject::GetValueDidChange() { return m_value_did_change; }
444
445void ValueObject::SetValueDidChange(bool value_changed) {
446 m_value_did_change = value_changed;
447}
448
449ValueObjectSP ValueObject::GetChildAtIndex(size_t idx, bool can_create) {
450 ValueObjectSP child_sp;
451 // We may need to update our value if we are dynamic
452 if (IsPossibleDynamicType())
453 UpdateValueIfNeeded(false);
454 if (idx < GetNumChildren()) {
455 // Check if we have already made the child value object?
456 if (can_create && !m_children.HasChildAtIndex(idx)) {
457 // No we haven't created the child at this index, so lets have our
458 // subclass do it and cache the result for quick future access.
459 m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
Enrico Granata407b5c62015-11-02 21:52:05 +0000460 }
Jim Ingham98e6daf2015-10-31 00:02:18 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 ValueObject *child = m_children.GetChildAtIndex(idx);
463 if (child != NULL)
464 return child->GetSP();
465 }
466 return child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467}
468
469ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470ValueObject::GetChildAtIndexPath(const std::initializer_list<size_t> &idxs,
471 size_t *index_of_error) {
472 return GetChildAtIndexPath(std::vector<size_t>(idxs), index_of_error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473}
474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475ValueObjectSP ValueObject::GetChildAtIndexPath(
476 const std::initializer_list<std::pair<size_t, bool>> &idxs,
477 size_t *index_of_error) {
478 return GetChildAtIndexPath(std::vector<std::pair<size_t, bool>>(idxs),
479 index_of_error);
Enrico Granata3309d882013-01-12 01:00:22 +0000480}
481
482lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483ValueObject::GetChildAtIndexPath(const std::vector<size_t> &idxs,
484 size_t *index_of_error) {
485 if (idxs.size() == 0)
486 return GetSP();
487 ValueObjectSP root(GetSP());
488 for (size_t idx : idxs) {
489 root = root->GetChildAtIndex(idx, true);
490 if (!root) {
491 if (index_of_error)
492 *index_of_error = idx;
493 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000494 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 }
496 return root;
497}
498
499lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
500 const std::vector<std::pair<size_t, bool>> &idxs, size_t *index_of_error) {
501 if (idxs.size() == 0)
502 return GetSP();
503 ValueObjectSP root(GetSP());
504 for (std::pair<size_t, bool> idx : idxs) {
505 root = root->GetChildAtIndex(idx.first, idx.second);
506 if (!root) {
507 if (index_of_error)
508 *index_of_error = idx.first;
509 return root;
510 }
511 }
512 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000513}
514
515lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516ValueObject::GetChildAtNamePath(const std::initializer_list<ConstString> &names,
517 ConstString *name_of_error) {
518 return GetChildAtNamePath(std::vector<ConstString>(names), name_of_error);
519}
520
521lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
522 const std::initializer_list<std::pair<ConstString, bool>> &names,
523 ConstString *name_of_error) {
524 return GetChildAtNamePath(std::vector<std::pair<ConstString, bool>>(names),
525 name_of_error);
Enrico Granata3309d882013-01-12 01:00:22 +0000526}
527
Enrico Granatae2e220a2013-09-12 00:48:47 +0000528lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529ValueObject::GetChildAtNamePath(const std::vector<ConstString> &names,
530 ConstString *name_of_error) {
531 if (names.size() == 0)
532 return GetSP();
533 ValueObjectSP root(GetSP());
534 for (ConstString name : names) {
535 root = root->GetChildMemberWithName(name, true);
536 if (!root) {
537 if (name_of_error)
538 *name_of_error = name;
539 return root;
540 }
541 }
542 return root;
Enrico Granataef8dde62015-12-21 23:10:17 +0000543}
544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
546 const std::vector<std::pair<ConstString, bool>> &names,
547 ConstString *name_of_error) {
548 if (names.size() == 0)
549 return GetSP();
550 ValueObjectSP root(GetSP());
551 for (std::pair<ConstString, bool> name : names) {
552 root = root->GetChildMemberWithName(name.first, name.second);
553 if (!root) {
554 if (name_of_error)
555 *name_of_error = name.first;
556 return root;
557 }
558 }
559 return root;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000560}
561
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562size_t ValueObject::GetIndexOfChildWithName(const ConstString &name) {
563 bool omit_empty_base_classes = true;
564 return GetCompilerType().GetIndexOfChildWithName(name.GetCString(),
565 omit_empty_base_classes);
Enrico Granatae2e220a2013-09-12 00:48:47 +0000566}
567
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568ValueObjectSP ValueObject::GetChildMemberWithName(const ConstString &name,
569 bool can_create) {
570 // when getting a child by name, it could be buried inside some base
571 // classes (which really aren't part of the expression path), so we
572 // need a vector of indexes that can get us down to the correct child
573 ValueObjectSP child_sp;
574
575 // We may need to update our value if we are dynamic
576 if (IsPossibleDynamicType())
577 UpdateValueIfNeeded(false);
578
579 std::vector<uint32_t> child_indexes;
580 bool omit_empty_base_classes = true;
581 const size_t num_child_indexes =
582 GetCompilerType().GetIndexOfChildMemberWithName(
583 name.GetCString(), omit_empty_base_classes, child_indexes);
584 if (num_child_indexes > 0) {
585 std::vector<uint32_t>::const_iterator pos = child_indexes.begin();
586 std::vector<uint32_t>::const_iterator end = child_indexes.end();
587
588 child_sp = GetChildAtIndex(*pos, can_create);
589 for (++pos; pos != end; ++pos) {
590 if (child_sp) {
591 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex(*pos, can_create));
592 child_sp = new_child_sp;
593 } else {
594 child_sp.reset();
595 }
Enrico Granatae2e220a2013-09-12 00:48:47 +0000596 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000597 }
598 return child_sp;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000599}
600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601size_t ValueObject::GetNumChildren(uint32_t max) {
602 UpdateValueIfNeeded();
603
604 if (max < UINT32_MAX) {
605 if (m_children_count_valid) {
606 size_t children_count = m_children.GetChildrenCount();
607 return children_count <= max ? children_count : max;
608 } else
609 return CalculateNumChildren(max);
610 }
611
612 if (!m_children_count_valid) {
613 SetNumChildren(CalculateNumChildren());
614 }
615 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616}
617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618bool ValueObject::MightHaveChildren() {
619 bool has_children = false;
620 const uint32_t type_info = GetTypeInfo();
621 if (type_info) {
622 if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
623 has_children = true;
624 } else {
625 has_children = GetNumChildren() > 0;
626 }
627 return has_children;
Greg Clayton4a792072012-10-23 01:50:10 +0000628}
629
630// Should only be called by ValueObject::GetNumChildren()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631void ValueObject::SetNumChildren(size_t num_children) {
632 m_children_count_valid = true;
633 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634}
635
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636void ValueObject::SetName(const ConstString &name) { m_name = name; }
637
638ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
639 bool synthetic_array_member,
640 int32_t synthetic_index) {
641 ValueObject *valobj = NULL;
642
643 bool omit_empty_base_classes = true;
644 bool ignore_array_bounds = synthetic_array_member;
645 std::string child_name_str;
646 uint32_t child_byte_size = 0;
647 int32_t child_byte_offset = 0;
648 uint32_t child_bitfield_bit_size = 0;
649 uint32_t child_bitfield_bit_offset = 0;
650 bool child_is_base_class = false;
651 bool child_is_deref_of_parent = false;
652 uint64_t language_flags = 0;
653
654 const bool transparent_pointers = synthetic_array_member == false;
655 CompilerType child_compiler_type;
656
657 ExecutionContext exe_ctx(GetExecutionContextRef());
658
659 child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
660 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
661 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
662 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
663 child_is_deref_of_parent, this, language_flags);
664 if (child_compiler_type) {
665 if (synthetic_index)
666 child_byte_offset += child_byte_size * synthetic_index;
667
668 ConstString child_name;
669 if (!child_name_str.empty())
670 child_name.SetCString(child_name_str.c_str());
671
672 valobj = new ValueObjectChild(
673 *this, child_compiler_type, child_name, child_byte_size,
674 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
675 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
676 language_flags);
677 // if (valobj)
678 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
679 }
680
681 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682}
683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
685 std::string &destination,
686 lldb::LanguageType lang) {
687 return GetSummaryAsCString(summary_ptr, destination,
688 TypeSummaryOptions().SetLanguage(lang));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689}
690
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
692 std::string &destination,
693 const TypeSummaryOptions &options) {
694 destination.clear();
695
696 // ideally we would like to bail out if passing NULL, but if we do so
697 // we end up not providing the summary for function pointers anymore
698 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
699 return false;
700
701 m_is_getting_summary = true;
702
703 TypeSummaryOptions actual_options(options);
704
705 if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
706 actual_options.SetLanguage(GetPreferredDisplayLanguage());
707
708 // this is a hot path in code and we prefer to avoid setting this string all
709 // too often also clearing out other
710 // information that we might care to see in a crash log. might be useful in
711 // very specific situations though.
712 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s.
713 Summary provider's description is %s",
714 GetTypeName().GetCString(),
715 GetName().GetCString(),
716 summary_ptr->GetDescription().c_str());*/
717
718 if (UpdateValueIfNeeded(false) && summary_ptr) {
719 if (HasSyntheticValue())
720 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on
721 // the synthetic children being
722 // up-to-date (e.g. ${svar%#})
723 summary_ptr->FormatObject(this, destination, actual_options);
724 }
725 m_is_getting_summary = false;
726 return !destination.empty();
Enrico Granatac1247f52014-11-06 21:23:20 +0000727}
728
Kate Stoneb9c1b512016-09-06 20:57:50 +0000729const char *ValueObject::GetSummaryAsCString(lldb::LanguageType lang) {
730 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) {
731 TypeSummaryOptions summary_options;
732 summary_options.SetLanguage(lang);
733 GetSummaryAsCString(GetSummaryFormat().get(), m_summary_str,
734 summary_options);
735 }
736 if (m_summary_str.empty())
737 return NULL;
738 return m_summary_str.c_str();
739}
740
741bool ValueObject::GetSummaryAsCString(std::string &destination,
742 const TypeSummaryOptions &options) {
743 return GetSummaryAsCString(GetSummaryFormat().get(), destination, options);
744}
745
746bool ValueObject::IsCStringContainer(bool check_pointer) {
747 CompilerType pointee_or_element_compiler_type;
748 const Flags type_flags(GetTypeInfo(&pointee_or_element_compiler_type));
749 bool is_char_arr_ptr(type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
750 pointee_or_element_compiler_type.IsCharType());
751 if (!is_char_arr_ptr)
752 return false;
753 if (!check_pointer)
754 return true;
755 if (type_flags.Test(eTypeIsArray))
756 return true;
757 addr_t cstr_address = LLDB_INVALID_ADDRESS;
758 AddressType cstr_address_type = eAddressTypeInvalid;
759 cstr_address = GetAddressOf(true, &cstr_address_type);
760 return (cstr_address != LLDB_INVALID_ADDRESS);
761}
762
763size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
764 uint32_t item_count) {
765 CompilerType pointee_or_element_compiler_type;
766 const uint32_t type_info = GetTypeInfo(&pointee_or_element_compiler_type);
767 const bool is_pointer_type = type_info & eTypeIsPointer;
768 const bool is_array_type = type_info & eTypeIsArray;
769 if (!(is_pointer_type || is_array_type))
770 return 0;
771
772 if (item_count == 0)
773 return 0;
774
775 ExecutionContext exe_ctx(GetExecutionContextRef());
776
777 const uint64_t item_type_size = pointee_or_element_compiler_type.GetByteSize(
778 exe_ctx.GetBestExecutionContextScope());
779 const uint64_t bytes = item_count * item_type_size;
780 const uint64_t offset = item_idx * item_type_size;
781
782 if (item_idx == 0 && item_count == 1) // simply a deref
783 {
784 if (is_pointer_type) {
785 Error error;
786 ValueObjectSP pointee_sp = Dereference(error);
787 if (error.Fail() || pointee_sp.get() == NULL)
788 return 0;
789 return pointee_sp->GetData(data, error);
790 } else {
791 ValueObjectSP child_sp = GetChildAtIndex(0, true);
792 if (child_sp.get() == NULL)
793 return 0;
794 Error error;
795 return child_sp->GetData(data, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 return true;
798 } else /* (items > 1) */
799 {
800 Error error;
801 lldb_private::DataBufferHeap *heap_buf_ptr = NULL;
802 lldb::DataBufferSP data_sp(heap_buf_ptr =
803 new lldb_private::DataBufferHeap());
Enrico Granata0c489f52012-03-01 04:24:26 +0000804
Kate Stoneb9c1b512016-09-06 20:57:50 +0000805 AddressType addr_type;
806 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type)
807 : GetAddressOf(true, &addr_type);
808
809 switch (addr_type) {
810 case eAddressTypeFile: {
811 ModuleSP module_sp(GetModule());
812 if (module_sp) {
813 addr = addr + offset;
814 Address so_addr;
815 module_sp->ResolveFileAddress(addr, so_addr);
816 ExecutionContext exe_ctx(GetExecutionContextRef());
817 Target *target = exe_ctx.GetTargetPtr();
818 if (target) {
819 heap_buf_ptr->SetByteSize(bytes);
820 size_t bytes_read = target->ReadMemory(
821 so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
822 if (error.Success()) {
823 data.SetData(data_sp);
824 return bytes_read;
825 }
826 }
827 }
828 } break;
829 case eAddressTypeLoad: {
830 ExecutionContext exe_ctx(GetExecutionContextRef());
831 Process *process = exe_ctx.GetProcessPtr();
832 if (process) {
833 heap_buf_ptr->SetByteSize(bytes);
834 size_t bytes_read = process->ReadMemory(
835 addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
836 if (error.Success() || bytes_read > 0) {
837 data.SetData(data_sp);
838 return bytes_read;
839 }
840 }
841 } break;
842 case eAddressTypeHost: {
843 const uint64_t max_bytes =
844 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
845 if (max_bytes > offset) {
846 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
847 addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
848 if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
849 break;
850 heap_buf_ptr->CopyData((uint8_t *)(addr + offset), bytes_read);
851 data.SetData(data_sp);
852 return bytes_read;
853 }
854 } break;
855 case eAddressTypeInvalid:
856 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000857 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858 }
859 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860}
861
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862uint64_t ValueObject::GetData(DataExtractor &data, Error &error) {
863 UpdateValueIfNeeded(false);
864 ExecutionContext exe_ctx(GetExecutionContextRef());
865 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
866 if (error.Fail()) {
867 if (m_data.GetByteSize()) {
868 data = m_data;
869 error.Clear();
870 return data.GetByteSize();
871 } else {
872 return 0;
873 }
874 }
875 data.SetAddressByteSize(m_data.GetAddressByteSize());
876 data.SetByteOrder(m_data.GetByteOrder());
877 return data.GetByteSize();
Enrico Granata49bfafb2014-11-18 23:36:25 +0000878}
879
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880bool ValueObject::SetData(DataExtractor &data, Error &error) {
881 error.Clear();
882 // Make sure our value is up to date first so that our location and location
883 // type is valid.
884 if (!UpdateValueIfNeeded(false)) {
885 error.SetErrorString("unable to read value");
886 return false;
887 }
888
889 uint64_t count = 0;
890 const Encoding encoding = GetCompilerType().GetEncoding(count);
891
892 const size_t byte_size = GetByteSize();
893
894 Value::ValueType value_type = m_value.GetValueType();
895
896 switch (value_type) {
897 case Value::eValueTypeScalar: {
898 Error set_error =
899 m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
900
901 if (!set_error.Success()) {
902 error.SetErrorStringWithFormat("unable to set scalar value: %s",
903 set_error.AsCString());
904 return false;
905 }
906 } break;
907 case Value::eValueTypeLoadAddress: {
908 // If it is a load address, then the scalar value is the storage location
909 // of the data, and we have to shove this value down to that load location.
910 ExecutionContext exe_ctx(GetExecutionContextRef());
911 Process *process = exe_ctx.GetProcessPtr();
912 if (process) {
913 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
914 size_t bytes_written = process->WriteMemory(
915 target_addr, data.GetDataStart(), byte_size, error);
916 if (!error.Success())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000917 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918 if (bytes_written != byte_size) {
919 error.SetErrorString("unable to write value to memory");
920 return false;
921 }
922 }
923 } break;
924 case Value::eValueTypeHostAddress: {
925 // If it is a host address, then we stuff the scalar as a DataBuffer into
926 // the Value's data.
927 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
928 m_data.SetData(buffer_sp, 0);
929 data.CopyByteOrderedData(0, byte_size,
930 const_cast<uint8_t *>(m_data.GetDataStart()),
931 byte_size, m_data.GetByteOrder());
932 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
933 } break;
934 case Value::eValueTypeFileAddress:
935 case Value::eValueTypeVector:
936 break;
937 }
938
939 // If we have reached this point, then we have successfully changed the value.
940 SetNeedsUpdate();
941 return true;
942}
943
944static bool CopyStringDataToBufferSP(const StreamString &source,
945 lldb::DataBufferSP &destination) {
946 destination.reset(new DataBufferHeap(source.GetSize() + 1, 0));
947 memcpy(destination->GetBytes(), source.GetString().c_str(), source.GetSize());
948 return true;
949}
950
951std::pair<size_t, bool>
952ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Error &error,
953 uint32_t max_length, bool honor_array,
954 Format item_format) {
955 bool was_capped = false;
956 StreamString s;
957 ExecutionContext exe_ctx(GetExecutionContextRef());
958 Target *target = exe_ctx.GetTargetPtr();
959
960 if (!target) {
961 s << "<no target to read from>";
962 error.SetErrorString("no target to read from");
963 CopyStringDataToBufferSP(s, buffer_sp);
964 return {0, was_capped};
965 }
966
967 if (max_length == 0)
968 max_length = target->GetMaximumSizeOfStringSummary();
969
970 size_t bytes_read = 0;
971 size_t total_bytes_read = 0;
972
973 CompilerType compiler_type = GetCompilerType();
974 CompilerType elem_or_pointee_compiler_type;
975 const Flags type_flags(GetTypeInfo(&elem_or_pointee_compiler_type));
976 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
977 elem_or_pointee_compiler_type.IsCharType()) {
Greg Claytonafacd142011-09-02 01:15:17 +0000978 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000979 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000980
Kate Stoneb9c1b512016-09-06 20:57:50 +0000981 size_t cstr_len = 0;
982 bool capped_data = false;
983 const bool is_array = type_flags.Test(eTypeIsArray);
984 if (is_array) {
985 // We have an array
986 uint64_t array_size = 0;
987 if (compiler_type.IsArrayType(NULL, &array_size, NULL)) {
988 cstr_len = array_size;
989 if (cstr_len > max_length) {
990 capped_data = true;
991 cstr_len = max_length;
Enrico Granata9128ee22011-09-06 19:20:51 +0000992 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 }
994 cstr_address = GetAddressOf(true, &cstr_address_type);
995 } else {
996 // We have a pointer
997 cstr_address = GetPointerValue(&cstr_address_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000998 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) {
1001 if (cstr_address_type == eAddressTypeHost && is_array) {
1002 const char *cstr = GetDataExtractor().PeekCStr(0);
1003 if (cstr == nullptr) {
1004 s << "<invalid address>";
1005 error.SetErrorString("invalid address");
1006 CopyStringDataToBufferSP(s, buffer_sp);
1007 return {0, was_capped};
Sean Callananed185ab2013-04-19 19:47:32 +00001008 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009 buffer_sp.reset(new DataBufferHeap(cstr_len, 0));
1010 memcpy(buffer_sp->GetBytes(), cstr, cstr_len);
1011 return {cstr_len, was_capped};
1012 } else {
1013 s << "<invalid address>";
1014 error.SetErrorString("invalid address");
Enrico Granata2206b482014-10-30 18:27:31 +00001015 CopyStringDataToBufferSP(s, buffer_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001016 return {0, was_capped};
1017 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001018 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019
1020 Address cstr_so_addr(cstr_address);
1021 DataExtractor data;
1022 if (cstr_len > 0 && honor_array) {
1023 // I am using GetPointeeData() here to abstract the fact that some
1024 // ValueObjects are actually frozen pointers in the host
1025 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1026 // automatically takes care of this
1027 GetPointeeData(data, 0, cstr_len);
1028
1029 if ((bytes_read = data.GetByteSize()) > 0) {
1030 total_bytes_read = bytes_read;
1031 for (size_t offset = 0; offset < bytes_read; offset++)
1032 s.Printf("%c", *data.PeekData(offset, 1));
1033 if (capped_data)
1034 was_capped = true;
1035 }
1036 } else {
1037 cstr_len = max_length;
1038 const size_t k_max_buf_size = 64;
1039
1040 size_t offset = 0;
1041
1042 int cstr_len_displayed = -1;
1043 bool capped_cstr = false;
1044 // I am using GetPointeeData() here to abstract the fact that some
1045 // ValueObjects are actually frozen pointers in the host
1046 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1047 // automatically takes care of this
1048 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) {
1049 total_bytes_read += bytes_read;
1050 const char *cstr = data.PeekCStr(0);
1051 size_t len = strnlen(cstr, k_max_buf_size);
1052 if (cstr_len_displayed < 0)
1053 cstr_len_displayed = len;
1054
1055 if (len == 0)
1056 break;
1057 cstr_len_displayed += len;
1058 if (len > bytes_read)
1059 len = bytes_read;
1060 if (len > cstr_len)
1061 len = cstr_len;
1062
1063 for (size_t offset = 0; offset < bytes_read; offset++)
1064 s.Printf("%c", *data.PeekData(offset, 1));
1065
1066 if (len < k_max_buf_size)
1067 break;
1068
1069 if (len >= cstr_len) {
1070 capped_cstr = true;
1071 break;
1072 }
1073
1074 cstr_len -= len;
1075 offset += len;
1076 }
1077
1078 if (cstr_len_displayed >= 0) {
1079 if (capped_cstr)
1080 was_capped = true;
1081 }
1082 }
1083 } else {
1084 error.SetErrorString("not a string object");
1085 s << "<not a string object>";
1086 }
1087 CopyStringDataToBufferSP(s, buffer_sp);
1088 return {total_bytes_read, was_capped};
1089}
1090
1091std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() {
1092 if (!UpdateValueIfNeeded(true))
1093 return {TypeValidatorResult::Success,
1094 ""}; // not the validator's job to discuss update problems
1095
1096 if (m_validation_result.hasValue())
1097 return m_validation_result.getValue();
1098
1099 if (!m_type_validator_sp)
1100 return {TypeValidatorResult::Success, ""}; // no validator no failure
1101
1102 auto outcome = m_type_validator_sp->FormatObject(this);
1103
1104 return (m_validation_result = {outcome.m_result, outcome.m_message})
1105 .getValue();
1106}
1107
1108const char *ValueObject::GetObjectDescription() {
1109
1110 if (!UpdateValueIfNeeded(true))
1111 return NULL;
1112
1113 if (!m_object_desc_str.empty())
1114 return m_object_desc_str.c_str();
1115
1116 ExecutionContext exe_ctx(GetExecutionContextRef());
1117 Process *process = exe_ctx.GetProcessPtr();
1118 if (process == NULL)
1119 return NULL;
1120
1121 StreamString s;
1122
1123 LanguageType language = GetObjectRuntimeLanguage();
1124 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1125
1126 if (runtime == NULL) {
1127 // Aw, hell, if the things a pointer, or even just an integer, let's try
1128 // ObjC anyway...
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001129 CompilerType compiler_type = GetCompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 if (compiler_type) {
1131 bool is_signed;
1132 if (compiler_type.IsIntegerType(is_signed) ||
1133 compiler_type.IsPointerType()) {
1134 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1135 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 }
1138
1139 if (runtime && runtime->GetObjectDescription(s, *this)) {
1140 m_object_desc_str.append(s.GetData());
1141 }
1142
1143 if (m_object_desc_str.empty())
1144 return NULL;
1145 else
1146 return m_object_desc_str.c_str();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001147}
1148
Kate Stoneb9c1b512016-09-06 20:57:50 +00001149bool ValueObject::GetValueAsCString(const lldb_private::TypeFormatImpl &format,
1150 std::string &destination) {
1151 if (UpdateValueIfNeeded(false))
1152 return format.FormatObject(this, destination);
1153 else
1154 return false;
Enrico Granata744794a2014-09-05 21:46:22 +00001155}
1156
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157bool ValueObject::GetValueAsCString(lldb::Format format,
1158 std::string &destination) {
1159 return GetValueAsCString(TypeFormatImpl_Format(format), destination);
1160}
Enrico Granata0a3958e2011-07-02 00:25:22 +00001161
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162const char *ValueObject::GetValueAsCString() {
1163 if (UpdateValueIfNeeded(true)) {
1164 lldb::TypeFormatImplSP format_sp;
1165 lldb::Format my_format = GetFormat();
1166 if (my_format == lldb::eFormatDefault) {
1167 if (m_type_format_sp)
1168 format_sp = m_type_format_sp;
1169 else {
1170 if (m_is_bitfield_for_scalar)
1171 my_format = eFormatUnsigned;
1172 else {
1173 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) {
1174 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1175 if (reg_info)
1176 my_format = reg_info->format;
1177 } else {
1178 my_format = GetValue().GetCompilerType().GetFormat();
1179 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001181 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001182 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 if (my_format != m_last_format || m_value_str.empty()) {
1184 m_last_format = my_format;
1185 if (!format_sp)
1186 format_sp.reset(new TypeFormatImpl_Format(my_format));
1187 if (GetValueAsCString(*format_sp.get(), m_value_str)) {
1188 if (!m_value_did_change && m_old_value_valid) {
1189 // The value was gotten successfully, so we consider the
1190 // value as changed if the value string differs
1191 SetValueDidChange(m_old_value_str != m_value_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 }
1196 if (m_value_str.empty())
1197 return NULL;
1198 return m_value_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199}
1200
Enrico Granatac3e320a2011-08-02 17:27:39 +00001201// if > 8bytes, 0 is returned. this method should mostly be used
1202// to read address values out of pointers
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203uint64_t ValueObject::GetValueAsUnsigned(uint64_t fail_value, bool *success) {
1204 // If our byte size is zero this is an aggregate type that has children
1205 if (CanProvideValue()) {
1206 Scalar scalar;
1207 if (ResolveValue(scalar)) {
1208 if (success)
1209 *success = true;
1210 return scalar.ULongLong(fail_value);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 // fallthrough, otherwise...
1213 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 if (success)
1216 *success = false;
1217 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001218}
1219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220int64_t ValueObject::GetValueAsSigned(int64_t fail_value, bool *success) {
1221 // If our byte size is zero this is an aggregate type that has children
1222 if (CanProvideValue()) {
1223 Scalar scalar;
1224 if (ResolveValue(scalar)) {
1225 if (success)
1226 *success = true;
1227 return scalar.SLongLong(fail_value);
Enrico Granatad7373f62013-10-31 18:57:50 +00001228 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 // fallthrough, otherwise...
1230 }
1231
1232 if (success)
1233 *success = false;
1234 return fail_value;
Enrico Granatad7373f62013-10-31 18:57:50 +00001235}
1236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237// if any more "special cases" are added to
1238// ValueObject::DumpPrintableRepresentation() please keep
1239// this call up to date by returning true for your new special cases. We will
1240// eventually move
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001241// to checking this call result before trying to display special cases
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242bool ValueObject::HasSpecialPrintableRepresentation(
1243 ValueObjectRepresentationStyle val_obj_display, Format custom_format) {
1244 Flags flags(GetTypeInfo());
1245 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1246 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1247 if (IsCStringContainer(true) &&
1248 (custom_format == eFormatCString || custom_format == eFormatCharArray ||
1249 custom_format == eFormatChar || custom_format == eFormatVectorOfChar))
1250 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 if (flags.Test(eTypeIsArray)) {
1253 if ((custom_format == eFormatBytes) ||
1254 (custom_format == eFormatBytesWithASCII))
1255 return true;
1256
1257 if ((custom_format == eFormatVectorOfChar) ||
1258 (custom_format == eFormatVectorOfFloat32) ||
1259 (custom_format == eFormatVectorOfFloat64) ||
1260 (custom_format == eFormatVectorOfSInt16) ||
1261 (custom_format == eFormatVectorOfSInt32) ||
1262 (custom_format == eFormatVectorOfSInt64) ||
1263 (custom_format == eFormatVectorOfSInt8) ||
1264 (custom_format == eFormatVectorOfUInt128) ||
1265 (custom_format == eFormatVectorOfUInt16) ||
1266 (custom_format == eFormatVectorOfUInt32) ||
1267 (custom_format == eFormatVectorOfUInt64) ||
1268 (custom_format == eFormatVectorOfUInt8))
1269 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001270 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 }
1272 return false;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001273}
1274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275bool ValueObject::DumpPrintableRepresentation(
1276 Stream &s, ValueObjectRepresentationStyle val_obj_display,
1277 Format custom_format, PrintableRepresentationSpecialCases special,
1278 bool do_dump_error) {
Enrico Granataf4efecd2011-07-12 22:56:10 +00001279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 Flags flags(GetTypeInfo());
Enrico Granata86cc9822012-03-19 22:58:49 +00001281
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) ==
1283 ePrintableRepresentationSpecialCasesAllow);
1284 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) ==
1285 ePrintableRepresentationSpecialCasesOnly);
1286
1287 if (allow_special) {
1288 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1289 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1290 // when being asked to get a printable display an array or pointer type
1291 // directly,
1292 // try to "do the right thing"
1293
1294 if (IsCStringContainer(true) &&
1295 (custom_format == eFormatCString ||
1296 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1297 custom_format ==
1298 eFormatVectorOfChar)) // print char[] & char* directly
1299 {
1300 Error error;
1301 lldb::DataBufferSP buffer_sp;
1302 std::pair<size_t, bool> read_string = ReadPointedString(
1303 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1304 (custom_format == eFormatCharArray));
1305 lldb_private::formatters::StringPrinter::
1306 ReadBufferAndDumpToStreamOptions options(*this);
1307 options.SetData(DataExtractor(
1308 buffer_sp, lldb::eByteOrderInvalid,
1309 8)); // none of this matters for a string - pass some defaults
1310 options.SetStream(&s);
1311 options.SetPrefixToken(0);
1312 options.SetQuote('"');
1313 options.SetSourceSize(buffer_sp->GetByteSize());
1314 options.SetIsTruncated(read_string.second);
1315 formatters::StringPrinter::ReadBufferAndDumpToStream<
1316 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1317 options);
1318 return !error.Fail();
1319 }
1320
1321 if (custom_format == eFormatEnum)
Enrico Granata85933ed2011-08-18 16:38:26 +00001322 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 // this only works for arrays, because I have no way to know when
1325 // the pointed memory ends, and no special \0 end of data marker
1326 if (flags.Test(eTypeIsArray)) {
1327 if ((custom_format == eFormatBytes) ||
1328 (custom_format == eFormatBytesWithASCII)) {
1329 const size_t count = GetNumChildren();
1330
1331 s << '[';
1332 for (size_t low = 0; low < count; low++) {
1333
1334 if (low)
1335 s << ',';
1336
1337 ValueObjectSP child = GetChildAtIndex(low, true);
1338 if (!child.get()) {
1339 s << "<invalid child>";
1340 continue;
Enrico Granata86cc9822012-03-19 22:58:49 +00001341 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 child->DumpPrintableRepresentation(
1343 s, ValueObject::eValueObjectRepresentationStyleValue,
1344 custom_format);
1345 }
1346
1347 s << ']';
1348
1349 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001350 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351
1352 if ((custom_format == eFormatVectorOfChar) ||
1353 (custom_format == eFormatVectorOfFloat32) ||
1354 (custom_format == eFormatVectorOfFloat64) ||
1355 (custom_format == eFormatVectorOfSInt16) ||
1356 (custom_format == eFormatVectorOfSInt32) ||
1357 (custom_format == eFormatVectorOfSInt64) ||
1358 (custom_format == eFormatVectorOfSInt8) ||
1359 (custom_format == eFormatVectorOfUInt128) ||
1360 (custom_format == eFormatVectorOfUInt16) ||
1361 (custom_format == eFormatVectorOfUInt32) ||
1362 (custom_format == eFormatVectorOfUInt64) ||
1363 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1364 // with ASCII or any vector
1365 // format should be printed
1366 // directly
Enrico Granata86cc9822012-03-19 22:58:49 +00001367 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 const size_t count = GetNumChildren();
1369
1370 Format format = FormatManager::GetSingleItemFormat(custom_format);
1371
1372 s << '[';
1373 for (size_t low = 0; low < count; low++) {
1374
1375 if (low)
1376 s << ',';
1377
1378 ValueObjectSP child = GetChildAtIndex(low, true);
1379 if (!child.get()) {
1380 s << "<invalid child>";
1381 continue;
Enrico Granata0dba9b32014-01-08 01:36:59 +00001382 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 child->DumpPrintableRepresentation(
1384 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1385 }
1386
1387 s << ']';
1388
1389 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 }
1392
1393 if ((custom_format == eFormatBoolean) ||
1394 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1395 (custom_format == eFormatCharPrintable) ||
1396 (custom_format == eFormatComplexFloat) ||
1397 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1398 (custom_format == eFormatHexUppercase) ||
1399 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1400 (custom_format == eFormatOSType) ||
1401 (custom_format == eFormatUnicode16) ||
1402 (custom_format == eFormatUnicode32) ||
1403 (custom_format == eFormatUnsigned) ||
1404 (custom_format == eFormatPointer) ||
1405 (custom_format == eFormatComplexInteger) ||
1406 (custom_format == eFormatComplex) ||
1407 (custom_format == eFormatDefault)) // use the [] operator
1408 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001409 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410 }
1411
1412 if (only_special)
1413 return false;
1414
1415 bool var_success = false;
1416
1417 {
1418 const char *cstr = NULL;
1419
1420 // this is a local stream that we are using to ensure that the data pointed
1421 // to by cstr survives
1422 // long enough for us to copy it to its destination - it is necessary to
1423 // have this temporary storage
1424 // area for cases where our desired output is not backed by some other
1425 // longer-term storage
1426 StreamString strm;
1427
1428 if (custom_format != eFormatInvalid)
1429 SetFormat(custom_format);
1430
1431 switch (val_obj_display) {
1432 case eValueObjectRepresentationStyleValue:
1433 cstr = GetValueAsCString();
1434 break;
1435
1436 case eValueObjectRepresentationStyleSummary:
1437 cstr = GetSummaryAsCString();
1438 break;
1439
1440 case eValueObjectRepresentationStyleLanguageSpecific:
1441 cstr = GetObjectDescription();
1442 break;
1443
1444 case eValueObjectRepresentationStyleLocation:
1445 cstr = GetLocationAsCString();
1446 break;
1447
1448 case eValueObjectRepresentationStyleChildrenCount:
1449 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1450 cstr = strm.GetString().c_str();
1451 break;
1452
1453 case eValueObjectRepresentationStyleType:
1454 cstr = GetTypeName().AsCString();
1455 break;
1456
1457 case eValueObjectRepresentationStyleName:
1458 cstr = GetName().AsCString();
1459 break;
1460
1461 case eValueObjectRepresentationStyleExpressionPath:
1462 GetExpressionPath(strm, false);
1463 cstr = strm.GetString().c_str();
1464 break;
1465 }
1466
1467 if (!cstr) {
1468 if (val_obj_display == eValueObjectRepresentationStyleValue)
1469 cstr = GetSummaryAsCString();
1470 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1471 if (!CanProvideValue()) {
1472 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1473 GetLocationAsCString());
1474 cstr = strm.GetString().c_str();
1475 } else
1476 cstr = GetValueAsCString();
1477 }
1478 }
1479
1480 if (cstr)
1481 s.PutCString(cstr);
1482 else {
1483 if (m_error.Fail()) {
1484 if (do_dump_error)
1485 s.Printf("<%s>", m_error.AsCString());
1486 else
1487 return false;
1488 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1489 s.PutCString("<no summary available>");
1490 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1491 s.PutCString("<no value available>");
1492 else if (val_obj_display ==
1493 eValueObjectRepresentationStyleLanguageSpecific)
1494 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1495 // have other runtimes
1496 // that support a
1497 // description
1498 else
1499 s.PutCString("<no printable representation>");
1500 }
1501
1502 // we should only return false here if we could not do *anything*
1503 // even if we have an error message as output, that's a success
1504 // from our callers' perspective, so return true
1505 var_success = true;
1506
1507 if (custom_format != eFormatInvalid)
1508 SetFormat(eFormatDefault);
1509 }
1510
1511 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001512}
1513
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1515 AddressType *address_type) {
1516 // Can't take address of a bitfield
1517 if (IsBitfield())
Greg Clayton73b472d2010-10-27 03:32:59 +00001518 return LLDB_INVALID_ADDRESS;
Greg Clayton73b472d2010-10-27 03:32:59 +00001519
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 if (!UpdateValueIfNeeded(false))
1521 return LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +00001522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 switch (m_value.GetValueType()) {
1524 case Value::eValueTypeScalar:
1525 case Value::eValueTypeVector:
1526 if (scalar_is_load_address) {
1527 if (address_type)
1528 *address_type = eAddressTypeLoad;
1529 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001530 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 case Value::eValueTypeLoadAddress:
1534 case Value::eValueTypeFileAddress: {
Enrico Granata9128ee22011-09-06 19:20:51 +00001535 if (address_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536 *address_type = m_value.GetValueAddressType();
1537 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1538 } break;
1539 case Value::eValueTypeHostAddress: {
1540 if (address_type)
1541 *address_type = m_value.GetValueAddressType();
1542 return LLDB_INVALID_ADDRESS;
1543 } break;
1544 }
1545 if (address_type)
1546 *address_type = eAddressTypeInvalid;
1547 return LLDB_INVALID_ADDRESS;
1548}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001549
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1551 addr_t address = LLDB_INVALID_ADDRESS;
1552 if (address_type)
1553 *address_type = eAddressTypeInvalid;
1554
1555 if (!UpdateValueIfNeeded(false))
Greg Clayton737b9322010-09-13 03:32:57 +00001556 return address;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557
1558 switch (m_value.GetValueType()) {
1559 case Value::eValueTypeScalar:
1560 case Value::eValueTypeVector:
1561 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1562 break;
1563
1564 case Value::eValueTypeHostAddress:
1565 case Value::eValueTypeLoadAddress:
1566 case Value::eValueTypeFileAddress: {
1567 lldb::offset_t data_offset = 0;
1568 address = m_data.GetPointer(&data_offset);
1569 } break;
1570 }
1571
1572 if (address_type)
1573 *address_type = GetAddressTypeOfChildren();
1574
1575 return address;
Greg Clayton737b9322010-09-13 03:32:57 +00001576}
1577
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578bool ValueObject::SetValueFromCString(const char *value_str, Error &error) {
1579 error.Clear();
1580 // Make sure our value is up to date first so that our location and location
1581 // type is valid.
1582 if (!UpdateValueIfNeeded(false)) {
1583 error.SetErrorString("unable to read value");
Greg Clayton81e871e2012-02-04 02:27:34 +00001584 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 }
1586
1587 uint64_t count = 0;
1588 const Encoding encoding = GetCompilerType().GetEncoding(count);
1589
1590 const size_t byte_size = GetByteSize();
1591
1592 Value::ValueType value_type = m_value.GetValueType();
1593
1594 if (value_type == Value::eValueTypeScalar) {
1595 // If the value is already a scalar, then let the scalar change itself:
1596 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1597 } else if (byte_size <= 16) {
1598 // If the value fits in a scalar, then make a new scalar and again let the
1599 // scalar code do the conversion, then figure out where to put the new
1600 // value.
1601 Scalar new_scalar;
1602 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1603 if (error.Success()) {
1604 switch (value_type) {
1605 case Value::eValueTypeLoadAddress: {
1606 // If it is a load address, then the scalar value is the storage
1607 // location
1608 // of the data, and we have to shove this value down to that load
1609 // location.
1610 ExecutionContext exe_ctx(GetExecutionContextRef());
1611 Process *process = exe_ctx.GetProcessPtr();
1612 if (process) {
1613 addr_t target_addr =
1614 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1615 size_t bytes_written = process->WriteScalarToMemory(
1616 target_addr, new_scalar, byte_size, error);
1617 if (!error.Success())
1618 return false;
1619 if (bytes_written != byte_size) {
1620 error.SetErrorString("unable to write value to memory");
1621 return false;
1622 }
1623 }
1624 } break;
1625 case Value::eValueTypeHostAddress: {
1626 // If it is a host address, then we stuff the scalar as a DataBuffer
1627 // into the Value's data.
1628 DataExtractor new_data;
1629 new_data.SetByteOrder(m_data.GetByteOrder());
1630
1631 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1632 m_data.SetData(buffer_sp, 0);
1633 bool success = new_scalar.GetData(new_data);
1634 if (success) {
1635 new_data.CopyByteOrderedData(
1636 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1637 byte_size, m_data.GetByteOrder());
1638 }
1639 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1640
1641 } break;
1642 case Value::eValueTypeFileAddress:
1643 case Value::eValueTypeScalar:
1644 case Value::eValueTypeVector:
1645 break;
1646 }
1647 } else {
1648 return false;
1649 }
1650 } else {
1651 // We don't support setting things bigger than a scalar at present.
1652 error.SetErrorString("unable to write aggregate data type");
1653 return false;
1654 }
1655
1656 // If we have reached this point, then we have successfully changed the value.
1657 SetNeedsUpdate();
1658 return true;
Greg Clayton81e871e2012-02-04 02:27:34 +00001659}
1660
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661bool ValueObject::GetDeclaration(Declaration &decl) {
1662 decl.Clear();
1663 return false;
Greg Clayton84db9102012-03-26 23:03:23 +00001664}
1665
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666ConstString ValueObject::GetTypeName() {
1667 return GetCompilerType().GetConstTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001668}
1669
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1671
1672ConstString ValueObject::GetQualifiedTypeName() {
1673 return GetCompilerType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001674}
1675
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676LanguageType ValueObject::GetObjectRuntimeLanguage() {
1677 return GetCompilerType().GetMinimumLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001678}
1679
Kate Stoneb9c1b512016-09-06 20:57:50 +00001680void ValueObject::AddSyntheticChild(const ConstString &key,
1681 ValueObject *valobj) {
1682 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683}
1684
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1686 ValueObjectSP synthetic_child_sp;
1687 std::map<ConstString, ValueObject *>::const_iterator pos =
1688 m_synthetic_children.find(key);
1689 if (pos != m_synthetic_children.end())
1690 synthetic_child_sp = pos->second->GetSP();
1691 return synthetic_child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692}
1693
Greg Clayton2452ab72013-02-08 22:02:02 +00001694uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1696 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00001697}
1698
Kate Stoneb9c1b512016-09-06 20:57:50 +00001699bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1700
1701bool ValueObject::IsArrayType() {
1702 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703}
1704
Kate Stoneb9c1b512016-09-06 20:57:50 +00001705bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1706
1707bool ValueObject::IsIntegerType(bool &is_signed) {
1708 return GetCompilerType().IsIntegerType(is_signed);
Greg Claytondaf515f2011-07-09 20:12:33 +00001709}
1710
Kate Stoneb9c1b512016-09-06 20:57:50 +00001711bool ValueObject::IsPointerOrReferenceType() {
1712 return GetCompilerType().IsPointerOrReferenceType();
Enrico Granata9fc19442011-07-06 02:13:41 +00001713}
1714
Kate Stoneb9c1b512016-09-06 20:57:50 +00001715bool ValueObject::IsPossibleDynamicType() {
1716 ExecutionContext exe_ctx(GetExecutionContextRef());
1717 Process *process = exe_ctx.GetProcessPtr();
1718 if (process)
1719 return process->IsPossibleDynamicValue(*this);
1720 else
1721 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001722}
Greg Clayton73b472d2010-10-27 03:32:59 +00001723
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724bool ValueObject::IsRuntimeSupportValue() {
1725 Process *process(GetProcessSP().get());
1726 if (process) {
1727 LanguageRuntime *runtime =
1728 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1729 if (!runtime)
1730 runtime = process->GetObjCLanguageRuntime();
1731 if (runtime)
1732 return runtime->IsRuntimeSupportValue(*this);
1733 }
1734 return false;
Greg Clayton007d5be2011-05-30 00:49:24 +00001735}
1736
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737bool ValueObject::IsNilReference() {
1738 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1739 return language->IsNilReference(*this);
1740 }
1741 return false;
Greg Claytondea8cb42011-06-29 22:09:02 +00001742}
1743
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744bool ValueObject::IsUninitializedReference() {
1745 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1746 return language->IsUninitializedReference(*this);
1747 }
1748 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00001749}
1750
Greg Claytondaf515f2011-07-09 20:12:33 +00001751// This allows you to create an array member using and index
1752// that doesn't not fall in the normal bounds of the array.
1753// Many times structure can be defined as:
1754// struct Collection
1755// {
1756// uint32_t item_count;
1757// Item item_array[0];
1758// };
1759// The size of the "item_array" is 1, but many times in practice
1760// there are more items in "item_array".
1761
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1763 bool can_create) {
1764 ValueObjectSP synthetic_child_sp;
1765 if (IsPointerType() || IsArrayType()) {
1766 char index_str[64];
1767 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1768 ConstString index_const_str(index_str);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001769 // Check if we have already created a synthetic array member in this
1770 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001771 synthetic_child_sp = GetSyntheticChild(index_const_str);
1772 if (!synthetic_child_sp) {
1773 ValueObject *synthetic_child;
1774 // We haven't made a synthetic array member for INDEX yet, so
1775 // lets make one and cache it for any future reference.
1776 synthetic_child = CreateChildAtIndex(0, true, index);
1777
1778 // Cache the value if we got one back...
1779 if (synthetic_child) {
1780 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001781 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 synthetic_child_sp->SetName(ConstString(index_str));
1783 synthetic_child_sp->m_is_array_item_for_pointer = true;
1784 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 }
1787 return synthetic_child_sp;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001788}
1789
Kate Stoneb9c1b512016-09-06 20:57:50 +00001790ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1791 bool can_create) {
1792 ValueObjectSP synthetic_child_sp;
1793 if (IsScalarType()) {
1794 char index_str[64];
1795 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1796 ConstString index_const_str(index_str);
Enrico Granata32556cd2014-08-26 20:54:04 +00001797 // Check if we have already created a synthetic array member in this
1798 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001799 synthetic_child_sp = GetSyntheticChild(index_const_str);
1800 if (!synthetic_child_sp) {
1801 uint32_t bit_field_size = to - from + 1;
1802 uint32_t bit_field_offset = from;
1803 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1804 bit_field_offset =
1805 GetByteSize() * 8 - bit_field_size - bit_field_offset;
1806 // We haven't made a synthetic array member for INDEX yet, so
1807 // lets make one and cache it for any future reference.
1808 ValueObjectChild *synthetic_child = new ValueObjectChild(
1809 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1810 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1811 0);
1812
1813 // Cache the value if we got one back...
1814 if (synthetic_child) {
1815 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata32556cd2014-08-26 20:54:04 +00001816 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 synthetic_child_sp->SetName(ConstString(index_str));
1818 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1819 }
Enrico Granata32556cd2014-08-26 20:54:04 +00001820 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001821 }
1822 return synthetic_child_sp;
Enrico Granata32556cd2014-08-26 20:54:04 +00001823}
1824
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1826 uint32_t offset, const CompilerType &type, bool can_create,
1827 ConstString name_const_str) {
1828
1829 ValueObjectSP synthetic_child_sp;
1830
1831 if (name_const_str.IsEmpty()) {
1832 char name_str[64];
1833 snprintf(name_str, sizeof(name_str), "@%i", offset);
1834 name_const_str.SetCString(name_str);
1835 }
1836
1837 // Check if we have already created a synthetic array member in this
1838 // valid object. If we have we will re-use it.
1839 synthetic_child_sp = GetSyntheticChild(name_const_str);
1840
1841 if (synthetic_child_sp.get())
1842 return synthetic_child_sp;
1843
1844 if (!can_create)
1845 return ValueObjectSP();
1846
1847 ExecutionContext exe_ctx(GetExecutionContextRef());
1848
1849 ValueObjectChild *synthetic_child = new ValueObjectChild(
1850 *this, type, name_const_str,
1851 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1852 false, false, eAddressTypeInvalid, 0);
1853 if (synthetic_child) {
1854 AddSyntheticChild(name_const_str, synthetic_child);
1855 synthetic_child_sp = synthetic_child->GetSP();
1856 synthetic_child_sp->SetName(name_const_str);
1857 synthetic_child_sp->m_is_child_at_offset = true;
1858 }
1859 return synthetic_child_sp;
1860}
1861
1862ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1863 const CompilerType &type,
1864 bool can_create,
1865 ConstString name_const_str) {
1866 ValueObjectSP synthetic_child_sp;
1867
1868 if (name_const_str.IsEmpty()) {
1869 char name_str[128];
1870 snprintf(name_str, sizeof(name_str), "base%s@%i",
1871 type.GetTypeName().AsCString("<unknown>"), offset);
1872 name_const_str.SetCString(name_str);
1873 }
1874
1875 // Check if we have already created a synthetic array member in this
1876 // valid object. If we have we will re-use it.
1877 synthetic_child_sp = GetSyntheticChild(name_const_str);
1878
1879 if (synthetic_child_sp.get())
1880 return synthetic_child_sp;
1881
1882 if (!can_create)
1883 return ValueObjectSP();
1884
1885 const bool is_base_class = true;
1886
1887 ExecutionContext exe_ctx(GetExecutionContextRef());
1888
1889 ValueObjectChild *synthetic_child = new ValueObjectChild(
1890 *this, type, name_const_str,
1891 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1892 is_base_class, false, eAddressTypeInvalid, 0);
1893 if (synthetic_child) {
1894 AddSyntheticChild(name_const_str, synthetic_child);
1895 synthetic_child_sp = synthetic_child->GetSP();
1896 synthetic_child_sp->SetName(name_const_str);
1897 }
1898 return synthetic_child_sp;
1899}
Enrico Granata32556cd2014-08-26 20:54:04 +00001900
Enrico Granatad55546b2011-07-22 00:16:08 +00001901// your expression path needs to have a leading . or ->
1902// (unless it somehow "looks like" an array, in which case it has
1903// a leading [ symbol). while the [ is meaningful and should be shown
1904// to the user, . and -> are just parser design, but by no means
1905// added information for the user.. strip them off
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1907 if (!expression || !expression[0])
Enrico Granatad55546b2011-07-22 00:16:08 +00001908 return expression;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001909 if (expression[0] == '.')
1910 return expression + 1;
1911 if (expression[0] == '-' && expression[1] == '>')
1912 return expression + 2;
1913 return expression;
Enrico Granatad55546b2011-07-22 00:16:08 +00001914}
1915
Greg Claytonafacd142011-09-02 01:15:17 +00001916ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001917ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1918 bool can_create) {
1919 ValueObjectSP synthetic_child_sp;
1920 ConstString name_const_string(expression);
1921 // Check if we have already created a synthetic array member in this
1922 // valid object. If we have we will re-use it.
1923 synthetic_child_sp = GetSyntheticChild(name_const_string);
1924 if (!synthetic_child_sp) {
1925 // We haven't made a synthetic array member for expression yet, so
1926 // lets make one and cache it for any future reference.
1927 synthetic_child_sp = GetValueForExpressionPath(
1928 expression, NULL, NULL, NULL,
1929 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1930 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1931 None));
1932
1933 // Cache the value if we got one back...
1934 if (synthetic_child_sp.get()) {
1935 // FIXME: this causes a "real" child to end up with its name changed to
1936 // the contents of expression
1937 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1938 synthetic_child_sp->SetName(
1939 ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001940 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001941 }
1942 return synthetic_child_sp;
Enrico Granatad55546b2011-07-22 00:16:08 +00001943}
1944
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
1946 if (use_synthetic == false)
1947 return;
1948
1949 TargetSP target_sp(GetTargetSP());
1950 if (target_sp && target_sp->GetEnableSyntheticValue() == false) {
1951 m_synthetic_value = NULL;
1952 return;
1953 }
1954
1955 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1956
1957 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1958 return;
1959
1960 if (m_synthetic_children_sp.get() == NULL)
1961 return;
1962
1963 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1964 return;
1965
1966 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1967}
1968
1969void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1970 if (use_dynamic == eNoDynamicValues)
1971 return;
1972
1973 if (!m_dynamic_value && !IsDynamic()) {
1974 ExecutionContext exe_ctx(GetExecutionContextRef());
1975 Process *process = exe_ctx.GetProcessPtr();
1976 if (process && process->IsPossibleDynamicValue(*this)) {
1977 ClearDynamicTypeInformation();
1978 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
Enrico Granatac5bc4122012-03-27 02:35:13 +00001979 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 }
Enrico Granatad55546b2011-07-22 00:16:08 +00001981}
1982
Kate Stoneb9c1b512016-09-06 20:57:50 +00001983ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1984 if (use_dynamic == eNoDynamicValues)
1985 return ValueObjectSP();
1986
1987 if (!IsDynamic() && m_dynamic_value == NULL) {
1988 CalculateDynamicValue(use_dynamic);
1989 }
1990 if (m_dynamic_value)
1991 return m_dynamic_value->GetSP();
1992 else
1993 return ValueObjectSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001994}
1995
Kate Stoneb9c1b512016-09-06 20:57:50 +00001996ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1997
1998lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1999
2000ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
2001 if (use_synthetic == false)
2002 return ValueObjectSP();
2003
2004 CalculateSyntheticValue(use_synthetic);
2005
2006 if (m_synthetic_value)
2007 return m_synthetic_value->GetSP();
2008 else
2009 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002010}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002011
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012bool ValueObject::HasSyntheticValue() {
2013 UpdateFormatsIfNeeded();
Jim Ingham60dbabb2011-12-08 19:44:08 +00002014
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 if (m_synthetic_children_sp.get() == NULL)
2016 return false;
Enrico Granata886147f2012-05-08 18:47:08 +00002017
Kate Stoneb9c1b512016-09-06 20:57:50 +00002018 CalculateSyntheticValue(true);
Enrico Granata86cc9822012-03-19 22:58:49 +00002019
Kate Stoneb9c1b512016-09-06 20:57:50 +00002020 if (m_synthetic_value)
2021 return true;
2022 else
Greg Claytone221f822011-01-21 01:59:00 +00002023 return false;
2024}
2025
Kate Stoneb9c1b512016-09-06 20:57:50 +00002026bool ValueObject::GetBaseClassPath(Stream &s) {
2027 if (IsBaseClass()) {
2028 bool parent_had_base_class =
2029 GetParent() && GetParent()->GetBaseClassPath(s);
2030 CompilerType compiler_type = GetCompilerType();
2031 std::string cxx_class_name;
2032 bool this_had_base_class =
2033 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2034 if (this_had_base_class) {
2035 if (parent_had_base_class)
2036 s.PutCString("::");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00002037 s.PutCString(cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002038 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002039 return parent_had_base_class || this_had_base_class;
2040 }
2041 return false;
Greg Claytone221f822011-01-21 01:59:00 +00002042}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002043
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044ValueObject *ValueObject::GetNonBaseClassParent() {
2045 if (GetParent()) {
2046 if (GetParent()->IsBaseClass())
2047 return GetParent()->GetNonBaseClassParent();
2048 else
2049 return GetParent();
2050 }
2051 return NULL;
2052}
Enrico Granataa3c8f042014-08-19 22:29:08 +00002053
Kate Stoneb9c1b512016-09-06 20:57:50 +00002054bool ValueObject::IsBaseClass(uint32_t &depth) {
2055 if (!IsBaseClass()) {
2056 depth = 0;
2057 return false;
2058 }
2059 if (GetParent()) {
2060 GetParent()->IsBaseClass(depth);
2061 depth = depth + 1;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002062 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002063 }
2064 // TODO: a base of no parent? weird..
2065 depth = 1;
2066 return true;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002067}
2068
Kate Stoneb9c1b512016-09-06 20:57:50 +00002069void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2070 GetExpressionPathFormat epformat) {
2071 // synthetic children do not actually "exist" as part of the hierarchy, and
2072 // sometimes they are consed up in ways
2073 // that don't make sense from an underlying language/API standpoint. So, use a
2074 // special code path here to return
2075 // something that can hopefully be used in expression
2076 if (m_is_synthetic_children_generated) {
2077 UpdateValueIfNeeded();
2078
2079 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2080 if (IsPointerOrReferenceType()) {
2081 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2082 GetValueAsUnsigned(0));
Enrico Granata986fa5f2014-12-09 21:41:16 +00002083 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002084 } else {
2085 uint64_t load_addr =
2086 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2087 if (load_addr != LLDB_INVALID_ADDRESS) {
2088 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2089 load_addr);
2090 return;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002092 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094
2095 if (CanProvideValue()) {
2096 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2097 GetValueAsCString());
2098 return;
Enrico Granata4becb372011-06-29 22:27:15 +00002099 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002100
2101 return;
2102 }
2103
2104 const bool is_deref_of_parent = IsDereferenceOfParent();
2105
2106 if (is_deref_of_parent &&
2107 epformat == eGetExpressionPathFormatDereferencePointers) {
2108 // this is the original format of GetExpressionPath() producing code like
2109 // *(a_ptr).memberName, which is entirely
2110 // fine, until you put this into
2111 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
2112 // a_ptr->memberName.
2113 // the eHonorPointers mode is meant to produce strings in this latter format
2114 s.PutCString("*(");
2115 }
2116
2117 ValueObject *parent = GetParent();
2118
2119 if (parent)
2120 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2121
2122 // if we are a deref_of_parent just because we are synthetic array
2123 // members made up to allow ptr[%d] syntax to work in variable
2124 // printing, then add our name ([%d]) to the expression path
2125 if (m_is_array_item_for_pointer &&
2126 epformat == eGetExpressionPathFormatHonorPointers)
2127 s.PutCString(m_name.AsCString());
2128
2129 if (!IsBaseClass()) {
2130 if (!is_deref_of_parent) {
2131 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2132 if (non_base_class_parent &&
2133 !non_base_class_parent->GetName().IsEmpty()) {
2134 CompilerType non_base_class_parent_compiler_type =
2135 non_base_class_parent->GetCompilerType();
2136 if (non_base_class_parent_compiler_type) {
2137 if (parent && parent->IsDereferenceOfParent() &&
2138 epformat == eGetExpressionPathFormatHonorPointers) {
2139 s.PutCString("->");
2140 } else {
2141 const uint32_t non_base_class_parent_type_info =
2142 non_base_class_parent_compiler_type.GetTypeInfo();
2143
2144 if (non_base_class_parent_type_info & eTypeIsPointer) {
2145 s.PutCString("->");
2146 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2147 !(non_base_class_parent_type_info & eTypeIsArray)) {
2148 s.PutChar('.');
2149 }
2150 }
2151 }
2152 }
2153
2154 const char *name = GetName().GetCString();
2155 if (name) {
2156 if (qualify_cxx_base_classes) {
2157 if (GetBaseClassPath(s))
2158 s.PutCString("::");
2159 }
2160 s.PutCString(name);
2161 }
2162 }
2163 }
2164
2165 if (is_deref_of_parent &&
2166 epformat == eGetExpressionPathFormatDereferencePointers) {
2167 s.PutChar(')');
2168 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002169}
2170
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171ValueObjectSP ValueObject::GetValueForExpressionPath(
2172 const char *expression, const char **first_unparsed,
2173 ExpressionPathScanEndReason *reason_to_stop,
2174 ExpressionPathEndResultType *final_value_type,
2175 const GetValueForExpressionPathOptions &options,
2176 ExpressionPathAftermath *final_task_on_target) {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002177
Kate Stoneb9c1b512016-09-06 20:57:50 +00002178 const char *dummy_first_unparsed;
2179 ExpressionPathScanEndReason dummy_reason_to_stop =
2180 ValueObject::eExpressionPathScanEndReasonUnknown;
2181 ExpressionPathEndResultType dummy_final_value_type =
2182 ValueObject::eExpressionPathEndResultTypeInvalid;
2183 ExpressionPathAftermath dummy_final_task_on_target =
2184 ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002185
Kate Stoneb9c1b512016-09-06 20:57:50 +00002186 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2187 expression, first_unparsed ? first_unparsed : &dummy_first_unparsed,
2188 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2189 final_value_type ? final_value_type : &dummy_final_value_type, options,
2190 final_task_on_target ? final_task_on_target
2191 : &dummy_final_task_on_target);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002192
Kate Stoneb9c1b512016-09-06 20:57:50 +00002193 if (!final_task_on_target ||
2194 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2195 return ret_val;
2196
2197 if (ret_val.get() &&
2198 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2199 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2200 // of plain objects
2201 {
2202 if ((final_task_on_target ? *final_task_on_target
2203 : dummy_final_task_on_target) ==
2204 ValueObject::eExpressionPathAftermathDereference) {
2205 Error error;
2206 ValueObjectSP final_value = ret_val->Dereference(error);
2207 if (error.Fail() || !final_value.get()) {
2208 if (reason_to_stop)
2209 *reason_to_stop =
2210 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2211 if (final_value_type)
2212 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002213 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002214 } else {
2215 if (final_task_on_target)
2216 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2217 return final_value;
2218 }
2219 }
2220 if (*final_task_on_target ==
2221 ValueObject::eExpressionPathAftermathTakeAddress) {
2222 Error error;
2223 ValueObjectSP final_value = ret_val->AddressOf(error);
2224 if (error.Fail() || !final_value.get()) {
2225 if (reason_to_stop)
2226 *reason_to_stop =
2227 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2228 if (final_value_type)
2229 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2230 return ValueObjectSP();
2231 } else {
2232 if (final_task_on_target)
2233 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2234 return final_value;
2235 }
2236 }
2237 }
2238 return ret_val; // final_task_on_target will still have its original value, so
2239 // you know I did not do it
2240}
2241
2242int ValueObject::GetValuesForExpressionPath(
2243 const char *expression, ValueObjectListSP &list,
2244 const char **first_unparsed, ExpressionPathScanEndReason *reason_to_stop,
2245 ExpressionPathEndResultType *final_value_type,
2246 const GetValueForExpressionPathOptions &options,
2247 ExpressionPathAftermath *final_task_on_target) {
2248 const char *dummy_first_unparsed;
2249 ExpressionPathScanEndReason dummy_reason_to_stop;
2250 ExpressionPathEndResultType dummy_final_value_type;
2251 ExpressionPathAftermath dummy_final_task_on_target =
2252 ValueObject::eExpressionPathAftermathNothing;
2253
2254 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2255 expression, first_unparsed ? first_unparsed : &dummy_first_unparsed,
2256 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2257 final_value_type ? final_value_type : &dummy_final_value_type, options,
2258 final_task_on_target ? final_task_on_target
2259 : &dummy_final_task_on_target);
2260
2261 if (!ret_val.get()) // if there are errors, I add nothing to the list
2262 return 0;
2263
2264 if ((reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) !=
2265 eExpressionPathScanEndReasonArrayRangeOperatorMet) {
2266 // I need not expand a range, just post-process the final value and return
2267 if (!final_task_on_target ||
2268 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing) {
2269 list->Append(ret_val);
2270 return 1;
2271 }
2272 if (ret_val.get() &&
2273 (final_value_type ? *final_value_type : dummy_final_value_type) ==
2274 eExpressionPathEndResultTypePlain) // I can only deref and
2275 // takeaddress of plain objects
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002276 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002277 if (*final_task_on_target ==
2278 ValueObject::eExpressionPathAftermathDereference) {
2279 Error error;
2280 ValueObjectSP final_value = ret_val->Dereference(error);
2281 if (error.Fail() || !final_value.get()) {
2282 if (reason_to_stop)
2283 *reason_to_stop =
2284 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2285 if (final_value_type)
2286 *final_value_type =
2287 ValueObject::eExpressionPathEndResultTypeInvalid;
2288 return 0;
2289 } else {
2290 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2291 list->Append(final_value);
2292 return 1;
2293 }
2294 }
2295 if (*final_task_on_target ==
2296 ValueObject::eExpressionPathAftermathTakeAddress) {
2297 Error error;
2298 ValueObjectSP final_value = ret_val->AddressOf(error);
2299 if (error.Fail() || !final_value.get()) {
2300 if (reason_to_stop)
2301 *reason_to_stop =
2302 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2303 if (final_value_type)
2304 *final_value_type =
2305 ValueObject::eExpressionPathEndResultTypeInvalid;
2306 return 0;
2307 } else {
2308 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2309 list->Append(final_value);
2310 return 1;
2311 }
2312 }
2313 }
2314 } else {
2315 return ExpandArraySliceExpression(
2316 first_unparsed ? *first_unparsed : dummy_first_unparsed,
2317 first_unparsed ? first_unparsed : &dummy_first_unparsed, ret_val, list,
2318 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2319 final_value_type ? final_value_type : &dummy_final_value_type, options,
2320 final_task_on_target ? final_task_on_target
2321 : &dummy_final_task_on_target);
2322 }
2323 // in any non-covered case, just do the obviously right thing
2324 list->Append(ret_val);
2325 return 1;
2326}
2327
2328ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
2329 const char *expression_cstr, const char **first_unparsed,
2330 ExpressionPathScanEndReason *reason_to_stop,
2331 ExpressionPathEndResultType *final_result,
2332 const GetValueForExpressionPathOptions &options,
2333 ExpressionPathAftermath *what_next) {
2334 ValueObjectSP root = GetSP();
2335
2336 if (!root.get())
2337 return ValueObjectSP();
2338
2339 *first_unparsed = expression_cstr;
2340
2341 while (true) {
2342
2343 const char *expression_cstr =
2344 *first_unparsed; // hide the top level expression_cstr
2345
2346 CompilerType root_compiler_type = root->GetCompilerType();
2347 CompilerType pointee_compiler_type;
2348 Flags pointee_compiler_type_info;
2349
2350 Flags root_compiler_type_info(
2351 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2352 if (pointee_compiler_type)
2353 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2354
2355 if (!expression_cstr || *expression_cstr == '\0') {
2356 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2357 return root;
2358 }
2359
2360 switch (*expression_cstr) {
2361 case '-': {
2362 if (options.m_check_dot_vs_arrow_syntax &&
2363 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2364 // use -> on a
2365 // non-pointer and I
2366 // must catch the error
2367 {
2368 *first_unparsed = expression_cstr;
2369 *reason_to_stop =
2370 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2371 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2372 return ValueObjectSP();
2373 }
2374 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2375 // extract an ObjC IVar
2376 // when this is forbidden
2377 root_compiler_type_info.Test(eTypeIsPointer) &&
2378 options.m_no_fragile_ivar) {
2379 *first_unparsed = expression_cstr;
2380 *reason_to_stop =
2381 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2382 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2383 return ValueObjectSP();
2384 }
2385 if (expression_cstr[1] != '>') {
2386 *first_unparsed = expression_cstr;
2387 *reason_to_stop =
2388 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2389 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2390 return ValueObjectSP();
2391 }
2392 expression_cstr++; // skip the -
2393 }
2394 LLVM_FALLTHROUGH;
2395 case '.': // or fallthrough from ->
2396 {
2397 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
2398 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2399 // use . on a pointer
2400 // and I must catch the
2401 // error
2402 {
2403 *first_unparsed = expression_cstr;
2404 *reason_to_stop =
2405 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2406 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2407 return ValueObjectSP();
2408 }
2409 expression_cstr++; // skip .
2410 const char *next_separator = strpbrk(expression_cstr + 1, "-.[");
2411 ConstString child_name;
2412 if (!next_separator) // if no other separator just expand this last layer
2413 {
2414 child_name.SetCString(expression_cstr);
2415 ValueObjectSP child_valobj_sp =
2416 root->GetChildMemberWithName(child_name, true);
2417
2418 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002419 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002420 *first_unparsed = "";
2421 *reason_to_stop =
2422 ValueObject::eExpressionPathScanEndReasonEndOfString;
2423 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2424 return child_valobj_sp;
2425 } else {
2426 switch (options.m_synthetic_children_traversal) {
2427 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2428 None:
2429 break;
2430 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2431 FromSynthetic:
2432 if (root->IsSynthetic()) {
2433 child_valobj_sp = root->GetNonSyntheticValue();
2434 if (child_valobj_sp.get())
2435 child_valobj_sp =
2436 child_valobj_sp->GetChildMemberWithName(child_name, true);
2437 }
2438 break;
2439 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2440 ToSynthetic:
2441 if (!root->IsSynthetic()) {
2442 child_valobj_sp = root->GetSyntheticValue();
2443 if (child_valobj_sp.get())
2444 child_valobj_sp =
2445 child_valobj_sp->GetChildMemberWithName(child_name, true);
2446 }
2447 break;
2448 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2449 Both:
2450 if (root->IsSynthetic()) {
2451 child_valobj_sp = root->GetNonSyntheticValue();
2452 if (child_valobj_sp.get())
2453 child_valobj_sp =
2454 child_valobj_sp->GetChildMemberWithName(child_name, true);
2455 } else {
2456 child_valobj_sp = root->GetSyntheticValue();
2457 if (child_valobj_sp.get())
2458 child_valobj_sp =
2459 child_valobj_sp->GetChildMemberWithName(child_name, true);
2460 }
2461 break;
2462 }
2463 }
2464
2465 // if we are here and options.m_no_synthetic_children is true,
2466 // child_valobj_sp is going to be a NULL SP,
2467 // so we hit the "else" branch, and return an error
2468 if (child_valobj_sp.get()) // if it worked, just return
2469 {
2470 *first_unparsed = "";
2471 *reason_to_stop =
2472 ValueObject::eExpressionPathScanEndReasonEndOfString;
2473 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2474 return child_valobj_sp;
2475 } else {
2476 *first_unparsed = expression_cstr;
2477 *reason_to_stop =
2478 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2479 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2480 return ValueObjectSP();
2481 }
2482 } else // other layers do expand
2483 {
2484 child_name.SetCStringWithLength(expression_cstr,
2485 next_separator - expression_cstr);
2486 ValueObjectSP child_valobj_sp =
2487 root->GetChildMemberWithName(child_name, true);
2488 if (child_valobj_sp.get()) // store the new root and move on
2489 {
2490 root = child_valobj_sp;
2491 *first_unparsed = next_separator;
2492 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2493 continue;
2494 } else {
2495 switch (options.m_synthetic_children_traversal) {
2496 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2497 None:
2498 break;
2499 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2500 FromSynthetic:
2501 if (root->IsSynthetic()) {
2502 child_valobj_sp = root->GetNonSyntheticValue();
2503 if (child_valobj_sp.get())
2504 child_valobj_sp =
2505 child_valobj_sp->GetChildMemberWithName(child_name, true);
2506 }
2507 break;
2508 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2509 ToSynthetic:
2510 if (!root->IsSynthetic()) {
2511 child_valobj_sp = root->GetSyntheticValue();
2512 if (child_valobj_sp.get())
2513 child_valobj_sp =
2514 child_valobj_sp->GetChildMemberWithName(child_name, true);
2515 }
2516 break;
2517 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2518 Both:
2519 if (root->IsSynthetic()) {
2520 child_valobj_sp = root->GetNonSyntheticValue();
2521 if (child_valobj_sp.get())
2522 child_valobj_sp =
2523 child_valobj_sp->GetChildMemberWithName(child_name, true);
2524 } else {
2525 child_valobj_sp = root->GetSyntheticValue();
2526 if (child_valobj_sp.get())
2527 child_valobj_sp =
2528 child_valobj_sp->GetChildMemberWithName(child_name, true);
2529 }
2530 break;
2531 }
2532 }
2533
2534 // if we are here and options.m_no_synthetic_children is true,
2535 // child_valobj_sp is going to be a NULL SP,
2536 // so we hit the "else" branch, and return an error
2537 if (child_valobj_sp.get()) // if it worked, move on
2538 {
2539 root = child_valobj_sp;
2540 *first_unparsed = next_separator;
2541 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2542 continue;
2543 } else {
2544 *first_unparsed = expression_cstr;
2545 *reason_to_stop =
2546 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2547 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2548 return ValueObjectSP();
2549 }
2550 }
2551 break;
2552 }
2553 case '[': {
2554 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2555 !root_compiler_type_info.Test(eTypeIsPointer) &&
2556 !root_compiler_type_info.Test(
2557 eTypeIsVector)) // if this is not a T[] nor a T*
2558 {
2559 if (!root_compiler_type_info.Test(
2560 eTypeIsScalar)) // if this is not even a scalar...
2561 {
2562 if (options.m_synthetic_children_traversal ==
2563 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2564 None) // ...only chance left is synthetic
2565 {
2566 *first_unparsed = expression_cstr;
2567 *reason_to_stop =
2568 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2569 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2570 return ValueObjectSP();
2571 }
2572 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2573 // check that we can
2574 // expand bitfields
2575 {
2576 *first_unparsed = expression_cstr;
2577 *reason_to_stop =
2578 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2579 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2580 return ValueObjectSP();
2581 }
2582 }
2583 if (*(expression_cstr + 1) ==
2584 ']') // if this is an unbounded range it only works for arrays
2585 {
2586 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2587 *first_unparsed = expression_cstr;
2588 *reason_to_stop =
2589 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2590 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2591 return ValueObjectSP();
2592 } else // even if something follows, we cannot expand unbounded ranges,
2593 // just let the caller do it
2594 {
2595 *first_unparsed = expression_cstr + 2;
2596 *reason_to_stop =
2597 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2598 *final_result =
2599 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2600 return root;
2601 }
2602 }
2603 const char *separator_position = ::strchr(expression_cstr + 1, '-');
2604 const char *close_bracket_position = ::strchr(expression_cstr + 1, ']');
2605 if (!close_bracket_position) // if there is no ], this is a syntax error
2606 {
2607 *first_unparsed = expression_cstr;
2608 *reason_to_stop =
2609 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2610 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2611 return ValueObjectSP();
2612 }
2613 if (!separator_position ||
2614 separator_position > close_bracket_position) // if no separator, this
2615 // is either [] or [N]
2616 {
2617 char *end = NULL;
2618 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
2619 if (!end || end != close_bracket_position) // if something weird is in
2620 // our way return an error
2621 {
2622 *first_unparsed = expression_cstr;
2623 *reason_to_stop =
2624 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2625 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2626 return ValueObjectSP();
2627 }
2628 if (end - expression_cstr ==
2629 1) // if this is [], only return a valid value for arrays
2630 {
2631 if (root_compiler_type_info.Test(eTypeIsArray)) {
2632 *first_unparsed = expression_cstr + 2;
2633 *reason_to_stop =
2634 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2635 *final_result =
2636 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002637 return root;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002638 } else {
2639 *first_unparsed = expression_cstr;
2640 *reason_to_stop =
2641 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2642 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2643 return ValueObjectSP();
2644 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002645 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002646 // from here on we do have a valid index
2647 if (root_compiler_type_info.Test(eTypeIsArray)) {
2648 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2649 if (!child_valobj_sp)
2650 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2651 if (!child_valobj_sp)
2652 if (root->HasSyntheticValue() &&
2653 root->GetSyntheticValue()->GetNumChildren() > index)
2654 child_valobj_sp =
2655 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2656 if (child_valobj_sp) {
2657 root = child_valobj_sp;
2658 *first_unparsed = end + 1; // skip ]
2659 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2660 continue;
2661 } else {
2662 *first_unparsed = expression_cstr;
2663 *reason_to_stop =
2664 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2665 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2666 return ValueObjectSP();
2667 }
2668 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2669 if (*what_next ==
2670 ValueObject::
2671 eExpressionPathAftermathDereference && // if this is a
2672 // ptr-to-scalar, I
2673 // am accessing it
2674 // by index and I
2675 // would have
2676 // deref'ed anyway,
2677 // then do it now
2678 // and use this as
2679 // a bitfield
2680 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2681 Error error;
2682 root = root->Dereference(error);
2683 if (error.Fail() || !root.get()) {
2684 *first_unparsed = expression_cstr;
2685 *reason_to_stop =
2686 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2687 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2688 return ValueObjectSP();
2689 } else {
2690 *what_next = eExpressionPathAftermathNothing;
2691 continue;
2692 }
2693 } else {
2694 if (root->GetCompilerType().GetMinimumLanguage() ==
2695 eLanguageTypeObjC &&
2696 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2697 root->HasSyntheticValue() &&
2698 (options.m_synthetic_children_traversal ==
2699 GetValueForExpressionPathOptions::
2700 SyntheticChildrenTraversal::ToSynthetic ||
2701 options.m_synthetic_children_traversal ==
2702 GetValueForExpressionPathOptions::
2703 SyntheticChildrenTraversal::Both)) {
2704 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2705 } else
2706 root = root->GetSyntheticArrayMember(index, true);
2707 if (!root.get()) {
2708 *first_unparsed = expression_cstr;
2709 *reason_to_stop =
2710 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2711 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2712 return ValueObjectSP();
2713 } else {
2714 *first_unparsed = end + 1; // skip ]
2715 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2716 continue;
2717 }
2718 }
2719 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2720 root = root->GetSyntheticBitFieldChild(index, index, true);
2721 if (!root.get()) {
2722 *first_unparsed = expression_cstr;
2723 *reason_to_stop =
2724 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2725 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2726 return ValueObjectSP();
2727 } else // we do not know how to expand members of bitfields, so we
2728 // just return and let the caller do any further processing
2729 {
2730 *first_unparsed = end + 1; // skip ]
2731 *reason_to_stop = ValueObject::
2732 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2733 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2734 return root;
2735 }
2736 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2737 root = root->GetChildAtIndex(index, true);
2738 if (!root.get()) {
2739 *first_unparsed = expression_cstr;
2740 *reason_to_stop =
2741 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2742 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2743 return ValueObjectSP();
2744 } else {
2745 *first_unparsed = end + 1; // skip ]
2746 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2747 continue;
2748 }
2749 } else if (options.m_synthetic_children_traversal ==
2750 GetValueForExpressionPathOptions::
2751 SyntheticChildrenTraversal::ToSynthetic ||
2752 options.m_synthetic_children_traversal ==
2753 GetValueForExpressionPathOptions::
2754 SyntheticChildrenTraversal::Both) {
2755 if (root->HasSyntheticValue())
2756 root = root->GetSyntheticValue();
2757 else if (!root->IsSynthetic()) {
2758 *first_unparsed = expression_cstr;
2759 *reason_to_stop =
2760 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2761 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2762 return ValueObjectSP();
2763 }
2764 // if we are here, then root itself is a synthetic VO.. should be good
2765 // to go
2766
2767 if (!root.get()) {
2768 *first_unparsed = expression_cstr;
2769 *reason_to_stop =
2770 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2771 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2772 return ValueObjectSP();
2773 }
2774 root = root->GetChildAtIndex(index, true);
2775 if (!root.get()) {
2776 *first_unparsed = expression_cstr;
2777 *reason_to_stop =
2778 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2779 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2780 return ValueObjectSP();
2781 } else {
2782 *first_unparsed = end + 1; // skip ]
2783 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2784 continue;
2785 }
2786 } else {
2787 *first_unparsed = expression_cstr;
2788 *reason_to_stop =
2789 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2790 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2791 return ValueObjectSP();
2792 }
2793 } else // we have a low and a high index
2794 {
2795 char *end = NULL;
2796 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
2797 if (!end || end != separator_position) // if something weird is in our
2798 // way return an error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002799 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002800 *first_unparsed = expression_cstr;
2801 *reason_to_stop =
2802 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2803 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2804 return ValueObjectSP();
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002805 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002806 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
2807 if (!end || end != close_bracket_position) // if something weird is in
2808 // our way return an error
2809 {
2810 *first_unparsed = expression_cstr;
2811 *reason_to_stop =
2812 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2813 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2814 return ValueObjectSP();
2815 }
2816 if (index_lower > index_higher) // swap indices if required
2817 {
2818 unsigned long temp = index_lower;
2819 index_lower = index_higher;
2820 index_higher = temp;
2821 }
2822 if (root_compiler_type_info.Test(
2823 eTypeIsScalar)) // expansion only works for scalars
2824 {
2825 root =
2826 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2827 if (!root.get()) {
2828 *first_unparsed = expression_cstr;
2829 *reason_to_stop =
2830 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2831 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2832 return ValueObjectSP();
2833 } else {
2834 *first_unparsed = end + 1; // skip ]
2835 *reason_to_stop = ValueObject::
2836 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2837 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2838 return root;
2839 }
2840 } else if (root_compiler_type_info.Test(
2841 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2842 // accessing it by index and I would
2843 // have deref'ed anyway, then do it
2844 // now and use this as a bitfield
2845 *what_next ==
2846 ValueObject::eExpressionPathAftermathDereference &&
2847 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2848 Error error;
2849 root = root->Dereference(error);
2850 if (error.Fail() || !root.get()) {
2851 *first_unparsed = expression_cstr;
2852 *reason_to_stop =
2853 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2854 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2855 return ValueObjectSP();
2856 } else {
2857 *what_next = ValueObject::eExpressionPathAftermathNothing;
2858 continue;
2859 }
2860 } else {
2861 *first_unparsed = expression_cstr;
2862 *reason_to_stop =
2863 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2864 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2865 return root;
2866 }
2867 }
2868 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002869 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 default: // some non-separator is in the way
2871 {
2872 *first_unparsed = expression_cstr;
2873 *reason_to_stop =
2874 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2875 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2876 return ValueObjectSP();
2877 break;
2878 }
2879 }
2880 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002881}
2882
Kate Stoneb9c1b512016-09-06 20:57:50 +00002883int ValueObject::ExpandArraySliceExpression(
2884 const char *expression_cstr, const char **first_unparsed,
2885 ValueObjectSP root, ValueObjectListSP &list,
2886 ExpressionPathScanEndReason *reason_to_stop,
2887 ExpressionPathEndResultType *final_result,
2888 const GetValueForExpressionPathOptions &options,
2889 ExpressionPathAftermath *what_next) {
2890 if (!root.get())
2891 return 0;
2892
2893 *first_unparsed = expression_cstr;
2894
2895 while (true) {
2896
2897 const char *expression_cstr =
2898 *first_unparsed; // hide the top level expression_cstr
2899
2900 CompilerType root_compiler_type = root->GetCompilerType();
2901 CompilerType pointee_compiler_type;
2902 Flags pointee_compiler_type_info;
2903 Flags root_compiler_type_info(
2904 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2905 if (pointee_compiler_type)
2906 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2907
2908 if (!expression_cstr || *expression_cstr == '\0') {
2909 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2910 list->Append(root);
2911 return 1;
2912 }
2913
2914 switch (*expression_cstr) {
2915 case '[': {
2916 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2917 !root_compiler_type_info.Test(
2918 eTypeIsPointer)) // if this is not a T[] nor a T*
2919 {
2920 if (!root_compiler_type_info.Test(eTypeIsScalar)) // if this is not even
2921 // a scalar, this
2922 // syntax is just
2923 // plain wrong!
2924 {
2925 *first_unparsed = expression_cstr;
2926 *reason_to_stop =
2927 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2928 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2929 return 0;
2930 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2931 // check that we can
2932 // expand bitfields
2933 {
2934 *first_unparsed = expression_cstr;
2935 *reason_to_stop =
2936 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2937 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2938 return 0;
2939 }
2940 }
2941 if (*(expression_cstr + 1) ==
2942 ']') // if this is an unbounded range it only works for arrays
2943 {
2944 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2945 *first_unparsed = expression_cstr;
2946 *reason_to_stop =
2947 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2948 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2949 return 0;
2950 } else // expand this into list
2951 {
2952 const size_t max_index = root->GetNumChildren() - 1;
2953 for (size_t index = 0; index < max_index; index++) {
2954 ValueObjectSP child = root->GetChildAtIndex(index, true);
2955 list->Append(child);
2956 }
2957 *first_unparsed = expression_cstr + 2;
2958 *reason_to_stop =
2959 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2960 *final_result =
2961 ValueObject::eExpressionPathEndResultTypeValueObjectList;
2962 return max_index; // tell me number of items I added to the VOList
2963 }
2964 }
2965 const char *separator_position = ::strchr(expression_cstr + 1, '-');
2966 const char *close_bracket_position = ::strchr(expression_cstr + 1, ']');
2967 if (!close_bracket_position) // if there is no ], this is a syntax error
2968 {
2969 *first_unparsed = expression_cstr;
2970 *reason_to_stop =
2971 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2972 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002973 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974 }
2975 if (!separator_position ||
2976 separator_position > close_bracket_position) // if no separator, this
2977 // is either [] or [N]
2978 {
2979 char *end = NULL;
2980 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
2981 if (!end || end != close_bracket_position) // if something weird is in
2982 // our way return an error
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002983 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984 *first_unparsed = expression_cstr;
2985 *reason_to_stop =
2986 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2987 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2988 return 0;
2989 }
2990 if (end - expression_cstr ==
2991 1) // if this is [], only return a valid value for arrays
2992 {
2993 if (root_compiler_type_info.Test(eTypeIsArray)) {
2994 const size_t max_index = root->GetNumChildren() - 1;
2995 for (size_t index = 0; index < max_index; index++) {
2996 ValueObjectSP child = root->GetChildAtIndex(index, true);
2997 list->Append(child);
2998 }
2999 *first_unparsed = expression_cstr + 2;
3000 *reason_to_stop =
3001 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3002 *final_result =
3003 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3004 return max_index; // tell me number of items I added to the VOList
3005 } else {
3006 *first_unparsed = expression_cstr;
3007 *reason_to_stop =
3008 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3009 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3010 return 0;
3011 }
3012 }
3013 // from here on we do have a valid index
3014 if (root_compiler_type_info.Test(eTypeIsArray)) {
3015 root = root->GetChildAtIndex(index, true);
3016 if (!root.get()) {
3017 *first_unparsed = expression_cstr;
3018 *reason_to_stop =
3019 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3020 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3021 return 0;
3022 } else {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003023 list->Append(root);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003024 *first_unparsed = end + 1; // skip ]
3025 *reason_to_stop =
3026 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3027 *final_result =
3028 ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003029 return 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003030 }
3031 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
3032 if (*what_next ==
3033 ValueObject::
3034 eExpressionPathAftermathDereference && // if this is a
3035 // ptr-to-scalar, I
3036 // am accessing it
3037 // by index and I
3038 // would have
3039 // deref'ed anyway,
3040 // then do it now
3041 // and use this as
3042 // a bitfield
3043 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3044 Error error;
3045 root = root->Dereference(error);
3046 if (error.Fail() || !root.get()) {
3047 *first_unparsed = expression_cstr;
3048 *reason_to_stop =
3049 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3050 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3051 return 0;
3052 } else {
3053 *what_next = eExpressionPathAftermathNothing;
3054 continue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003055 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003056 } else {
3057 root = root->GetSyntheticArrayMember(index, true);
3058 if (!root.get()) {
3059 *first_unparsed = expression_cstr;
3060 *reason_to_stop =
3061 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3062 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3063 return 0;
3064 } else {
3065 list->Append(root);
3066 *first_unparsed = end + 1; // skip ]
3067 *reason_to_stop = ValueObject::
3068 eExpressionPathScanEndReasonRangeOperatorExpanded;
3069 *final_result =
3070 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3071 return 1;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003072 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003073 }
3074 } else /*if (ClangASTContext::IsScalarType(root_compiler_type))*/
Enrico Granata9f1e2042012-04-24 22:15:37 +00003075 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003076 root = root->GetSyntheticBitFieldChild(index, index, true);
3077 if (!root.get()) {
3078 *first_unparsed = expression_cstr;
3079 *reason_to_stop =
3080 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3081 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3082 return 0;
3083 } else // we do not know how to expand members of bitfields, so we
3084 // just return and let the caller do any further processing
3085 {
3086 list->Append(root);
3087 *first_unparsed = end + 1; // skip ]
3088 *reason_to_stop =
3089 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3090 *final_result =
3091 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3092 return 1;
3093 }
Enrico Granata9f1e2042012-04-24 22:15:37 +00003094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003095 } else // we have a low and a high index
3096 {
3097 char *end = NULL;
3098 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
3099 if (!end || end != separator_position) // if something weird is in our
3100 // way return an error
3101 {
3102 *first_unparsed = expression_cstr;
3103 *reason_to_stop =
3104 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3105 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3106 return 0;
3107 }
3108 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
3109 if (!end || end != close_bracket_position) // if something weird is in
3110 // our way return an error
3111 {
3112 *first_unparsed = expression_cstr;
3113 *reason_to_stop =
3114 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3115 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3116 return 0;
3117 }
3118 if (index_lower > index_higher) // swap indices if required
3119 {
3120 unsigned long temp = index_lower;
3121 index_lower = index_higher;
3122 index_higher = temp;
3123 }
3124 if (root_compiler_type_info.Test(
3125 eTypeIsScalar)) // expansion only works for scalars
3126 {
3127 root =
3128 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3129 if (!root.get()) {
3130 *first_unparsed = expression_cstr;
3131 *reason_to_stop =
3132 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3133 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3134 return 0;
3135 } else {
3136 list->Append(root);
3137 *first_unparsed = end + 1; // skip ]
3138 *reason_to_stop =
3139 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3140 *final_result =
3141 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3142 return 1;
3143 }
3144 } else if (root_compiler_type_info.Test(
3145 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
3146 // accessing it by index and I would
3147 // have deref'ed anyway, then do it
3148 // now and use this as a bitfield
3149 *what_next ==
3150 ValueObject::eExpressionPathAftermathDereference &&
3151 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3152 Error error;
3153 root = root->Dereference(error);
3154 if (error.Fail() || !root.get()) {
3155 *first_unparsed = expression_cstr;
3156 *reason_to_stop =
3157 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3158 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3159 return 0;
3160 } else {
3161 *what_next = ValueObject::eExpressionPathAftermathNothing;
3162 continue;
3163 }
3164 } else {
3165 for (unsigned long index = index_lower; index <= index_higher;
3166 index++) {
3167 ValueObjectSP child = root->GetChildAtIndex(index, true);
3168 list->Append(child);
3169 }
3170 *first_unparsed = end + 1;
3171 *reason_to_stop =
3172 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3173 *final_result =
3174 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3175 return index_higher - index_lower +
3176 1; // tell me number of items I added to the VOList
3177 }
3178 }
3179 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 default: // some non-[ separator, or something entirely wrong, is in the way
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003182 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003183 *first_unparsed = expression_cstr;
3184 *reason_to_stop =
3185 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3186 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3187 return 0;
3188 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003189 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003190 }
3191 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003192}
3193
Kate Stoneb9c1b512016-09-06 20:57:50 +00003194void ValueObject::LogValueObject(Log *log) {
3195 if (log)
3196 return LogValueObject(log, DumpValueObjectOptions(*this));
Enrico Granata538a88a2014-10-09 18:24:30 +00003197}
3198
Kate Stoneb9c1b512016-09-06 20:57:50 +00003199void ValueObject::LogValueObject(Log *log,
3200 const DumpValueObjectOptions &options) {
3201 if (log) {
3202 StreamString s;
3203 Dump(s, options);
3204 if (s.GetSize())
3205 log->PutCString(s.GetData());
3206 }
Greg Clayton759e7442014-07-19 00:12:57 +00003207}
3208
Kate Stoneb9c1b512016-09-06 20:57:50 +00003209void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
Chaoren Lind7bdc272015-07-31 00:35:40 +00003210
Kate Stoneb9c1b512016-09-06 20:57:50 +00003211void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
3212 ValueObjectPrinter printer(this, &s, options);
3213 printer.PrintValueObject();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003214}
3215
Kate Stoneb9c1b512016-09-06 20:57:50 +00003216ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
3217 ValueObjectSP valobj_sp;
3218
3219 if (UpdateValueIfNeeded(false) && m_error.Success()) {
3220 ExecutionContext exe_ctx(GetExecutionContextRef());
3221
3222 DataExtractor data;
3223 data.SetByteOrder(m_data.GetByteOrder());
3224 data.SetAddressByteSize(m_data.GetAddressByteSize());
3225
3226 if (IsBitfield()) {
3227 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3228 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3229 } else
3230 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3231
3232 valobj_sp = ValueObjectConstResult::Create(
3233 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
3234 GetAddressOf());
3235 }
3236
3237 if (!valobj_sp) {
3238 ExecutionContext exe_ctx(GetExecutionContextRef());
3239 valobj_sp = ValueObjectConstResult::Create(
3240 exe_ctx.GetBestExecutionContextScope(), m_error);
3241 }
3242 return valobj_sp;
3243}
3244
3245ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
3246 lldb::DynamicValueType dynValue, bool synthValue) {
3247 ValueObjectSP result_sp(GetSP());
3248
3249 switch (dynValue) {
3250 case lldb::eDynamicCanRunTarget:
3251 case lldb::eDynamicDontRunTarget: {
3252 if (!result_sp->IsDynamic()) {
3253 if (result_sp->GetDynamicValue(dynValue))
3254 result_sp = result_sp->GetDynamicValue(dynValue);
3255 }
3256 } break;
3257 case lldb::eNoDynamicValues: {
3258 if (result_sp->IsDynamic()) {
3259 if (result_sp->GetStaticValue())
3260 result_sp = result_sp->GetStaticValue();
3261 }
3262 } break;
3263 }
3264
3265 if (synthValue) {
3266 if (!result_sp->IsSynthetic()) {
3267 if (result_sp->GetSyntheticValue())
3268 result_sp = result_sp->GetSyntheticValue();
3269 }
3270 } else {
3271 if (result_sp->IsSynthetic()) {
3272 if (result_sp->GetNonSyntheticValue())
3273 result_sp = result_sp->GetNonSyntheticValue();
3274 }
3275 }
3276
3277 return result_sp;
3278}
3279
3280lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
3281 CompilerType pointee_type;
3282 CompilerType this_type(GetCompilerType());
3283 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3284 if (type_info) {
3285 bool ptr_or_ref = false;
3286 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
3287 ptr_or_ref = true;
3288 type_info = pointee_type.GetTypeInfo();
3289 }
3290
3291 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
3292 if ((type_info & cpp_class) == cpp_class) {
3293 if (ptr_or_ref) {
3294 address_type = GetAddressTypeOfChildren();
3295 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3296 } else
3297 return GetAddressOf(false, &address_type);
3298 }
3299 }
3300
3301 address_type = eAddressTypeInvalid;
3302 return LLDB_INVALID_ADDRESS;
3303}
3304
3305ValueObjectSP ValueObject::Dereference(Error &error) {
3306 if (m_deref_valobj)
3307 return m_deref_valobj->GetSP();
3308
3309 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
3310 if (is_pointer_or_reference_type) {
3311 bool omit_empty_base_classes = true;
3312 bool ignore_array_bounds = false;
3313
3314 std::string child_name_str;
3315 uint32_t child_byte_size = 0;
3316 int32_t child_byte_offset = 0;
3317 uint32_t child_bitfield_bit_size = 0;
3318 uint32_t child_bitfield_bit_offset = 0;
3319 bool child_is_base_class = false;
3320 bool child_is_deref_of_parent = false;
3321 const bool transparent_pointers = false;
3322 CompilerType compiler_type = GetCompilerType();
3323 CompilerType child_compiler_type;
3324 uint64_t language_flags;
3325
3326 ExecutionContext exe_ctx(GetExecutionContextRef());
3327
3328 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
3329 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
3330 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
3331 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
3332 child_is_deref_of_parent, this, language_flags);
3333 if (child_compiler_type && child_byte_size) {
3334 ConstString child_name;
3335 if (!child_name_str.empty())
3336 child_name.SetCString(child_name_str.c_str());
3337
3338 m_deref_valobj = new ValueObjectChild(
3339 *this, child_compiler_type, child_name, child_byte_size,
3340 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
3341 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
3342 language_flags);
3343 }
3344 }
3345
3346 if (m_deref_valobj) {
Greg Clayton54979cd2010-12-15 05:08:08 +00003347 error.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003348 return m_deref_valobj->GetSP();
3349 } else {
3350 StreamString strm;
3351 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003352
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353 if (is_pointer_or_reference_type)
3354 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
3355 GetTypeName().AsCString("<invalid type>"),
3356 strm.GetString().c_str());
Sean Callananed185ab2013-04-19 19:47:32 +00003357 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00003358 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
3359 GetTypeName().AsCString("<invalid type>"),
3360 strm.GetString().c_str());
3361 return ValueObjectSP();
3362 }
3363}
3364
3365ValueObjectSP ValueObject::AddressOf(Error &error) {
3366 if (m_addr_of_valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00003367 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003368
Kate Stoneb9c1b512016-09-06 20:57:50 +00003369 AddressType address_type = eAddressTypeInvalid;
3370 const bool scalar_is_load_address = false;
3371 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
3372 error.Clear();
3373 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
3374 switch (address_type) {
3375 case eAddressTypeInvalid: {
3376 StreamString expr_path_strm;
3377 GetExpressionPath(expr_path_strm, true);
3378 error.SetErrorStringWithFormat("'%s' is not in memory",
3379 expr_path_strm.GetString().c_str());
3380 } break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003381
Kate Stoneb9c1b512016-09-06 20:57:50 +00003382 case eAddressTypeFile:
3383 case eAddressTypeLoad: {
3384 CompilerType compiler_type = GetCompilerType();
3385 if (compiler_type) {
3386 std::string name(1, '&');
3387 name.append(m_name.AsCString(""));
3388 ExecutionContext exe_ctx(GetExecutionContextRef());
3389 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
3390 exe_ctx.GetBestExecutionContextScope(),
3391 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
3392 eAddressTypeInvalid, m_data.GetAddressByteSize());
3393 }
3394 } break;
3395 default:
3396 break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003397 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003398 } else {
3399 StreamString expr_path_strm;
3400 GetExpressionPath(expr_path_strm, true);
3401 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
3402 expr_path_strm.GetString().c_str());
3403 }
3404
3405 return m_addr_of_valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003406}
3407
Kate Stoneb9c1b512016-09-06 20:57:50 +00003408ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
3409 return ValueObjectCast::Create(*this, GetName(), compiler_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003410}
3411
Kate Stoneb9c1b512016-09-06 20:57:50 +00003412ValueObjectSP ValueObject::CastPointerType(const char *name,
3413 CompilerType &compiler_type) {
3414 ValueObjectSP valobj_sp;
3415 AddressType address_type;
3416 addr_t ptr_value = GetPointerValue(&address_type);
3417
3418 if (ptr_value != LLDB_INVALID_ADDRESS) {
3419 Address ptr_addr(ptr_value);
3420 ExecutionContext exe_ctx(GetExecutionContextRef());
3421 valobj_sp = ValueObjectMemory::Create(
3422 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
3423 }
3424 return valobj_sp;
Jim Ingham6035b672011-03-31 00:19:25 +00003425}
3426
Kate Stoneb9c1b512016-09-06 20:57:50 +00003427ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
3428 ValueObjectSP valobj_sp;
3429 AddressType address_type;
3430 addr_t ptr_value = GetPointerValue(&address_type);
3431
3432 if (ptr_value != LLDB_INVALID_ADDRESS) {
3433 Address ptr_addr(ptr_value);
3434 ExecutionContext exe_ctx(GetExecutionContextRef());
3435 valobj_sp = ValueObjectMemory::Create(
3436 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
3437 }
3438 return valobj_sp;
3439}
3440
3441ValueObject::EvaluationPoint::EvaluationPoint()
3442 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
3443
3444ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
3445 bool use_selected)
3446 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
3447 ExecutionContext exe_ctx(exe_scope);
3448 TargetSP target_sp(exe_ctx.GetTargetSP());
3449 if (target_sp) {
3450 m_exe_ctx_ref.SetTargetSP(target_sp);
3451 ProcessSP process_sp(exe_ctx.GetProcessSP());
3452 if (!process_sp)
3453 process_sp = target_sp->GetProcessSP();
3454
3455 if (process_sp) {
3456 m_mod_id = process_sp->GetModID();
3457 m_exe_ctx_ref.SetProcessSP(process_sp);
3458
3459 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3460
3461 if (!thread_sp) {
3462 if (use_selected)
3463 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3464 }
3465
3466 if (thread_sp) {
3467 m_exe_ctx_ref.SetThreadSP(thread_sp);
3468
3469 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3470 if (!frame_sp) {
3471 if (use_selected)
3472 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003474 if (frame_sp)
3475 m_exe_ctx_ref.SetFrameSP(frame_sp);
3476 }
Jim Ingham6035b672011-03-31 00:19:25 +00003477 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003478 }
Jim Ingham6035b672011-03-31 00:19:25 +00003479}
3480
Kate Stoneb9c1b512016-09-06 20:57:50 +00003481ValueObject::EvaluationPoint::EvaluationPoint(
3482 const ValueObject::EvaluationPoint &rhs)
3483 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
Jim Ingham6035b672011-03-31 00:19:25 +00003484
Kate Stoneb9c1b512016-09-06 20:57:50 +00003485ValueObject::EvaluationPoint::~EvaluationPoint() {}
Jim Ingham6035b672011-03-31 00:19:25 +00003486
Kate Stoneb9c1b512016-09-06 20:57:50 +00003487// This function checks the EvaluationPoint against the current process state.
3488// If the current
3489// state matches the evaluation point, or the evaluation point is already
3490// invalid, then we return
3491// false, meaning "no change". If the current state is different, we update our
3492// state, and return
3493// true meaning "yes, change". If we did see a change, we also set
3494// m_needs_update to true, so
Jim Ingham6035b672011-03-31 00:19:25 +00003495// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003496// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003497
Kate Stoneb9c1b512016-09-06 20:57:50 +00003498bool ValueObject::EvaluationPoint::SyncWithProcessState(
3499 bool accept_invalid_exe_ctx) {
3500 // Start with the target, if it is NULL, then we're obviously not going to get
3501 // any further:
3502 const bool thread_and_frame_only_if_stopped = true;
3503 ExecutionContext exe_ctx(
3504 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3505
3506 if (exe_ctx.GetTargetPtr() == NULL)
3507 return false;
3508
3509 // If we don't have a process nothing can change.
3510 Process *process = exe_ctx.GetProcessPtr();
3511 if (process == NULL)
3512 return false;
3513
3514 // If our stop id is the current stop ID, nothing has changed:
3515 ProcessModID current_mod_id = process->GetModID();
3516
3517 // If the current stop id is 0, either we haven't run yet, or the process
3518 // state has been cleared.
3519 // In either case, we aren't going to be able to sync with the process state.
3520 if (current_mod_id.GetStopID() == 0)
3521 return false;
3522
3523 bool changed = false;
3524 const bool was_valid = m_mod_id.IsValid();
3525 if (was_valid) {
3526 if (m_mod_id == current_mod_id) {
3527 // Everything is already up to date in this object, no need to
3528 // update the execution context scope.
3529 changed = false;
3530 } else {
3531 m_mod_id = current_mod_id;
3532 m_needs_update = true;
3533 changed = true;
3534 }
3535 }
3536
3537 // Now re-look up the thread and frame in case the underlying objects have
3538 // gone away & been recreated.
3539 // That way we'll be sure to return a valid exe_scope.
3540 // If we used to have a thread or a frame but can't find it anymore, then mark
3541 // ourselves as invalid.
3542
3543 if (!accept_invalid_exe_ctx) {
3544 if (m_exe_ctx_ref.HasThreadRef()) {
3545 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3546 if (thread_sp) {
3547 if (m_exe_ctx_ref.HasFrameRef()) {
3548 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3549 if (!frame_sp) {
3550 // We used to have a frame, but now it is gone
3551 SetInvalid();
3552 changed = was_valid;
3553 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003554 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003555 } else {
3556 // We used to have a thread, but now it is gone
3557 SetInvalid();
3558 changed = was_valid;
3559 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003560 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003561 }
Enrico Granatabb642e52015-05-16 01:27:00 +00003562
Kate Stoneb9c1b512016-09-06 20:57:50 +00003563 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003564}
3565
Kate Stoneb9c1b512016-09-06 20:57:50 +00003566void ValueObject::EvaluationPoint::SetUpdated() {
3567 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3568 if (process_sp)
3569 m_mod_id = process_sp->GetModID();
3570 m_needs_update = false;
Johnny Chen44805302011-07-19 19:48:13 +00003571}
Enrico Granata9128ee22011-09-06 19:20:51 +00003572
Kate Stoneb9c1b512016-09-06 20:57:50 +00003573void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3574 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3575 eClearUserVisibleDataItemsValue)
3576 m_value_str.clear();
3577
3578 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3579 eClearUserVisibleDataItemsLocation)
3580 m_location_str.clear();
3581
3582 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3583 eClearUserVisibleDataItemsSummary)
3584 m_summary_str.clear();
3585
3586 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3587 eClearUserVisibleDataItemsDescription)
3588 m_object_desc_str.clear();
3589
3590 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3591 eClearUserVisibleDataItemsSyntheticChildren) {
3592 if (m_synthetic_value)
3593 m_synthetic_value = NULL;
3594 }
3595
3596 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3597 eClearUserVisibleDataItemsValidator)
3598 m_validation_result.reset();
Enrico Granata9128ee22011-09-06 19:20:51 +00003599}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003600
Kate Stoneb9c1b512016-09-06 20:57:50 +00003601SymbolContextScope *ValueObject::GetSymbolContextScope() {
3602 if (m_parent) {
3603 if (!m_parent->IsPointerOrReferenceType())
3604 return m_parent->GetSymbolContextScope();
3605 }
3606 return NULL;
Enrico Granata972be532014-12-17 21:18:43 +00003607}
3608
Kate Stoneb9c1b512016-09-06 20:57:50 +00003609lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
3610 const char *name, const char *expression, const ExecutionContext &exe_ctx) {
3611 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3612 EvaluateExpressionOptions());
3613}
Enrico Granata972be532014-12-17 21:18:43 +00003614
Kate Stoneb9c1b512016-09-06 20:57:50 +00003615lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
3616 const char *name, const char *expression, const ExecutionContext &exe_ctx,
3617 const EvaluateExpressionOptions &options) {
3618 lldb::ValueObjectSP retval_sp;
3619 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3620 if (!target_sp)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003621 return retval_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003622 if (!expression || !*expression)
3623 return retval_sp;
3624 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3625 retval_sp, options);
3626 if (retval_sp && name && *name)
3627 retval_sp->SetName(ConstString(name));
3628 return retval_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003629}
3630
3631lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00003632ValueObject::CreateValueObjectFromAddress(const char *name, uint64_t address,
3633 const ExecutionContext &exe_ctx,
3634 CompilerType type) {
3635 if (type) {
3636 CompilerType pointer_type(type.GetPointerType());
3637 if (pointer_type) {
3638 lldb::DataBufferSP buffer(
3639 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3640 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3641 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3642 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3643 exe_ctx.GetAddressByteSize()));
3644 if (ptr_result_valobj_sp) {
3645 ptr_result_valobj_sp->GetValue().SetValueType(
3646 Value::eValueTypeLoadAddress);
3647 Error err;
3648 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
3649 if (ptr_result_valobj_sp && name && *name)
3650 ptr_result_valobj_sp->SetName(ConstString(name));
3651 }
3652 return ptr_result_valobj_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003653 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003654 }
3655 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003656}
3657
Kate Stoneb9c1b512016-09-06 20:57:50 +00003658lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
3659 const char *name, const DataExtractor &data,
3660 const ExecutionContext &exe_ctx, CompilerType type) {
3661 lldb::ValueObjectSP new_value_sp;
3662 new_value_sp = ValueObjectConstResult::Create(
3663 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3664 LLDB_INVALID_ADDRESS);
3665 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
3666 if (new_value_sp && name && *name)
3667 new_value_sp->SetName(ConstString(name));
3668 return new_value_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003669}
Enrico Granata4873e522013-04-11 22:48:58 +00003670
Kate Stoneb9c1b512016-09-06 20:57:50 +00003671ModuleSP ValueObject::GetModule() {
3672 ValueObject *root(GetRoot());
3673 if (root != this)
3674 return root->GetModule();
3675 return lldb::ModuleSP();
3676}
3677
3678ValueObject *ValueObject::GetRoot() {
3679 if (m_root)
3680 return m_root;
3681 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3682 return (vo->m_parent != nullptr);
3683 }));
3684}
3685
3686ValueObject *
3687ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3688 ValueObject *vo = this;
3689 while (vo) {
3690 if (f(vo) == false)
3691 break;
3692 vo = vo->m_parent;
3693 }
3694 return vo;
3695}
3696
3697AddressType ValueObject::GetAddressTypeOfChildren() {
3698 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3699 ValueObject *root(GetRoot());
Enrico Granata4873e522013-04-11 22:48:58 +00003700 if (root != this)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003701 return root->GetAddressTypeOfChildren();
3702 }
3703 return m_address_type_of_ptr_or_ref_children;
Enrico Granata4873e522013-04-11 22:48:58 +00003704}
3705
Kate Stoneb9c1b512016-09-06 20:57:50 +00003706lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3707 ValueObject *with_dv_info = this;
3708 while (with_dv_info) {
3709 if (with_dv_info->HasDynamicValueTypeInfo())
3710 return with_dv_info->GetDynamicValueTypeImpl();
3711 with_dv_info = with_dv_info->m_parent;
3712 }
3713 return lldb::eNoDynamicValues;
Enrico Granatade61eba2015-01-22 03:07:34 +00003714}
3715
Kate Stoneb9c1b512016-09-06 20:57:50 +00003716lldb::Format ValueObject::GetFormat() const {
3717 const ValueObject *with_fmt_info = this;
3718 while (with_fmt_info) {
3719 if (with_fmt_info->m_format != lldb::eFormatDefault)
3720 return with_fmt_info->m_format;
3721 with_fmt_info = with_fmt_info->m_parent;
3722 }
3723 return m_format;
Enrico Granata4873e522013-04-11 22:48:58 +00003724}
3725
Kate Stoneb9c1b512016-09-06 20:57:50 +00003726lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3727 lldb::LanguageType type = m_preferred_display_language;
3728 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3729 if (GetRoot()) {
3730 if (GetRoot() == this) {
3731 if (StackFrameSP frame_sp = GetFrameSP()) {
3732 const SymbolContext &sc(
3733 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3734 if (CompileUnit *cu = sc.comp_unit)
3735 type = cu->GetLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +00003736 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003737 } else {
3738 type = GetRoot()->GetPreferredDisplayLanguage();
3739 }
Enrico Granatac1247f52014-11-06 21:23:20 +00003740 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003741 }
3742 return (m_preferred_display_language = type); // only compute it once
Enrico Granataed3228a2015-01-21 01:47:13 +00003743}
3744
Kate Stoneb9c1b512016-09-06 20:57:50 +00003745void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3746 m_preferred_display_language = lt;
Enrico Granatac1247f52014-11-06 21:23:20 +00003747}
3748
Kate Stoneb9c1b512016-09-06 20:57:50 +00003749void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3750 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3751 SetPreferredDisplayLanguage(lt);
Enrico Granata73e8c4d2015-10-07 02:36:35 +00003752}
3753
Kate Stoneb9c1b512016-09-06 20:57:50 +00003754bool ValueObject::CanProvideValue() {
3755 // we need to support invalid types as providers of values because some
3756 // bare-board
3757 // debugging scenarios have no notion of types, but still manage to have raw
3758 // numeric
3759 // values for things like registers. sigh.
3760 const CompilerType &type(GetCompilerType());
3761 return (false == type.IsValid()) ||
3762 (0 != (type.GetTypeInfo() & eTypeHasValue));
Sean Callanan7375f3e2014-12-09 21:18:59 +00003763}
3764
Kate Stoneb9c1b512016-09-06 20:57:50 +00003765bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3766
3767ValueObjectSP ValueObject::Persist() {
3768 if (!UpdateValueIfNeeded())
3769 return nullptr;
3770
3771 TargetSP target_sp(GetTargetSP());
3772 if (!target_sp)
3773 return nullptr;
3774
3775 PersistentExpressionState *persistent_state =
3776 target_sp->GetPersistentExpressionStateForLanguage(
3777 GetPreferredDisplayLanguage());
3778
3779 if (!persistent_state)
3780 return nullptr;
3781
3782 ConstString name(persistent_state->GetNextPersistentVariableName());
3783
3784 ValueObjectSP const_result_sp =
3785 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3786
3787 ExpressionVariableSP clang_var_sp =
3788 persistent_state->CreatePersistentVariable(const_result_sp);
3789 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3790 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3791
3792 return clang_var_sp->GetValueObject();
Enrico Granatad07cfd32014-10-08 18:27:36 +00003793}
Enrico Granata0c10a852014-12-08 23:13:56 +00003794
Kate Stoneb9c1b512016-09-06 20:57:50 +00003795bool ValueObject::IsSyntheticChildrenGenerated() {
3796 return m_is_synthetic_children_generated;
Enrico Granata0c10a852014-12-08 23:13:56 +00003797}
Enrico Granatae29df232014-12-09 19:51:20 +00003798
Kate Stoneb9c1b512016-09-06 20:57:50 +00003799void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3800 m_is_synthetic_children_generated = b;
Enrico Granatae29df232014-12-09 19:51:20 +00003801}
3802
Kate Stoneb9c1b512016-09-06 20:57:50 +00003803uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
Enrico Granatadc62ffd2015-11-09 19:27:34 +00003804
Kate Stoneb9c1b512016-09-06 20:57:50 +00003805void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }