blob: 30ec0daf91678e57d5b7fe1e7b82532d5805a1f3 [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 }
Zachary Turnerc2d55582016-11-18 03:51:19 +00002610
2611 if (!separator_position || separator_position > close_bracket_position) {
2612 // if no separator, this is of the form [N]. Note that this cannot
2613 // be an unbounded range of the form [], because that case was handled
2614 // above with an unconditional return.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002615 char *end = NULL;
2616 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002617 if (end != close_bracket_position) // if something weird is in
2618 // our way return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002619 {
2620 *first_unparsed = expression_cstr;
2621 *reason_to_stop =
2622 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2623 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2624 return ValueObjectSP();
2625 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002626 // from here on we do have a valid index
2627 if (root_compiler_type_info.Test(eTypeIsArray)) {
2628 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2629 if (!child_valobj_sp)
2630 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2631 if (!child_valobj_sp)
2632 if (root->HasSyntheticValue() &&
2633 root->GetSyntheticValue()->GetNumChildren() > index)
2634 child_valobj_sp =
2635 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2636 if (child_valobj_sp) {
2637 root = child_valobj_sp;
2638 *first_unparsed = end + 1; // skip ]
2639 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2640 continue;
2641 } else {
2642 *first_unparsed = expression_cstr;
2643 *reason_to_stop =
2644 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2645 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2646 return ValueObjectSP();
2647 }
2648 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2649 if (*what_next ==
2650 ValueObject::
2651 eExpressionPathAftermathDereference && // if this is a
2652 // ptr-to-scalar, I
2653 // am accessing it
2654 // by index and I
2655 // would have
2656 // deref'ed anyway,
2657 // then do it now
2658 // and use this as
2659 // a bitfield
2660 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2661 Error error;
2662 root = root->Dereference(error);
2663 if (error.Fail() || !root.get()) {
2664 *first_unparsed = expression_cstr;
2665 *reason_to_stop =
2666 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2667 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2668 return ValueObjectSP();
2669 } else {
2670 *what_next = eExpressionPathAftermathNothing;
2671 continue;
2672 }
2673 } else {
2674 if (root->GetCompilerType().GetMinimumLanguage() ==
2675 eLanguageTypeObjC &&
2676 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2677 root->HasSyntheticValue() &&
2678 (options.m_synthetic_children_traversal ==
2679 GetValueForExpressionPathOptions::
2680 SyntheticChildrenTraversal::ToSynthetic ||
2681 options.m_synthetic_children_traversal ==
2682 GetValueForExpressionPathOptions::
2683 SyntheticChildrenTraversal::Both)) {
2684 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2685 } else
2686 root = root->GetSyntheticArrayMember(index, true);
2687 if (!root.get()) {
2688 *first_unparsed = expression_cstr;
2689 *reason_to_stop =
2690 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2691 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2692 return ValueObjectSP();
2693 } else {
2694 *first_unparsed = end + 1; // skip ]
2695 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2696 continue;
2697 }
2698 }
2699 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2700 root = root->GetSyntheticBitFieldChild(index, index, true);
2701 if (!root.get()) {
2702 *first_unparsed = expression_cstr;
2703 *reason_to_stop =
2704 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2705 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2706 return ValueObjectSP();
2707 } else // we do not know how to expand members of bitfields, so we
2708 // just return and let the caller do any further processing
2709 {
2710 *first_unparsed = end + 1; // skip ]
2711 *reason_to_stop = ValueObject::
2712 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2713 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2714 return root;
2715 }
2716 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2717 root = root->GetChildAtIndex(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 {
2725 *first_unparsed = end + 1; // skip ]
2726 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2727 continue;
2728 }
2729 } else if (options.m_synthetic_children_traversal ==
2730 GetValueForExpressionPathOptions::
2731 SyntheticChildrenTraversal::ToSynthetic ||
2732 options.m_synthetic_children_traversal ==
2733 GetValueForExpressionPathOptions::
2734 SyntheticChildrenTraversal::Both) {
2735 if (root->HasSyntheticValue())
2736 root = root->GetSyntheticValue();
2737 else if (!root->IsSynthetic()) {
2738 *first_unparsed = expression_cstr;
2739 *reason_to_stop =
2740 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2741 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2742 return ValueObjectSP();
2743 }
2744 // if we are here, then root itself is a synthetic VO.. should be good
2745 // to go
2746
2747 if (!root.get()) {
2748 *first_unparsed = expression_cstr;
2749 *reason_to_stop =
2750 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2751 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2752 return ValueObjectSP();
2753 }
2754 root = root->GetChildAtIndex(index, true);
2755 if (!root.get()) {
2756 *first_unparsed = expression_cstr;
2757 *reason_to_stop =
2758 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2759 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2760 return ValueObjectSP();
2761 } else {
2762 *first_unparsed = end + 1; // skip ]
2763 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2764 continue;
2765 }
2766 } else {
2767 *first_unparsed = expression_cstr;
2768 *reason_to_stop =
2769 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2770 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2771 return ValueObjectSP();
2772 }
2773 } else // we have a low and a high index
2774 {
2775 char *end = NULL;
2776 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002777 if (end != separator_position) // if something weird is in our
2778 // way return an error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002779 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002780 *first_unparsed = expression_cstr;
2781 *reason_to_stop =
2782 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2783 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2784 return ValueObjectSP();
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002786 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002787 if (end != close_bracket_position) // if something weird is in
2788 // our way return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002789 {
2790 *first_unparsed = expression_cstr;
2791 *reason_to_stop =
2792 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2793 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2794 return ValueObjectSP();
2795 }
2796 if (index_lower > index_higher) // swap indices if required
Zachary Turnerc2d55582016-11-18 03:51:19 +00002797 std::swap(index_lower, index_higher);
2798
Kate Stoneb9c1b512016-09-06 20:57:50 +00002799 if (root_compiler_type_info.Test(
2800 eTypeIsScalar)) // expansion only works for scalars
2801 {
2802 root =
2803 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2804 if (!root.get()) {
2805 *first_unparsed = expression_cstr;
2806 *reason_to_stop =
2807 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2808 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2809 return ValueObjectSP();
2810 } else {
2811 *first_unparsed = end + 1; // skip ]
2812 *reason_to_stop = ValueObject::
2813 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2814 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2815 return root;
2816 }
2817 } else if (root_compiler_type_info.Test(
2818 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2819 // accessing it by index and I would
2820 // have deref'ed anyway, then do it
2821 // now and use this as a bitfield
2822 *what_next ==
2823 ValueObject::eExpressionPathAftermathDereference &&
2824 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2825 Error error;
2826 root = root->Dereference(error);
2827 if (error.Fail() || !root.get()) {
2828 *first_unparsed = expression_cstr;
2829 *reason_to_stop =
2830 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2831 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2832 return ValueObjectSP();
2833 } else {
2834 *what_next = ValueObject::eExpressionPathAftermathNothing;
2835 continue;
2836 }
2837 } else {
2838 *first_unparsed = expression_cstr;
2839 *reason_to_stop =
2840 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2841 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2842 return root;
2843 }
2844 }
2845 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002847 default: // some non-separator is in the way
2848 {
2849 *first_unparsed = expression_cstr;
2850 *reason_to_stop =
2851 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2852 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2853 return ValueObjectSP();
2854 break;
2855 }
2856 }
2857 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002858}
2859
Kate Stoneb9c1b512016-09-06 20:57:50 +00002860int ValueObject::ExpandArraySliceExpression(
2861 const char *expression_cstr, const char **first_unparsed,
2862 ValueObjectSP root, ValueObjectListSP &list,
2863 ExpressionPathScanEndReason *reason_to_stop,
2864 ExpressionPathEndResultType *final_result,
2865 const GetValueForExpressionPathOptions &options,
2866 ExpressionPathAftermath *what_next) {
2867 if (!root.get())
2868 return 0;
2869
2870 *first_unparsed = expression_cstr;
2871
2872 while (true) {
2873
2874 const char *expression_cstr =
2875 *first_unparsed; // hide the top level expression_cstr
2876
2877 CompilerType root_compiler_type = root->GetCompilerType();
2878 CompilerType pointee_compiler_type;
2879 Flags pointee_compiler_type_info;
2880 Flags root_compiler_type_info(
2881 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2882 if (pointee_compiler_type)
2883 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2884
2885 if (!expression_cstr || *expression_cstr == '\0') {
2886 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2887 list->Append(root);
2888 return 1;
2889 }
2890
2891 switch (*expression_cstr) {
2892 case '[': {
2893 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2894 !root_compiler_type_info.Test(
2895 eTypeIsPointer)) // if this is not a T[] nor a T*
2896 {
2897 if (!root_compiler_type_info.Test(eTypeIsScalar)) // if this is not even
2898 // a scalar, this
2899 // syntax is just
2900 // plain wrong!
2901 {
2902 *first_unparsed = expression_cstr;
2903 *reason_to_stop =
2904 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2905 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2906 return 0;
2907 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2908 // check that we can
2909 // expand bitfields
2910 {
2911 *first_unparsed = expression_cstr;
2912 *reason_to_stop =
2913 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2914 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2915 return 0;
2916 }
2917 }
2918 if (*(expression_cstr + 1) ==
2919 ']') // if this is an unbounded range it only works for arrays
2920 {
2921 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2922 *first_unparsed = expression_cstr;
2923 *reason_to_stop =
2924 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2925 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2926 return 0;
2927 } else // expand this into list
2928 {
2929 const size_t max_index = root->GetNumChildren() - 1;
2930 for (size_t index = 0; index < max_index; index++) {
2931 ValueObjectSP child = root->GetChildAtIndex(index, true);
2932 list->Append(child);
2933 }
2934 *first_unparsed = expression_cstr + 2;
2935 *reason_to_stop =
2936 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2937 *final_result =
2938 ValueObject::eExpressionPathEndResultTypeValueObjectList;
2939 return max_index; // tell me number of items I added to the VOList
2940 }
2941 }
2942 const char *separator_position = ::strchr(expression_cstr + 1, '-');
2943 const char *close_bracket_position = ::strchr(expression_cstr + 1, ']');
2944 if (!close_bracket_position) // if there is no ], this is a syntax error
2945 {
2946 *first_unparsed = expression_cstr;
2947 *reason_to_stop =
2948 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2949 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002950 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002951 }
2952 if (!separator_position ||
2953 separator_position > close_bracket_position) // if no separator, this
2954 // is either [] or [N]
2955 {
2956 char *end = NULL;
2957 unsigned long index = ::strtoul(expression_cstr + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002958 if (end != close_bracket_position) // if something weird is in
2959 // our way return an error
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002960 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002961 *first_unparsed = expression_cstr;
2962 *reason_to_stop =
2963 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2964 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2965 return 0;
2966 }
2967 if (end - expression_cstr ==
2968 1) // if this is [], only return a valid value for arrays
2969 {
2970 if (root_compiler_type_info.Test(eTypeIsArray)) {
2971 const size_t max_index = root->GetNumChildren() - 1;
2972 for (size_t index = 0; index < max_index; index++) {
2973 ValueObjectSP child = root->GetChildAtIndex(index, true);
2974 list->Append(child);
2975 }
2976 *first_unparsed = expression_cstr + 2;
2977 *reason_to_stop =
2978 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2979 *final_result =
2980 ValueObject::eExpressionPathEndResultTypeValueObjectList;
2981 return max_index; // tell me number of items I added to the VOList
2982 } else {
2983 *first_unparsed = expression_cstr;
2984 *reason_to_stop =
2985 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2986 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2987 return 0;
2988 }
2989 }
2990 // from here on we do have a valid index
2991 if (root_compiler_type_info.Test(eTypeIsArray)) {
2992 root = root->GetChildAtIndex(index, true);
2993 if (!root.get()) {
2994 *first_unparsed = expression_cstr;
2995 *reason_to_stop =
2996 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2997 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2998 return 0;
2999 } else {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003000 list->Append(root);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 *first_unparsed = end + 1; // skip ]
3002 *reason_to_stop =
3003 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3004 *final_result =
3005 ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003006 return 1;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003007 }
3008 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
3009 if (*what_next ==
3010 ValueObject::
3011 eExpressionPathAftermathDereference && // if this is a
3012 // ptr-to-scalar, I
3013 // am accessing it
3014 // by index and I
3015 // would have
3016 // deref'ed anyway,
3017 // then do it now
3018 // and use this as
3019 // a bitfield
3020 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3021 Error error;
3022 root = root->Dereference(error);
3023 if (error.Fail() || !root.get()) {
3024 *first_unparsed = expression_cstr;
3025 *reason_to_stop =
3026 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3027 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3028 return 0;
3029 } else {
3030 *what_next = eExpressionPathAftermathNothing;
3031 continue;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003032 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033 } else {
3034 root = root->GetSyntheticArrayMember(index, true);
3035 if (!root.get()) {
3036 *first_unparsed = expression_cstr;
3037 *reason_to_stop =
3038 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3039 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3040 return 0;
3041 } else {
3042 list->Append(root);
3043 *first_unparsed = end + 1; // skip ]
3044 *reason_to_stop = ValueObject::
3045 eExpressionPathScanEndReasonRangeOperatorExpanded;
3046 *final_result =
3047 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3048 return 1;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003049 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003050 }
3051 } else /*if (ClangASTContext::IsScalarType(root_compiler_type))*/
Enrico Granata9f1e2042012-04-24 22:15:37 +00003052 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003053 root = root->GetSyntheticBitFieldChild(index, index, true);
3054 if (!root.get()) {
3055 *first_unparsed = expression_cstr;
3056 *reason_to_stop =
3057 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3058 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3059 return 0;
3060 } else // we do not know how to expand members of bitfields, so we
3061 // just return and let the caller do any further processing
3062 {
3063 list->Append(root);
3064 *first_unparsed = end + 1; // skip ]
3065 *reason_to_stop =
3066 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3067 *final_result =
3068 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3069 return 1;
3070 }
Enrico Granata9f1e2042012-04-24 22:15:37 +00003071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003072 } else // we have a low and a high index
3073 {
3074 char *end = NULL;
3075 unsigned long index_lower = ::strtoul(expression_cstr + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00003076 if (end != separator_position) // if something weird is in our
3077 // way return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00003078 {
3079 *first_unparsed = expression_cstr;
3080 *reason_to_stop =
3081 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3082 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3083 return 0;
3084 }
3085 unsigned long index_higher = ::strtoul(separator_position + 1, &end, 0);
Zachary Turnerc2d55582016-11-18 03:51:19 +00003086 if (end != close_bracket_position) // if something weird is in
3087 // our way return an error
Kate Stoneb9c1b512016-09-06 20:57:50 +00003088 {
3089 *first_unparsed = expression_cstr;
3090 *reason_to_stop =
3091 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3092 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3093 return 0;
3094 }
3095 if (index_lower > index_higher) // swap indices if required
Zachary Turnerc2d55582016-11-18 03:51:19 +00003096 std::swap(index_lower, index_higher);
3097
Kate Stoneb9c1b512016-09-06 20:57:50 +00003098 if (root_compiler_type_info.Test(
3099 eTypeIsScalar)) // expansion only works for scalars
3100 {
3101 root =
3102 root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3103 if (!root.get()) {
3104 *first_unparsed = expression_cstr;
3105 *reason_to_stop =
3106 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3107 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3108 return 0;
3109 } else {
3110 list->Append(root);
3111 *first_unparsed = end + 1; // skip ]
3112 *reason_to_stop =
3113 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3114 *final_result =
3115 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3116 return 1;
3117 }
3118 } else if (root_compiler_type_info.Test(
3119 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
3120 // accessing it by index and I would
3121 // have deref'ed anyway, then do it
3122 // now and use this as a bitfield
3123 *what_next ==
3124 ValueObject::eExpressionPathAftermathDereference &&
3125 pointee_compiler_type_info.Test(eTypeIsScalar)) {
3126 Error error;
3127 root = root->Dereference(error);
3128 if (error.Fail() || !root.get()) {
3129 *first_unparsed = expression_cstr;
3130 *reason_to_stop =
3131 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3132 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3133 return 0;
3134 } else {
3135 *what_next = ValueObject::eExpressionPathAftermathNothing;
3136 continue;
3137 }
3138 } else {
3139 for (unsigned long index = index_lower; index <= index_higher;
3140 index++) {
3141 ValueObjectSP child = root->GetChildAtIndex(index, true);
3142 list->Append(child);
3143 }
3144 *first_unparsed = end + 1;
3145 *reason_to_stop =
3146 ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3147 *final_result =
3148 ValueObject::eExpressionPathEndResultTypeValueObjectList;
3149 return index_higher - index_lower +
3150 1; // tell me number of items I added to the VOList
3151 }
3152 }
3153 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003155 default: // some non-[ separator, or something entirely wrong, is in the way
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003156 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157 *first_unparsed = expression_cstr;
3158 *reason_to_stop =
3159 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3160 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3161 return 0;
3162 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003163 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003164 }
3165 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003166}
3167
Kate Stoneb9c1b512016-09-06 20:57:50 +00003168void ValueObject::LogValueObject(Log *log) {
3169 if (log)
3170 return LogValueObject(log, DumpValueObjectOptions(*this));
Enrico Granata538a88a2014-10-09 18:24:30 +00003171}
3172
Kate Stoneb9c1b512016-09-06 20:57:50 +00003173void ValueObject::LogValueObject(Log *log,
3174 const DumpValueObjectOptions &options) {
3175 if (log) {
3176 StreamString s;
3177 Dump(s, options);
3178 if (s.GetSize())
3179 log->PutCString(s.GetData());
3180 }
Greg Clayton759e7442014-07-19 00:12:57 +00003181}
3182
Kate Stoneb9c1b512016-09-06 20:57:50 +00003183void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
Chaoren Lind7bdc272015-07-31 00:35:40 +00003184
Kate Stoneb9c1b512016-09-06 20:57:50 +00003185void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
3186 ValueObjectPrinter printer(this, &s, options);
3187 printer.PrintValueObject();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003188}
3189
Kate Stoneb9c1b512016-09-06 20:57:50 +00003190ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
3191 ValueObjectSP valobj_sp;
3192
3193 if (UpdateValueIfNeeded(false) && m_error.Success()) {
3194 ExecutionContext exe_ctx(GetExecutionContextRef());
3195
3196 DataExtractor data;
3197 data.SetByteOrder(m_data.GetByteOrder());
3198 data.SetAddressByteSize(m_data.GetAddressByteSize());
3199
3200 if (IsBitfield()) {
3201 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3202 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3203 } else
3204 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
3205
3206 valobj_sp = ValueObjectConstResult::Create(
3207 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
3208 GetAddressOf());
3209 }
3210
3211 if (!valobj_sp) {
3212 ExecutionContext exe_ctx(GetExecutionContextRef());
3213 valobj_sp = ValueObjectConstResult::Create(
3214 exe_ctx.GetBestExecutionContextScope(), m_error);
3215 }
3216 return valobj_sp;
3217}
3218
3219ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
3220 lldb::DynamicValueType dynValue, bool synthValue) {
3221 ValueObjectSP result_sp(GetSP());
3222
3223 switch (dynValue) {
3224 case lldb::eDynamicCanRunTarget:
3225 case lldb::eDynamicDontRunTarget: {
3226 if (!result_sp->IsDynamic()) {
3227 if (result_sp->GetDynamicValue(dynValue))
3228 result_sp = result_sp->GetDynamicValue(dynValue);
3229 }
3230 } break;
3231 case lldb::eNoDynamicValues: {
3232 if (result_sp->IsDynamic()) {
3233 if (result_sp->GetStaticValue())
3234 result_sp = result_sp->GetStaticValue();
3235 }
3236 } break;
3237 }
3238
3239 if (synthValue) {
3240 if (!result_sp->IsSynthetic()) {
3241 if (result_sp->GetSyntheticValue())
3242 result_sp = result_sp->GetSyntheticValue();
3243 }
3244 } else {
3245 if (result_sp->IsSynthetic()) {
3246 if (result_sp->GetNonSyntheticValue())
3247 result_sp = result_sp->GetNonSyntheticValue();
3248 }
3249 }
3250
3251 return result_sp;
3252}
3253
3254lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
3255 CompilerType pointee_type;
3256 CompilerType this_type(GetCompilerType());
3257 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3258 if (type_info) {
3259 bool ptr_or_ref = false;
3260 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
3261 ptr_or_ref = true;
3262 type_info = pointee_type.GetTypeInfo();
3263 }
3264
3265 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
3266 if ((type_info & cpp_class) == cpp_class) {
3267 if (ptr_or_ref) {
3268 address_type = GetAddressTypeOfChildren();
3269 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3270 } else
3271 return GetAddressOf(false, &address_type);
3272 }
3273 }
3274
3275 address_type = eAddressTypeInvalid;
3276 return LLDB_INVALID_ADDRESS;
3277}
3278
3279ValueObjectSP ValueObject::Dereference(Error &error) {
3280 if (m_deref_valobj)
3281 return m_deref_valobj->GetSP();
3282
3283 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
3284 if (is_pointer_or_reference_type) {
3285 bool omit_empty_base_classes = true;
3286 bool ignore_array_bounds = false;
3287
3288 std::string child_name_str;
3289 uint32_t child_byte_size = 0;
3290 int32_t child_byte_offset = 0;
3291 uint32_t child_bitfield_bit_size = 0;
3292 uint32_t child_bitfield_bit_offset = 0;
3293 bool child_is_base_class = false;
3294 bool child_is_deref_of_parent = false;
3295 const bool transparent_pointers = false;
3296 CompilerType compiler_type = GetCompilerType();
3297 CompilerType child_compiler_type;
3298 uint64_t language_flags;
3299
3300 ExecutionContext exe_ctx(GetExecutionContextRef());
3301
3302 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
3303 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
3304 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
3305 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
3306 child_is_deref_of_parent, this, language_flags);
3307 if (child_compiler_type && child_byte_size) {
3308 ConstString child_name;
3309 if (!child_name_str.empty())
3310 child_name.SetCString(child_name_str.c_str());
3311
3312 m_deref_valobj = new ValueObjectChild(
3313 *this, child_compiler_type, child_name, child_byte_size,
3314 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
3315 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
3316 language_flags);
3317 }
3318 }
3319
3320 if (m_deref_valobj) {
Greg Clayton54979cd2010-12-15 05:08:08 +00003321 error.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00003322 return m_deref_valobj->GetSP();
3323 } else {
3324 StreamString strm;
3325 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003326
Kate Stoneb9c1b512016-09-06 20:57:50 +00003327 if (is_pointer_or_reference_type)
3328 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
3329 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00003330 strm.GetData());
Sean Callananed185ab2013-04-19 19:47:32 +00003331 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00003332 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
3333 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00003334 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003335 return ValueObjectSP();
3336 }
3337}
3338
3339ValueObjectSP ValueObject::AddressOf(Error &error) {
3340 if (m_addr_of_valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00003341 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003342
Kate Stoneb9c1b512016-09-06 20:57:50 +00003343 AddressType address_type = eAddressTypeInvalid;
3344 const bool scalar_is_load_address = false;
3345 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
3346 error.Clear();
3347 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
3348 switch (address_type) {
3349 case eAddressTypeInvalid: {
3350 StreamString expr_path_strm;
3351 GetExpressionPath(expr_path_strm, true);
3352 error.SetErrorStringWithFormat("'%s' is not in memory",
Zachary Turnerc1564272016-11-16 21:15:24 +00003353 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003354 } break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003355
Kate Stoneb9c1b512016-09-06 20:57:50 +00003356 case eAddressTypeFile:
3357 case eAddressTypeLoad: {
3358 CompilerType compiler_type = GetCompilerType();
3359 if (compiler_type) {
3360 std::string name(1, '&');
3361 name.append(m_name.AsCString(""));
3362 ExecutionContext exe_ctx(GetExecutionContextRef());
3363 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
3364 exe_ctx.GetBestExecutionContextScope(),
3365 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
3366 eAddressTypeInvalid, m_data.GetAddressByteSize());
3367 }
3368 } break;
3369 default:
3370 break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003371 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003372 } else {
3373 StreamString expr_path_strm;
3374 GetExpressionPath(expr_path_strm, true);
3375 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
Zachary Turnerc1564272016-11-16 21:15:24 +00003376 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003377 }
3378
3379 return m_addr_of_valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003380}
3381
Kate Stoneb9c1b512016-09-06 20:57:50 +00003382ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
3383 return ValueObjectCast::Create(*this, GetName(), compiler_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003384}
3385
Kate Stoneb9c1b512016-09-06 20:57:50 +00003386ValueObjectSP ValueObject::CastPointerType(const char *name,
3387 CompilerType &compiler_type) {
3388 ValueObjectSP valobj_sp;
3389 AddressType address_type;
3390 addr_t ptr_value = GetPointerValue(&address_type);
3391
3392 if (ptr_value != LLDB_INVALID_ADDRESS) {
3393 Address ptr_addr(ptr_value);
3394 ExecutionContext exe_ctx(GetExecutionContextRef());
3395 valobj_sp = ValueObjectMemory::Create(
3396 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
3397 }
3398 return valobj_sp;
Jim Ingham6035b672011-03-31 00:19:25 +00003399}
3400
Kate Stoneb9c1b512016-09-06 20:57:50 +00003401ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
3402 ValueObjectSP valobj_sp;
3403 AddressType address_type;
3404 addr_t ptr_value = GetPointerValue(&address_type);
3405
3406 if (ptr_value != LLDB_INVALID_ADDRESS) {
3407 Address ptr_addr(ptr_value);
3408 ExecutionContext exe_ctx(GetExecutionContextRef());
3409 valobj_sp = ValueObjectMemory::Create(
3410 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
3411 }
3412 return valobj_sp;
3413}
3414
3415ValueObject::EvaluationPoint::EvaluationPoint()
3416 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
3417
3418ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
3419 bool use_selected)
3420 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
3421 ExecutionContext exe_ctx(exe_scope);
3422 TargetSP target_sp(exe_ctx.GetTargetSP());
3423 if (target_sp) {
3424 m_exe_ctx_ref.SetTargetSP(target_sp);
3425 ProcessSP process_sp(exe_ctx.GetProcessSP());
3426 if (!process_sp)
3427 process_sp = target_sp->GetProcessSP();
3428
3429 if (process_sp) {
3430 m_mod_id = process_sp->GetModID();
3431 m_exe_ctx_ref.SetProcessSP(process_sp);
3432
3433 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3434
3435 if (!thread_sp) {
3436 if (use_selected)
3437 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3438 }
3439
3440 if (thread_sp) {
3441 m_exe_ctx_ref.SetThreadSP(thread_sp);
3442
3443 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3444 if (!frame_sp) {
3445 if (use_selected)
3446 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003448 if (frame_sp)
3449 m_exe_ctx_ref.SetFrameSP(frame_sp);
3450 }
Jim Ingham6035b672011-03-31 00:19:25 +00003451 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003452 }
Jim Ingham6035b672011-03-31 00:19:25 +00003453}
3454
Kate Stoneb9c1b512016-09-06 20:57:50 +00003455ValueObject::EvaluationPoint::EvaluationPoint(
3456 const ValueObject::EvaluationPoint &rhs)
3457 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
Jim Ingham6035b672011-03-31 00:19:25 +00003458
Kate Stoneb9c1b512016-09-06 20:57:50 +00003459ValueObject::EvaluationPoint::~EvaluationPoint() {}
Jim Ingham6035b672011-03-31 00:19:25 +00003460
Kate Stoneb9c1b512016-09-06 20:57:50 +00003461// This function checks the EvaluationPoint against the current process state.
3462// If the current
3463// state matches the evaluation point, or the evaluation point is already
3464// invalid, then we return
3465// false, meaning "no change". If the current state is different, we update our
3466// state, and return
3467// true meaning "yes, change". If we did see a change, we also set
3468// m_needs_update to true, so
Jim Ingham6035b672011-03-31 00:19:25 +00003469// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003470// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003471
Kate Stoneb9c1b512016-09-06 20:57:50 +00003472bool ValueObject::EvaluationPoint::SyncWithProcessState(
3473 bool accept_invalid_exe_ctx) {
3474 // Start with the target, if it is NULL, then we're obviously not going to get
3475 // any further:
3476 const bool thread_and_frame_only_if_stopped = true;
3477 ExecutionContext exe_ctx(
3478 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3479
3480 if (exe_ctx.GetTargetPtr() == NULL)
3481 return false;
3482
3483 // If we don't have a process nothing can change.
3484 Process *process = exe_ctx.GetProcessPtr();
3485 if (process == NULL)
3486 return false;
3487
3488 // If our stop id is the current stop ID, nothing has changed:
3489 ProcessModID current_mod_id = process->GetModID();
3490
3491 // If the current stop id is 0, either we haven't run yet, or the process
3492 // state has been cleared.
3493 // In either case, we aren't going to be able to sync with the process state.
3494 if (current_mod_id.GetStopID() == 0)
3495 return false;
3496
3497 bool changed = false;
3498 const bool was_valid = m_mod_id.IsValid();
3499 if (was_valid) {
3500 if (m_mod_id == current_mod_id) {
3501 // Everything is already up to date in this object, no need to
3502 // update the execution context scope.
3503 changed = false;
3504 } else {
3505 m_mod_id = current_mod_id;
3506 m_needs_update = true;
3507 changed = true;
3508 }
3509 }
3510
3511 // Now re-look up the thread and frame in case the underlying objects have
3512 // gone away & been recreated.
3513 // That way we'll be sure to return a valid exe_scope.
3514 // If we used to have a thread or a frame but can't find it anymore, then mark
3515 // ourselves as invalid.
3516
3517 if (!accept_invalid_exe_ctx) {
3518 if (m_exe_ctx_ref.HasThreadRef()) {
3519 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3520 if (thread_sp) {
3521 if (m_exe_ctx_ref.HasFrameRef()) {
3522 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3523 if (!frame_sp) {
3524 // We used to have a frame, but now it is gone
3525 SetInvalid();
3526 changed = was_valid;
3527 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003529 } else {
3530 // We used to have a thread, but now it is gone
3531 SetInvalid();
3532 changed = was_valid;
3533 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003534 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003535 }
Enrico Granatabb642e52015-05-16 01:27:00 +00003536
Kate Stoneb9c1b512016-09-06 20:57:50 +00003537 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003538}
3539
Kate Stoneb9c1b512016-09-06 20:57:50 +00003540void ValueObject::EvaluationPoint::SetUpdated() {
3541 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3542 if (process_sp)
3543 m_mod_id = process_sp->GetModID();
3544 m_needs_update = false;
Johnny Chen44805302011-07-19 19:48:13 +00003545}
Enrico Granata9128ee22011-09-06 19:20:51 +00003546
Kate Stoneb9c1b512016-09-06 20:57:50 +00003547void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3548 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3549 eClearUserVisibleDataItemsValue)
3550 m_value_str.clear();
3551
3552 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3553 eClearUserVisibleDataItemsLocation)
3554 m_location_str.clear();
3555
3556 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3557 eClearUserVisibleDataItemsSummary)
3558 m_summary_str.clear();
3559
3560 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3561 eClearUserVisibleDataItemsDescription)
3562 m_object_desc_str.clear();
3563
3564 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3565 eClearUserVisibleDataItemsSyntheticChildren) {
3566 if (m_synthetic_value)
3567 m_synthetic_value = NULL;
3568 }
3569
3570 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3571 eClearUserVisibleDataItemsValidator)
3572 m_validation_result.reset();
Enrico Granata9128ee22011-09-06 19:20:51 +00003573}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003574
Kate Stoneb9c1b512016-09-06 20:57:50 +00003575SymbolContextScope *ValueObject::GetSymbolContextScope() {
3576 if (m_parent) {
3577 if (!m_parent->IsPointerOrReferenceType())
3578 return m_parent->GetSymbolContextScope();
3579 }
3580 return NULL;
Enrico Granata972be532014-12-17 21:18:43 +00003581}
3582
Zachary Turneraa5611f2016-11-13 03:29:46 +00003583lldb::ValueObjectSP
3584ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3585 llvm::StringRef expression,
3586 const ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003587 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3588 EvaluateExpressionOptions());
3589}
Enrico Granata972be532014-12-17 21:18:43 +00003590
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003592 llvm::StringRef name, llvm::StringRef expression,
3593 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003594 lldb::ValueObjectSP retval_sp;
3595 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3596 if (!target_sp)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003597 return retval_sp;
Zachary Turneraa5611f2016-11-13 03:29:46 +00003598 if (expression.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003599 return retval_sp;
3600 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3601 retval_sp, options);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003602 if (retval_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003603 retval_sp->SetName(ConstString(name));
3604 return retval_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003605}
3606
Zachary Turneraa5611f2016-11-13 03:29:46 +00003607lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3608 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3609 CompilerType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003610 if (type) {
3611 CompilerType pointer_type(type.GetPointerType());
3612 if (pointer_type) {
3613 lldb::DataBufferSP buffer(
3614 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3615 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3616 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3617 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3618 exe_ctx.GetAddressByteSize()));
3619 if (ptr_result_valobj_sp) {
3620 ptr_result_valobj_sp->GetValue().SetValueType(
3621 Value::eValueTypeLoadAddress);
3622 Error err;
3623 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003624 if (ptr_result_valobj_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003625 ptr_result_valobj_sp->SetName(ConstString(name));
3626 }
3627 return ptr_result_valobj_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003628 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003629 }
3630 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003631}
3632
Kate Stoneb9c1b512016-09-06 20:57:50 +00003633lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003634 llvm::StringRef name, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003635 const ExecutionContext &exe_ctx, CompilerType type) {
3636 lldb::ValueObjectSP new_value_sp;
3637 new_value_sp = ValueObjectConstResult::Create(
3638 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3639 LLDB_INVALID_ADDRESS);
3640 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003641 if (new_value_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003642 new_value_sp->SetName(ConstString(name));
3643 return new_value_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003644}
Enrico Granata4873e522013-04-11 22:48:58 +00003645
Kate Stoneb9c1b512016-09-06 20:57:50 +00003646ModuleSP ValueObject::GetModule() {
3647 ValueObject *root(GetRoot());
3648 if (root != this)
3649 return root->GetModule();
3650 return lldb::ModuleSP();
3651}
3652
3653ValueObject *ValueObject::GetRoot() {
3654 if (m_root)
3655 return m_root;
3656 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3657 return (vo->m_parent != nullptr);
3658 }));
3659}
3660
3661ValueObject *
3662ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3663 ValueObject *vo = this;
3664 while (vo) {
3665 if (f(vo) == false)
3666 break;
3667 vo = vo->m_parent;
3668 }
3669 return vo;
3670}
3671
3672AddressType ValueObject::GetAddressTypeOfChildren() {
3673 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3674 ValueObject *root(GetRoot());
Enrico Granata4873e522013-04-11 22:48:58 +00003675 if (root != this)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003676 return root->GetAddressTypeOfChildren();
3677 }
3678 return m_address_type_of_ptr_or_ref_children;
Enrico Granata4873e522013-04-11 22:48:58 +00003679}
3680
Kate Stoneb9c1b512016-09-06 20:57:50 +00003681lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3682 ValueObject *with_dv_info = this;
3683 while (with_dv_info) {
3684 if (with_dv_info->HasDynamicValueTypeInfo())
3685 return with_dv_info->GetDynamicValueTypeImpl();
3686 with_dv_info = with_dv_info->m_parent;
3687 }
3688 return lldb::eNoDynamicValues;
Enrico Granatade61eba2015-01-22 03:07:34 +00003689}
3690
Kate Stoneb9c1b512016-09-06 20:57:50 +00003691lldb::Format ValueObject::GetFormat() const {
3692 const ValueObject *with_fmt_info = this;
3693 while (with_fmt_info) {
3694 if (with_fmt_info->m_format != lldb::eFormatDefault)
3695 return with_fmt_info->m_format;
3696 with_fmt_info = with_fmt_info->m_parent;
3697 }
3698 return m_format;
Enrico Granata4873e522013-04-11 22:48:58 +00003699}
3700
Kate Stoneb9c1b512016-09-06 20:57:50 +00003701lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3702 lldb::LanguageType type = m_preferred_display_language;
3703 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3704 if (GetRoot()) {
3705 if (GetRoot() == this) {
3706 if (StackFrameSP frame_sp = GetFrameSP()) {
3707 const SymbolContext &sc(
3708 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3709 if (CompileUnit *cu = sc.comp_unit)
3710 type = cu->GetLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +00003711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003712 } else {
3713 type = GetRoot()->GetPreferredDisplayLanguage();
3714 }
Enrico Granatac1247f52014-11-06 21:23:20 +00003715 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003716 }
3717 return (m_preferred_display_language = type); // only compute it once
Enrico Granataed3228a2015-01-21 01:47:13 +00003718}
3719
Kate Stoneb9c1b512016-09-06 20:57:50 +00003720void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3721 m_preferred_display_language = lt;
Enrico Granatac1247f52014-11-06 21:23:20 +00003722}
3723
Kate Stoneb9c1b512016-09-06 20:57:50 +00003724void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3725 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3726 SetPreferredDisplayLanguage(lt);
Enrico Granata73e8c4d2015-10-07 02:36:35 +00003727}
3728
Kate Stoneb9c1b512016-09-06 20:57:50 +00003729bool ValueObject::CanProvideValue() {
3730 // we need to support invalid types as providers of values because some
3731 // bare-board
3732 // debugging scenarios have no notion of types, but still manage to have raw
3733 // numeric
3734 // values for things like registers. sigh.
3735 const CompilerType &type(GetCompilerType());
3736 return (false == type.IsValid()) ||
3737 (0 != (type.GetTypeInfo() & eTypeHasValue));
Sean Callanan7375f3e2014-12-09 21:18:59 +00003738}
3739
Kate Stoneb9c1b512016-09-06 20:57:50 +00003740bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3741
3742ValueObjectSP ValueObject::Persist() {
3743 if (!UpdateValueIfNeeded())
3744 return nullptr;
3745
3746 TargetSP target_sp(GetTargetSP());
3747 if (!target_sp)
3748 return nullptr;
3749
3750 PersistentExpressionState *persistent_state =
3751 target_sp->GetPersistentExpressionStateForLanguage(
3752 GetPreferredDisplayLanguage());
3753
3754 if (!persistent_state)
3755 return nullptr;
3756
3757 ConstString name(persistent_state->GetNextPersistentVariableName());
3758
3759 ValueObjectSP const_result_sp =
3760 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3761
3762 ExpressionVariableSP clang_var_sp =
3763 persistent_state->CreatePersistentVariable(const_result_sp);
3764 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3765 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3766
3767 return clang_var_sp->GetValueObject();
Enrico Granatad07cfd32014-10-08 18:27:36 +00003768}
Enrico Granata0c10a852014-12-08 23:13:56 +00003769
Kate Stoneb9c1b512016-09-06 20:57:50 +00003770bool ValueObject::IsSyntheticChildrenGenerated() {
3771 return m_is_synthetic_children_generated;
Enrico Granata0c10a852014-12-08 23:13:56 +00003772}
Enrico Granatae29df232014-12-09 19:51:20 +00003773
Kate Stoneb9c1b512016-09-06 20:57:50 +00003774void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3775 m_is_synthetic_children_generated = b;
Enrico Granatae29df232014-12-09 19:51:20 +00003776}
3777
Kate Stoneb9c1b512016-09-06 20:57:50 +00003778uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
Enrico Granatadc62ffd2015-11-09 19:27:34 +00003779
Kate Stoneb9c1b512016-09-06 20:57:50 +00003780void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }