blob: e349e03f23693d0a9c692898c7215d8385e91e87 [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));
Zachary Turnerc1564272016-11-16 21:15:24 +0000381 m_location_str = sstr.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 } 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));
Zachary Turnerc1564272016-11-16 21:15:24 +0000947 memcpy(destination->GetBytes(), source.GetString().data(), source.GetSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000948 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)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001140 m_object_desc_str.append(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 }
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
Enrico Granata65d86e42016-11-07 23:32:20 +00001282 bool allow_special =
1283 (special == ValueObject::PrintableRepresentationSpecialCases::eAllow);
1284 const bool only_special = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001285
1286 if (allow_special) {
1287 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1288 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1289 // when being asked to get a printable display an array or pointer type
1290 // directly,
1291 // try to "do the right thing"
1292
1293 if (IsCStringContainer(true) &&
1294 (custom_format == eFormatCString ||
1295 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1296 custom_format ==
1297 eFormatVectorOfChar)) // print char[] & char* directly
1298 {
1299 Error error;
1300 lldb::DataBufferSP buffer_sp;
1301 std::pair<size_t, bool> read_string = ReadPointedString(
1302 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1303 (custom_format == eFormatCharArray));
1304 lldb_private::formatters::StringPrinter::
1305 ReadBufferAndDumpToStreamOptions options(*this);
1306 options.SetData(DataExtractor(
1307 buffer_sp, lldb::eByteOrderInvalid,
1308 8)); // none of this matters for a string - pass some defaults
1309 options.SetStream(&s);
1310 options.SetPrefixToken(0);
1311 options.SetQuote('"');
1312 options.SetSourceSize(buffer_sp->GetByteSize());
1313 options.SetIsTruncated(read_string.second);
1314 formatters::StringPrinter::ReadBufferAndDumpToStream<
1315 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1316 options);
1317 return !error.Fail();
1318 }
1319
1320 if (custom_format == eFormatEnum)
Enrico Granata85933ed2011-08-18 16:38:26 +00001321 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001322
Kate Stoneb9c1b512016-09-06 20:57:50 +00001323 // this only works for arrays, because I have no way to know when
1324 // the pointed memory ends, and no special \0 end of data marker
1325 if (flags.Test(eTypeIsArray)) {
1326 if ((custom_format == eFormatBytes) ||
1327 (custom_format == eFormatBytesWithASCII)) {
1328 const size_t count = GetNumChildren();
1329
1330 s << '[';
1331 for (size_t low = 0; low < count; low++) {
1332
1333 if (low)
1334 s << ',';
1335
1336 ValueObjectSP child = GetChildAtIndex(low, true);
1337 if (!child.get()) {
1338 s << "<invalid child>";
1339 continue;
Enrico Granata86cc9822012-03-19 22:58:49 +00001340 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001341 child->DumpPrintableRepresentation(
1342 s, ValueObject::eValueObjectRepresentationStyleValue,
1343 custom_format);
1344 }
1345
1346 s << ']';
1347
1348 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001349 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001350
1351 if ((custom_format == eFormatVectorOfChar) ||
1352 (custom_format == eFormatVectorOfFloat32) ||
1353 (custom_format == eFormatVectorOfFloat64) ||
1354 (custom_format == eFormatVectorOfSInt16) ||
1355 (custom_format == eFormatVectorOfSInt32) ||
1356 (custom_format == eFormatVectorOfSInt64) ||
1357 (custom_format == eFormatVectorOfSInt8) ||
1358 (custom_format == eFormatVectorOfUInt128) ||
1359 (custom_format == eFormatVectorOfUInt16) ||
1360 (custom_format == eFormatVectorOfUInt32) ||
1361 (custom_format == eFormatVectorOfUInt64) ||
1362 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1363 // with ASCII or any vector
1364 // format should be printed
1365 // directly
Enrico Granata86cc9822012-03-19 22:58:49 +00001366 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 const size_t count = GetNumChildren();
1368
1369 Format format = FormatManager::GetSingleItemFormat(custom_format);
1370
1371 s << '[';
1372 for (size_t low = 0; low < count; low++) {
1373
1374 if (low)
1375 s << ',';
1376
1377 ValueObjectSP child = GetChildAtIndex(low, true);
1378 if (!child.get()) {
1379 s << "<invalid child>";
1380 continue;
Enrico Granata0dba9b32014-01-08 01:36:59 +00001381 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 child->DumpPrintableRepresentation(
1383 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1384 }
1385
1386 s << ']';
1387
1388 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001389 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 }
1391
1392 if ((custom_format == eFormatBoolean) ||
1393 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1394 (custom_format == eFormatCharPrintable) ||
1395 (custom_format == eFormatComplexFloat) ||
1396 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1397 (custom_format == eFormatHexUppercase) ||
1398 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1399 (custom_format == eFormatOSType) ||
1400 (custom_format == eFormatUnicode16) ||
1401 (custom_format == eFormatUnicode32) ||
1402 (custom_format == eFormatUnsigned) ||
1403 (custom_format == eFormatPointer) ||
1404 (custom_format == eFormatComplexInteger) ||
1405 (custom_format == eFormatComplex) ||
1406 (custom_format == eFormatDefault)) // use the [] operator
1407 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001408 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001409 }
1410
1411 if (only_special)
1412 return false;
1413
1414 bool var_success = false;
1415
1416 {
Zachary Turnerc1564272016-11-16 21:15:24 +00001417 llvm::StringRef str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001418
1419 // this is a local stream that we are using to ensure that the data pointed
Zachary Turnerc1564272016-11-16 21:15:24 +00001420 // to by cstr survives long enough for us to copy it to its destination - it
1421 // is necessary to have this temporary storage area for cases where our
1422 // desired output is not backed by some other longer-term storage
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 StreamString strm;
1424
1425 if (custom_format != eFormatInvalid)
1426 SetFormat(custom_format);
1427
1428 switch (val_obj_display) {
1429 case eValueObjectRepresentationStyleValue:
Zachary Turnerc1564272016-11-16 21:15:24 +00001430 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 break;
1432
1433 case eValueObjectRepresentationStyleSummary:
Zachary Turnerc1564272016-11-16 21:15:24 +00001434 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 break;
1436
1437 case eValueObjectRepresentationStyleLanguageSpecific:
Zachary Turnerc1564272016-11-16 21:15:24 +00001438 str = GetObjectDescription();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 break;
1440
1441 case eValueObjectRepresentationStyleLocation:
Zachary Turnerc1564272016-11-16 21:15:24 +00001442 str = GetLocationAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 break;
1444
1445 case eValueObjectRepresentationStyleChildrenCount:
1446 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
Zachary Turnerc1564272016-11-16 21:15:24 +00001447 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001448 break;
1449
1450 case eValueObjectRepresentationStyleType:
Zachary Turnerc1564272016-11-16 21:15:24 +00001451 str = GetTypeName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 break;
1453
1454 case eValueObjectRepresentationStyleName:
Zachary Turnerc1564272016-11-16 21:15:24 +00001455 str = GetName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 break;
1457
1458 case eValueObjectRepresentationStyleExpressionPath:
1459 GetExpressionPath(strm, false);
Zachary Turnerc1564272016-11-16 21:15:24 +00001460 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 break;
1462 }
1463
Zachary Turnerc1564272016-11-16 21:15:24 +00001464 if (str.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465 if (val_obj_display == eValueObjectRepresentationStyleValue)
Zachary Turnerc1564272016-11-16 21:15:24 +00001466 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1468 if (!CanProvideValue()) {
1469 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1470 GetLocationAsCString());
Zachary Turnerc1564272016-11-16 21:15:24 +00001471 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001472 } else
Zachary Turnerc1564272016-11-16 21:15:24 +00001473 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 }
1475 }
1476
Zachary Turnerc1564272016-11-16 21:15:24 +00001477 if (!str.empty())
1478 s << str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479 else {
1480 if (m_error.Fail()) {
1481 if (do_dump_error)
1482 s.Printf("<%s>", m_error.AsCString());
1483 else
1484 return false;
1485 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1486 s.PutCString("<no summary available>");
1487 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1488 s.PutCString("<no value available>");
1489 else if (val_obj_display ==
1490 eValueObjectRepresentationStyleLanguageSpecific)
1491 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1492 // have other runtimes
1493 // that support a
1494 // description
1495 else
1496 s.PutCString("<no printable representation>");
1497 }
1498
1499 // we should only return false here if we could not do *anything*
1500 // even if we have an error message as output, that's a success
1501 // from our callers' perspective, so return true
1502 var_success = true;
1503
1504 if (custom_format != eFormatInvalid)
1505 SetFormat(eFormatDefault);
1506 }
1507
1508 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001509}
1510
Kate Stoneb9c1b512016-09-06 20:57:50 +00001511addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1512 AddressType *address_type) {
1513 // Can't take address of a bitfield
1514 if (IsBitfield())
Greg Clayton73b472d2010-10-27 03:32:59 +00001515 return LLDB_INVALID_ADDRESS;
Greg Clayton73b472d2010-10-27 03:32:59 +00001516
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 if (!UpdateValueIfNeeded(false))
1518 return LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +00001519
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 switch (m_value.GetValueType()) {
1521 case Value::eValueTypeScalar:
1522 case Value::eValueTypeVector:
1523 if (scalar_is_load_address) {
1524 if (address_type)
1525 *address_type = eAddressTypeLoad;
1526 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001527 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 case Value::eValueTypeLoadAddress:
1531 case Value::eValueTypeFileAddress: {
Enrico Granata9128ee22011-09-06 19:20:51 +00001532 if (address_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 *address_type = m_value.GetValueAddressType();
1534 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1535 } break;
1536 case Value::eValueTypeHostAddress: {
1537 if (address_type)
1538 *address_type = m_value.GetValueAddressType();
1539 return LLDB_INVALID_ADDRESS;
1540 } break;
1541 }
1542 if (address_type)
1543 *address_type = eAddressTypeInvalid;
1544 return LLDB_INVALID_ADDRESS;
1545}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1548 addr_t address = LLDB_INVALID_ADDRESS;
1549 if (address_type)
1550 *address_type = eAddressTypeInvalid;
1551
1552 if (!UpdateValueIfNeeded(false))
Greg Clayton737b9322010-09-13 03:32:57 +00001553 return address;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554
1555 switch (m_value.GetValueType()) {
1556 case Value::eValueTypeScalar:
1557 case Value::eValueTypeVector:
1558 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1559 break;
1560
1561 case Value::eValueTypeHostAddress:
1562 case Value::eValueTypeLoadAddress:
1563 case Value::eValueTypeFileAddress: {
1564 lldb::offset_t data_offset = 0;
1565 address = m_data.GetPointer(&data_offset);
1566 } break;
1567 }
1568
1569 if (address_type)
1570 *address_type = GetAddressTypeOfChildren();
1571
1572 return address;
Greg Clayton737b9322010-09-13 03:32:57 +00001573}
1574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575bool ValueObject::SetValueFromCString(const char *value_str, Error &error) {
1576 error.Clear();
1577 // Make sure our value is up to date first so that our location and location
1578 // type is valid.
1579 if (!UpdateValueIfNeeded(false)) {
1580 error.SetErrorString("unable to read value");
Greg Clayton81e871e2012-02-04 02:27:34 +00001581 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 }
1583
1584 uint64_t count = 0;
1585 const Encoding encoding = GetCompilerType().GetEncoding(count);
1586
1587 const size_t byte_size = GetByteSize();
1588
1589 Value::ValueType value_type = m_value.GetValueType();
1590
1591 if (value_type == Value::eValueTypeScalar) {
1592 // If the value is already a scalar, then let the scalar change itself:
1593 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1594 } else if (byte_size <= 16) {
1595 // If the value fits in a scalar, then make a new scalar and again let the
1596 // scalar code do the conversion, then figure out where to put the new
1597 // value.
1598 Scalar new_scalar;
1599 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1600 if (error.Success()) {
1601 switch (value_type) {
1602 case Value::eValueTypeLoadAddress: {
1603 // If it is a load address, then the scalar value is the storage
1604 // location
1605 // of the data, and we have to shove this value down to that load
1606 // location.
1607 ExecutionContext exe_ctx(GetExecutionContextRef());
1608 Process *process = exe_ctx.GetProcessPtr();
1609 if (process) {
1610 addr_t target_addr =
1611 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1612 size_t bytes_written = process->WriteScalarToMemory(
1613 target_addr, new_scalar, byte_size, error);
1614 if (!error.Success())
1615 return false;
1616 if (bytes_written != byte_size) {
1617 error.SetErrorString("unable to write value to memory");
1618 return false;
1619 }
1620 }
1621 } break;
1622 case Value::eValueTypeHostAddress: {
1623 // If it is a host address, then we stuff the scalar as a DataBuffer
1624 // into the Value's data.
1625 DataExtractor new_data;
1626 new_data.SetByteOrder(m_data.GetByteOrder());
1627
1628 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1629 m_data.SetData(buffer_sp, 0);
1630 bool success = new_scalar.GetData(new_data);
1631 if (success) {
1632 new_data.CopyByteOrderedData(
1633 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1634 byte_size, m_data.GetByteOrder());
1635 }
1636 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1637
1638 } break;
1639 case Value::eValueTypeFileAddress:
1640 case Value::eValueTypeScalar:
1641 case Value::eValueTypeVector:
1642 break;
1643 }
1644 } else {
1645 return false;
1646 }
1647 } else {
1648 // We don't support setting things bigger than a scalar at present.
1649 error.SetErrorString("unable to write aggregate data type");
1650 return false;
1651 }
1652
1653 // If we have reached this point, then we have successfully changed the value.
1654 SetNeedsUpdate();
1655 return true;
Greg Clayton81e871e2012-02-04 02:27:34 +00001656}
1657
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658bool ValueObject::GetDeclaration(Declaration &decl) {
1659 decl.Clear();
1660 return false;
Greg Clayton84db9102012-03-26 23:03:23 +00001661}
1662
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663ConstString ValueObject::GetTypeName() {
1664 return GetCompilerType().GetConstTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001665}
1666
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1668
1669ConstString ValueObject::GetQualifiedTypeName() {
1670 return GetCompilerType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001671}
1672
Kate Stoneb9c1b512016-09-06 20:57:50 +00001673LanguageType ValueObject::GetObjectRuntimeLanguage() {
1674 return GetCompilerType().GetMinimumLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001675}
1676
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677void ValueObject::AddSyntheticChild(const ConstString &key,
1678 ValueObject *valobj) {
1679 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680}
1681
Kate Stoneb9c1b512016-09-06 20:57:50 +00001682ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1683 ValueObjectSP synthetic_child_sp;
1684 std::map<ConstString, ValueObject *>::const_iterator pos =
1685 m_synthetic_children.find(key);
1686 if (pos != m_synthetic_children.end())
1687 synthetic_child_sp = pos->second->GetSP();
1688 return synthetic_child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689}
1690
Greg Clayton2452ab72013-02-08 22:02:02 +00001691uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001692ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1693 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00001694}
1695
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1697
1698bool ValueObject::IsArrayType() {
1699 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700}
1701
Kate Stoneb9c1b512016-09-06 20:57:50 +00001702bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1703
1704bool ValueObject::IsIntegerType(bool &is_signed) {
1705 return GetCompilerType().IsIntegerType(is_signed);
Greg Claytondaf515f2011-07-09 20:12:33 +00001706}
1707
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708bool ValueObject::IsPointerOrReferenceType() {
1709 return GetCompilerType().IsPointerOrReferenceType();
Enrico Granata9fc19442011-07-06 02:13:41 +00001710}
1711
Kate Stoneb9c1b512016-09-06 20:57:50 +00001712bool ValueObject::IsPossibleDynamicType() {
1713 ExecutionContext exe_ctx(GetExecutionContextRef());
1714 Process *process = exe_ctx.GetProcessPtr();
1715 if (process)
1716 return process->IsPossibleDynamicValue(*this);
1717 else
1718 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001719}
Greg Clayton73b472d2010-10-27 03:32:59 +00001720
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721bool ValueObject::IsRuntimeSupportValue() {
1722 Process *process(GetProcessSP().get());
1723 if (process) {
1724 LanguageRuntime *runtime =
1725 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1726 if (!runtime)
1727 runtime = process->GetObjCLanguageRuntime();
1728 if (runtime)
1729 return runtime->IsRuntimeSupportValue(*this);
1730 }
1731 return false;
Greg Clayton007d5be2011-05-30 00:49:24 +00001732}
1733
Kate Stoneb9c1b512016-09-06 20:57:50 +00001734bool ValueObject::IsNilReference() {
1735 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1736 return language->IsNilReference(*this);
1737 }
1738 return false;
Greg Claytondea8cb42011-06-29 22:09:02 +00001739}
1740
Kate Stoneb9c1b512016-09-06 20:57:50 +00001741bool ValueObject::IsUninitializedReference() {
1742 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1743 return language->IsUninitializedReference(*this);
1744 }
1745 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00001746}
1747
Greg Claytondaf515f2011-07-09 20:12:33 +00001748// This allows you to create an array member using and index
1749// that doesn't not fall in the normal bounds of the array.
1750// Many times structure can be defined as:
1751// struct Collection
1752// {
1753// uint32_t item_count;
1754// Item item_array[0];
1755// };
1756// The size of the "item_array" is 1, but many times in practice
1757// there are more items in "item_array".
1758
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1760 bool can_create) {
1761 ValueObjectSP synthetic_child_sp;
1762 if (IsPointerType() || IsArrayType()) {
1763 char index_str[64];
1764 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1765 ConstString index_const_str(index_str);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001766 // Check if we have already created a synthetic array member in this
1767 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768 synthetic_child_sp = GetSyntheticChild(index_const_str);
1769 if (!synthetic_child_sp) {
1770 ValueObject *synthetic_child;
1771 // We haven't made a synthetic array member for INDEX yet, so
1772 // lets make one and cache it for any future reference.
1773 synthetic_child = CreateChildAtIndex(0, true, index);
1774
1775 // Cache the value if we got one back...
1776 if (synthetic_child) {
1777 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001778 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001779 synthetic_child_sp->SetName(ConstString(index_str));
1780 synthetic_child_sp->m_is_array_item_for_pointer = true;
1781 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001782 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001783 }
1784 return synthetic_child_sp;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001785}
1786
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1788 bool can_create) {
1789 ValueObjectSP synthetic_child_sp;
1790 if (IsScalarType()) {
1791 char index_str[64];
1792 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1793 ConstString index_const_str(index_str);
Enrico Granata32556cd2014-08-26 20:54:04 +00001794 // Check if we have already created a synthetic array member in this
1795 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 synthetic_child_sp = GetSyntheticChild(index_const_str);
1797 if (!synthetic_child_sp) {
1798 uint32_t bit_field_size = to - from + 1;
1799 uint32_t bit_field_offset = from;
1800 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1801 bit_field_offset =
1802 GetByteSize() * 8 - bit_field_size - bit_field_offset;
1803 // We haven't made a synthetic array member for INDEX yet, so
1804 // lets make one and cache it for any future reference.
1805 ValueObjectChild *synthetic_child = new ValueObjectChild(
1806 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1807 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1808 0);
1809
1810 // Cache the value if we got one back...
1811 if (synthetic_child) {
1812 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata32556cd2014-08-26 20:54:04 +00001813 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 synthetic_child_sp->SetName(ConstString(index_str));
1815 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1816 }
Enrico Granata32556cd2014-08-26 20:54:04 +00001817 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001818 }
1819 return synthetic_child_sp;
Enrico Granata32556cd2014-08-26 20:54:04 +00001820}
1821
Kate Stoneb9c1b512016-09-06 20:57:50 +00001822ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1823 uint32_t offset, const CompilerType &type, bool can_create,
1824 ConstString name_const_str) {
1825
1826 ValueObjectSP synthetic_child_sp;
1827
1828 if (name_const_str.IsEmpty()) {
1829 char name_str[64];
1830 snprintf(name_str, sizeof(name_str), "@%i", offset);
1831 name_const_str.SetCString(name_str);
1832 }
1833
1834 // Check if we have already created a synthetic array member in this
1835 // valid object. If we have we will re-use it.
1836 synthetic_child_sp = GetSyntheticChild(name_const_str);
1837
1838 if (synthetic_child_sp.get())
1839 return synthetic_child_sp;
1840
1841 if (!can_create)
1842 return ValueObjectSP();
1843
1844 ExecutionContext exe_ctx(GetExecutionContextRef());
1845
1846 ValueObjectChild *synthetic_child = new ValueObjectChild(
1847 *this, type, name_const_str,
1848 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1849 false, false, eAddressTypeInvalid, 0);
1850 if (synthetic_child) {
1851 AddSyntheticChild(name_const_str, synthetic_child);
1852 synthetic_child_sp = synthetic_child->GetSP();
1853 synthetic_child_sp->SetName(name_const_str);
1854 synthetic_child_sp->m_is_child_at_offset = true;
1855 }
1856 return synthetic_child_sp;
1857}
1858
1859ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1860 const CompilerType &type,
1861 bool can_create,
1862 ConstString name_const_str) {
1863 ValueObjectSP synthetic_child_sp;
1864
1865 if (name_const_str.IsEmpty()) {
1866 char name_str[128];
1867 snprintf(name_str, sizeof(name_str), "base%s@%i",
1868 type.GetTypeName().AsCString("<unknown>"), offset);
1869 name_const_str.SetCString(name_str);
1870 }
1871
1872 // Check if we have already created a synthetic array member in this
1873 // valid object. If we have we will re-use it.
1874 synthetic_child_sp = GetSyntheticChild(name_const_str);
1875
1876 if (synthetic_child_sp.get())
1877 return synthetic_child_sp;
1878
1879 if (!can_create)
1880 return ValueObjectSP();
1881
1882 const bool is_base_class = true;
1883
1884 ExecutionContext exe_ctx(GetExecutionContextRef());
1885
1886 ValueObjectChild *synthetic_child = new ValueObjectChild(
1887 *this, type, name_const_str,
1888 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1889 is_base_class, false, eAddressTypeInvalid, 0);
1890 if (synthetic_child) {
1891 AddSyntheticChild(name_const_str, synthetic_child);
1892 synthetic_child_sp = synthetic_child->GetSP();
1893 synthetic_child_sp->SetName(name_const_str);
1894 }
1895 return synthetic_child_sp;
1896}
Enrico Granata32556cd2014-08-26 20:54:04 +00001897
Enrico Granatad55546b2011-07-22 00:16:08 +00001898// your expression path needs to have a leading . or ->
1899// (unless it somehow "looks like" an array, in which case it has
1900// a leading [ symbol). while the [ is meaningful and should be shown
1901// to the user, . and -> are just parser design, but by no means
1902// added information for the user.. strip them off
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1904 if (!expression || !expression[0])
Enrico Granatad55546b2011-07-22 00:16:08 +00001905 return expression;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 if (expression[0] == '.')
1907 return expression + 1;
1908 if (expression[0] == '-' && expression[1] == '>')
1909 return expression + 2;
1910 return expression;
Enrico Granatad55546b2011-07-22 00:16:08 +00001911}
1912
Greg Claytonafacd142011-09-02 01:15:17 +00001913ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1915 bool can_create) {
1916 ValueObjectSP synthetic_child_sp;
1917 ConstString name_const_string(expression);
1918 // Check if we have already created a synthetic array member in this
1919 // valid object. If we have we will re-use it.
1920 synthetic_child_sp = GetSyntheticChild(name_const_string);
1921 if (!synthetic_child_sp) {
1922 // We haven't made a synthetic array member for expression yet, so
1923 // lets make one and cache it for any future reference.
1924 synthetic_child_sp = GetValueForExpressionPath(
1925 expression, NULL, NULL, NULL,
1926 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1927 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1928 None));
1929
1930 // Cache the value if we got one back...
1931 if (synthetic_child_sp.get()) {
1932 // FIXME: this causes a "real" child to end up with its name changed to
1933 // the contents of expression
1934 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1935 synthetic_child_sp->SetName(
1936 ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001937 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001938 }
1939 return synthetic_child_sp;
Enrico Granatad55546b2011-07-22 00:16:08 +00001940}
1941
Kate Stoneb9c1b512016-09-06 20:57:50 +00001942void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
1943 if (use_synthetic == false)
1944 return;
1945
1946 TargetSP target_sp(GetTargetSP());
1947 if (target_sp && target_sp->GetEnableSyntheticValue() == false) {
1948 m_synthetic_value = NULL;
1949 return;
1950 }
1951
1952 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1953
1954 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1955 return;
1956
1957 if (m_synthetic_children_sp.get() == NULL)
1958 return;
1959
1960 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1961 return;
1962
1963 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1964}
1965
1966void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1967 if (use_dynamic == eNoDynamicValues)
1968 return;
1969
1970 if (!m_dynamic_value && !IsDynamic()) {
1971 ExecutionContext exe_ctx(GetExecutionContextRef());
1972 Process *process = exe_ctx.GetProcessPtr();
1973 if (process && process->IsPossibleDynamicValue(*this)) {
1974 ClearDynamicTypeInformation();
1975 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
Enrico Granatac5bc4122012-03-27 02:35:13 +00001976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 }
Enrico Granatad55546b2011-07-22 00:16:08 +00001978}
1979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1981 if (use_dynamic == eNoDynamicValues)
1982 return ValueObjectSP();
1983
1984 if (!IsDynamic() && m_dynamic_value == NULL) {
1985 CalculateDynamicValue(use_dynamic);
1986 }
1987 if (m_dynamic_value)
1988 return m_dynamic_value->GetSP();
1989 else
1990 return ValueObjectSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001991}
1992
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1994
1995lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1996
1997ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
1998 if (use_synthetic == false)
1999 return ValueObjectSP();
2000
2001 CalculateSyntheticValue(use_synthetic);
2002
2003 if (m_synthetic_value)
2004 return m_synthetic_value->GetSP();
2005 else
2006 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002007}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002008
Kate Stoneb9c1b512016-09-06 20:57:50 +00002009bool ValueObject::HasSyntheticValue() {
2010 UpdateFormatsIfNeeded();
Jim Ingham60dbabb2011-12-08 19:44:08 +00002011
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012 if (m_synthetic_children_sp.get() == NULL)
2013 return false;
Enrico Granata886147f2012-05-08 18:47:08 +00002014
Kate Stoneb9c1b512016-09-06 20:57:50 +00002015 CalculateSyntheticValue(true);
Enrico Granata86cc9822012-03-19 22:58:49 +00002016
Kate Stoneb9c1b512016-09-06 20:57:50 +00002017 if (m_synthetic_value)
2018 return true;
2019 else
Greg Claytone221f822011-01-21 01:59:00 +00002020 return false;
2021}
2022
Kate Stoneb9c1b512016-09-06 20:57:50 +00002023bool ValueObject::GetBaseClassPath(Stream &s) {
2024 if (IsBaseClass()) {
2025 bool parent_had_base_class =
2026 GetParent() && GetParent()->GetBaseClassPath(s);
2027 CompilerType compiler_type = GetCompilerType();
2028 std::string cxx_class_name;
2029 bool this_had_base_class =
2030 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2031 if (this_had_base_class) {
2032 if (parent_had_base_class)
2033 s.PutCString("::");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00002034 s.PutCString(cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002036 return parent_had_base_class || this_had_base_class;
2037 }
2038 return false;
Greg Claytone221f822011-01-21 01:59:00 +00002039}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002040
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041ValueObject *ValueObject::GetNonBaseClassParent() {
2042 if (GetParent()) {
2043 if (GetParent()->IsBaseClass())
2044 return GetParent()->GetNonBaseClassParent();
2045 else
2046 return GetParent();
2047 }
2048 return NULL;
2049}
Enrico Granataa3c8f042014-08-19 22:29:08 +00002050
Kate Stoneb9c1b512016-09-06 20:57:50 +00002051bool ValueObject::IsBaseClass(uint32_t &depth) {
2052 if (!IsBaseClass()) {
2053 depth = 0;
2054 return false;
2055 }
2056 if (GetParent()) {
2057 GetParent()->IsBaseClass(depth);
2058 depth = depth + 1;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002059 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002060 }
2061 // TODO: a base of no parent? weird..
2062 depth = 1;
2063 return true;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002064}
2065
Kate Stoneb9c1b512016-09-06 20:57:50 +00002066void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2067 GetExpressionPathFormat epformat) {
2068 // synthetic children do not actually "exist" as part of the hierarchy, and
2069 // sometimes they are consed up in ways
2070 // that don't make sense from an underlying language/API standpoint. So, use a
2071 // special code path here to return
2072 // something that can hopefully be used in expression
2073 if (m_is_synthetic_children_generated) {
2074 UpdateValueIfNeeded();
2075
2076 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2077 if (IsPointerOrReferenceType()) {
2078 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2079 GetValueAsUnsigned(0));
Enrico Granata986fa5f2014-12-09 21:41:16 +00002080 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002081 } else {
2082 uint64_t load_addr =
2083 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2084 if (load_addr != LLDB_INVALID_ADDRESS) {
2085 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2086 load_addr);
2087 return;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002088 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002089 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002090 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002091
2092 if (CanProvideValue()) {
2093 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2094 GetValueAsCString());
2095 return;
Enrico Granata4becb372011-06-29 22:27:15 +00002096 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002097
2098 return;
2099 }
2100
2101 const bool is_deref_of_parent = IsDereferenceOfParent();
2102
2103 if (is_deref_of_parent &&
2104 epformat == eGetExpressionPathFormatDereferencePointers) {
2105 // this is the original format of GetExpressionPath() producing code like
2106 // *(a_ptr).memberName, which is entirely
2107 // fine, until you put this into
2108 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
2109 // a_ptr->memberName.
2110 // the eHonorPointers mode is meant to produce strings in this latter format
2111 s.PutCString("*(");
2112 }
2113
2114 ValueObject *parent = GetParent();
2115
2116 if (parent)
2117 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2118
2119 // if we are a deref_of_parent just because we are synthetic array
2120 // members made up to allow ptr[%d] syntax to work in variable
2121 // printing, then add our name ([%d]) to the expression path
2122 if (m_is_array_item_for_pointer &&
2123 epformat == eGetExpressionPathFormatHonorPointers)
2124 s.PutCString(m_name.AsCString());
2125
2126 if (!IsBaseClass()) {
2127 if (!is_deref_of_parent) {
2128 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2129 if (non_base_class_parent &&
2130 !non_base_class_parent->GetName().IsEmpty()) {
2131 CompilerType non_base_class_parent_compiler_type =
2132 non_base_class_parent->GetCompilerType();
2133 if (non_base_class_parent_compiler_type) {
2134 if (parent && parent->IsDereferenceOfParent() &&
2135 epformat == eGetExpressionPathFormatHonorPointers) {
2136 s.PutCString("->");
2137 } else {
2138 const uint32_t non_base_class_parent_type_info =
2139 non_base_class_parent_compiler_type.GetTypeInfo();
2140
2141 if (non_base_class_parent_type_info & eTypeIsPointer) {
2142 s.PutCString("->");
2143 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2144 !(non_base_class_parent_type_info & eTypeIsArray)) {
2145 s.PutChar('.');
2146 }
2147 }
2148 }
2149 }
2150
2151 const char *name = GetName().GetCString();
2152 if (name) {
2153 if (qualify_cxx_base_classes) {
2154 if (GetBaseClassPath(s))
2155 s.PutCString("::");
2156 }
2157 s.PutCString(name);
2158 }
2159 }
2160 }
2161
2162 if (is_deref_of_parent &&
2163 epformat == eGetExpressionPathFormatDereferencePointers) {
2164 s.PutChar(')');
2165 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002166}
2167
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168ValueObjectSP ValueObject::GetValueForExpressionPath(
2169 const char *expression, const char **first_unparsed,
2170 ExpressionPathScanEndReason *reason_to_stop,
2171 ExpressionPathEndResultType *final_value_type,
2172 const GetValueForExpressionPathOptions &options,
2173 ExpressionPathAftermath *final_task_on_target) {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002174
Kate Stoneb9c1b512016-09-06 20:57:50 +00002175 const char *dummy_first_unparsed;
2176 ExpressionPathScanEndReason dummy_reason_to_stop =
2177 ValueObject::eExpressionPathScanEndReasonUnknown;
2178 ExpressionPathEndResultType dummy_final_value_type =
2179 ValueObject::eExpressionPathEndResultTypeInvalid;
2180 ExpressionPathAftermath dummy_final_task_on_target =
2181 ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002182
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2184 expression, first_unparsed ? first_unparsed : &dummy_first_unparsed,
2185 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2186 final_value_type ? final_value_type : &dummy_final_value_type, options,
2187 final_task_on_target ? final_task_on_target
2188 : &dummy_final_task_on_target);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002189
Kate Stoneb9c1b512016-09-06 20:57:50 +00002190 if (!final_task_on_target ||
2191 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2192 return ret_val;
2193
2194 if (ret_val.get() &&
2195 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2196 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2197 // of plain objects
2198 {
2199 if ((final_task_on_target ? *final_task_on_target
2200 : dummy_final_task_on_target) ==
2201 ValueObject::eExpressionPathAftermathDereference) {
2202 Error error;
2203 ValueObjectSP final_value = ret_val->Dereference(error);
2204 if (error.Fail() || !final_value.get()) {
2205 if (reason_to_stop)
2206 *reason_to_stop =
2207 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2208 if (final_value_type)
2209 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002210 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002211 } else {
2212 if (final_task_on_target)
2213 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2214 return final_value;
2215 }
2216 }
2217 if (*final_task_on_target ==
2218 ValueObject::eExpressionPathAftermathTakeAddress) {
2219 Error error;
2220 ValueObjectSP final_value = ret_val->AddressOf(error);
2221 if (error.Fail() || !final_value.get()) {
2222 if (reason_to_stop)
2223 *reason_to_stop =
2224 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2225 if (final_value_type)
2226 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2227 return ValueObjectSP();
2228 } else {
2229 if (final_task_on_target)
2230 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2231 return final_value;
2232 }
2233 }
2234 }
2235 return ret_val; // final_task_on_target will still have its original value, so
2236 // you know I did not do it
2237}
2238
2239int ValueObject::GetValuesForExpressionPath(
2240 const char *expression, ValueObjectListSP &list,
2241 const char **first_unparsed, ExpressionPathScanEndReason *reason_to_stop,
2242 ExpressionPathEndResultType *final_value_type,
2243 const GetValueForExpressionPathOptions &options,
2244 ExpressionPathAftermath *final_task_on_target) {
2245 const char *dummy_first_unparsed;
2246 ExpressionPathScanEndReason dummy_reason_to_stop;
2247 ExpressionPathEndResultType dummy_final_value_type;
2248 ExpressionPathAftermath dummy_final_task_on_target =
2249 ValueObject::eExpressionPathAftermathNothing;
2250
2251 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2252 expression, first_unparsed ? first_unparsed : &dummy_first_unparsed,
2253 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2254 final_value_type ? final_value_type : &dummy_final_value_type, options,
2255 final_task_on_target ? final_task_on_target
2256 : &dummy_final_task_on_target);
2257
2258 if (!ret_val.get()) // if there are errors, I add nothing to the list
2259 return 0;
2260
2261 if ((reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) !=
2262 eExpressionPathScanEndReasonArrayRangeOperatorMet) {
2263 // I need not expand a range, just post-process the final value and return
2264 if (!final_task_on_target ||
2265 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing) {
2266 list->Append(ret_val);
2267 return 1;
2268 }
2269 if (ret_val.get() &&
2270 (final_value_type ? *final_value_type : dummy_final_value_type) ==
2271 eExpressionPathEndResultTypePlain) // I can only deref and
2272 // takeaddress of plain objects
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002273 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002274 if (*final_task_on_target ==
2275 ValueObject::eExpressionPathAftermathDereference) {
2276 Error error;
2277 ValueObjectSP final_value = ret_val->Dereference(error);
2278 if (error.Fail() || !final_value.get()) {
2279 if (reason_to_stop)
2280 *reason_to_stop =
2281 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2282 if (final_value_type)
2283 *final_value_type =
2284 ValueObject::eExpressionPathEndResultTypeInvalid;
2285 return 0;
2286 } else {
2287 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2288 list->Append(final_value);
2289 return 1;
2290 }
2291 }
2292 if (*final_task_on_target ==
2293 ValueObject::eExpressionPathAftermathTakeAddress) {
2294 Error error;
2295 ValueObjectSP final_value = ret_val->AddressOf(error);
2296 if (error.Fail() || !final_value.get()) {
2297 if (reason_to_stop)
2298 *reason_to_stop =
2299 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2300 if (final_value_type)
2301 *final_value_type =
2302 ValueObject::eExpressionPathEndResultTypeInvalid;
2303 return 0;
2304 } else {
2305 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2306 list->Append(final_value);
2307 return 1;
2308 }
2309 }
2310 }
2311 } else {
2312 return ExpandArraySliceExpression(
2313 first_unparsed ? *first_unparsed : dummy_first_unparsed,
2314 first_unparsed ? first_unparsed : &dummy_first_unparsed, ret_val, list,
2315 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2316 final_value_type ? final_value_type : &dummy_final_value_type, options,
2317 final_task_on_target ? final_task_on_target
2318 : &dummy_final_task_on_target);
2319 }
2320 // in any non-covered case, just do the obviously right thing
2321 list->Append(ret_val);
2322 return 1;
2323}
2324
2325ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
2326 const char *expression_cstr, const char **first_unparsed,
2327 ExpressionPathScanEndReason *reason_to_stop,
2328 ExpressionPathEndResultType *final_result,
2329 const GetValueForExpressionPathOptions &options,
2330 ExpressionPathAftermath *what_next) {
2331 ValueObjectSP root = GetSP();
2332
2333 if (!root.get())
2334 return ValueObjectSP();
2335
2336 *first_unparsed = expression_cstr;
2337
2338 while (true) {
2339
2340 const char *expression_cstr =
2341 *first_unparsed; // hide the top level expression_cstr
2342
2343 CompilerType root_compiler_type = root->GetCompilerType();
2344 CompilerType pointee_compiler_type;
2345 Flags pointee_compiler_type_info;
2346
2347 Flags root_compiler_type_info(
2348 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2349 if (pointee_compiler_type)
2350 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2351
2352 if (!expression_cstr || *expression_cstr == '\0') {
2353 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2354 return root;
2355 }
2356
2357 switch (*expression_cstr) {
2358 case '-': {
2359 if (options.m_check_dot_vs_arrow_syntax &&
2360 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2361 // use -> on a
2362 // non-pointer and I
2363 // must catch the error
2364 {
2365 *first_unparsed = expression_cstr;
2366 *reason_to_stop =
2367 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2368 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2369 return ValueObjectSP();
2370 }
2371 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2372 // extract an ObjC IVar
2373 // when this is forbidden
2374 root_compiler_type_info.Test(eTypeIsPointer) &&
2375 options.m_no_fragile_ivar) {
2376 *first_unparsed = expression_cstr;
2377 *reason_to_stop =
2378 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2379 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2380 return ValueObjectSP();
2381 }
2382 if (expression_cstr[1] != '>') {
2383 *first_unparsed = expression_cstr;
2384 *reason_to_stop =
2385 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2386 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2387 return ValueObjectSP();
2388 }
2389 expression_cstr++; // skip the -
2390 }
2391 LLVM_FALLTHROUGH;
2392 case '.': // or fallthrough from ->
2393 {
2394 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
2395 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2396 // use . on a pointer
2397 // and I must catch the
2398 // error
2399 {
2400 *first_unparsed = expression_cstr;
2401 *reason_to_stop =
2402 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2403 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2404 return ValueObjectSP();
2405 }
2406 expression_cstr++; // skip .
2407 const char *next_separator = strpbrk(expression_cstr + 1, "-.[");
2408 ConstString child_name;
2409 if (!next_separator) // if no other separator just expand this last layer
2410 {
2411 child_name.SetCString(expression_cstr);
2412 ValueObjectSP child_valobj_sp =
2413 root->GetChildMemberWithName(child_name, true);
2414
2415 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002416 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 *first_unparsed = "";
2418 *reason_to_stop =
2419 ValueObject::eExpressionPathScanEndReasonEndOfString;
2420 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2421 return child_valobj_sp;
2422 } else {
2423 switch (options.m_synthetic_children_traversal) {
2424 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2425 None:
2426 break;
2427 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2428 FromSynthetic:
2429 if (root->IsSynthetic()) {
2430 child_valobj_sp = root->GetNonSyntheticValue();
2431 if (child_valobj_sp.get())
2432 child_valobj_sp =
2433 child_valobj_sp->GetChildMemberWithName(child_name, true);
2434 }
2435 break;
2436 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2437 ToSynthetic:
2438 if (!root->IsSynthetic()) {
2439 child_valobj_sp = root->GetSyntheticValue();
2440 if (child_valobj_sp.get())
2441 child_valobj_sp =
2442 child_valobj_sp->GetChildMemberWithName(child_name, true);
2443 }
2444 break;
2445 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2446 Both:
2447 if (root->IsSynthetic()) {
2448 child_valobj_sp = root->GetNonSyntheticValue();
2449 if (child_valobj_sp.get())
2450 child_valobj_sp =
2451 child_valobj_sp->GetChildMemberWithName(child_name, true);
2452 } else {
2453 child_valobj_sp = root->GetSyntheticValue();
2454 if (child_valobj_sp.get())
2455 child_valobj_sp =
2456 child_valobj_sp->GetChildMemberWithName(child_name, true);
2457 }
2458 break;
2459 }
2460 }
2461
2462 // if we are here and options.m_no_synthetic_children is true,
2463 // child_valobj_sp is going to be a NULL SP,
2464 // so we hit the "else" branch, and return an error
2465 if (child_valobj_sp.get()) // if it worked, just return
2466 {
2467 *first_unparsed = "";
2468 *reason_to_stop =
2469 ValueObject::eExpressionPathScanEndReasonEndOfString;
2470 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2471 return child_valobj_sp;
2472 } else {
2473 *first_unparsed = expression_cstr;
2474 *reason_to_stop =
2475 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2476 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2477 return ValueObjectSP();
2478 }
2479 } else // other layers do expand
2480 {
2481 child_name.SetCStringWithLength(expression_cstr,
2482 next_separator - expression_cstr);
2483 ValueObjectSP child_valobj_sp =
2484 root->GetChildMemberWithName(child_name, true);
2485 if (child_valobj_sp.get()) // store the new root and move on
2486 {
2487 root = child_valobj_sp;
2488 *first_unparsed = next_separator;
2489 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2490 continue;
2491 } else {
2492 switch (options.m_synthetic_children_traversal) {
2493 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2494 None:
2495 break;
2496 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2497 FromSynthetic:
2498 if (root->IsSynthetic()) {
2499 child_valobj_sp = root->GetNonSyntheticValue();
2500 if (child_valobj_sp.get())
2501 child_valobj_sp =
2502 child_valobj_sp->GetChildMemberWithName(child_name, true);
2503 }
2504 break;
2505 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2506 ToSynthetic:
2507 if (!root->IsSynthetic()) {
2508 child_valobj_sp = root->GetSyntheticValue();
2509 if (child_valobj_sp.get())
2510 child_valobj_sp =
2511 child_valobj_sp->GetChildMemberWithName(child_name, true);
2512 }
2513 break;
2514 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2515 Both:
2516 if (root->IsSynthetic()) {
2517 child_valobj_sp = root->GetNonSyntheticValue();
2518 if (child_valobj_sp.get())
2519 child_valobj_sp =
2520 child_valobj_sp->GetChildMemberWithName(child_name, true);
2521 } else {
2522 child_valobj_sp = root->GetSyntheticValue();
2523 if (child_valobj_sp.get())
2524 child_valobj_sp =
2525 child_valobj_sp->GetChildMemberWithName(child_name, true);
2526 }
2527 break;
2528 }
2529 }
2530
2531 // if we are here and options.m_no_synthetic_children is true,
2532 // child_valobj_sp is going to be a NULL SP,
2533 // so we hit the "else" branch, and return an error
2534 if (child_valobj_sp.get()) // if it worked, move on
2535 {
2536 root = child_valobj_sp;
2537 *first_unparsed = next_separator;
2538 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2539 continue;
2540 } else {
2541 *first_unparsed = expression_cstr;
2542 *reason_to_stop =
2543 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2544 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2545 return ValueObjectSP();
2546 }
2547 }
2548 break;
2549 }
2550 case '[': {
2551 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2552 !root_compiler_type_info.Test(eTypeIsPointer) &&
2553 !root_compiler_type_info.Test(
2554 eTypeIsVector)) // if this is not a T[] nor a T*
2555 {
2556 if (!root_compiler_type_info.Test(
2557 eTypeIsScalar)) // if this is not even a scalar...
2558 {
2559 if (options.m_synthetic_children_traversal ==
2560 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2561 None) // ...only chance left is synthetic
2562 {
2563 *first_unparsed = expression_cstr;
2564 *reason_to_stop =
2565 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2566 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2567 return ValueObjectSP();
2568 }
2569 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2570 // check that we can
2571 // expand bitfields
2572 {
2573 *first_unparsed = expression_cstr;
2574 *reason_to_stop =
2575 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2576 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2577 return ValueObjectSP();
2578 }
2579 }
2580 if (*(expression_cstr + 1) ==
2581 ']') // if this is an unbounded range it only works for arrays
2582 {
2583 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2584 *first_unparsed = expression_cstr;
2585 *reason_to_stop =
2586 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2587 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2588 return ValueObjectSP();
2589 } else // even if something follows, we cannot expand unbounded ranges,
2590 // just let the caller do it
2591 {
2592 *first_unparsed = expression_cstr + 2;
2593 *reason_to_stop =
2594 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2595 *final_result =
2596 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2597 return root;
2598 }
2599 }
2600 const char *separator_position = ::strchr(expression_cstr + 1, '-');
2601 const char *close_bracket_position = ::strchr(expression_cstr + 1, ']');
2602 if (!close_bracket_position) // if there is no ], this is a syntax error
2603 {
2604 *first_unparsed = expression_cstr;
2605 *reason_to_stop =
2606 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2607 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2608 return ValueObjectSP();
2609 }
2610 if (!separator_position ||
2611 separator_position > close_bracket_position) // if no separator, this
2612 // is either [] or [N]
2613 {
2614 char *end = NULL;
2615 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
2616 if (!end || end != close_bracket_position) // if something weird is in
2617 // our way return an error
2618 {
2619 *first_unparsed = expression_cstr;
2620 *reason_to_stop =
2621 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2622 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2623 return ValueObjectSP();
2624 }
2625 if (end - expression_cstr ==
2626 1) // if this is [], only return a valid value for arrays
2627 {
2628 if (root_compiler_type_info.Test(eTypeIsArray)) {
2629 *first_unparsed = expression_cstr + 2;
2630 *reason_to_stop =
2631 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2632 *final_result =
2633 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002634 return root;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002635 } else {
2636 *first_unparsed = expression_cstr;
2637 *reason_to_stop =
2638 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2639 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2640 return ValueObjectSP();
2641 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002642 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643 // from here on we do have a valid index
2644 if (root_compiler_type_info.Test(eTypeIsArray)) {
2645 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2646 if (!child_valobj_sp)
2647 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2648 if (!child_valobj_sp)
2649 if (root->HasSyntheticValue() &&
2650 root->GetSyntheticValue()->GetNumChildren() > index)
2651 child_valobj_sp =
2652 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2653 if (child_valobj_sp) {
2654 root = child_valobj_sp;
2655 *first_unparsed = end + 1; // skip ]
2656 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2657 continue;
2658 } else {
2659 *first_unparsed = expression_cstr;
2660 *reason_to_stop =
2661 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2662 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2663 return ValueObjectSP();
2664 }
2665 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2666 if (*what_next ==
2667 ValueObject::
2668 eExpressionPathAftermathDereference && // if this is a
2669 // ptr-to-scalar, I
2670 // am accessing it
2671 // by index and I
2672 // would have
2673 // deref'ed anyway,
2674 // then do it now
2675 // and use this as
2676 // a bitfield
2677 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2678 Error error;
2679 root = root->Dereference(error);
2680 if (error.Fail() || !root.get()) {
2681 *first_unparsed = expression_cstr;
2682 *reason_to_stop =
2683 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2684 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2685 return ValueObjectSP();
2686 } else {
2687 *what_next = eExpressionPathAftermathNothing;
2688 continue;
2689 }
2690 } else {
2691 if (root->GetCompilerType().GetMinimumLanguage() ==
2692 eLanguageTypeObjC &&
2693 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2694 root->HasSyntheticValue() &&
2695 (options.m_synthetic_children_traversal ==
2696 GetValueForExpressionPathOptions::
2697 SyntheticChildrenTraversal::ToSynthetic ||
2698 options.m_synthetic_children_traversal ==
2699 GetValueForExpressionPathOptions::
2700 SyntheticChildrenTraversal::Both)) {
2701 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2702 } else
2703 root = root->GetSyntheticArrayMember(index, true);
2704 if (!root.get()) {
2705 *first_unparsed = expression_cstr;
2706 *reason_to_stop =
2707 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2708 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2709 return ValueObjectSP();
2710 } else {
2711 *first_unparsed = end + 1; // skip ]
2712 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2713 continue;
2714 }
2715 }
2716 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2717 root = root->GetSyntheticBitFieldChild(index, index, true);
2718 if (!root.get()) {
2719 *first_unparsed = expression_cstr;
2720 *reason_to_stop =
2721 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2722 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2723 return ValueObjectSP();
2724 } else // we do not know how to expand members of bitfields, so we
2725 // just return and let the caller do any further processing
2726 {
2727 *first_unparsed = end + 1; // skip ]
2728 *reason_to_stop = ValueObject::
2729 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2730 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2731 return root;
2732 }
2733 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2734 root = root->GetChildAtIndex(index, true);
2735 if (!root.get()) {
2736 *first_unparsed = expression_cstr;
2737 *reason_to_stop =
2738 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2739 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2740 return ValueObjectSP();
2741 } else {
2742 *first_unparsed = end + 1; // skip ]
2743 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2744 continue;
2745 }
2746 } else if (options.m_synthetic_children_traversal ==
2747 GetValueForExpressionPathOptions::
2748 SyntheticChildrenTraversal::ToSynthetic ||
2749 options.m_synthetic_children_traversal ==
2750 GetValueForExpressionPathOptions::
2751 SyntheticChildrenTraversal::Both) {
2752 if (root->HasSyntheticValue())
2753 root = root->GetSyntheticValue();
2754 else if (!root->IsSynthetic()) {
2755 *first_unparsed = expression_cstr;
2756 *reason_to_stop =
2757 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2758 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2759 return ValueObjectSP();
2760 }
2761 // if we are here, then root itself is a synthetic VO.. should be good
2762 // to go
2763
2764 if (!root.get()) {
2765 *first_unparsed = expression_cstr;
2766 *reason_to_stop =
2767 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2768 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2769 return ValueObjectSP();
2770 }
2771 root = root->GetChildAtIndex(index, true);
2772 if (!root.get()) {
2773 *first_unparsed = expression_cstr;
2774 *reason_to_stop =
2775 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2776 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2777 return ValueObjectSP();
2778 } else {
2779 *first_unparsed = end + 1; // skip ]
2780 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2781 continue;
2782 }
2783 } else {
2784 *first_unparsed = expression_cstr;
2785 *reason_to_stop =
2786 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2787 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2788 return ValueObjectSP();
2789 }
2790 } else // we have a low and a high index
2791 {
2792 char *end = NULL;
2793 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
2794 if (!end || end != separator_position) // if something weird is in our
2795 // way return an error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002796 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002797 *first_unparsed = expression_cstr;
2798 *reason_to_stop =
2799 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2800 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2801 return ValueObjectSP();
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002803 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
2804 if (!end || end != close_bracket_position) // if something weird is in
2805 // our way return an error
2806 {
2807 *first_unparsed = expression_cstr;
2808 *reason_to_stop =
2809 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2810 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2811 return ValueObjectSP();
2812 }
2813 if (index_lower > index_higher) // swap indices if required
2814 {
2815 unsigned long temp = index_lower;
2816 index_lower = index_higher;
2817 index_higher = temp;
2818 }
2819 if (root_compiler_type_info.Test(
2820 eTypeIsScalar)) // expansion only works for scalars
2821 {
2822 root =
2823 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2824 if (!root.get()) {
2825 *first_unparsed = expression_cstr;
2826 *reason_to_stop =
2827 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2828 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2829 return ValueObjectSP();
2830 } else {
2831 *first_unparsed = end + 1; // skip ]
2832 *reason_to_stop = ValueObject::
2833 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2834 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2835 return root;
2836 }
2837 } else if (root_compiler_type_info.Test(
2838 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2839 // accessing it by index and I would
2840 // have deref'ed anyway, then do it
2841 // now and use this as a bitfield
2842 *what_next ==
2843 ValueObject::eExpressionPathAftermathDereference &&
2844 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2845 Error error;
2846 root = root->Dereference(error);
2847 if (error.Fail() || !root.get()) {
2848 *first_unparsed = expression_cstr;
2849 *reason_to_stop =
2850 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2851 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2852 return ValueObjectSP();
2853 } else {
2854 *what_next = ValueObject::eExpressionPathAftermathNothing;
2855 continue;
2856 }
2857 } else {
2858 *first_unparsed = expression_cstr;
2859 *reason_to_stop =
2860 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2861 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2862 return root;
2863 }
2864 }
2865 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002866 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002867 default: // some non-separator is in the way
2868 {
2869 *first_unparsed = expression_cstr;
2870 *reason_to_stop =
2871 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2872 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2873 return ValueObjectSP();
2874 break;
2875 }
2876 }
2877 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002878}
2879
Kate Stoneb9c1b512016-09-06 20:57:50 +00002880int ValueObject::ExpandArraySliceExpression(
2881 const char *expression_cstr, const char **first_unparsed,
2882 ValueObjectSP root, ValueObjectListSP &list,
2883 ExpressionPathScanEndReason *reason_to_stop,
2884 ExpressionPathEndResultType *final_result,
2885 const GetValueForExpressionPathOptions &options,
2886 ExpressionPathAftermath *what_next) {
2887 if (!root.get())
2888 return 0;
2889
2890 *first_unparsed = expression_cstr;
2891
2892 while (true) {
2893
2894 const char *expression_cstr =
2895 *first_unparsed; // hide the top level expression_cstr
2896
2897 CompilerType root_compiler_type = root->GetCompilerType();
2898 CompilerType pointee_compiler_type;
2899 Flags pointee_compiler_type_info;
2900 Flags root_compiler_type_info(
2901 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2902 if (pointee_compiler_type)
2903 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2904
2905 if (!expression_cstr || *expression_cstr == '\0') {
2906 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2907 list->Append(root);
2908 return 1;
2909 }
2910
2911 switch (*expression_cstr) {
2912 case '[': {
2913 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2914 !root_compiler_type_info.Test(
2915 eTypeIsPointer)) // if this is not a T[] nor a T*
2916 {
2917 if (!root_compiler_type_info.Test(eTypeIsScalar)) // if this is not even
2918 // a scalar, this
2919 // syntax is just
2920 // plain wrong!
2921 {
2922 *first_unparsed = expression_cstr;
2923 *reason_to_stop =
2924 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2925 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2926 return 0;
2927 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2928 // check that we can
2929 // expand bitfields
2930 {
2931 *first_unparsed = expression_cstr;
2932 *reason_to_stop =
2933 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2934 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2935 return 0;
2936 }
2937 }
2938 if (*(expression_cstr + 1) ==
2939 ']') // if this is an unbounded range it only works for arrays
2940 {
2941 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2942 *first_unparsed = expression_cstr;
2943 *reason_to_stop =
2944 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2945 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2946 return 0;
2947 } else // expand this into list
2948 {
2949 const size_t max_index = root->GetNumChildren() - 1;
2950 for (size_t index = 0; index < max_index; index++) {
2951 ValueObjectSP child = root->GetChildAtIndex(index, true);
2952 list->Append(child);
2953 }
2954 *first_unparsed = expression_cstr + 2;
2955 *reason_to_stop =
2956 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2957 *final_result =
2958 ValueObject::eExpressionPathEndResultTypeValueObjectList;
2959 return max_index; // tell me number of items I added to the VOList
2960 }
2961 }
2962 const char *separator_position = ::strchr(expression_cstr + 1, '-');
2963 const char *close_bracket_position = ::strchr(expression_cstr + 1, ']');
2964 if (!close_bracket_position) // if there is no ], this is a syntax error
2965 {
2966 *first_unparsed = expression_cstr;
2967 *reason_to_stop =
2968 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2969 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002970 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002971 }
2972 if (!separator_position ||
2973 separator_position > close_bracket_position) // if no separator, this
2974 // is either [] or [N]
2975 {
2976 char *end = NULL;
2977 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
2978 if (!end || end != close_bracket_position) // if something weird is in
2979 // our way return an error
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002980 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002981 *first_unparsed = expression_cstr;
2982 *reason_to_stop =
2983 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2984 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2985 return 0;
2986 }
2987 if (end - expression_cstr ==
2988 1) // if this is [], only return a valid value for arrays
2989 {
2990 if (root_compiler_type_info.Test(eTypeIsArray)) {
2991 const size_t max_index = root->GetNumChildren() - 1;
2992 for (size_t index = 0; index < max_index; index++) {
2993 ValueObjectSP child = root->GetChildAtIndex(index, true);
2994 list->Append(child);
2995 }
2996 *first_unparsed = expression_cstr + 2;
2997 *reason_to_stop =
2998 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2999 *final_result =
3000 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3001 return max_index; // tell me number of items I added to the VOList
3002 } else {
3003 *first_unparsed = expression_cstr;
3004 *reason_to_stop =
3005 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3006 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3007 return 0;
3008 }
3009 }
3010 // from here on we do have a valid index
3011 if (root_compiler_type_info.Test(eTypeIsArray)) {
3012 root = root->GetChildAtIndex(index, true);
3013 if (!root.get()) {
3014 *first_unparsed = expression_cstr;
3015 *reason_to_stop =
3016 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3017 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3018 return 0;
3019 } else {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003020 list->Append(root);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003021 *first_unparsed = end + 1; // skip ]
3022 *reason_to_stop =
3023 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3024 *final_result =
3025 ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003026 return 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027 }
3028 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
3029 if (*what_next ==
3030 ValueObject::
3031 eExpressionPathAftermathDereference && // if this is a
3032 // ptr-to-scalar, I
3033 // am accessing it
3034 // by index and I
3035 // would have
3036 // deref'ed anyway,
3037 // then do it now
3038 // and use this as
3039 // a bitfield
3040 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3041 Error error;
3042 root = root->Dereference(error);
3043 if (error.Fail() || !root.get()) {
3044 *first_unparsed = expression_cstr;
3045 *reason_to_stop =
3046 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3047 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3048 return 0;
3049 } else {
3050 *what_next = eExpressionPathAftermathNothing;
3051 continue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003052 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 } else {
3054 root = root->GetSyntheticArrayMember(index, true);
3055 if (!root.get()) {
3056 *first_unparsed = expression_cstr;
3057 *reason_to_stop =
3058 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3059 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3060 return 0;
3061 } else {
3062 list->Append(root);
3063 *first_unparsed = end + 1; // skip ]
3064 *reason_to_stop = ValueObject::
3065 eExpressionPathScanEndReasonRangeOperatorExpanded;
3066 *final_result =
3067 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3068 return 1;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003070 }
3071 } else /*if (ClangASTContext::IsScalarType(root_compiler_type))*/
Enrico Granata9f1e2042012-04-24 22:15:37 +00003072 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003073 root = root->GetSyntheticBitFieldChild(index, index, true);
3074 if (!root.get()) {
3075 *first_unparsed = expression_cstr;
3076 *reason_to_stop =
3077 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3078 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3079 return 0;
3080 } else // we do not know how to expand members of bitfields, so we
3081 // just return and let the caller do any further processing
3082 {
3083 list->Append(root);
3084 *first_unparsed = end + 1; // skip ]
3085 *reason_to_stop =
3086 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3087 *final_result =
3088 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3089 return 1;
3090 }
Enrico Granata9f1e2042012-04-24 22:15:37 +00003091 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003092 } else // we have a low and a high index
3093 {
3094 char *end = NULL;
3095 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
3096 if (!end || end != separator_position) // if something weird is in our
3097 // way return an error
3098 {
3099 *first_unparsed = expression_cstr;
3100 *reason_to_stop =
3101 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3102 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3103 return 0;
3104 }
3105 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
3106 if (!end || end != close_bracket_position) // if something weird is in
3107 // our way return an error
3108 {
3109 *first_unparsed = expression_cstr;
3110 *reason_to_stop =
3111 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3112 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3113 return 0;
3114 }
3115 if (index_lower > index_higher) // swap indices if required
3116 {
3117 unsigned long temp = index_lower;
3118 index_lower = index_higher;
3119 index_higher = temp;
3120 }
3121 if (root_compiler_type_info.Test(
3122 eTypeIsScalar)) // expansion only works for scalars
3123 {
3124 root =
3125 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3126 if (!root.get()) {
3127 *first_unparsed = expression_cstr;
3128 *reason_to_stop =
3129 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3130 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3131 return 0;
3132 } else {
3133 list->Append(root);
3134 *first_unparsed = end + 1; // skip ]
3135 *reason_to_stop =
3136 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3137 *final_result =
3138 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3139 return 1;
3140 }
3141 } else if (root_compiler_type_info.Test(
3142 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
3143 // accessing it by index and I would
3144 // have deref'ed anyway, then do it
3145 // now and use this as a bitfield
3146 *what_next ==
3147 ValueObject::eExpressionPathAftermathDereference &&
3148 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3149 Error error;
3150 root = root->Dereference(error);
3151 if (error.Fail() || !root.get()) {
3152 *first_unparsed = expression_cstr;
3153 *reason_to_stop =
3154 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3155 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3156 return 0;
3157 } else {
3158 *what_next = ValueObject::eExpressionPathAftermathNothing;
3159 continue;
3160 }
3161 } else {
3162 for (unsigned long index = index_lower; index <= index_higher;
3163 index++) {
3164 ValueObjectSP child = root->GetChildAtIndex(index, true);
3165 list->Append(child);
3166 }
3167 *first_unparsed = end + 1;
3168 *reason_to_stop =
3169 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3170 *final_result =
3171 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3172 return index_higher - index_lower +
3173 1; // tell me number of items I added to the VOList
3174 }
3175 }
3176 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003178 default: // some non-[ separator, or something entirely wrong, is in the way
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003179 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003180 *first_unparsed = expression_cstr;
3181 *reason_to_stop =
3182 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3183 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3184 return 0;
3185 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003186 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003187 }
3188 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003189}
3190
Kate Stoneb9c1b512016-09-06 20:57:50 +00003191void ValueObject::LogValueObject(Log *log) {
3192 if (log)
3193 return LogValueObject(log, DumpValueObjectOptions(*this));
Enrico Granata538a88a2014-10-09 18:24:30 +00003194}
3195
Kate Stoneb9c1b512016-09-06 20:57:50 +00003196void ValueObject::LogValueObject(Log *log,
3197 const DumpValueObjectOptions &options) {
3198 if (log) {
3199 StreamString s;
3200 Dump(s, options);
3201 if (s.GetSize())
3202 log->PutCString(s.GetData());
3203 }
Greg Clayton759e7442014-07-19 00:12:57 +00003204}
3205
Kate Stoneb9c1b512016-09-06 20:57:50 +00003206void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
Chaoren Lind7bdc272015-07-31 00:35:40 +00003207
Kate Stoneb9c1b512016-09-06 20:57:50 +00003208void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
3209 ValueObjectPrinter printer(this, &s, options);
3210 printer.PrintValueObject();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003211}
3212
Kate Stoneb9c1b512016-09-06 20:57:50 +00003213ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
3214 ValueObjectSP valobj_sp;
3215
3216 if (UpdateValueIfNeeded(false) && m_error.Success()) {
3217 ExecutionContext exe_ctx(GetExecutionContextRef());
3218
3219 DataExtractor data;
3220 data.SetByteOrder(m_data.GetByteOrder());
3221 data.SetAddressByteSize(m_data.GetAddressByteSize());
3222
3223 if (IsBitfield()) {
3224 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3225 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3226 } else
3227 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3228
3229 valobj_sp = ValueObjectConstResult::Create(
3230 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
3231 GetAddressOf());
3232 }
3233
3234 if (!valobj_sp) {
3235 ExecutionContext exe_ctx(GetExecutionContextRef());
3236 valobj_sp = ValueObjectConstResult::Create(
3237 exe_ctx.GetBestExecutionContextScope(), m_error);
3238 }
3239 return valobj_sp;
3240}
3241
3242ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
3243 lldb::DynamicValueType dynValue, bool synthValue) {
3244 ValueObjectSP result_sp(GetSP());
3245
3246 switch (dynValue) {
3247 case lldb::eDynamicCanRunTarget:
3248 case lldb::eDynamicDontRunTarget: {
3249 if (!result_sp->IsDynamic()) {
3250 if (result_sp->GetDynamicValue(dynValue))
3251 result_sp = result_sp->GetDynamicValue(dynValue);
3252 }
3253 } break;
3254 case lldb::eNoDynamicValues: {
3255 if (result_sp->IsDynamic()) {
3256 if (result_sp->GetStaticValue())
3257 result_sp = result_sp->GetStaticValue();
3258 }
3259 } break;
3260 }
3261
3262 if (synthValue) {
3263 if (!result_sp->IsSynthetic()) {
3264 if (result_sp->GetSyntheticValue())
3265 result_sp = result_sp->GetSyntheticValue();
3266 }
3267 } else {
3268 if (result_sp->IsSynthetic()) {
3269 if (result_sp->GetNonSyntheticValue())
3270 result_sp = result_sp->GetNonSyntheticValue();
3271 }
3272 }
3273
3274 return result_sp;
3275}
3276
3277lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
3278 CompilerType pointee_type;
3279 CompilerType this_type(GetCompilerType());
3280 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3281 if (type_info) {
3282 bool ptr_or_ref = false;
3283 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
3284 ptr_or_ref = true;
3285 type_info = pointee_type.GetTypeInfo();
3286 }
3287
3288 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
3289 if ((type_info & cpp_class) == cpp_class) {
3290 if (ptr_or_ref) {
3291 address_type = GetAddressTypeOfChildren();
3292 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3293 } else
3294 return GetAddressOf(false, &address_type);
3295 }
3296 }
3297
3298 address_type = eAddressTypeInvalid;
3299 return LLDB_INVALID_ADDRESS;
3300}
3301
3302ValueObjectSP ValueObject::Dereference(Error &error) {
3303 if (m_deref_valobj)
3304 return m_deref_valobj->GetSP();
3305
3306 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
3307 if (is_pointer_or_reference_type) {
3308 bool omit_empty_base_classes = true;
3309 bool ignore_array_bounds = false;
3310
3311 std::string child_name_str;
3312 uint32_t child_byte_size = 0;
3313 int32_t child_byte_offset = 0;
3314 uint32_t child_bitfield_bit_size = 0;
3315 uint32_t child_bitfield_bit_offset = 0;
3316 bool child_is_base_class = false;
3317 bool child_is_deref_of_parent = false;
3318 const bool transparent_pointers = false;
3319 CompilerType compiler_type = GetCompilerType();
3320 CompilerType child_compiler_type;
3321 uint64_t language_flags;
3322
3323 ExecutionContext exe_ctx(GetExecutionContextRef());
3324
3325 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
3326 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
3327 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
3328 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
3329 child_is_deref_of_parent, this, language_flags);
3330 if (child_compiler_type && child_byte_size) {
3331 ConstString child_name;
3332 if (!child_name_str.empty())
3333 child_name.SetCString(child_name_str.c_str());
3334
3335 m_deref_valobj = new ValueObjectChild(
3336 *this, child_compiler_type, child_name, child_byte_size,
3337 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
3338 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
3339 language_flags);
3340 }
3341 }
3342
3343 if (m_deref_valobj) {
Greg Clayton54979cd2010-12-15 05:08:08 +00003344 error.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003345 return m_deref_valobj->GetSP();
3346 } else {
3347 StreamString strm;
3348 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003349
Kate Stoneb9c1b512016-09-06 20:57:50 +00003350 if (is_pointer_or_reference_type)
3351 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
3352 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00003353 strm.GetData());
Sean Callananed185ab2013-04-19 19:47:32 +00003354 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00003355 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
3356 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00003357 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003358 return ValueObjectSP();
3359 }
3360}
3361
3362ValueObjectSP ValueObject::AddressOf(Error &error) {
3363 if (m_addr_of_valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00003364 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003365
Kate Stoneb9c1b512016-09-06 20:57:50 +00003366 AddressType address_type = eAddressTypeInvalid;
3367 const bool scalar_is_load_address = false;
3368 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
3369 error.Clear();
3370 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
3371 switch (address_type) {
3372 case eAddressTypeInvalid: {
3373 StreamString expr_path_strm;
3374 GetExpressionPath(expr_path_strm, true);
3375 error.SetErrorStringWithFormat("'%s' is not in memory",
Zachary Turnerc1564272016-11-16 21:15:24 +00003376 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003377 } break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003378
Kate Stoneb9c1b512016-09-06 20:57:50 +00003379 case eAddressTypeFile:
3380 case eAddressTypeLoad: {
3381 CompilerType compiler_type = GetCompilerType();
3382 if (compiler_type) {
3383 std::string name(1, '&');
3384 name.append(m_name.AsCString(""));
3385 ExecutionContext exe_ctx(GetExecutionContextRef());
3386 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
3387 exe_ctx.GetBestExecutionContextScope(),
3388 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
3389 eAddressTypeInvalid, m_data.GetAddressByteSize());
3390 }
3391 } break;
3392 default:
3393 break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003394 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003395 } else {
3396 StreamString expr_path_strm;
3397 GetExpressionPath(expr_path_strm, true);
3398 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
Zachary Turnerc1564272016-11-16 21:15:24 +00003399 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003400 }
3401
3402 return m_addr_of_valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003403}
3404
Kate Stoneb9c1b512016-09-06 20:57:50 +00003405ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
3406 return ValueObjectCast::Create(*this, GetName(), compiler_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003407}
3408
Kate Stoneb9c1b512016-09-06 20:57:50 +00003409ValueObjectSP ValueObject::CastPointerType(const char *name,
3410 CompilerType &compiler_type) {
3411 ValueObjectSP valobj_sp;
3412 AddressType address_type;
3413 addr_t ptr_value = GetPointerValue(&address_type);
3414
3415 if (ptr_value != LLDB_INVALID_ADDRESS) {
3416 Address ptr_addr(ptr_value);
3417 ExecutionContext exe_ctx(GetExecutionContextRef());
3418 valobj_sp = ValueObjectMemory::Create(
3419 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
3420 }
3421 return valobj_sp;
Jim Ingham6035b672011-03-31 00:19:25 +00003422}
3423
Kate Stoneb9c1b512016-09-06 20:57:50 +00003424ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
3425 ValueObjectSP valobj_sp;
3426 AddressType address_type;
3427 addr_t ptr_value = GetPointerValue(&address_type);
3428
3429 if (ptr_value != LLDB_INVALID_ADDRESS) {
3430 Address ptr_addr(ptr_value);
3431 ExecutionContext exe_ctx(GetExecutionContextRef());
3432 valobj_sp = ValueObjectMemory::Create(
3433 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
3434 }
3435 return valobj_sp;
3436}
3437
3438ValueObject::EvaluationPoint::EvaluationPoint()
3439 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
3440
3441ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
3442 bool use_selected)
3443 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
3444 ExecutionContext exe_ctx(exe_scope);
3445 TargetSP target_sp(exe_ctx.GetTargetSP());
3446 if (target_sp) {
3447 m_exe_ctx_ref.SetTargetSP(target_sp);
3448 ProcessSP process_sp(exe_ctx.GetProcessSP());
3449 if (!process_sp)
3450 process_sp = target_sp->GetProcessSP();
3451
3452 if (process_sp) {
3453 m_mod_id = process_sp->GetModID();
3454 m_exe_ctx_ref.SetProcessSP(process_sp);
3455
3456 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3457
3458 if (!thread_sp) {
3459 if (use_selected)
3460 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3461 }
3462
3463 if (thread_sp) {
3464 m_exe_ctx_ref.SetThreadSP(thread_sp);
3465
3466 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3467 if (!frame_sp) {
3468 if (use_selected)
3469 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003470 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003471 if (frame_sp)
3472 m_exe_ctx_ref.SetFrameSP(frame_sp);
3473 }
Jim Ingham6035b672011-03-31 00:19:25 +00003474 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003475 }
Jim Ingham6035b672011-03-31 00:19:25 +00003476}
3477
Kate Stoneb9c1b512016-09-06 20:57:50 +00003478ValueObject::EvaluationPoint::EvaluationPoint(
3479 const ValueObject::EvaluationPoint &rhs)
3480 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
Jim Ingham6035b672011-03-31 00:19:25 +00003481
Kate Stoneb9c1b512016-09-06 20:57:50 +00003482ValueObject::EvaluationPoint::~EvaluationPoint() {}
Jim Ingham6035b672011-03-31 00:19:25 +00003483
Kate Stoneb9c1b512016-09-06 20:57:50 +00003484// This function checks the EvaluationPoint against the current process state.
3485// If the current
3486// state matches the evaluation point, or the evaluation point is already
3487// invalid, then we return
3488// false, meaning "no change". If the current state is different, we update our
3489// state, and return
3490// true meaning "yes, change". If we did see a change, we also set
3491// m_needs_update to true, so
Jim Ingham6035b672011-03-31 00:19:25 +00003492// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003493// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003494
Kate Stoneb9c1b512016-09-06 20:57:50 +00003495bool ValueObject::EvaluationPoint::SyncWithProcessState(
3496 bool accept_invalid_exe_ctx) {
3497 // Start with the target, if it is NULL, then we're obviously not going to get
3498 // any further:
3499 const bool thread_and_frame_only_if_stopped = true;
3500 ExecutionContext exe_ctx(
3501 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3502
3503 if (exe_ctx.GetTargetPtr() == NULL)
3504 return false;
3505
3506 // If we don't have a process nothing can change.
3507 Process *process = exe_ctx.GetProcessPtr();
3508 if (process == NULL)
3509 return false;
3510
3511 // If our stop id is the current stop ID, nothing has changed:
3512 ProcessModID current_mod_id = process->GetModID();
3513
3514 // If the current stop id is 0, either we haven't run yet, or the process
3515 // state has been cleared.
3516 // In either case, we aren't going to be able to sync with the process state.
3517 if (current_mod_id.GetStopID() == 0)
3518 return false;
3519
3520 bool changed = false;
3521 const bool was_valid = m_mod_id.IsValid();
3522 if (was_valid) {
3523 if (m_mod_id == current_mod_id) {
3524 // Everything is already up to date in this object, no need to
3525 // update the execution context scope.
3526 changed = false;
3527 } else {
3528 m_mod_id = current_mod_id;
3529 m_needs_update = true;
3530 changed = true;
3531 }
3532 }
3533
3534 // Now re-look up the thread and frame in case the underlying objects have
3535 // gone away & been recreated.
3536 // That way we'll be sure to return a valid exe_scope.
3537 // If we used to have a thread or a frame but can't find it anymore, then mark
3538 // ourselves as invalid.
3539
3540 if (!accept_invalid_exe_ctx) {
3541 if (m_exe_ctx_ref.HasThreadRef()) {
3542 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3543 if (thread_sp) {
3544 if (m_exe_ctx_ref.HasFrameRef()) {
3545 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3546 if (!frame_sp) {
3547 // We used to have a frame, but now it is gone
3548 SetInvalid();
3549 changed = was_valid;
3550 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003551 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003552 } else {
3553 // We used to have a thread, but now it is gone
3554 SetInvalid();
3555 changed = was_valid;
3556 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003558 }
Enrico Granatabb642e52015-05-16 01:27:00 +00003559
Kate Stoneb9c1b512016-09-06 20:57:50 +00003560 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003561}
3562
Kate Stoneb9c1b512016-09-06 20:57:50 +00003563void ValueObject::EvaluationPoint::SetUpdated() {
3564 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3565 if (process_sp)
3566 m_mod_id = process_sp->GetModID();
3567 m_needs_update = false;
Johnny Chen44805302011-07-19 19:48:13 +00003568}
Enrico Granata9128ee22011-09-06 19:20:51 +00003569
Kate Stoneb9c1b512016-09-06 20:57:50 +00003570void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3571 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3572 eClearUserVisibleDataItemsValue)
3573 m_value_str.clear();
3574
3575 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3576 eClearUserVisibleDataItemsLocation)
3577 m_location_str.clear();
3578
3579 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3580 eClearUserVisibleDataItemsSummary)
3581 m_summary_str.clear();
3582
3583 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3584 eClearUserVisibleDataItemsDescription)
3585 m_object_desc_str.clear();
3586
3587 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3588 eClearUserVisibleDataItemsSyntheticChildren) {
3589 if (m_synthetic_value)
3590 m_synthetic_value = NULL;
3591 }
3592
3593 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3594 eClearUserVisibleDataItemsValidator)
3595 m_validation_result.reset();
Enrico Granata9128ee22011-09-06 19:20:51 +00003596}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003597
Kate Stoneb9c1b512016-09-06 20:57:50 +00003598SymbolContextScope *ValueObject::GetSymbolContextScope() {
3599 if (m_parent) {
3600 if (!m_parent->IsPointerOrReferenceType())
3601 return m_parent->GetSymbolContextScope();
3602 }
3603 return NULL;
Enrico Granata972be532014-12-17 21:18:43 +00003604}
3605
Zachary Turneraa5611f2016-11-13 03:29:46 +00003606lldb::ValueObjectSP
3607ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3608 llvm::StringRef expression,
3609 const ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3611 EvaluateExpressionOptions());
3612}
Enrico Granata972be532014-12-17 21:18:43 +00003613
Kate Stoneb9c1b512016-09-06 20:57:50 +00003614lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003615 llvm::StringRef name, llvm::StringRef expression,
3616 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003617 lldb::ValueObjectSP retval_sp;
3618 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3619 if (!target_sp)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003620 return retval_sp;
Zachary Turneraa5611f2016-11-13 03:29:46 +00003621 if (expression.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003622 return retval_sp;
3623 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3624 retval_sp, options);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003625 if (retval_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003626 retval_sp->SetName(ConstString(name));
3627 return retval_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003628}
3629
Zachary Turneraa5611f2016-11-13 03:29:46 +00003630lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3631 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3632 CompilerType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003633 if (type) {
3634 CompilerType pointer_type(type.GetPointerType());
3635 if (pointer_type) {
3636 lldb::DataBufferSP buffer(
3637 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3638 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3639 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3640 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3641 exe_ctx.GetAddressByteSize()));
3642 if (ptr_result_valobj_sp) {
3643 ptr_result_valobj_sp->GetValue().SetValueType(
3644 Value::eValueTypeLoadAddress);
3645 Error err;
3646 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003647 if (ptr_result_valobj_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003648 ptr_result_valobj_sp->SetName(ConstString(name));
3649 }
3650 return ptr_result_valobj_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003651 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003652 }
3653 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003654}
3655
Kate Stoneb9c1b512016-09-06 20:57:50 +00003656lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003657 llvm::StringRef name, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003658 const ExecutionContext &exe_ctx, CompilerType type) {
3659 lldb::ValueObjectSP new_value_sp;
3660 new_value_sp = ValueObjectConstResult::Create(
3661 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3662 LLDB_INVALID_ADDRESS);
3663 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003664 if (new_value_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003665 new_value_sp->SetName(ConstString(name));
3666 return new_value_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003667}
Enrico Granata4873e522013-04-11 22:48:58 +00003668
Kate Stoneb9c1b512016-09-06 20:57:50 +00003669ModuleSP ValueObject::GetModule() {
3670 ValueObject *root(GetRoot());
3671 if (root != this)
3672 return root->GetModule();
3673 return lldb::ModuleSP();
3674}
3675
3676ValueObject *ValueObject::GetRoot() {
3677 if (m_root)
3678 return m_root;
3679 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3680 return (vo->m_parent != nullptr);
3681 }));
3682}
3683
3684ValueObject *
3685ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3686 ValueObject *vo = this;
3687 while (vo) {
3688 if (f(vo) == false)
3689 break;
3690 vo = vo->m_parent;
3691 }
3692 return vo;
3693}
3694
3695AddressType ValueObject::GetAddressTypeOfChildren() {
3696 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3697 ValueObject *root(GetRoot());
Enrico Granata4873e522013-04-11 22:48:58 +00003698 if (root != this)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003699 return root->GetAddressTypeOfChildren();
3700 }
3701 return m_address_type_of_ptr_or_ref_children;
Enrico Granata4873e522013-04-11 22:48:58 +00003702}
3703
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3705 ValueObject *with_dv_info = this;
3706 while (with_dv_info) {
3707 if (with_dv_info->HasDynamicValueTypeInfo())
3708 return with_dv_info->GetDynamicValueTypeImpl();
3709 with_dv_info = with_dv_info->m_parent;
3710 }
3711 return lldb::eNoDynamicValues;
Enrico Granatade61eba2015-01-22 03:07:34 +00003712}
3713
Kate Stoneb9c1b512016-09-06 20:57:50 +00003714lldb::Format ValueObject::GetFormat() const {
3715 const ValueObject *with_fmt_info = this;
3716 while (with_fmt_info) {
3717 if (with_fmt_info->m_format != lldb::eFormatDefault)
3718 return with_fmt_info->m_format;
3719 with_fmt_info = with_fmt_info->m_parent;
3720 }
3721 return m_format;
Enrico Granata4873e522013-04-11 22:48:58 +00003722}
3723
Kate Stoneb9c1b512016-09-06 20:57:50 +00003724lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3725 lldb::LanguageType type = m_preferred_display_language;
3726 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3727 if (GetRoot()) {
3728 if (GetRoot() == this) {
3729 if (StackFrameSP frame_sp = GetFrameSP()) {
3730 const SymbolContext &sc(
3731 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3732 if (CompileUnit *cu = sc.comp_unit)
3733 type = cu->GetLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +00003734 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003735 } else {
3736 type = GetRoot()->GetPreferredDisplayLanguage();
3737 }
Enrico Granatac1247f52014-11-06 21:23:20 +00003738 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003739 }
3740 return (m_preferred_display_language = type); // only compute it once
Enrico Granataed3228a2015-01-21 01:47:13 +00003741}
3742
Kate Stoneb9c1b512016-09-06 20:57:50 +00003743void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3744 m_preferred_display_language = lt;
Enrico Granatac1247f52014-11-06 21:23:20 +00003745}
3746
Kate Stoneb9c1b512016-09-06 20:57:50 +00003747void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3748 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3749 SetPreferredDisplayLanguage(lt);
Enrico Granata73e8c4d2015-10-07 02:36:35 +00003750}
3751
Kate Stoneb9c1b512016-09-06 20:57:50 +00003752bool ValueObject::CanProvideValue() {
3753 // we need to support invalid types as providers of values because some
3754 // bare-board
3755 // debugging scenarios have no notion of types, but still manage to have raw
3756 // numeric
3757 // values for things like registers. sigh.
3758 const CompilerType &type(GetCompilerType());
3759 return (false == type.IsValid()) ||
3760 (0 != (type.GetTypeInfo() & eTypeHasValue));
Sean Callanan7375f3e2014-12-09 21:18:59 +00003761}
3762
Kate Stoneb9c1b512016-09-06 20:57:50 +00003763bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3764
3765ValueObjectSP ValueObject::Persist() {
3766 if (!UpdateValueIfNeeded())
3767 return nullptr;
3768
3769 TargetSP target_sp(GetTargetSP());
3770 if (!target_sp)
3771 return nullptr;
3772
3773 PersistentExpressionState *persistent_state =
3774 target_sp->GetPersistentExpressionStateForLanguage(
3775 GetPreferredDisplayLanguage());
3776
3777 if (!persistent_state)
3778 return nullptr;
3779
3780 ConstString name(persistent_state->GetNextPersistentVariableName());
3781
3782 ValueObjectSP const_result_sp =
3783 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3784
3785 ExpressionVariableSP clang_var_sp =
3786 persistent_state->CreatePersistentVariable(const_result_sp);
3787 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3788 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3789
3790 return clang_var_sp->GetValueObject();
Enrico Granatad07cfd32014-10-08 18:27:36 +00003791}
Enrico Granata0c10a852014-12-08 23:13:56 +00003792
Kate Stoneb9c1b512016-09-06 20:57:50 +00003793bool ValueObject::IsSyntheticChildrenGenerated() {
3794 return m_is_synthetic_children_generated;
Enrico Granata0c10a852014-12-08 23:13:56 +00003795}
Enrico Granatae29df232014-12-09 19:51:20 +00003796
Kate Stoneb9c1b512016-09-06 20:57:50 +00003797void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3798 m_is_synthetic_children_generated = b;
Enrico Granatae29df232014-12-09 19:51:20 +00003799}
3800
Kate Stoneb9c1b512016-09-06 20:57:50 +00003801uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
Enrico Granatadc62ffd2015-11-09 19:27:34 +00003802
Kate Stoneb9c1b512016-09-06 20:57:50 +00003803void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }