blob: 9e6b9da78b991379bef5a7f776f0ebfa035a080d [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
Zachary Turner2f3df612017-04-06 21:28:29 +000012#include "lldb/Core/Address.h" // for Address
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Core/Module.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000014#include "lldb/Core/Scalar.h" // for Scalar
Enrico Granata21fd13f2012-10-27 02:05:48 +000015#include "lldb/Core/ValueObjectCast.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000017#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000018#include "lldb/Core/ValueObjectDynamicValue.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000019#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000020#include "lldb/Core/ValueObjectSyntheticFilter.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000021#include "lldb/DataFormatters/DataVisualization.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000022#include "lldb/DataFormatters/FormatManager.h" // for FormatManager
Enrico Granata2206b482014-10-30 18:27:31 +000023#include "lldb/DataFormatters/StringPrinter.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000024#include "lldb/DataFormatters/TypeFormat.h" // for TypeFormatImpl_F...
25#include "lldb/DataFormatters/TypeSummary.h" // for TypeSummaryOptions
26#include "lldb/DataFormatters/TypeValidator.h" // for TypeValidatorImp...
Enrico Granata4d93b8c2013-09-30 19:11:51 +000027#include "lldb/DataFormatters/ValueObjectPrinter.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000028#include "lldb/Expression/ExpressionVariable.h" // for ExpressionVariable
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Symbol/ClangASTContext.h"
Enrico Granatac1247f52014-11-06 21:23:20 +000030#include "lldb/Symbol/CompileUnit.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000031#include "lldb/Symbol/CompilerType.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000032#include "lldb/Symbol/Declaration.h" // for Declaration
33#include "lldb/Symbol/SymbolContext.h" // for SymbolContext
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Symbol/Type.h"
Jim Ingham53c47f12010-09-10 23:12:17 +000035#include "lldb/Target/ExecutionContext.h"
Enrico Granata407b5c62015-11-02 21:52:05 +000036#include "lldb/Target/Language.h"
Jim Ingham5a369122010-09-28 01:25:32 +000037#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000038#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/Process.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000040#include "lldb/Target/StackFrame.h" // for StackFrame
Greg Claytonf5e56de2010-09-14 23:36:40 +000041#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/Thread.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000043#include "lldb/Target/ThreadList.h" // for ThreadList
44#include "lldb/Utility/DataBuffer.h" // for DataBuffer
45#include "lldb/Utility/DataBufferHeap.h"
46#include "lldb/Utility/Flags.h" // for Flags
47#include "lldb/Utility/Log.h"
48#include "lldb/Utility/Logging.h" // for GetLogIfAllCateg...
49#include "lldb/Utility/SharingPtr.h" // for SharingPtr
50#include "lldb/Utility/Stream.h" // for Stream
51#include "lldb/Utility/StreamString.h"
52#include "lldb/lldb-private-types.h" // for RegisterInfo
53
54#include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
55
56#include <algorithm> // for min
57#include <cstdint> // for uint32_t, uint64_t
58#include <cstdlib> // for size_t, NULL
59#include <memory> // for shared_ptr, oper...
60#include <tuple> // for tie, tuple
61
62#include <assert.h> // for assert
63#include <inttypes.h> // for PRIu64, PRIx64
64#include <stdio.h> // for snprintf
65#include <string.h> // for memcpy, memcmp
66
67namespace lldb_private {
68class ExecutionContextScope;
69}
70namespace lldb_private {
71class SymbolContextScope;
72}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073
74using namespace lldb;
75using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000076using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
Greg Claytonafacd142011-09-02 01:15:17 +000078static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80//----------------------------------------------------------------------
81// ValueObject constructor
82//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000083ValueObject::ValueObject(ValueObject &parent)
84 : UserID(++g_value_obj_uid), // Unique identifier for every value object
85 m_parent(&parent), m_root(NULL), m_update_point(parent.GetUpdatePoint()),
86 m_name(), m_data(), m_value(), m_error(), m_value_str(),
87 m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(),
88 m_validation_result(), m_manager(parent.GetManager()), m_children(),
89 m_synthetic_children(), m_dynamic_value(NULL), m_synthetic_value(NULL),
90 m_deref_valobj(NULL), m_format(eFormatDefault),
91 m_last_format(eFormatDefault), m_last_format_mgr_revision(0),
92 m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(),
93 m_type_validator_sp(), m_user_id_of_forced_summary(),
94 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
95 m_value_checksum(),
96 m_preferred_display_language(lldb::eLanguageTypeUnknown),
97 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
98 m_children_count_valid(false), m_old_value_valid(false),
99 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
100 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
101 m_is_getting_summary(false),
102 m_did_calculate_complete_objc_class_type(false),
103 m_is_synthetic_children_generated(
104 parent.m_is_synthetic_children_generated) {
105 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000106}
107
108//----------------------------------------------------------------------
109// ValueObject constructor
110//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111ValueObject::ValueObject(ExecutionContextScope *exe_scope,
112 AddressType child_ptr_or_ref_addr_type)
113 : UserID(++g_value_obj_uid), // Unique identifier for every value object
114 m_parent(NULL), m_root(NULL), m_update_point(exe_scope), m_name(),
115 m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(),
116 m_location_str(), m_summary_str(), m_object_desc_str(),
117 m_validation_result(), m_manager(), m_children(), m_synthetic_children(),
118 m_dynamic_value(NULL), m_synthetic_value(NULL), m_deref_valobj(NULL),
119 m_format(eFormatDefault), m_last_format(eFormatDefault),
120 m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(),
121 m_synthetic_children_sp(), m_type_validator_sp(),
122 m_user_id_of_forced_summary(),
123 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
124 m_value_checksum(),
125 m_preferred_display_language(lldb::eLanguageTypeUnknown),
126 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
127 m_children_count_valid(false), m_old_value_valid(false),
128 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
129 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
130 m_is_getting_summary(false),
131 m_did_calculate_complete_objc_class_type(false),
132 m_is_synthetic_children_generated(false) {
133 m_manager = new ValueObjectManager();
134 m_manager->ManageObject(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
137//----------------------------------------------------------------------
138// Destructor
139//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140ValueObject::~ValueObject() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142bool ValueObject::UpdateValueIfNeeded(bool update_format) {
Greg Claytonb71f3842010-10-05 03:13:51 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 bool did_change_formats = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 if (update_format)
147 did_change_formats = UpdateFormatsIfNeeded();
Greg Claytonefbc7d22012-03-09 04:23:44 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 // If this is a constant value, then our success is predicated on whether
150 // we have an error or not
151 if (GetIsConstant()) {
152 // if you are constant, things might still have changed behind your back
153 // (e.g. you are a frozen object and things have changed deeper than you
154 // cared to freeze-dry yourself)
155 // in this case, your value has not changed, but "computed" entries might
156 // have, so you might now have
157 // a different summary, or a different object description. clear these so we
158 // will recompute them
159 if (update_format && !did_change_formats)
160 ClearUserVisibleData(eClearUserVisibleDataItemsSummary |
161 eClearUserVisibleDataItemsDescription);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 return m_error.Success();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164
165 bool first_update = IsChecksumEmpty();
166
167 if (NeedsUpdating()) {
168 m_update_point.SetUpdated();
169
170 // Save the old value using swap to avoid a string copy which
171 // also will clear our m_value_str
172 if (m_value_str.empty()) {
173 m_old_value_valid = false;
174 } else {
175 m_old_value_valid = true;
176 m_old_value_str.swap(m_value_str);
177 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
178 }
179
180 ClearUserVisibleData();
181
182 if (IsInScope()) {
183 const bool value_was_valid = GetValueIsValid();
184 SetValueDidChange(false);
185
186 m_error.Clear();
187
188 // Call the pure virtual function to update the value
189
190 bool need_compare_checksums = false;
191 llvm::SmallVector<uint8_t, 16> old_checksum;
192
193 if (!first_update && CanProvideValue()) {
194 need_compare_checksums = true;
195 old_checksum.resize(m_value_checksum.size());
196 std::copy(m_value_checksum.begin(), m_value_checksum.end(),
197 old_checksum.begin());
198 }
199
200 bool success = UpdateValue();
201
202 SetValueIsValid(success);
203
204 if (success) {
205 const uint64_t max_checksum_size = 128;
206 m_data.Checksum(m_value_checksum, max_checksum_size);
207 } else {
208 need_compare_checksums = false;
209 m_value_checksum.clear();
210 }
211
212 assert(!need_compare_checksums ||
213 (!old_checksum.empty() && !m_value_checksum.empty()));
214
215 if (first_update)
216 SetValueDidChange(false);
217 else if (!m_value_did_change && success == false) {
218 // The value wasn't gotten successfully, so we mark this
219 // as changed if the value used to be valid and now isn't
220 SetValueDidChange(value_was_valid);
221 } else if (need_compare_checksums) {
222 SetValueDidChange(memcmp(&old_checksum[0], &m_value_checksum[0],
223 m_value_checksum.size()));
224 }
225
226 } else {
227 m_error.SetErrorString("out of scope");
228 }
229 }
230 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233bool ValueObject::UpdateFormatsIfNeeded() {
234 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
235 if (log)
236 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject "
237 "rev: %d - Global rev: %d",
238 GetName().GetCString(), static_cast<void *>(this),
239 m_last_format_mgr_revision,
240 DataVisualization::GetCurrentRevision());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 bool any_change = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if ((m_last_format_mgr_revision != DataVisualization::GetCurrentRevision())) {
245 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
246 any_change = true;
247
248 SetValueFormat(DataVisualization::GetFormat(*this, eNoDynamicValues));
249 SetSummaryFormat(
250 DataVisualization::GetSummaryFormat(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000251#ifndef LLDB_DISABLE_PYTHON
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 SetSyntheticChildren(
253 DataVisualization::GetSyntheticChildren(*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000254#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
256 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 return any_change;
Enrico Granata4becb372011-06-29 22:27:15 +0000259}
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261void ValueObject::SetNeedsUpdate() {
262 m_update_point.SetNeedsUpdate();
263 // We have to clear the value string here so ConstResult children will notice
264 // if their values are
265 // changed by hand (i.e. with SetValueAsCString).
266 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269void ValueObject::ClearDynamicTypeInformation() {
270 m_children_count_valid = false;
271 m_did_calculate_complete_objc_class_type = false;
272 m_last_format_mgr_revision = 0;
273 m_override_type = CompilerType();
274 SetValueFormat(lldb::TypeFormatImplSP());
275 SetSummaryFormat(lldb::TypeSummaryImplSP());
276 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000277}
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279CompilerType ValueObject::MaybeCalculateCompleteType() {
280 CompilerType compiler_type(GetCompilerTypeImpl());
281
282 if (m_did_calculate_complete_objc_class_type) {
283 if (m_override_type.IsValid())
284 return m_override_type;
Sean Callanan72772842012-02-22 23:57:45 +0000285 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 return compiler_type;
287 }
288
289 CompilerType class_type;
290 bool is_pointer_type = false;
291
292 if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type)) {
293 is_pointer_type = true;
294 } else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type)) {
295 class_type = compiler_type;
296 } else {
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000297 return compiler_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 }
299
300 m_did_calculate_complete_objc_class_type = true;
301
302 if (class_type) {
303 ConstString class_name(class_type.GetConstTypeName());
304
305 if (class_name) {
306 ProcessSP process_sp(
307 GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
308
309 if (process_sp) {
310 ObjCLanguageRuntime *objc_language_runtime(
311 process_sp->GetObjCLanguageRuntime());
312
313 if (objc_language_runtime) {
314 TypeSP complete_objc_class_type_sp =
315 objc_language_runtime->LookupInCompleteClassCache(class_name);
316
317 if (complete_objc_class_type_sp) {
318 CompilerType complete_class(
319 complete_objc_class_type_sp->GetFullCompilerType());
320
321 if (complete_class.GetCompleteType()) {
322 if (is_pointer_type) {
323 m_override_type = complete_class.GetPointerType();
324 } else {
325 m_override_type = complete_class;
326 }
327
328 if (m_override_type.IsValid())
329 return m_override_type;
330 }
331 }
332 }
333 }
334 }
335 }
336 return compiler_type;
Sean Callanan72772842012-02-22 23:57:45 +0000337}
338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339CompilerType ValueObject::GetCompilerType() {
340 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000341}
342
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343TypeImpl ValueObject::GetTypeImpl() { return TypeImpl(GetCompilerType()); }
344
345DataExtractor &ValueObject::GetDataExtractor() {
346 UpdateValueIfNeeded(false);
347 return m_data;
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000348}
349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350const Error &ValueObject::GetError() {
351 UpdateValueIfNeeded(false);
352 return m_error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355const ConstString &ValueObject::GetName() const { return m_name; }
356
357const char *ValueObject::GetLocationAsCString() {
358 return GetLocationAsCStringImpl(m_value, m_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359}
360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
362 const DataExtractor &data) {
363 if (UpdateValueIfNeeded(false)) {
364 if (m_location_str.empty()) {
365 StreamString sstr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 Value::ValueType value_type = value.GetValueType();
Enrico Granata82fabf82013-04-30 20:45:04 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 switch (value_type) {
370 case Value::eValueTypeScalar:
371 case Value::eValueTypeVector:
372 if (value.GetContextType() == Value::eContextTypeRegisterInfo) {
373 RegisterInfo *reg_info = value.GetRegisterInfo();
374 if (reg_info) {
375 if (reg_info->name)
376 m_location_str = reg_info->name;
377 else if (reg_info->alt_name)
378 m_location_str = reg_info->alt_name;
379 if (m_location_str.empty())
380 m_location_str = (reg_info->encoding == lldb::eEncodingVector)
381 ? "vector"
382 : "scalar";
383 }
384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 if (m_location_str.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 m_location_str =
387 (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
388 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389
Kate Stoneb9c1b512016-09-06 20:57:50 +0000390 case Value::eValueTypeLoadAddress:
391 case Value::eValueTypeFileAddress:
392 case Value::eValueTypeHostAddress: {
393 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
394 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size,
395 value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
Zachary Turnerc1564272016-11-16 21:15:24 +0000396 m_location_str = sstr.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 } break;
398 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 }
401 return m_location_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402}
403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404Value &ValueObject::GetValue() { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406const Value &ValueObject::GetValue() const { return m_value; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408bool ValueObject::ResolveValue(Scalar &scalar) {
409 if (UpdateValueIfNeeded(
410 false)) // make sure that you are up to date before returning anything
411 {
412 ExecutionContext exe_ctx(GetExecutionContextRef());
413 Value tmp_value(m_value);
414 scalar = tmp_value.ResolveValue(&exe_ctx);
415 if (scalar.IsValid()) {
416 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
417 if (bitfield_bit_size)
418 return scalar.ExtractBitfield(bitfield_bit_size,
419 GetBitfieldBitOffset());
420 return true;
Enrico Granata6fd87d52011-08-04 01:41:02 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 }
423 return false;
424}
425
426bool ValueObject::IsLogicalTrue(Error &error) {
427 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
428 LazyBool is_logical_true = language->IsLogicalTrue(*this, error);
429 switch (is_logical_true) {
430 case eLazyBoolYes:
431 case eLazyBoolNo:
432 return (is_logical_true == true);
433 case eLazyBoolCalculate:
434 break;
435 }
436 }
437
438 Scalar scalar_value;
439
440 if (!ResolveValue(scalar_value)) {
441 error.SetErrorString("failed to get a scalar result");
Greg Claytondcad5022011-12-29 01:26:56 +0000442 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 }
444
445 bool ret;
446 if (scalar_value.ULongLong(1) == 0)
447 ret = false;
448 else
449 ret = true;
450 error.Clear();
451 return ret;
Greg Clayton8f343b02010-11-04 01:54:29 +0000452}
453
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454bool ValueObject::GetValueIsValid() const { return m_value_is_valid; }
455
456void ValueObject::SetValueIsValid(bool b) { m_value_is_valid = b; }
457
458bool ValueObject::GetValueDidChange() { return m_value_did_change; }
459
460void ValueObject::SetValueDidChange(bool value_changed) {
461 m_value_did_change = value_changed;
462}
463
464ValueObjectSP ValueObject::GetChildAtIndex(size_t idx, bool can_create) {
465 ValueObjectSP child_sp;
466 // We may need to update our value if we are dynamic
467 if (IsPossibleDynamicType())
468 UpdateValueIfNeeded(false);
469 if (idx < GetNumChildren()) {
470 // Check if we have already made the child value object?
471 if (can_create && !m_children.HasChildAtIndex(idx)) {
472 // No we haven't created the child at this index, so lets have our
473 // subclass do it and cache the result for quick future access.
474 m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
Enrico Granata407b5c62015-11-02 21:52:05 +0000475 }
Jim Ingham98e6daf2015-10-31 00:02:18 +0000476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 ValueObject *child = m_children.GetChildAtIndex(idx);
478 if (child != NULL)
479 return child->GetSP();
480 }
481 return child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482}
483
Enrico Granata3309d882013-01-12 01:00:22 +0000484lldb::ValueObjectSP
Lang Hames088d0012017-04-26 18:15:40 +0000485ValueObject::GetChildAtIndexPath(llvm::ArrayRef<size_t> idxs,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 size_t *index_of_error) {
487 if (idxs.size() == 0)
488 return GetSP();
489 ValueObjectSP root(GetSP());
490 for (size_t idx : idxs) {
491 root = root->GetChildAtIndex(idx, true);
492 if (!root) {
493 if (index_of_error)
494 *index_of_error = idx;
495 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 }
498 return root;
499}
500
501lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
Lang Hames088d0012017-04-26 18:15:40 +0000502 llvm::ArrayRef<std::pair<size_t, bool>> idxs, size_t *index_of_error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 if (idxs.size() == 0)
504 return GetSP();
505 ValueObjectSP root(GetSP());
506 for (std::pair<size_t, bool> idx : idxs) {
507 root = root->GetChildAtIndex(idx.first, idx.second);
508 if (!root) {
509 if (index_of_error)
510 *index_of_error = idx.first;
511 return root;
512 }
513 }
514 return root;
Enrico Granata3309d882013-01-12 01:00:22 +0000515}
516
517lldb::ValueObjectSP
Lang Hames088d0012017-04-26 18:15:40 +0000518ValueObject::GetChildAtNamePath(llvm::ArrayRef<ConstString> names,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 ConstString *name_of_error) {
520 if (names.size() == 0)
521 return GetSP();
522 ValueObjectSP root(GetSP());
523 for (ConstString name : names) {
524 root = root->GetChildMemberWithName(name, true);
525 if (!root) {
526 if (name_of_error)
527 *name_of_error = name;
528 return root;
529 }
530 }
531 return root;
Enrico Granataef8dde62015-12-21 23:10:17 +0000532}
533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
Lang Hames088d0012017-04-26 18:15:40 +0000535 llvm::ArrayRef<std::pair<ConstString, bool>> names,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536 ConstString *name_of_error) {
537 if (names.size() == 0)
538 return GetSP();
539 ValueObjectSP root(GetSP());
540 for (std::pair<ConstString, bool> name : names) {
541 root = root->GetChildMemberWithName(name.first, name.second);
542 if (!root) {
543 if (name_of_error)
544 *name_of_error = name.first;
545 return root;
546 }
547 }
548 return root;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000549}
550
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551size_t ValueObject::GetIndexOfChildWithName(const ConstString &name) {
552 bool omit_empty_base_classes = true;
553 return GetCompilerType().GetIndexOfChildWithName(name.GetCString(),
554 omit_empty_base_classes);
Enrico Granatae2e220a2013-09-12 00:48:47 +0000555}
556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557ValueObjectSP ValueObject::GetChildMemberWithName(const ConstString &name,
558 bool can_create) {
559 // when getting a child by name, it could be buried inside some base
560 // classes (which really aren't part of the expression path), so we
561 // need a vector of indexes that can get us down to the correct child
562 ValueObjectSP child_sp;
563
564 // We may need to update our value if we are dynamic
565 if (IsPossibleDynamicType())
566 UpdateValueIfNeeded(false);
567
568 std::vector<uint32_t> child_indexes;
569 bool omit_empty_base_classes = true;
570 const size_t num_child_indexes =
571 GetCompilerType().GetIndexOfChildMemberWithName(
572 name.GetCString(), omit_empty_base_classes, child_indexes);
573 if (num_child_indexes > 0) {
574 std::vector<uint32_t>::const_iterator pos = child_indexes.begin();
575 std::vector<uint32_t>::const_iterator end = child_indexes.end();
576
577 child_sp = GetChildAtIndex(*pos, can_create);
578 for (++pos; pos != end; ++pos) {
579 if (child_sp) {
580 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex(*pos, can_create));
581 child_sp = new_child_sp;
582 } else {
583 child_sp.reset();
584 }
Enrico Granatae2e220a2013-09-12 00:48:47 +0000585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 }
587 return child_sp;
Enrico Granatae2e220a2013-09-12 00:48:47 +0000588}
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590size_t ValueObject::GetNumChildren(uint32_t max) {
591 UpdateValueIfNeeded();
592
593 if (max < UINT32_MAX) {
594 if (m_children_count_valid) {
595 size_t children_count = m_children.GetChildrenCount();
596 return children_count <= max ? children_count : max;
597 } else
598 return CalculateNumChildren(max);
599 }
600
601 if (!m_children_count_valid) {
602 SetNumChildren(CalculateNumChildren());
603 }
604 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}
606
Kate Stoneb9c1b512016-09-06 20:57:50 +0000607bool ValueObject::MightHaveChildren() {
608 bool has_children = false;
609 const uint32_t type_info = GetTypeInfo();
610 if (type_info) {
611 if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
612 has_children = true;
613 } else {
614 has_children = GetNumChildren() > 0;
615 }
616 return has_children;
Greg Clayton4a792072012-10-23 01:50:10 +0000617}
618
619// Should only be called by ValueObject::GetNumChildren()
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620void ValueObject::SetNumChildren(size_t num_children) {
621 m_children_count_valid = true;
622 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623}
624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625void ValueObject::SetName(const ConstString &name) { m_name = name; }
626
627ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
628 bool synthetic_array_member,
629 int32_t synthetic_index) {
630 ValueObject *valobj = NULL;
631
632 bool omit_empty_base_classes = true;
633 bool ignore_array_bounds = synthetic_array_member;
634 std::string child_name_str;
635 uint32_t child_byte_size = 0;
636 int32_t child_byte_offset = 0;
637 uint32_t child_bitfield_bit_size = 0;
638 uint32_t child_bitfield_bit_offset = 0;
639 bool child_is_base_class = false;
640 bool child_is_deref_of_parent = false;
641 uint64_t language_flags = 0;
642
643 const bool transparent_pointers = synthetic_array_member == false;
644 CompilerType child_compiler_type;
645
646 ExecutionContext exe_ctx(GetExecutionContextRef());
647
648 child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
649 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
650 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
651 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
652 child_is_deref_of_parent, this, language_flags);
653 if (child_compiler_type) {
654 if (synthetic_index)
655 child_byte_offset += child_byte_size * synthetic_index;
656
657 ConstString child_name;
658 if (!child_name_str.empty())
659 child_name.SetCString(child_name_str.c_str());
660
661 valobj = new ValueObjectChild(
662 *this, child_compiler_type, child_name, child_byte_size,
663 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
664 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
665 language_flags);
666 // if (valobj)
667 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
668 }
669
670 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671}
672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
674 std::string &destination,
675 lldb::LanguageType lang) {
676 return GetSummaryAsCString(summary_ptr, destination,
677 TypeSummaryOptions().SetLanguage(lang));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678}
679
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
681 std::string &destination,
682 const TypeSummaryOptions &options) {
683 destination.clear();
684
685 // ideally we would like to bail out if passing NULL, but if we do so
686 // we end up not providing the summary for function pointers anymore
687 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
688 return false;
689
690 m_is_getting_summary = true;
691
692 TypeSummaryOptions actual_options(options);
693
694 if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
695 actual_options.SetLanguage(GetPreferredDisplayLanguage());
696
697 // this is a hot path in code and we prefer to avoid setting this string all
698 // too often also clearing out other
699 // information that we might care to see in a crash log. might be useful in
700 // very specific situations though.
701 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s.
702 Summary provider's description is %s",
703 GetTypeName().GetCString(),
704 GetName().GetCString(),
705 summary_ptr->GetDescription().c_str());*/
706
707 if (UpdateValueIfNeeded(false) && summary_ptr) {
708 if (HasSyntheticValue())
709 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on
710 // the synthetic children being
711 // up-to-date (e.g. ${svar%#})
712 summary_ptr->FormatObject(this, destination, actual_options);
713 }
714 m_is_getting_summary = false;
715 return !destination.empty();
Enrico Granatac1247f52014-11-06 21:23:20 +0000716}
717
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718const char *ValueObject::GetSummaryAsCString(lldb::LanguageType lang) {
719 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) {
720 TypeSummaryOptions summary_options;
721 summary_options.SetLanguage(lang);
722 GetSummaryAsCString(GetSummaryFormat().get(), m_summary_str,
723 summary_options);
724 }
725 if (m_summary_str.empty())
726 return NULL;
727 return m_summary_str.c_str();
728}
729
730bool ValueObject::GetSummaryAsCString(std::string &destination,
731 const TypeSummaryOptions &options) {
732 return GetSummaryAsCString(GetSummaryFormat().get(), destination, options);
733}
734
735bool ValueObject::IsCStringContainer(bool check_pointer) {
736 CompilerType pointee_or_element_compiler_type;
737 const Flags type_flags(GetTypeInfo(&pointee_or_element_compiler_type));
738 bool is_char_arr_ptr(type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
739 pointee_or_element_compiler_type.IsCharType());
740 if (!is_char_arr_ptr)
741 return false;
742 if (!check_pointer)
743 return true;
744 if (type_flags.Test(eTypeIsArray))
745 return true;
746 addr_t cstr_address = LLDB_INVALID_ADDRESS;
747 AddressType cstr_address_type = eAddressTypeInvalid;
748 cstr_address = GetAddressOf(true, &cstr_address_type);
749 return (cstr_address != LLDB_INVALID_ADDRESS);
750}
751
752size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
753 uint32_t item_count) {
754 CompilerType pointee_or_element_compiler_type;
755 const uint32_t type_info = GetTypeInfo(&pointee_or_element_compiler_type);
756 const bool is_pointer_type = type_info & eTypeIsPointer;
757 const bool is_array_type = type_info & eTypeIsArray;
758 if (!(is_pointer_type || is_array_type))
759 return 0;
760
761 if (item_count == 0)
762 return 0;
763
764 ExecutionContext exe_ctx(GetExecutionContextRef());
765
766 const uint64_t item_type_size = pointee_or_element_compiler_type.GetByteSize(
767 exe_ctx.GetBestExecutionContextScope());
768 const uint64_t bytes = item_count * item_type_size;
769 const uint64_t offset = item_idx * item_type_size;
770
771 if (item_idx == 0 && item_count == 1) // simply a deref
772 {
773 if (is_pointer_type) {
774 Error error;
775 ValueObjectSP pointee_sp = Dereference(error);
776 if (error.Fail() || pointee_sp.get() == NULL)
777 return 0;
778 return pointee_sp->GetData(data, error);
779 } else {
780 ValueObjectSP child_sp = GetChildAtIndex(0, true);
781 if (child_sp.get() == NULL)
782 return 0;
783 Error error;
784 return child_sp->GetData(data, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 return true;
787 } else /* (items > 1) */
788 {
789 Error error;
790 lldb_private::DataBufferHeap *heap_buf_ptr = NULL;
791 lldb::DataBufferSP data_sp(heap_buf_ptr =
792 new lldb_private::DataBufferHeap());
Enrico Granata0c489f52012-03-01 04:24:26 +0000793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 AddressType addr_type;
795 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type)
796 : GetAddressOf(true, &addr_type);
797
798 switch (addr_type) {
799 case eAddressTypeFile: {
800 ModuleSP module_sp(GetModule());
801 if (module_sp) {
802 addr = addr + offset;
803 Address so_addr;
804 module_sp->ResolveFileAddress(addr, so_addr);
805 ExecutionContext exe_ctx(GetExecutionContextRef());
806 Target *target = exe_ctx.GetTargetPtr();
807 if (target) {
808 heap_buf_ptr->SetByteSize(bytes);
809 size_t bytes_read = target->ReadMemory(
810 so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
811 if (error.Success()) {
812 data.SetData(data_sp);
813 return bytes_read;
814 }
815 }
816 }
817 } break;
818 case eAddressTypeLoad: {
819 ExecutionContext exe_ctx(GetExecutionContextRef());
820 Process *process = exe_ctx.GetProcessPtr();
821 if (process) {
822 heap_buf_ptr->SetByteSize(bytes);
823 size_t bytes_read = process->ReadMemory(
824 addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
825 if (error.Success() || bytes_read > 0) {
826 data.SetData(data_sp);
827 return bytes_read;
828 }
829 }
830 } break;
831 case eAddressTypeHost: {
832 const uint64_t max_bytes =
833 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
834 if (max_bytes > offset) {
835 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
836 addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
837 if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
838 break;
839 heap_buf_ptr->CopyData((uint8_t *)(addr + offset), bytes_read);
840 data.SetData(data_sp);
841 return bytes_read;
842 }
843 } break;
844 case eAddressTypeInvalid:
845 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 }
848 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849}
850
Kate Stoneb9c1b512016-09-06 20:57:50 +0000851uint64_t ValueObject::GetData(DataExtractor &data, Error &error) {
852 UpdateValueIfNeeded(false);
853 ExecutionContext exe_ctx(GetExecutionContextRef());
854 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
855 if (error.Fail()) {
856 if (m_data.GetByteSize()) {
857 data = m_data;
858 error.Clear();
859 return data.GetByteSize();
860 } else {
861 return 0;
862 }
863 }
864 data.SetAddressByteSize(m_data.GetAddressByteSize());
865 data.SetByteOrder(m_data.GetByteOrder());
866 return data.GetByteSize();
Enrico Granata49bfafb2014-11-18 23:36:25 +0000867}
868
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869bool ValueObject::SetData(DataExtractor &data, Error &error) {
870 error.Clear();
871 // Make sure our value is up to date first so that our location and location
872 // type is valid.
873 if (!UpdateValueIfNeeded(false)) {
874 error.SetErrorString("unable to read value");
875 return false;
876 }
877
878 uint64_t count = 0;
879 const Encoding encoding = GetCompilerType().GetEncoding(count);
880
881 const size_t byte_size = GetByteSize();
882
883 Value::ValueType value_type = m_value.GetValueType();
884
885 switch (value_type) {
886 case Value::eValueTypeScalar: {
887 Error set_error =
888 m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
889
890 if (!set_error.Success()) {
891 error.SetErrorStringWithFormat("unable to set scalar value: %s",
892 set_error.AsCString());
893 return false;
894 }
895 } break;
896 case Value::eValueTypeLoadAddress: {
897 // If it is a load address, then the scalar value is the storage location
898 // of the data, and we have to shove this value down to that load location.
899 ExecutionContext exe_ctx(GetExecutionContextRef());
900 Process *process = exe_ctx.GetProcessPtr();
901 if (process) {
902 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
903 size_t bytes_written = process->WriteMemory(
904 target_addr, data.GetDataStart(), byte_size, error);
905 if (!error.Success())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000906 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000907 if (bytes_written != byte_size) {
908 error.SetErrorString("unable to write value to memory");
909 return false;
910 }
911 }
912 } break;
913 case Value::eValueTypeHostAddress: {
914 // If it is a host address, then we stuff the scalar as a DataBuffer into
915 // the Value's data.
916 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
917 m_data.SetData(buffer_sp, 0);
918 data.CopyByteOrderedData(0, byte_size,
919 const_cast<uint8_t *>(m_data.GetDataStart()),
920 byte_size, m_data.GetByteOrder());
921 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
922 } break;
923 case Value::eValueTypeFileAddress:
924 case Value::eValueTypeVector:
925 break;
926 }
927
928 // If we have reached this point, then we have successfully changed the value.
929 SetNeedsUpdate();
930 return true;
931}
932
933static bool CopyStringDataToBufferSP(const StreamString &source,
934 lldb::DataBufferSP &destination) {
935 destination.reset(new DataBufferHeap(source.GetSize() + 1, 0));
Zachary Turnerc1564272016-11-16 21:15:24 +0000936 memcpy(destination->GetBytes(), source.GetString().data(), source.GetSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000937 return true;
938}
939
940std::pair<size_t, bool>
941ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Error &error,
942 uint32_t max_length, bool honor_array,
943 Format item_format) {
944 bool was_capped = false;
945 StreamString s;
946 ExecutionContext exe_ctx(GetExecutionContextRef());
947 Target *target = exe_ctx.GetTargetPtr();
948
949 if (!target) {
950 s << "<no target to read from>";
951 error.SetErrorString("no target to read from");
952 CopyStringDataToBufferSP(s, buffer_sp);
953 return {0, was_capped};
954 }
955
956 if (max_length == 0)
957 max_length = target->GetMaximumSizeOfStringSummary();
958
959 size_t bytes_read = 0;
960 size_t total_bytes_read = 0;
961
962 CompilerType compiler_type = GetCompilerType();
963 CompilerType elem_or_pointee_compiler_type;
964 const Flags type_flags(GetTypeInfo(&elem_or_pointee_compiler_type));
965 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
966 elem_or_pointee_compiler_type.IsCharType()) {
Greg Claytonafacd142011-09-02 01:15:17 +0000967 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000968 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 size_t cstr_len = 0;
971 bool capped_data = false;
972 const bool is_array = type_flags.Test(eTypeIsArray);
973 if (is_array) {
974 // We have an array
975 uint64_t array_size = 0;
976 if (compiler_type.IsArrayType(NULL, &array_size, NULL)) {
977 cstr_len = array_size;
978 if (cstr_len > max_length) {
979 capped_data = true;
980 cstr_len = max_length;
Enrico Granata9128ee22011-09-06 19:20:51 +0000981 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 }
983 cstr_address = GetAddressOf(true, &cstr_address_type);
984 } else {
985 // We have a pointer
986 cstr_address = GetPointerValue(&cstr_address_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000987 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000988
Kate Stoneb9c1b512016-09-06 20:57:50 +0000989 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) {
990 if (cstr_address_type == eAddressTypeHost && is_array) {
991 const char *cstr = GetDataExtractor().PeekCStr(0);
992 if (cstr == nullptr) {
993 s << "<invalid address>";
994 error.SetErrorString("invalid address");
995 CopyStringDataToBufferSP(s, buffer_sp);
996 return {0, was_capped};
Sean Callananed185ab2013-04-19 19:47:32 +0000997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998 buffer_sp.reset(new DataBufferHeap(cstr_len, 0));
999 memcpy(buffer_sp->GetBytes(), cstr, cstr_len);
1000 return {cstr_len, was_capped};
1001 } else {
1002 s << "<invalid address>";
1003 error.SetErrorString("invalid address");
Enrico Granata2206b482014-10-30 18:27:31 +00001004 CopyStringDataToBufferSP(s, buffer_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 return {0, was_capped};
1006 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001007 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008
1009 Address cstr_so_addr(cstr_address);
1010 DataExtractor data;
1011 if (cstr_len > 0 && honor_array) {
1012 // I am using GetPointeeData() here to abstract the fact that some
1013 // ValueObjects are actually frozen pointers in the host
1014 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1015 // automatically takes care of this
1016 GetPointeeData(data, 0, cstr_len);
1017
1018 if ((bytes_read = data.GetByteSize()) > 0) {
1019 total_bytes_read = bytes_read;
1020 for (size_t offset = 0; offset < bytes_read; offset++)
1021 s.Printf("%c", *data.PeekData(offset, 1));
1022 if (capped_data)
1023 was_capped = true;
1024 }
1025 } else {
1026 cstr_len = max_length;
1027 const size_t k_max_buf_size = 64;
1028
1029 size_t offset = 0;
1030
1031 int cstr_len_displayed = -1;
1032 bool capped_cstr = false;
1033 // I am using GetPointeeData() here to abstract the fact that some
1034 // ValueObjects are actually frozen pointers in the host
1035 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1036 // automatically takes care of this
1037 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) {
1038 total_bytes_read += bytes_read;
1039 const char *cstr = data.PeekCStr(0);
1040 size_t len = strnlen(cstr, k_max_buf_size);
1041 if (cstr_len_displayed < 0)
1042 cstr_len_displayed = len;
1043
1044 if (len == 0)
1045 break;
1046 cstr_len_displayed += len;
1047 if (len > bytes_read)
1048 len = bytes_read;
1049 if (len > cstr_len)
1050 len = cstr_len;
1051
1052 for (size_t offset = 0; offset < bytes_read; offset++)
1053 s.Printf("%c", *data.PeekData(offset, 1));
1054
1055 if (len < k_max_buf_size)
1056 break;
1057
1058 if (len >= cstr_len) {
1059 capped_cstr = true;
1060 break;
1061 }
1062
1063 cstr_len -= len;
1064 offset += len;
1065 }
1066
1067 if (cstr_len_displayed >= 0) {
1068 if (capped_cstr)
1069 was_capped = true;
1070 }
1071 }
1072 } else {
1073 error.SetErrorString("not a string object");
1074 s << "<not a string object>";
1075 }
1076 CopyStringDataToBufferSP(s, buffer_sp);
1077 return {total_bytes_read, was_capped};
1078}
1079
1080std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() {
1081 if (!UpdateValueIfNeeded(true))
1082 return {TypeValidatorResult::Success,
1083 ""}; // not the validator's job to discuss update problems
1084
1085 if (m_validation_result.hasValue())
1086 return m_validation_result.getValue();
1087
1088 if (!m_type_validator_sp)
1089 return {TypeValidatorResult::Success, ""}; // no validator no failure
1090
1091 auto outcome = m_type_validator_sp->FormatObject(this);
1092
1093 return (m_validation_result = {outcome.m_result, outcome.m_message})
1094 .getValue();
1095}
1096
1097const char *ValueObject::GetObjectDescription() {
1098
1099 if (!UpdateValueIfNeeded(true))
1100 return NULL;
1101
1102 if (!m_object_desc_str.empty())
1103 return m_object_desc_str.c_str();
1104
1105 ExecutionContext exe_ctx(GetExecutionContextRef());
1106 Process *process = exe_ctx.GetProcessPtr();
1107 if (process == NULL)
1108 return NULL;
1109
1110 StreamString s;
1111
1112 LanguageType language = GetObjectRuntimeLanguage();
1113 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1114
1115 if (runtime == NULL) {
1116 // Aw, hell, if the things a pointer, or even just an integer, let's try
1117 // ObjC anyway...
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001118 CompilerType compiler_type = GetCompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 if (compiler_type) {
1120 bool is_signed;
1121 if (compiler_type.IsIntegerType(is_signed) ||
1122 compiler_type.IsPointerType()) {
1123 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1124 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 }
1127
1128 if (runtime && runtime->GetObjectDescription(s, *this)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001129 m_object_desc_str.append(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130 }
1131
1132 if (m_object_desc_str.empty())
1133 return NULL;
1134 else
1135 return m_object_desc_str.c_str();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001136}
1137
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138bool ValueObject::GetValueAsCString(const lldb_private::TypeFormatImpl &format,
1139 std::string &destination) {
1140 if (UpdateValueIfNeeded(false))
1141 return format.FormatObject(this, destination);
1142 else
1143 return false;
Enrico Granata744794a2014-09-05 21:46:22 +00001144}
1145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146bool ValueObject::GetValueAsCString(lldb::Format format,
1147 std::string &destination) {
1148 return GetValueAsCString(TypeFormatImpl_Format(format), destination);
1149}
Enrico Granata0a3958e2011-07-02 00:25:22 +00001150
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151const char *ValueObject::GetValueAsCString() {
1152 if (UpdateValueIfNeeded(true)) {
1153 lldb::TypeFormatImplSP format_sp;
1154 lldb::Format my_format = GetFormat();
1155 if (my_format == lldb::eFormatDefault) {
1156 if (m_type_format_sp)
1157 format_sp = m_type_format_sp;
1158 else {
1159 if (m_is_bitfield_for_scalar)
1160 my_format = eFormatUnsigned;
1161 else {
1162 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) {
1163 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1164 if (reg_info)
1165 my_format = reg_info->format;
1166 } else {
1167 my_format = GetValue().GetCompilerType().GetFormat();
1168 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001169 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001171 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 if (my_format != m_last_format || m_value_str.empty()) {
1173 m_last_format = my_format;
1174 if (!format_sp)
1175 format_sp.reset(new TypeFormatImpl_Format(my_format));
1176 if (GetValueAsCString(*format_sp.get(), m_value_str)) {
1177 if (!m_value_did_change && m_old_value_valid) {
1178 // The value was gotten successfully, so we consider the
1179 // value as changed if the value string differs
1180 SetValueDidChange(m_old_value_str != m_value_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001184 }
1185 if (m_value_str.empty())
1186 return NULL;
1187 return m_value_str.c_str();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188}
1189
Enrico Granatac3e320a2011-08-02 17:27:39 +00001190// if > 8bytes, 0 is returned. this method should mostly be used
1191// to read address values out of pointers
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192uint64_t ValueObject::GetValueAsUnsigned(uint64_t fail_value, bool *success) {
1193 // If our byte size is zero this is an aggregate type that has children
1194 if (CanProvideValue()) {
1195 Scalar scalar;
1196 if (ResolveValue(scalar)) {
1197 if (success)
1198 *success = true;
1199 return scalar.ULongLong(fail_value);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001200 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001201 // fallthrough, otherwise...
1202 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001203
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 if (success)
1205 *success = false;
1206 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001207}
1208
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209int64_t ValueObject::GetValueAsSigned(int64_t fail_value, bool *success) {
1210 // If our byte size is zero this is an aggregate type that has children
1211 if (CanProvideValue()) {
1212 Scalar scalar;
1213 if (ResolveValue(scalar)) {
1214 if (success)
1215 *success = true;
1216 return scalar.SLongLong(fail_value);
Enrico Granatad7373f62013-10-31 18:57:50 +00001217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 // fallthrough, otherwise...
1219 }
1220
1221 if (success)
1222 *success = false;
1223 return fail_value;
Enrico Granatad7373f62013-10-31 18:57:50 +00001224}
1225
Kate Stoneb9c1b512016-09-06 20:57:50 +00001226// if any more "special cases" are added to
1227// ValueObject::DumpPrintableRepresentation() please keep
1228// this call up to date by returning true for your new special cases. We will
1229// eventually move
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001230// to checking this call result before trying to display special cases
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231bool ValueObject::HasSpecialPrintableRepresentation(
1232 ValueObjectRepresentationStyle val_obj_display, Format custom_format) {
1233 Flags flags(GetTypeInfo());
1234 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1235 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1236 if (IsCStringContainer(true) &&
1237 (custom_format == eFormatCString || custom_format == eFormatCharArray ||
1238 custom_format == eFormatChar || custom_format == eFormatVectorOfChar))
1239 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001240
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241 if (flags.Test(eTypeIsArray)) {
1242 if ((custom_format == eFormatBytes) ||
1243 (custom_format == eFormatBytesWithASCII))
1244 return true;
1245
1246 if ((custom_format == eFormatVectorOfChar) ||
1247 (custom_format == eFormatVectorOfFloat32) ||
1248 (custom_format == eFormatVectorOfFloat64) ||
1249 (custom_format == eFormatVectorOfSInt16) ||
1250 (custom_format == eFormatVectorOfSInt32) ||
1251 (custom_format == eFormatVectorOfSInt64) ||
1252 (custom_format == eFormatVectorOfSInt8) ||
1253 (custom_format == eFormatVectorOfUInt128) ||
1254 (custom_format == eFormatVectorOfUInt16) ||
1255 (custom_format == eFormatVectorOfUInt32) ||
1256 (custom_format == eFormatVectorOfUInt64) ||
1257 (custom_format == eFormatVectorOfUInt8))
1258 return true;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 }
1261 return false;
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001262}
1263
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264bool ValueObject::DumpPrintableRepresentation(
1265 Stream &s, ValueObjectRepresentationStyle val_obj_display,
1266 Format custom_format, PrintableRepresentationSpecialCases special,
1267 bool do_dump_error) {
Enrico Granataf4efecd2011-07-12 22:56:10 +00001268
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 Flags flags(GetTypeInfo());
Enrico Granata86cc9822012-03-19 22:58:49 +00001270
Enrico Granata65d86e42016-11-07 23:32:20 +00001271 bool allow_special =
1272 (special == ValueObject::PrintableRepresentationSpecialCases::eAllow);
1273 const bool only_special = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274
1275 if (allow_special) {
1276 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1277 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1278 // when being asked to get a printable display an array or pointer type
1279 // directly,
1280 // try to "do the right thing"
1281
1282 if (IsCStringContainer(true) &&
1283 (custom_format == eFormatCString ||
1284 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1285 custom_format ==
1286 eFormatVectorOfChar)) // print char[] & char* directly
1287 {
1288 Error error;
1289 lldb::DataBufferSP buffer_sp;
1290 std::pair<size_t, bool> read_string = ReadPointedString(
1291 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1292 (custom_format == eFormatCharArray));
1293 lldb_private::formatters::StringPrinter::
1294 ReadBufferAndDumpToStreamOptions options(*this);
1295 options.SetData(DataExtractor(
1296 buffer_sp, lldb::eByteOrderInvalid,
1297 8)); // none of this matters for a string - pass some defaults
1298 options.SetStream(&s);
1299 options.SetPrefixToken(0);
1300 options.SetQuote('"');
1301 options.SetSourceSize(buffer_sp->GetByteSize());
1302 options.SetIsTruncated(read_string.second);
1303 formatters::StringPrinter::ReadBufferAndDumpToStream<
1304 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1305 options);
1306 return !error.Fail();
1307 }
1308
1309 if (custom_format == eFormatEnum)
Enrico Granata85933ed2011-08-18 16:38:26 +00001310 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001311
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 // this only works for arrays, because I have no way to know when
1313 // the pointed memory ends, and no special \0 end of data marker
1314 if (flags.Test(eTypeIsArray)) {
1315 if ((custom_format == eFormatBytes) ||
1316 (custom_format == eFormatBytesWithASCII)) {
1317 const size_t count = GetNumChildren();
1318
1319 s << '[';
1320 for (size_t low = 0; low < count; low++) {
1321
1322 if (low)
1323 s << ',';
1324
1325 ValueObjectSP child = GetChildAtIndex(low, true);
1326 if (!child.get()) {
1327 s << "<invalid child>";
1328 continue;
Enrico Granata86cc9822012-03-19 22:58:49 +00001329 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 child->DumpPrintableRepresentation(
1331 s, ValueObject::eValueObjectRepresentationStyleValue,
1332 custom_format);
1333 }
1334
1335 s << ']';
1336
1337 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339
1340 if ((custom_format == eFormatVectorOfChar) ||
1341 (custom_format == eFormatVectorOfFloat32) ||
1342 (custom_format == eFormatVectorOfFloat64) ||
1343 (custom_format == eFormatVectorOfSInt16) ||
1344 (custom_format == eFormatVectorOfSInt32) ||
1345 (custom_format == eFormatVectorOfSInt64) ||
1346 (custom_format == eFormatVectorOfSInt8) ||
1347 (custom_format == eFormatVectorOfUInt128) ||
1348 (custom_format == eFormatVectorOfUInt16) ||
1349 (custom_format == eFormatVectorOfUInt32) ||
1350 (custom_format == eFormatVectorOfUInt64) ||
1351 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1352 // with ASCII or any vector
1353 // format should be printed
1354 // directly
Enrico Granata86cc9822012-03-19 22:58:49 +00001355 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 const size_t count = GetNumChildren();
1357
1358 Format format = FormatManager::GetSingleItemFormat(custom_format);
1359
1360 s << '[';
1361 for (size_t low = 0; low < count; low++) {
1362
1363 if (low)
1364 s << ',';
1365
1366 ValueObjectSP child = GetChildAtIndex(low, true);
1367 if (!child.get()) {
1368 s << "<invalid child>";
1369 continue;
Enrico Granata0dba9b32014-01-08 01:36:59 +00001370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 child->DumpPrintableRepresentation(
1372 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1373 }
1374
1375 s << ']';
1376
1377 return true;
Enrico Granata86cc9822012-03-19 22:58:49 +00001378 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 }
1380
1381 if ((custom_format == eFormatBoolean) ||
1382 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1383 (custom_format == eFormatCharPrintable) ||
1384 (custom_format == eFormatComplexFloat) ||
1385 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1386 (custom_format == eFormatHexUppercase) ||
1387 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1388 (custom_format == eFormatOSType) ||
1389 (custom_format == eFormatUnicode16) ||
1390 (custom_format == eFormatUnicode32) ||
1391 (custom_format == eFormatUnsigned) ||
1392 (custom_format == eFormatPointer) ||
1393 (custom_format == eFormatComplexInteger) ||
1394 (custom_format == eFormatComplex) ||
1395 (custom_format == eFormatDefault)) // use the [] operator
1396 return false;
Enrico Granata86cc9822012-03-19 22:58:49 +00001397 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 }
1399
1400 if (only_special)
1401 return false;
1402
1403 bool var_success = false;
1404
1405 {
Zachary Turnerc1564272016-11-16 21:15:24 +00001406 llvm::StringRef str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407
1408 // this is a local stream that we are using to ensure that the data pointed
Zachary Turnerc1564272016-11-16 21:15:24 +00001409 // to by cstr survives long enough for us to copy it to its destination - it
1410 // is necessary to have this temporary storage area for cases where our
1411 // desired output is not backed by some other longer-term storage
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 StreamString strm;
1413
1414 if (custom_format != eFormatInvalid)
1415 SetFormat(custom_format);
1416
1417 switch (val_obj_display) {
1418 case eValueObjectRepresentationStyleValue:
Zachary Turnerc1564272016-11-16 21:15:24 +00001419 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 break;
1421
1422 case eValueObjectRepresentationStyleSummary:
Zachary Turnerc1564272016-11-16 21:15:24 +00001423 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001424 break;
1425
1426 case eValueObjectRepresentationStyleLanguageSpecific:
Zachary Turnerc1564272016-11-16 21:15:24 +00001427 str = GetObjectDescription();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428 break;
1429
1430 case eValueObjectRepresentationStyleLocation:
Zachary Turnerc1564272016-11-16 21:15:24 +00001431 str = GetLocationAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 break;
1433
1434 case eValueObjectRepresentationStyleChildrenCount:
1435 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
Zachary Turnerc1564272016-11-16 21:15:24 +00001436 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 break;
1438
1439 case eValueObjectRepresentationStyleType:
Zachary Turnerc1564272016-11-16 21:15:24 +00001440 str = GetTypeName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 break;
1442
1443 case eValueObjectRepresentationStyleName:
Zachary Turnerc1564272016-11-16 21:15:24 +00001444 str = GetName().GetStringRef();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 break;
1446
1447 case eValueObjectRepresentationStyleExpressionPath:
1448 GetExpressionPath(strm, false);
Zachary Turnerc1564272016-11-16 21:15:24 +00001449 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001450 break;
1451 }
1452
Zachary Turnerc1564272016-11-16 21:15:24 +00001453 if (str.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454 if (val_obj_display == eValueObjectRepresentationStyleValue)
Zachary Turnerc1564272016-11-16 21:15:24 +00001455 str = GetSummaryAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001456 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1457 if (!CanProvideValue()) {
1458 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1459 GetLocationAsCString());
Zachary Turnerc1564272016-11-16 21:15:24 +00001460 str = strm.GetString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 } else
Zachary Turnerc1564272016-11-16 21:15:24 +00001462 str = GetValueAsCString();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 }
1464 }
1465
Zachary Turnerc1564272016-11-16 21:15:24 +00001466 if (!str.empty())
1467 s << str;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 else {
1469 if (m_error.Fail()) {
1470 if (do_dump_error)
1471 s.Printf("<%s>", m_error.AsCString());
1472 else
1473 return false;
1474 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1475 s.PutCString("<no summary available>");
1476 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1477 s.PutCString("<no value available>");
1478 else if (val_obj_display ==
1479 eValueObjectRepresentationStyleLanguageSpecific)
1480 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1481 // have other runtimes
1482 // that support a
1483 // description
1484 else
1485 s.PutCString("<no printable representation>");
1486 }
1487
1488 // we should only return false here if we could not do *anything*
1489 // even if we have an error message as output, that's a success
1490 // from our callers' perspective, so return true
1491 var_success = true;
1492
1493 if (custom_format != eFormatInvalid)
1494 SetFormat(eFormatDefault);
1495 }
1496
1497 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001498}
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1501 AddressType *address_type) {
1502 // Can't take address of a bitfield
1503 if (IsBitfield())
Greg Clayton73b472d2010-10-27 03:32:59 +00001504 return LLDB_INVALID_ADDRESS;
Greg Clayton73b472d2010-10-27 03:32:59 +00001505
Kate Stoneb9c1b512016-09-06 20:57:50 +00001506 if (!UpdateValueIfNeeded(false))
1507 return LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +00001508
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 switch (m_value.GetValueType()) {
1510 case Value::eValueTypeScalar:
1511 case Value::eValueTypeVector:
1512 if (scalar_is_load_address) {
1513 if (address_type)
1514 *address_type = eAddressTypeLoad;
1515 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 case Value::eValueTypeLoadAddress:
1520 case Value::eValueTypeFileAddress: {
Enrico Granata9128ee22011-09-06 19:20:51 +00001521 if (address_type)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 *address_type = m_value.GetValueAddressType();
1523 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1524 } break;
1525 case Value::eValueTypeHostAddress: {
1526 if (address_type)
1527 *address_type = m_value.GetValueAddressType();
1528 return LLDB_INVALID_ADDRESS;
1529 } break;
1530 }
1531 if (address_type)
1532 *address_type = eAddressTypeInvalid;
1533 return LLDB_INVALID_ADDRESS;
1534}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001535
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1537 addr_t address = LLDB_INVALID_ADDRESS;
1538 if (address_type)
1539 *address_type = eAddressTypeInvalid;
1540
1541 if (!UpdateValueIfNeeded(false))
Greg Clayton737b9322010-09-13 03:32:57 +00001542 return address;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543
1544 switch (m_value.GetValueType()) {
1545 case Value::eValueTypeScalar:
1546 case Value::eValueTypeVector:
1547 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1548 break;
1549
1550 case Value::eValueTypeHostAddress:
1551 case Value::eValueTypeLoadAddress:
1552 case Value::eValueTypeFileAddress: {
1553 lldb::offset_t data_offset = 0;
1554 address = m_data.GetPointer(&data_offset);
1555 } break;
1556 }
1557
1558 if (address_type)
1559 *address_type = GetAddressTypeOfChildren();
1560
1561 return address;
Greg Clayton737b9322010-09-13 03:32:57 +00001562}
1563
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564bool ValueObject::SetValueFromCString(const char *value_str, Error &error) {
1565 error.Clear();
1566 // Make sure our value is up to date first so that our location and location
1567 // type is valid.
1568 if (!UpdateValueIfNeeded(false)) {
1569 error.SetErrorString("unable to read value");
Greg Clayton81e871e2012-02-04 02:27:34 +00001570 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 }
1572
1573 uint64_t count = 0;
1574 const Encoding encoding = GetCompilerType().GetEncoding(count);
1575
1576 const size_t byte_size = GetByteSize();
1577
1578 Value::ValueType value_type = m_value.GetValueType();
1579
1580 if (value_type == Value::eValueTypeScalar) {
1581 // If the value is already a scalar, then let the scalar change itself:
1582 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1583 } else if (byte_size <= 16) {
1584 // If the value fits in a scalar, then make a new scalar and again let the
1585 // scalar code do the conversion, then figure out where to put the new
1586 // value.
1587 Scalar new_scalar;
1588 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1589 if (error.Success()) {
1590 switch (value_type) {
1591 case Value::eValueTypeLoadAddress: {
1592 // If it is a load address, then the scalar value is the storage
1593 // location
1594 // of the data, and we have to shove this value down to that load
1595 // location.
1596 ExecutionContext exe_ctx(GetExecutionContextRef());
1597 Process *process = exe_ctx.GetProcessPtr();
1598 if (process) {
1599 addr_t target_addr =
1600 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1601 size_t bytes_written = process->WriteScalarToMemory(
1602 target_addr, new_scalar, byte_size, error);
1603 if (!error.Success())
1604 return false;
1605 if (bytes_written != byte_size) {
1606 error.SetErrorString("unable to write value to memory");
1607 return false;
1608 }
1609 }
1610 } break;
1611 case Value::eValueTypeHostAddress: {
1612 // If it is a host address, then we stuff the scalar as a DataBuffer
1613 // into the Value's data.
1614 DataExtractor new_data;
1615 new_data.SetByteOrder(m_data.GetByteOrder());
1616
1617 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1618 m_data.SetData(buffer_sp, 0);
1619 bool success = new_scalar.GetData(new_data);
1620 if (success) {
1621 new_data.CopyByteOrderedData(
1622 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1623 byte_size, m_data.GetByteOrder());
1624 }
1625 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1626
1627 } break;
1628 case Value::eValueTypeFileAddress:
1629 case Value::eValueTypeScalar:
1630 case Value::eValueTypeVector:
1631 break;
1632 }
1633 } else {
1634 return false;
1635 }
1636 } else {
1637 // We don't support setting things bigger than a scalar at present.
1638 error.SetErrorString("unable to write aggregate data type");
1639 return false;
1640 }
1641
1642 // If we have reached this point, then we have successfully changed the value.
1643 SetNeedsUpdate();
1644 return true;
Greg Clayton81e871e2012-02-04 02:27:34 +00001645}
1646
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647bool ValueObject::GetDeclaration(Declaration &decl) {
1648 decl.Clear();
1649 return false;
Greg Clayton84db9102012-03-26 23:03:23 +00001650}
1651
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652ConstString ValueObject::GetTypeName() {
1653 return GetCompilerType().GetConstTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001654}
1655
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1657
1658ConstString ValueObject::GetQualifiedTypeName() {
1659 return GetCompilerType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001660}
1661
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662LanguageType ValueObject::GetObjectRuntimeLanguage() {
1663 return GetCompilerType().GetMinimumLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001664}
1665
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666void ValueObject::AddSyntheticChild(const ConstString &key,
1667 ValueObject *valobj) {
1668 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001669}
1670
Kate Stoneb9c1b512016-09-06 20:57:50 +00001671ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1672 ValueObjectSP synthetic_child_sp;
1673 std::map<ConstString, ValueObject *>::const_iterator pos =
1674 m_synthetic_children.find(key);
1675 if (pos != m_synthetic_children.end())
1676 synthetic_child_sp = pos->second->GetSP();
1677 return synthetic_child_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678}
1679
Greg Clayton2452ab72013-02-08 22:02:02 +00001680uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001681ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1682 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00001683}
1684
Kate Stoneb9c1b512016-09-06 20:57:50 +00001685bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1686
1687bool ValueObject::IsArrayType() {
1688 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689}
1690
Kate Stoneb9c1b512016-09-06 20:57:50 +00001691bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1692
1693bool ValueObject::IsIntegerType(bool &is_signed) {
1694 return GetCompilerType().IsIntegerType(is_signed);
Greg Claytondaf515f2011-07-09 20:12:33 +00001695}
1696
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697bool ValueObject::IsPointerOrReferenceType() {
1698 return GetCompilerType().IsPointerOrReferenceType();
Enrico Granata9fc19442011-07-06 02:13:41 +00001699}
1700
Kate Stoneb9c1b512016-09-06 20:57:50 +00001701bool ValueObject::IsPossibleDynamicType() {
1702 ExecutionContext exe_ctx(GetExecutionContextRef());
1703 Process *process = exe_ctx.GetProcessPtr();
1704 if (process)
1705 return process->IsPossibleDynamicValue(*this);
1706 else
1707 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001708}
Greg Clayton73b472d2010-10-27 03:32:59 +00001709
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710bool ValueObject::IsRuntimeSupportValue() {
1711 Process *process(GetProcessSP().get());
1712 if (process) {
1713 LanguageRuntime *runtime =
1714 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1715 if (!runtime)
1716 runtime = process->GetObjCLanguageRuntime();
1717 if (runtime)
1718 return runtime->IsRuntimeSupportValue(*this);
1719 }
1720 return false;
Greg Clayton007d5be2011-05-30 00:49:24 +00001721}
1722
Kate Stoneb9c1b512016-09-06 20:57:50 +00001723bool ValueObject::IsNilReference() {
1724 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1725 return language->IsNilReference(*this);
1726 }
1727 return false;
Greg Claytondea8cb42011-06-29 22:09:02 +00001728}
1729
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730bool ValueObject::IsUninitializedReference() {
1731 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1732 return language->IsUninitializedReference(*this);
1733 }
1734 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00001735}
1736
Greg Claytondaf515f2011-07-09 20:12:33 +00001737// This allows you to create an array member using and index
1738// that doesn't not fall in the normal bounds of the array.
1739// Many times structure can be defined as:
1740// struct Collection
1741// {
1742// uint32_t item_count;
1743// Item item_array[0];
1744// };
1745// The size of the "item_array" is 1, but many times in practice
1746// there are more items in "item_array".
1747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1749 bool can_create) {
1750 ValueObjectSP synthetic_child_sp;
1751 if (IsPointerType() || IsArrayType()) {
1752 char index_str[64];
1753 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1754 ConstString index_const_str(index_str);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001755 // Check if we have already created a synthetic array member in this
1756 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001757 synthetic_child_sp = GetSyntheticChild(index_const_str);
1758 if (!synthetic_child_sp) {
1759 ValueObject *synthetic_child;
1760 // We haven't made a synthetic array member for INDEX yet, so
1761 // lets make one and cache it for any future reference.
1762 synthetic_child = CreateChildAtIndex(0, true, index);
1763
1764 // Cache the value if we got one back...
1765 if (synthetic_child) {
1766 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001767 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768 synthetic_child_sp->SetName(ConstString(index_str));
1769 synthetic_child_sp->m_is_array_item_for_pointer = true;
1770 }
Enrico Granata6f3533f2011-07-29 19:53:35 +00001771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 }
1773 return synthetic_child_sp;
Enrico Granata6f3533f2011-07-29 19:53:35 +00001774}
1775
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1777 bool can_create) {
1778 ValueObjectSP synthetic_child_sp;
1779 if (IsScalarType()) {
1780 char index_str[64];
1781 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1782 ConstString index_const_str(index_str);
Enrico Granata32556cd2014-08-26 20:54:04 +00001783 // Check if we have already created a synthetic array member in this
1784 // valid object. If we have we will re-use it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001785 synthetic_child_sp = GetSyntheticChild(index_const_str);
1786 if (!synthetic_child_sp) {
1787 uint32_t bit_field_size = to - from + 1;
1788 uint32_t bit_field_offset = from;
1789 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1790 bit_field_offset =
1791 GetByteSize() * 8 - bit_field_size - bit_field_offset;
1792 // We haven't made a synthetic array member for INDEX yet, so
1793 // lets make one and cache it for any future reference.
1794 ValueObjectChild *synthetic_child = new ValueObjectChild(
1795 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1796 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1797 0);
1798
1799 // Cache the value if we got one back...
1800 if (synthetic_child) {
1801 AddSyntheticChild(index_const_str, synthetic_child);
Enrico Granata32556cd2014-08-26 20:54:04 +00001802 synthetic_child_sp = synthetic_child->GetSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803 synthetic_child_sp->SetName(ConstString(index_str));
1804 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1805 }
Enrico Granata32556cd2014-08-26 20:54:04 +00001806 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 }
1808 return synthetic_child_sp;
Enrico Granata32556cd2014-08-26 20:54:04 +00001809}
1810
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1812 uint32_t offset, const CompilerType &type, bool can_create,
1813 ConstString name_const_str) {
1814
1815 ValueObjectSP synthetic_child_sp;
1816
1817 if (name_const_str.IsEmpty()) {
1818 char name_str[64];
1819 snprintf(name_str, sizeof(name_str), "@%i", offset);
1820 name_const_str.SetCString(name_str);
1821 }
1822
1823 // Check if we have already created a synthetic array member in this
1824 // valid object. If we have we will re-use it.
1825 synthetic_child_sp = GetSyntheticChild(name_const_str);
1826
1827 if (synthetic_child_sp.get())
1828 return synthetic_child_sp;
1829
1830 if (!can_create)
1831 return ValueObjectSP();
1832
1833 ExecutionContext exe_ctx(GetExecutionContextRef());
1834
1835 ValueObjectChild *synthetic_child = new ValueObjectChild(
1836 *this, type, name_const_str,
1837 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1838 false, false, eAddressTypeInvalid, 0);
1839 if (synthetic_child) {
1840 AddSyntheticChild(name_const_str, synthetic_child);
1841 synthetic_child_sp = synthetic_child->GetSP();
1842 synthetic_child_sp->SetName(name_const_str);
1843 synthetic_child_sp->m_is_child_at_offset = true;
1844 }
1845 return synthetic_child_sp;
1846}
1847
1848ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1849 const CompilerType &type,
1850 bool can_create,
1851 ConstString name_const_str) {
1852 ValueObjectSP synthetic_child_sp;
1853
1854 if (name_const_str.IsEmpty()) {
1855 char name_str[128];
1856 snprintf(name_str, sizeof(name_str), "base%s@%i",
1857 type.GetTypeName().AsCString("<unknown>"), offset);
1858 name_const_str.SetCString(name_str);
1859 }
1860
1861 // Check if we have already created a synthetic array member in this
1862 // valid object. If we have we will re-use it.
1863 synthetic_child_sp = GetSyntheticChild(name_const_str);
1864
1865 if (synthetic_child_sp.get())
1866 return synthetic_child_sp;
1867
1868 if (!can_create)
1869 return ValueObjectSP();
1870
1871 const bool is_base_class = true;
1872
1873 ExecutionContext exe_ctx(GetExecutionContextRef());
1874
1875 ValueObjectChild *synthetic_child = new ValueObjectChild(
1876 *this, type, name_const_str,
1877 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1878 is_base_class, false, eAddressTypeInvalid, 0);
1879 if (synthetic_child) {
1880 AddSyntheticChild(name_const_str, synthetic_child);
1881 synthetic_child_sp = synthetic_child->GetSP();
1882 synthetic_child_sp->SetName(name_const_str);
1883 }
1884 return synthetic_child_sp;
1885}
Enrico Granata32556cd2014-08-26 20:54:04 +00001886
Enrico Granatad55546b2011-07-22 00:16:08 +00001887// your expression path needs to have a leading . or ->
1888// (unless it somehow "looks like" an array, in which case it has
1889// a leading [ symbol). while the [ is meaningful and should be shown
1890// to the user, . and -> are just parser design, but by no means
1891// added information for the user.. strip them off
Kate Stoneb9c1b512016-09-06 20:57:50 +00001892static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1893 if (!expression || !expression[0])
Enrico Granatad55546b2011-07-22 00:16:08 +00001894 return expression;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001895 if (expression[0] == '.')
1896 return expression + 1;
1897 if (expression[0] == '-' && expression[1] == '>')
1898 return expression + 2;
1899 return expression;
Enrico Granatad55546b2011-07-22 00:16:08 +00001900}
1901
Greg Claytonafacd142011-09-02 01:15:17 +00001902ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1904 bool can_create) {
1905 ValueObjectSP synthetic_child_sp;
1906 ConstString name_const_string(expression);
1907 // Check if we have already created a synthetic array member in this
1908 // valid object. If we have we will re-use it.
1909 synthetic_child_sp = GetSyntheticChild(name_const_string);
1910 if (!synthetic_child_sp) {
1911 // We haven't made a synthetic array member for expression yet, so
1912 // lets make one and cache it for any future reference.
1913 synthetic_child_sp = GetValueForExpressionPath(
Zachary Turnerd2daca72016-11-18 17:55:04 +00001914 expression, NULL, NULL,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1916 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1917 None));
1918
1919 // Cache the value if we got one back...
1920 if (synthetic_child_sp.get()) {
1921 // FIXME: this causes a "real" child to end up with its name changed to
1922 // the contents of expression
1923 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1924 synthetic_child_sp->SetName(
1925 ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001926 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001927 }
1928 return synthetic_child_sp;
Enrico Granatad55546b2011-07-22 00:16:08 +00001929}
1930
Kate Stoneb9c1b512016-09-06 20:57:50 +00001931void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
1932 if (use_synthetic == false)
1933 return;
1934
1935 TargetSP target_sp(GetTargetSP());
1936 if (target_sp && target_sp->GetEnableSyntheticValue() == false) {
1937 m_synthetic_value = NULL;
1938 return;
1939 }
1940
1941 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1942
1943 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1944 return;
1945
1946 if (m_synthetic_children_sp.get() == NULL)
1947 return;
1948
1949 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1950 return;
1951
1952 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1953}
1954
1955void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1956 if (use_dynamic == eNoDynamicValues)
1957 return;
1958
1959 if (!m_dynamic_value && !IsDynamic()) {
1960 ExecutionContext exe_ctx(GetExecutionContextRef());
1961 Process *process = exe_ctx.GetProcessPtr();
1962 if (process && process->IsPossibleDynamicValue(*this)) {
1963 ClearDynamicTypeInformation();
1964 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
Enrico Granatac5bc4122012-03-27 02:35:13 +00001965 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 }
Enrico Granatad55546b2011-07-22 00:16:08 +00001967}
1968
Kate Stoneb9c1b512016-09-06 20:57:50 +00001969ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1970 if (use_dynamic == eNoDynamicValues)
1971 return ValueObjectSP();
1972
1973 if (!IsDynamic() && m_dynamic_value == NULL) {
1974 CalculateDynamicValue(use_dynamic);
1975 }
1976 if (m_dynamic_value)
1977 return m_dynamic_value->GetSP();
1978 else
1979 return ValueObjectSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001980}
1981
Kate Stoneb9c1b512016-09-06 20:57:50 +00001982ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1983
1984lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1985
1986ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
1987 if (use_synthetic == false)
1988 return ValueObjectSP();
1989
1990 CalculateSyntheticValue(use_synthetic);
1991
1992 if (m_synthetic_value)
1993 return m_synthetic_value->GetSP();
1994 else
1995 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001996}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001997
Kate Stoneb9c1b512016-09-06 20:57:50 +00001998bool ValueObject::HasSyntheticValue() {
1999 UpdateFormatsIfNeeded();
Jim Ingham60dbabb2011-12-08 19:44:08 +00002000
Kate Stoneb9c1b512016-09-06 20:57:50 +00002001 if (m_synthetic_children_sp.get() == NULL)
2002 return false;
Enrico Granata886147f2012-05-08 18:47:08 +00002003
Kate Stoneb9c1b512016-09-06 20:57:50 +00002004 CalculateSyntheticValue(true);
Enrico Granata86cc9822012-03-19 22:58:49 +00002005
Kate Stoneb9c1b512016-09-06 20:57:50 +00002006 if (m_synthetic_value)
2007 return true;
2008 else
Greg Claytone221f822011-01-21 01:59:00 +00002009 return false;
2010}
2011
Kate Stoneb9c1b512016-09-06 20:57:50 +00002012bool ValueObject::GetBaseClassPath(Stream &s) {
2013 if (IsBaseClass()) {
2014 bool parent_had_base_class =
2015 GetParent() && GetParent()->GetBaseClassPath(s);
2016 CompilerType compiler_type = GetCompilerType();
2017 std::string cxx_class_name;
2018 bool this_had_base_class =
2019 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2020 if (this_had_base_class) {
2021 if (parent_had_base_class)
2022 s.PutCString("::");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00002023 s.PutCString(cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002024 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002025 return parent_had_base_class || this_had_base_class;
2026 }
2027 return false;
Greg Claytone221f822011-01-21 01:59:00 +00002028}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002029
Kate Stoneb9c1b512016-09-06 20:57:50 +00002030ValueObject *ValueObject::GetNonBaseClassParent() {
2031 if (GetParent()) {
2032 if (GetParent()->IsBaseClass())
2033 return GetParent()->GetNonBaseClassParent();
2034 else
2035 return GetParent();
2036 }
2037 return NULL;
2038}
Enrico Granataa3c8f042014-08-19 22:29:08 +00002039
Kate Stoneb9c1b512016-09-06 20:57:50 +00002040bool ValueObject::IsBaseClass(uint32_t &depth) {
2041 if (!IsBaseClass()) {
2042 depth = 0;
2043 return false;
2044 }
2045 if (GetParent()) {
2046 GetParent()->IsBaseClass(depth);
2047 depth = depth + 1;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002048 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002049 }
2050 // TODO: a base of no parent? weird..
2051 depth = 1;
2052 return true;
Enrico Granataa3c8f042014-08-19 22:29:08 +00002053}
2054
Kate Stoneb9c1b512016-09-06 20:57:50 +00002055void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2056 GetExpressionPathFormat epformat) {
2057 // synthetic children do not actually "exist" as part of the hierarchy, and
2058 // sometimes they are consed up in ways
2059 // that don't make sense from an underlying language/API standpoint. So, use a
2060 // special code path here to return
2061 // something that can hopefully be used in expression
2062 if (m_is_synthetic_children_generated) {
2063 UpdateValueIfNeeded();
2064
2065 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2066 if (IsPointerOrReferenceType()) {
2067 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2068 GetValueAsUnsigned(0));
Enrico Granata986fa5f2014-12-09 21:41:16 +00002069 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002070 } else {
2071 uint64_t load_addr =
2072 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2073 if (load_addr != LLDB_INVALID_ADDRESS) {
2074 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2075 load_addr);
2076 return;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002078 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002080
2081 if (CanProvideValue()) {
2082 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2083 GetValueAsCString());
2084 return;
Enrico Granata4becb372011-06-29 22:27:15 +00002085 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002086
2087 return;
2088 }
2089
2090 const bool is_deref_of_parent = IsDereferenceOfParent();
2091
2092 if (is_deref_of_parent &&
2093 epformat == eGetExpressionPathFormatDereferencePointers) {
2094 // this is the original format of GetExpressionPath() producing code like
2095 // *(a_ptr).memberName, which is entirely
2096 // fine, until you put this into
2097 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
2098 // a_ptr->memberName.
2099 // the eHonorPointers mode is meant to produce strings in this latter format
2100 s.PutCString("*(");
2101 }
2102
2103 ValueObject *parent = GetParent();
2104
2105 if (parent)
2106 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2107
2108 // if we are a deref_of_parent just because we are synthetic array
2109 // members made up to allow ptr[%d] syntax to work in variable
2110 // printing, then add our name ([%d]) to the expression path
2111 if (m_is_array_item_for_pointer &&
2112 epformat == eGetExpressionPathFormatHonorPointers)
2113 s.PutCString(m_name.AsCString());
2114
2115 if (!IsBaseClass()) {
2116 if (!is_deref_of_parent) {
2117 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2118 if (non_base_class_parent &&
2119 !non_base_class_parent->GetName().IsEmpty()) {
2120 CompilerType non_base_class_parent_compiler_type =
2121 non_base_class_parent->GetCompilerType();
2122 if (non_base_class_parent_compiler_type) {
2123 if (parent && parent->IsDereferenceOfParent() &&
2124 epformat == eGetExpressionPathFormatHonorPointers) {
2125 s.PutCString("->");
2126 } else {
2127 const uint32_t non_base_class_parent_type_info =
2128 non_base_class_parent_compiler_type.GetTypeInfo();
2129
2130 if (non_base_class_parent_type_info & eTypeIsPointer) {
2131 s.PutCString("->");
2132 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2133 !(non_base_class_parent_type_info & eTypeIsArray)) {
2134 s.PutChar('.');
2135 }
2136 }
2137 }
2138 }
2139
2140 const char *name = GetName().GetCString();
2141 if (name) {
2142 if (qualify_cxx_base_classes) {
2143 if (GetBaseClassPath(s))
2144 s.PutCString("::");
2145 }
2146 s.PutCString(name);
2147 }
2148 }
2149 }
2150
2151 if (is_deref_of_parent &&
2152 epformat == eGetExpressionPathFormatDereferencePointers) {
2153 s.PutChar(')');
2154 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002155}
2156
Kate Stoneb9c1b512016-09-06 20:57:50 +00002157ValueObjectSP ValueObject::GetValueForExpressionPath(
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002158 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002159 ExpressionPathEndResultType *final_value_type,
2160 const GetValueForExpressionPathOptions &options,
2161 ExpressionPathAftermath *final_task_on_target) {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002162
Kate Stoneb9c1b512016-09-06 20:57:50 +00002163 ExpressionPathScanEndReason dummy_reason_to_stop =
2164 ValueObject::eExpressionPathScanEndReasonUnknown;
2165 ExpressionPathEndResultType dummy_final_value_type =
2166 ValueObject::eExpressionPathEndResultTypeInvalid;
2167 ExpressionPathAftermath dummy_final_task_on_target =
2168 ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002169
Kate Stoneb9c1b512016-09-06 20:57:50 +00002170 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
Zachary Turnerd2daca72016-11-18 17:55:04 +00002171 expression, reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002172 final_value_type ? final_value_type : &dummy_final_value_type, options,
2173 final_task_on_target ? final_task_on_target
2174 : &dummy_final_task_on_target);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002175
Kate Stoneb9c1b512016-09-06 20:57:50 +00002176 if (!final_task_on_target ||
2177 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2178 return ret_val;
2179
2180 if (ret_val.get() &&
2181 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2182 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2183 // of plain objects
2184 {
2185 if ((final_task_on_target ? *final_task_on_target
2186 : dummy_final_task_on_target) ==
2187 ValueObject::eExpressionPathAftermathDereference) {
2188 Error error;
2189 ValueObjectSP final_value = ret_val->Dereference(error);
2190 if (error.Fail() || !final_value.get()) {
2191 if (reason_to_stop)
2192 *reason_to_stop =
2193 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2194 if (final_value_type)
2195 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002196 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002197 } else {
2198 if (final_task_on_target)
2199 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2200 return final_value;
2201 }
2202 }
2203 if (*final_task_on_target ==
2204 ValueObject::eExpressionPathAftermathTakeAddress) {
2205 Error error;
2206 ValueObjectSP final_value = ret_val->AddressOf(error);
2207 if (error.Fail() || !final_value.get()) {
2208 if (reason_to_stop)
2209 *reason_to_stop =
2210 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2211 if (final_value_type)
2212 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2213 return ValueObjectSP();
2214 } else {
2215 if (final_task_on_target)
2216 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2217 return final_value;
2218 }
2219 }
2220 }
2221 return ret_val; // final_task_on_target will still have its original value, so
2222 // you know I did not do it
2223}
2224
Kate Stoneb9c1b512016-09-06 20:57:50 +00002225ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002226 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002227 ExpressionPathEndResultType *final_result,
2228 const GetValueForExpressionPathOptions &options,
2229 ExpressionPathAftermath *what_next) {
2230 ValueObjectSP root = GetSP();
2231
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002232 if (!root)
2233 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002234
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002235 llvm::StringRef remainder = expression;
Zachary Turner655c4522016-11-18 06:34:45 +00002236
Kate Stoneb9c1b512016-09-06 20:57:50 +00002237 while (true) {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002238 llvm::StringRef temp_expression = remainder;
Zachary Turner655c4522016-11-18 06:34:45 +00002239
Kate Stoneb9c1b512016-09-06 20:57:50 +00002240 CompilerType root_compiler_type = root->GetCompilerType();
2241 CompilerType pointee_compiler_type;
2242 Flags pointee_compiler_type_info;
2243
2244 Flags root_compiler_type_info(
2245 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2246 if (pointee_compiler_type)
2247 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2248
Zachary Turnerd2daca72016-11-18 17:55:04 +00002249 if (temp_expression.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002250 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2251 return root;
2252 }
2253
Zachary Turnerd2daca72016-11-18 17:55:04 +00002254 switch (temp_expression.front()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002255 case '-': {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002256 temp_expression = temp_expression.drop_front();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002257 if (options.m_check_dot_vs_arrow_syntax &&
2258 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2259 // use -> on a
2260 // non-pointer and I
2261 // must catch the error
2262 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002263 *reason_to_stop =
2264 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2265 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2266 return ValueObjectSP();
2267 }
2268 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2269 // extract an ObjC IVar
2270 // when this is forbidden
2271 root_compiler_type_info.Test(eTypeIsPointer) &&
2272 options.m_no_fragile_ivar) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002273 *reason_to_stop =
2274 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2275 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2276 return ValueObjectSP();
2277 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002278 if (!temp_expression.startswith(">")) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002279 *reason_to_stop =
2280 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2281 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2282 return ValueObjectSP();
2283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002284 }
2285 LLVM_FALLTHROUGH;
2286 case '.': // or fallthrough from ->
2287 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002288 if (options.m_check_dot_vs_arrow_syntax &&
2289 temp_expression.front() == '.' &&
Kate Stoneb9c1b512016-09-06 20:57:50 +00002290 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2291 // use . on a pointer
2292 // and I must catch the
2293 // error
2294 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295 *reason_to_stop =
2296 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2297 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002298 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002299 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002300 temp_expression = temp_expression.drop_front(); // skip . or >
2301
2302 size_t next_sep_pos = temp_expression.find_first_of("-.[", 1);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002303 ConstString child_name;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002304 if (next_sep_pos == llvm::StringRef::npos) // if no other separator just
2305 // expand this last layer
Kate Stoneb9c1b512016-09-06 20:57:50 +00002306 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002307 child_name.SetString(temp_expression);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002308 ValueObjectSP child_valobj_sp =
2309 root->GetChildMemberWithName(child_name, true);
2310
2311 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002312 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002313 *reason_to_stop =
2314 ValueObject::eExpressionPathScanEndReasonEndOfString;
2315 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2316 return child_valobj_sp;
2317 } else {
2318 switch (options.m_synthetic_children_traversal) {
2319 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2320 None:
2321 break;
2322 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2323 FromSynthetic:
2324 if (root->IsSynthetic()) {
2325 child_valobj_sp = root->GetNonSyntheticValue();
2326 if (child_valobj_sp.get())
2327 child_valobj_sp =
2328 child_valobj_sp->GetChildMemberWithName(child_name, true);
2329 }
2330 break;
2331 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2332 ToSynthetic:
2333 if (!root->IsSynthetic()) {
2334 child_valobj_sp = root->GetSyntheticValue();
2335 if (child_valobj_sp.get())
2336 child_valobj_sp =
2337 child_valobj_sp->GetChildMemberWithName(child_name, true);
2338 }
2339 break;
2340 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2341 Both:
2342 if (root->IsSynthetic()) {
2343 child_valobj_sp = root->GetNonSyntheticValue();
2344 if (child_valobj_sp.get())
2345 child_valobj_sp =
2346 child_valobj_sp->GetChildMemberWithName(child_name, true);
2347 } else {
2348 child_valobj_sp = root->GetSyntheticValue();
2349 if (child_valobj_sp.get())
2350 child_valobj_sp =
2351 child_valobj_sp->GetChildMemberWithName(child_name, true);
2352 }
2353 break;
2354 }
2355 }
2356
2357 // if we are here and options.m_no_synthetic_children is true,
2358 // child_valobj_sp is going to be a NULL SP,
2359 // so we hit the "else" branch, and return an error
2360 if (child_valobj_sp.get()) // if it worked, just return
2361 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362 *reason_to_stop =
2363 ValueObject::eExpressionPathScanEndReasonEndOfString;
2364 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2365 return child_valobj_sp;
2366 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002367 *reason_to_stop =
2368 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2369 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002370 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002371 }
2372 } else // other layers do expand
2373 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002374 llvm::StringRef next_separator = temp_expression.substr(next_sep_pos);
2375
2376 child_name.SetString(temp_expression.slice(0, next_sep_pos));
2377
Kate Stoneb9c1b512016-09-06 20:57:50 +00002378 ValueObjectSP child_valobj_sp =
2379 root->GetChildMemberWithName(child_name, true);
2380 if (child_valobj_sp.get()) // store the new root and move on
2381 {
2382 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002383 remainder = next_separator;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002384 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2385 continue;
2386 } else {
2387 switch (options.m_synthetic_children_traversal) {
2388 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2389 None:
2390 break;
2391 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2392 FromSynthetic:
2393 if (root->IsSynthetic()) {
2394 child_valobj_sp = root->GetNonSyntheticValue();
2395 if (child_valobj_sp.get())
2396 child_valobj_sp =
2397 child_valobj_sp->GetChildMemberWithName(child_name, true);
2398 }
2399 break;
2400 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2401 ToSynthetic:
2402 if (!root->IsSynthetic()) {
2403 child_valobj_sp = root->GetSyntheticValue();
2404 if (child_valobj_sp.get())
2405 child_valobj_sp =
2406 child_valobj_sp->GetChildMemberWithName(child_name, true);
2407 }
2408 break;
2409 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2410 Both:
2411 if (root->IsSynthetic()) {
2412 child_valobj_sp = root->GetNonSyntheticValue();
2413 if (child_valobj_sp.get())
2414 child_valobj_sp =
2415 child_valobj_sp->GetChildMemberWithName(child_name, true);
2416 } else {
2417 child_valobj_sp = root->GetSyntheticValue();
2418 if (child_valobj_sp.get())
2419 child_valobj_sp =
2420 child_valobj_sp->GetChildMemberWithName(child_name, true);
2421 }
2422 break;
2423 }
2424 }
2425
2426 // if we are here and options.m_no_synthetic_children is true,
2427 // child_valobj_sp is going to be a NULL SP,
2428 // so we hit the "else" branch, and return an error
2429 if (child_valobj_sp.get()) // if it worked, move on
2430 {
2431 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002432 remainder = next_separator;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002433 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2434 continue;
2435 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002436 *reason_to_stop =
2437 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2438 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002439 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002440 }
2441 }
2442 break;
2443 }
2444 case '[': {
2445 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2446 !root_compiler_type_info.Test(eTypeIsPointer) &&
2447 !root_compiler_type_info.Test(
2448 eTypeIsVector)) // if this is not a T[] nor a T*
2449 {
2450 if (!root_compiler_type_info.Test(
2451 eTypeIsScalar)) // if this is not even a scalar...
2452 {
2453 if (options.m_synthetic_children_traversal ==
2454 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2455 None) // ...only chance left is synthetic
2456 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002457 *reason_to_stop =
2458 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2459 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2460 return ValueObjectSP();
2461 }
2462 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2463 // check that we can
2464 // expand bitfields
2465 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002466 *reason_to_stop =
2467 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2468 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2469 return ValueObjectSP();
2470 }
2471 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002472 if (temp_expression[1] ==
Kate Stoneb9c1b512016-09-06 20:57:50 +00002473 ']') // if this is an unbounded range it only works for arrays
2474 {
2475 if (!root_compiler_type_info.Test(eTypeIsArray)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002476 *reason_to_stop =
2477 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2478 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002479 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002480 } else // even if something follows, we cannot expand unbounded ranges,
2481 // just let the caller do it
2482 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002483 *reason_to_stop =
2484 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2485 *final_result =
2486 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2487 return root;
2488 }
2489 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002490
2491 size_t close_bracket_position = temp_expression.find(']', 1);
2492 if (close_bracket_position ==
2493 llvm::StringRef::npos) // if there is no ], this is a syntax error
Kate Stoneb9c1b512016-09-06 20:57:50 +00002494 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002495 *reason_to_stop =
2496 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2497 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002498 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002499 }
Zachary Turnerc2d55582016-11-18 03:51:19 +00002500
Zachary Turnerd2daca72016-11-18 17:55:04 +00002501 llvm::StringRef bracket_expr =
2502 temp_expression.slice(1, close_bracket_position);
2503
2504 // If this was an empty expression it would have been caught by the if
2505 // above.
2506 assert(!bracket_expr.empty());
2507
2508 if (!bracket_expr.contains('-')) {
Zachary Turnerc2d55582016-11-18 03:51:19 +00002509 // if no separator, this is of the form [N]. Note that this cannot
2510 // be an unbounded range of the form [], because that case was handled
2511 // above with an unconditional return.
Zachary Turnerd2daca72016-11-18 17:55:04 +00002512 unsigned long index = 0;
2513 if (bracket_expr.getAsInteger(0, index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002514 *reason_to_stop =
2515 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2516 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002517 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002518 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002519
Kate Stoneb9c1b512016-09-06 20:57:50 +00002520 // from here on we do have a valid index
2521 if (root_compiler_type_info.Test(eTypeIsArray)) {
2522 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2523 if (!child_valobj_sp)
2524 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2525 if (!child_valobj_sp)
2526 if (root->HasSyntheticValue() &&
2527 root->GetSyntheticValue()->GetNumChildren() > index)
2528 child_valobj_sp =
2529 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2530 if (child_valobj_sp) {
2531 root = child_valobj_sp;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002532 remainder =
2533 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002534 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2535 continue;
2536 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002537 *reason_to_stop =
2538 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2539 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002540 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002541 }
2542 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2543 if (*what_next ==
2544 ValueObject::
2545 eExpressionPathAftermathDereference && // if this is a
2546 // ptr-to-scalar, I
2547 // am accessing it
2548 // by index and I
2549 // would have
2550 // deref'ed anyway,
2551 // then do it now
2552 // and use this as
2553 // a bitfield
2554 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2555 Error error;
2556 root = root->Dereference(error);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002557 if (error.Fail() || !root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002558 *reason_to_stop =
2559 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2560 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002561 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002562 } else {
2563 *what_next = eExpressionPathAftermathNothing;
2564 continue;
2565 }
2566 } else {
2567 if (root->GetCompilerType().GetMinimumLanguage() ==
2568 eLanguageTypeObjC &&
2569 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2570 root->HasSyntheticValue() &&
2571 (options.m_synthetic_children_traversal ==
2572 GetValueForExpressionPathOptions::
2573 SyntheticChildrenTraversal::ToSynthetic ||
2574 options.m_synthetic_children_traversal ==
2575 GetValueForExpressionPathOptions::
2576 SyntheticChildrenTraversal::Both)) {
2577 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2578 } else
2579 root = root->GetSyntheticArrayMember(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002580 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002581 *reason_to_stop =
2582 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2583 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002584 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002585 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002586 remainder =
2587 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002588 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2589 continue;
2590 }
2591 }
2592 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2593 root = root->GetSyntheticBitFieldChild(index, index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002594 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002595 *reason_to_stop =
2596 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2597 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002598 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002599 } else // we do not know how to expand members of bitfields, so we
2600 // just return and let the caller do any further processing
2601 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002602 *reason_to_stop = ValueObject::
2603 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2604 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2605 return root;
2606 }
2607 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2608 root = root->GetChildAtIndex(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002609 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002610 *reason_to_stop =
2611 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2612 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2613 return ValueObjectSP();
2614 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002615 remainder =
2616 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002617 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2618 continue;
2619 }
2620 } else if (options.m_synthetic_children_traversal ==
2621 GetValueForExpressionPathOptions::
2622 SyntheticChildrenTraversal::ToSynthetic ||
2623 options.m_synthetic_children_traversal ==
2624 GetValueForExpressionPathOptions::
2625 SyntheticChildrenTraversal::Both) {
2626 if (root->HasSyntheticValue())
2627 root = root->GetSyntheticValue();
2628 else if (!root->IsSynthetic()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002629 *reason_to_stop =
2630 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2631 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002632 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002633 }
2634 // if we are here, then root itself is a synthetic VO.. should be good
2635 // to go
2636
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002637 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002638 *reason_to_stop =
2639 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2640 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002641 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002642 }
2643 root = root->GetChildAtIndex(index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002644 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002645 *reason_to_stop =
2646 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2647 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002648 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002649 } else {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002650 remainder =
2651 temp_expression.substr(close_bracket_position + 1); // skip ]
Kate Stoneb9c1b512016-09-06 20:57:50 +00002652 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2653 continue;
2654 }
2655 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002656 *reason_to_stop =
2657 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2658 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002659 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002660 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002661 } else {
2662 // we have a low and a high index
2663 llvm::StringRef sleft, sright;
2664 unsigned long low_index, high_index;
2665 std::tie(sleft, sright) = bracket_expr.split('-');
2666 if (sleft.getAsInteger(0, low_index) ||
2667 sright.getAsInteger(0, high_index)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002668 *reason_to_stop =
2669 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2670 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002671 return nullptr;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002672 }
Zachary Turnerd2daca72016-11-18 17:55:04 +00002673
2674 if (low_index > high_index) // swap indices if required
2675 std::swap(low_index, high_index);
Zachary Turnerc2d55582016-11-18 03:51:19 +00002676
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677 if (root_compiler_type_info.Test(
2678 eTypeIsScalar)) // expansion only works for scalars
2679 {
Zachary Turnerd2daca72016-11-18 17:55:04 +00002680 root = root->GetSyntheticBitFieldChild(low_index, high_index, true);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002681 if (!root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002682 *reason_to_stop =
2683 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2684 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002685 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002686 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002687 *reason_to_stop = ValueObject::
2688 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2689 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2690 return root;
2691 }
2692 } else if (root_compiler_type_info.Test(
2693 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2694 // accessing it by index and I would
2695 // have deref'ed anyway, then do it
2696 // now and use this as a bitfield
2697 *what_next ==
2698 ValueObject::eExpressionPathAftermathDereference &&
2699 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2700 Error error;
2701 root = root->Dereference(error);
Zachary Turner2a3d10a2016-11-18 19:23:39 +00002702 if (error.Fail() || !root) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002703 *reason_to_stop =
2704 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2705 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002706 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002707 } else {
2708 *what_next = ValueObject::eExpressionPathAftermathNothing;
2709 continue;
2710 }
2711 } else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002712 *reason_to_stop =
2713 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2714 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2715 return root;
2716 }
2717 }
2718 break;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002719 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002720 default: // some non-separator is in the way
2721 {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002722 *reason_to_stop =
2723 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2724 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Zachary Turnerd2daca72016-11-18 17:55:04 +00002725 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002726 }
2727 }
2728 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002729}
2730
Kate Stoneb9c1b512016-09-06 20:57:50 +00002731void ValueObject::LogValueObject(Log *log) {
2732 if (log)
2733 return LogValueObject(log, DumpValueObjectOptions(*this));
Enrico Granata538a88a2014-10-09 18:24:30 +00002734}
2735
Kate Stoneb9c1b512016-09-06 20:57:50 +00002736void ValueObject::LogValueObject(Log *log,
2737 const DumpValueObjectOptions &options) {
2738 if (log) {
2739 StreamString s;
2740 Dump(s, options);
2741 if (s.GetSize())
2742 log->PutCString(s.GetData());
2743 }
Greg Clayton759e7442014-07-19 00:12:57 +00002744}
2745
Kate Stoneb9c1b512016-09-06 20:57:50 +00002746void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
Chaoren Lind7bdc272015-07-31 00:35:40 +00002747
Kate Stoneb9c1b512016-09-06 20:57:50 +00002748void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
2749 ValueObjectPrinter printer(this, &s, options);
2750 printer.PrintValueObject();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002751}
2752
Kate Stoneb9c1b512016-09-06 20:57:50 +00002753ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
2754 ValueObjectSP valobj_sp;
2755
2756 if (UpdateValueIfNeeded(false) && m_error.Success()) {
2757 ExecutionContext exe_ctx(GetExecutionContextRef());
2758
2759 DataExtractor data;
2760 data.SetByteOrder(m_data.GetByteOrder());
2761 data.SetAddressByteSize(m_data.GetAddressByteSize());
2762
2763 if (IsBitfield()) {
2764 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
2765 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2766 } else
2767 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2768
2769 valobj_sp = ValueObjectConstResult::Create(
2770 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
2771 GetAddressOf());
2772 }
2773
2774 if (!valobj_sp) {
2775 ExecutionContext exe_ctx(GetExecutionContextRef());
2776 valobj_sp = ValueObjectConstResult::Create(
2777 exe_ctx.GetBestExecutionContextScope(), m_error);
2778 }
2779 return valobj_sp;
2780}
2781
2782ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
2783 lldb::DynamicValueType dynValue, bool synthValue) {
2784 ValueObjectSP result_sp(GetSP());
2785
2786 switch (dynValue) {
2787 case lldb::eDynamicCanRunTarget:
2788 case lldb::eDynamicDontRunTarget: {
2789 if (!result_sp->IsDynamic()) {
2790 if (result_sp->GetDynamicValue(dynValue))
2791 result_sp = result_sp->GetDynamicValue(dynValue);
2792 }
2793 } break;
2794 case lldb::eNoDynamicValues: {
2795 if (result_sp->IsDynamic()) {
2796 if (result_sp->GetStaticValue())
2797 result_sp = result_sp->GetStaticValue();
2798 }
2799 } break;
2800 }
2801
2802 if (synthValue) {
2803 if (!result_sp->IsSynthetic()) {
2804 if (result_sp->GetSyntheticValue())
2805 result_sp = result_sp->GetSyntheticValue();
2806 }
2807 } else {
2808 if (result_sp->IsSynthetic()) {
2809 if (result_sp->GetNonSyntheticValue())
2810 result_sp = result_sp->GetNonSyntheticValue();
2811 }
2812 }
2813
2814 return result_sp;
2815}
2816
2817lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
2818 CompilerType pointee_type;
2819 CompilerType this_type(GetCompilerType());
2820 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
2821 if (type_info) {
2822 bool ptr_or_ref = false;
2823 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
2824 ptr_or_ref = true;
2825 type_info = pointee_type.GetTypeInfo();
2826 }
2827
2828 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
2829 if ((type_info & cpp_class) == cpp_class) {
2830 if (ptr_or_ref) {
2831 address_type = GetAddressTypeOfChildren();
2832 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
2833 } else
2834 return GetAddressOf(false, &address_type);
2835 }
2836 }
2837
2838 address_type = eAddressTypeInvalid;
2839 return LLDB_INVALID_ADDRESS;
2840}
2841
2842ValueObjectSP ValueObject::Dereference(Error &error) {
2843 if (m_deref_valobj)
2844 return m_deref_valobj->GetSP();
2845
2846 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
2847 if (is_pointer_or_reference_type) {
2848 bool omit_empty_base_classes = true;
2849 bool ignore_array_bounds = false;
2850
2851 std::string child_name_str;
2852 uint32_t child_byte_size = 0;
2853 int32_t child_byte_offset = 0;
2854 uint32_t child_bitfield_bit_size = 0;
2855 uint32_t child_bitfield_bit_offset = 0;
2856 bool child_is_base_class = false;
2857 bool child_is_deref_of_parent = false;
2858 const bool transparent_pointers = false;
2859 CompilerType compiler_type = GetCompilerType();
2860 CompilerType child_compiler_type;
2861 uint64_t language_flags;
2862
2863 ExecutionContext exe_ctx(GetExecutionContextRef());
2864
2865 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
2866 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
2867 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
2868 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
2869 child_is_deref_of_parent, this, language_flags);
2870 if (child_compiler_type && child_byte_size) {
2871 ConstString child_name;
2872 if (!child_name_str.empty())
2873 child_name.SetCString(child_name_str.c_str());
2874
2875 m_deref_valobj = new ValueObjectChild(
2876 *this, child_compiler_type, child_name, child_byte_size,
2877 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
2878 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
2879 language_flags);
2880 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +00002881 } else if (HasSyntheticValue()) {
2882 m_deref_valobj =
2883 GetSyntheticValue()
2884 ->GetChildMemberWithName(ConstString("$$dereference$$"), true)
2885 .get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002886 }
2887
2888 if (m_deref_valobj) {
Greg Clayton54979cd2010-12-15 05:08:08 +00002889 error.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002890 return m_deref_valobj->GetSP();
2891 } else {
2892 StreamString strm;
2893 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002894
Kate Stoneb9c1b512016-09-06 20:57:50 +00002895 if (is_pointer_or_reference_type)
2896 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
2897 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00002898 strm.GetData());
Sean Callananed185ab2013-04-19 19:47:32 +00002899 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002900 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
2901 GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +00002902 strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002903 return ValueObjectSP();
2904 }
2905}
2906
2907ValueObjectSP ValueObject::AddressOf(Error &error) {
2908 if (m_addr_of_valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00002909 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002910
Kate Stoneb9c1b512016-09-06 20:57:50 +00002911 AddressType address_type = eAddressTypeInvalid;
2912 const bool scalar_is_load_address = false;
2913 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
2914 error.Clear();
2915 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
2916 switch (address_type) {
2917 case eAddressTypeInvalid: {
2918 StreamString expr_path_strm;
2919 GetExpressionPath(expr_path_strm, true);
2920 error.SetErrorStringWithFormat("'%s' is not in memory",
Zachary Turnerc1564272016-11-16 21:15:24 +00002921 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002922 } break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002923
Kate Stoneb9c1b512016-09-06 20:57:50 +00002924 case eAddressTypeFile:
2925 case eAddressTypeLoad: {
2926 CompilerType compiler_type = GetCompilerType();
2927 if (compiler_type) {
2928 std::string name(1, '&');
2929 name.append(m_name.AsCString(""));
2930 ExecutionContext exe_ctx(GetExecutionContextRef());
2931 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
2932 exe_ctx.GetBestExecutionContextScope(),
2933 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
2934 eAddressTypeInvalid, m_data.GetAddressByteSize());
2935 }
2936 } break;
2937 default:
2938 break;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002939 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002940 } else {
2941 StreamString expr_path_strm;
2942 GetExpressionPath(expr_path_strm, true);
2943 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
Zachary Turnerc1564272016-11-16 21:15:24 +00002944 expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002945 }
2946
2947 return m_addr_of_valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00002948}
2949
Kate Stoneb9c1b512016-09-06 20:57:50 +00002950ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
2951 return ValueObjectCast::Create(*this, GetName(), compiler_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00002952}
2953
Tamas Berghammer4fbb55b2017-03-31 20:48:00 +00002954lldb::ValueObjectSP ValueObject::Clone(const ConstString &new_name) {
2955 return ValueObjectCast::Create(*this, new_name, GetCompilerType());
2956}
2957
Kate Stoneb9c1b512016-09-06 20:57:50 +00002958ValueObjectSP ValueObject::CastPointerType(const char *name,
2959 CompilerType &compiler_type) {
2960 ValueObjectSP valobj_sp;
2961 AddressType address_type;
2962 addr_t ptr_value = GetPointerValue(&address_type);
2963
2964 if (ptr_value != LLDB_INVALID_ADDRESS) {
2965 Address ptr_addr(ptr_value);
2966 ExecutionContext exe_ctx(GetExecutionContextRef());
2967 valobj_sp = ValueObjectMemory::Create(
2968 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
2969 }
2970 return valobj_sp;
Jim Ingham6035b672011-03-31 00:19:25 +00002971}
2972
Kate Stoneb9c1b512016-09-06 20:57:50 +00002973ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
2974 ValueObjectSP valobj_sp;
2975 AddressType address_type;
2976 addr_t ptr_value = GetPointerValue(&address_type);
2977
2978 if (ptr_value != LLDB_INVALID_ADDRESS) {
2979 Address ptr_addr(ptr_value);
2980 ExecutionContext exe_ctx(GetExecutionContextRef());
2981 valobj_sp = ValueObjectMemory::Create(
2982 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
2983 }
2984 return valobj_sp;
2985}
2986
2987ValueObject::EvaluationPoint::EvaluationPoint()
2988 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
2989
2990ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
2991 bool use_selected)
2992 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
2993 ExecutionContext exe_ctx(exe_scope);
2994 TargetSP target_sp(exe_ctx.GetTargetSP());
2995 if (target_sp) {
2996 m_exe_ctx_ref.SetTargetSP(target_sp);
2997 ProcessSP process_sp(exe_ctx.GetProcessSP());
2998 if (!process_sp)
2999 process_sp = target_sp->GetProcessSP();
3000
3001 if (process_sp) {
3002 m_mod_id = process_sp->GetModID();
3003 m_exe_ctx_ref.SetProcessSP(process_sp);
3004
3005 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3006
3007 if (!thread_sp) {
3008 if (use_selected)
3009 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3010 }
3011
3012 if (thread_sp) {
3013 m_exe_ctx_ref.SetThreadSP(thread_sp);
3014
3015 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3016 if (!frame_sp) {
3017 if (use_selected)
3018 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003019 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003020 if (frame_sp)
3021 m_exe_ctx_ref.SetFrameSP(frame_sp);
3022 }
Jim Ingham6035b672011-03-31 00:19:25 +00003023 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003024 }
Jim Ingham6035b672011-03-31 00:19:25 +00003025}
3026
Kate Stoneb9c1b512016-09-06 20:57:50 +00003027ValueObject::EvaluationPoint::EvaluationPoint(
3028 const ValueObject::EvaluationPoint &rhs)
3029 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
Jim Ingham6035b672011-03-31 00:19:25 +00003030
Kate Stoneb9c1b512016-09-06 20:57:50 +00003031ValueObject::EvaluationPoint::~EvaluationPoint() {}
Jim Ingham6035b672011-03-31 00:19:25 +00003032
Kate Stoneb9c1b512016-09-06 20:57:50 +00003033// This function checks the EvaluationPoint against the current process state.
3034// If the current
3035// state matches the evaluation point, or the evaluation point is already
3036// invalid, then we return
3037// false, meaning "no change". If the current state is different, we update our
3038// state, and return
3039// true meaning "yes, change". If we did see a change, we also set
3040// m_needs_update to true, so
Jim Ingham6035b672011-03-31 00:19:25 +00003041// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003042// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003043
Kate Stoneb9c1b512016-09-06 20:57:50 +00003044bool ValueObject::EvaluationPoint::SyncWithProcessState(
3045 bool accept_invalid_exe_ctx) {
3046 // Start with the target, if it is NULL, then we're obviously not going to get
3047 // any further:
3048 const bool thread_and_frame_only_if_stopped = true;
3049 ExecutionContext exe_ctx(
3050 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3051
3052 if (exe_ctx.GetTargetPtr() == NULL)
3053 return false;
3054
3055 // If we don't have a process nothing can change.
3056 Process *process = exe_ctx.GetProcessPtr();
3057 if (process == NULL)
3058 return false;
3059
3060 // If our stop id is the current stop ID, nothing has changed:
3061 ProcessModID current_mod_id = process->GetModID();
3062
3063 // If the current stop id is 0, either we haven't run yet, or the process
3064 // state has been cleared.
3065 // In either case, we aren't going to be able to sync with the process state.
3066 if (current_mod_id.GetStopID() == 0)
3067 return false;
3068
3069 bool changed = false;
3070 const bool was_valid = m_mod_id.IsValid();
3071 if (was_valid) {
3072 if (m_mod_id == current_mod_id) {
3073 // Everything is already up to date in this object, no need to
3074 // update the execution context scope.
3075 changed = false;
3076 } else {
3077 m_mod_id = current_mod_id;
3078 m_needs_update = true;
3079 changed = true;
3080 }
3081 }
3082
3083 // Now re-look up the thread and frame in case the underlying objects have
3084 // gone away & been recreated.
3085 // That way we'll be sure to return a valid exe_scope.
3086 // If we used to have a thread or a frame but can't find it anymore, then mark
3087 // ourselves as invalid.
3088
3089 if (!accept_invalid_exe_ctx) {
3090 if (m_exe_ctx_ref.HasThreadRef()) {
3091 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3092 if (thread_sp) {
3093 if (m_exe_ctx_ref.HasFrameRef()) {
3094 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3095 if (!frame_sp) {
3096 // We used to have a frame, but now it is gone
3097 SetInvalid();
3098 changed = was_valid;
3099 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003101 } else {
3102 // We used to have a thread, but now it is gone
3103 SetInvalid();
3104 changed = was_valid;
3105 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003107 }
Enrico Granatabb642e52015-05-16 01:27:00 +00003108
Kate Stoneb9c1b512016-09-06 20:57:50 +00003109 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003110}
3111
Kate Stoneb9c1b512016-09-06 20:57:50 +00003112void ValueObject::EvaluationPoint::SetUpdated() {
3113 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3114 if (process_sp)
3115 m_mod_id = process_sp->GetModID();
3116 m_needs_update = false;
Johnny Chen44805302011-07-19 19:48:13 +00003117}
Enrico Granata9128ee22011-09-06 19:20:51 +00003118
Kate Stoneb9c1b512016-09-06 20:57:50 +00003119void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3120 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3121 eClearUserVisibleDataItemsValue)
3122 m_value_str.clear();
3123
3124 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3125 eClearUserVisibleDataItemsLocation)
3126 m_location_str.clear();
3127
3128 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3129 eClearUserVisibleDataItemsSummary)
3130 m_summary_str.clear();
3131
3132 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3133 eClearUserVisibleDataItemsDescription)
3134 m_object_desc_str.clear();
3135
3136 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3137 eClearUserVisibleDataItemsSyntheticChildren) {
3138 if (m_synthetic_value)
3139 m_synthetic_value = NULL;
3140 }
3141
3142 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3143 eClearUserVisibleDataItemsValidator)
3144 m_validation_result.reset();
Enrico Granata9128ee22011-09-06 19:20:51 +00003145}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003146
Kate Stoneb9c1b512016-09-06 20:57:50 +00003147SymbolContextScope *ValueObject::GetSymbolContextScope() {
3148 if (m_parent) {
3149 if (!m_parent->IsPointerOrReferenceType())
3150 return m_parent->GetSymbolContextScope();
3151 }
3152 return NULL;
Enrico Granata972be532014-12-17 21:18:43 +00003153}
3154
Zachary Turneraa5611f2016-11-13 03:29:46 +00003155lldb::ValueObjectSP
3156ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3157 llvm::StringRef expression,
3158 const ExecutionContext &exe_ctx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003159 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3160 EvaluateExpressionOptions());
3161}
Enrico Granata972be532014-12-17 21:18:43 +00003162
Kate Stoneb9c1b512016-09-06 20:57:50 +00003163lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003164 llvm::StringRef name, llvm::StringRef expression,
3165 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003166 lldb::ValueObjectSP retval_sp;
3167 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3168 if (!target_sp)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003169 return retval_sp;
Zachary Turneraa5611f2016-11-13 03:29:46 +00003170 if (expression.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003171 return retval_sp;
3172 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3173 retval_sp, options);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003174 if (retval_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003175 retval_sp->SetName(ConstString(name));
3176 return retval_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003177}
3178
Zachary Turneraa5611f2016-11-13 03:29:46 +00003179lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3180 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3181 CompilerType type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003182 if (type) {
3183 CompilerType pointer_type(type.GetPointerType());
3184 if (pointer_type) {
3185 lldb::DataBufferSP buffer(
3186 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3187 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3188 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3189 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3190 exe_ctx.GetAddressByteSize()));
3191 if (ptr_result_valobj_sp) {
3192 ptr_result_valobj_sp->GetValue().SetValueType(
3193 Value::eValueTypeLoadAddress);
3194 Error err;
3195 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003196 if (ptr_result_valobj_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003197 ptr_result_valobj_sp->SetName(ConstString(name));
3198 }
3199 return ptr_result_valobj_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003200 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003201 }
3202 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003203}
3204
Kate Stoneb9c1b512016-09-06 20:57:50 +00003205lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
Zachary Turneraa5611f2016-11-13 03:29:46 +00003206 llvm::StringRef name, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003207 const ExecutionContext &exe_ctx, CompilerType type) {
3208 lldb::ValueObjectSP new_value_sp;
3209 new_value_sp = ValueObjectConstResult::Create(
3210 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3211 LLDB_INVALID_ADDRESS);
3212 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
Zachary Turneraa5611f2016-11-13 03:29:46 +00003213 if (new_value_sp && !name.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +00003214 new_value_sp->SetName(ConstString(name));
3215 return new_value_sp;
Enrico Granatab2698cd2012-09-13 18:27:09 +00003216}
Enrico Granata4873e522013-04-11 22:48:58 +00003217
Kate Stoneb9c1b512016-09-06 20:57:50 +00003218ModuleSP ValueObject::GetModule() {
3219 ValueObject *root(GetRoot());
3220 if (root != this)
3221 return root->GetModule();
3222 return lldb::ModuleSP();
3223}
3224
3225ValueObject *ValueObject::GetRoot() {
3226 if (m_root)
3227 return m_root;
3228 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3229 return (vo->m_parent != nullptr);
3230 }));
3231}
3232
3233ValueObject *
3234ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3235 ValueObject *vo = this;
3236 while (vo) {
3237 if (f(vo) == false)
3238 break;
3239 vo = vo->m_parent;
3240 }
3241 return vo;
3242}
3243
3244AddressType ValueObject::GetAddressTypeOfChildren() {
3245 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3246 ValueObject *root(GetRoot());
Enrico Granata4873e522013-04-11 22:48:58 +00003247 if (root != this)
Kate Stoneb9c1b512016-09-06 20:57:50 +00003248 return root->GetAddressTypeOfChildren();
3249 }
3250 return m_address_type_of_ptr_or_ref_children;
Enrico Granata4873e522013-04-11 22:48:58 +00003251}
3252
Kate Stoneb9c1b512016-09-06 20:57:50 +00003253lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3254 ValueObject *with_dv_info = this;
3255 while (with_dv_info) {
3256 if (with_dv_info->HasDynamicValueTypeInfo())
3257 return with_dv_info->GetDynamicValueTypeImpl();
3258 with_dv_info = with_dv_info->m_parent;
3259 }
3260 return lldb::eNoDynamicValues;
Enrico Granatade61eba2015-01-22 03:07:34 +00003261}
3262
Kate Stoneb9c1b512016-09-06 20:57:50 +00003263lldb::Format ValueObject::GetFormat() const {
3264 const ValueObject *with_fmt_info = this;
3265 while (with_fmt_info) {
3266 if (with_fmt_info->m_format != lldb::eFormatDefault)
3267 return with_fmt_info->m_format;
3268 with_fmt_info = with_fmt_info->m_parent;
3269 }
3270 return m_format;
Enrico Granata4873e522013-04-11 22:48:58 +00003271}
3272
Kate Stoneb9c1b512016-09-06 20:57:50 +00003273lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3274 lldb::LanguageType type = m_preferred_display_language;
3275 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3276 if (GetRoot()) {
3277 if (GetRoot() == this) {
3278 if (StackFrameSP frame_sp = GetFrameSP()) {
3279 const SymbolContext &sc(
3280 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3281 if (CompileUnit *cu = sc.comp_unit)
3282 type = cu->GetLanguage();
Enrico Granatac1247f52014-11-06 21:23:20 +00003283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003284 } else {
3285 type = GetRoot()->GetPreferredDisplayLanguage();
3286 }
Enrico Granatac1247f52014-11-06 21:23:20 +00003287 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003288 }
3289 return (m_preferred_display_language = type); // only compute it once
Enrico Granataed3228a2015-01-21 01:47:13 +00003290}
3291
Kate Stoneb9c1b512016-09-06 20:57:50 +00003292void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3293 m_preferred_display_language = lt;
Enrico Granatac1247f52014-11-06 21:23:20 +00003294}
3295
Kate Stoneb9c1b512016-09-06 20:57:50 +00003296void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3297 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3298 SetPreferredDisplayLanguage(lt);
Enrico Granata73e8c4d2015-10-07 02:36:35 +00003299}
3300
Kate Stoneb9c1b512016-09-06 20:57:50 +00003301bool ValueObject::CanProvideValue() {
3302 // we need to support invalid types as providers of values because some
3303 // bare-board
3304 // debugging scenarios have no notion of types, but still manage to have raw
3305 // numeric
3306 // values for things like registers. sigh.
3307 const CompilerType &type(GetCompilerType());
3308 return (false == type.IsValid()) ||
3309 (0 != (type.GetTypeInfo() & eTypeHasValue));
Sean Callanan7375f3e2014-12-09 21:18:59 +00003310}
3311
Kate Stoneb9c1b512016-09-06 20:57:50 +00003312bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3313
3314ValueObjectSP ValueObject::Persist() {
3315 if (!UpdateValueIfNeeded())
3316 return nullptr;
3317
3318 TargetSP target_sp(GetTargetSP());
3319 if (!target_sp)
3320 return nullptr;
3321
3322 PersistentExpressionState *persistent_state =
3323 target_sp->GetPersistentExpressionStateForLanguage(
3324 GetPreferredDisplayLanguage());
3325
3326 if (!persistent_state)
3327 return nullptr;
3328
3329 ConstString name(persistent_state->GetNextPersistentVariableName());
3330
3331 ValueObjectSP const_result_sp =
3332 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3333
3334 ExpressionVariableSP clang_var_sp =
3335 persistent_state->CreatePersistentVariable(const_result_sp);
3336 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3337 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3338
3339 return clang_var_sp->GetValueObject();
Enrico Granatad07cfd32014-10-08 18:27:36 +00003340}
Enrico Granata0c10a852014-12-08 23:13:56 +00003341
Kate Stoneb9c1b512016-09-06 20:57:50 +00003342bool ValueObject::IsSyntheticChildrenGenerated() {
3343 return m_is_synthetic_children_generated;
Enrico Granata0c10a852014-12-08 23:13:56 +00003344}
Enrico Granatae29df232014-12-09 19:51:20 +00003345
Kate Stoneb9c1b512016-09-06 20:57:50 +00003346void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3347 m_is_synthetic_children_generated = b;
Enrico Granatae29df232014-12-09 19:51:20 +00003348}
3349
Kate Stoneb9c1b512016-09-06 20:57:50 +00003350uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
Enrico Granatadc62ffd2015-11-09 19:27:34 +00003351
Kate Stoneb9c1b512016-09-06 20:57:50 +00003352void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }
Greg Clayton8369b282016-12-28 21:22:37 +00003353
3354ValueObjectManager::ValueObjectManager(lldb::ValueObjectSP in_valobj_sp,
3355 lldb::DynamicValueType use_dynamic,
3356 bool use_synthetic) : m_root_valobj_sp(),
3357 m_user_valobj_sp(), m_use_dynamic(use_dynamic), m_stop_id(UINT32_MAX),
3358 m_use_synthetic(use_synthetic) {
3359 if (!in_valobj_sp)
3360 return;
3361 // If the user passes in a value object that is dynamic or synthetic, then
3362 // water it down to the static type.
3363 m_root_valobj_sp = in_valobj_sp->GetQualifiedRepresentationIfAvailable(lldb::eNoDynamicValues, false);
3364}
3365
3366bool ValueObjectManager::IsValid() const {
3367 if (!m_root_valobj_sp)
3368 return false;
3369 lldb::TargetSP target_sp = GetTargetSP();
3370 if (target_sp)
3371 return target_sp->IsValid();
3372 return false;
3373}
3374
3375lldb::ValueObjectSP ValueObjectManager::GetSP() {
3376 lldb::ProcessSP process_sp = GetProcessSP();
3377 if (!process_sp)
3378 return lldb::ValueObjectSP();
3379
3380 const uint32_t current_stop_id = process_sp->GetLastNaturalStopID();
3381 if (current_stop_id == m_stop_id)
3382 return m_user_valobj_sp;
3383
3384 m_stop_id = current_stop_id;
3385
3386 if (!m_root_valobj_sp) {
3387 m_user_valobj_sp.reset();
3388 return m_root_valobj_sp;
3389 }
3390
3391 m_user_valobj_sp = m_root_valobj_sp;
3392
3393 if (m_use_dynamic != lldb::eNoDynamicValues) {
3394 lldb::ValueObjectSP dynamic_sp = m_user_valobj_sp->GetDynamicValue(m_use_dynamic);
3395 if (dynamic_sp)
3396 m_user_valobj_sp = dynamic_sp;
3397 }
3398
3399 if (m_use_synthetic) {
3400 lldb::ValueObjectSP synthetic_sp = m_user_valobj_sp->GetSyntheticValue(m_use_synthetic);
3401 if (synthetic_sp)
3402 m_user_valobj_sp = synthetic_sp;
3403 }
3404
3405 return m_user_valobj_sp;
3406}
3407
3408void ValueObjectManager::SetUseDynamic(lldb::DynamicValueType use_dynamic) {
3409 if (use_dynamic != m_use_dynamic) {
3410 m_use_dynamic = use_dynamic;
3411 m_user_valobj_sp.reset();
3412 m_stop_id = UINT32_MAX;
3413 }
3414}
3415
3416void ValueObjectManager::SetUseSynthetic(bool use_synthetic) {
3417 if (m_use_synthetic != use_synthetic) {
3418 m_use_synthetic = use_synthetic;
3419 m_user_valobj_sp.reset();
3420 m_stop_id = UINT32_MAX;
3421 }
3422}
3423
3424lldb::TargetSP ValueObjectManager::GetTargetSP() const {
3425 if (!m_root_valobj_sp)
3426 return m_root_valobj_sp->GetTargetSP();
3427 return lldb::TargetSP();
3428}
3429
3430lldb::ProcessSP ValueObjectManager::GetProcessSP() const {
3431 if (m_root_valobj_sp)
3432 return m_root_valobj_sp->GetProcessSP();
3433 return lldb::ProcessSP();
3434}
3435
3436lldb::ThreadSP ValueObjectManager::GetThreadSP() const {
3437 if (m_root_valobj_sp)
3438 return m_root_valobj_sp->GetThreadSP();
3439 return lldb::ThreadSP();
3440}
3441
3442lldb::StackFrameSP ValueObjectManager::GetFrameSP() const {
3443 if (m_root_valobj_sp)
3444 return m_root_valobj_sp->GetFrameSP();
3445 return lldb::StackFrameSP();
3446}
3447