1//===-- ValueObjectPrinter.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/DataFormatters/ValueObjectPrinter.h"
10
11#include "lldb/Core/ValueObject.h"
12#include "lldb/DataFormatters/DataVisualization.h"
13#include "lldb/Interpreter/CommandInterpreter.h"
14#include "lldb/Target/Language.h"
15#include "lldb/Target/Target.h"
16#include "lldb/Utility/Stream.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21ValueObjectPrinter::ValueObjectPrinter(ValueObject &valobj, Stream *s)
22 : m_orig_valobj(valobj) {
23 DumpValueObjectOptions options(valobj);
24 Init(valobj, s, options, ptr_depth: m_options.m_max_ptr_depth, curr_depth: 0, printed_instance_pointers: nullptr);
25}
26
27ValueObjectPrinter::ValueObjectPrinter(ValueObject &valobj, Stream *s,
28 const DumpValueObjectOptions &options)
29 : m_orig_valobj(valobj) {
30 Init(valobj, s, options, ptr_depth: m_options.m_max_ptr_depth, curr_depth: 0, printed_instance_pointers: nullptr);
31}
32
33ValueObjectPrinter::ValueObjectPrinter(
34 ValueObject &valobj, Stream *s, const DumpValueObjectOptions &options,
35 const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
36 InstancePointersSetSP printed_instance_pointers)
37 : m_orig_valobj(valobj) {
38 Init(valobj, s, options, ptr_depth, curr_depth, printed_instance_pointers);
39}
40
41void ValueObjectPrinter::Init(
42 ValueObject &valobj, Stream *s, const DumpValueObjectOptions &options,
43 const DumpValueObjectOptions::PointerDepth &ptr_depth, uint32_t curr_depth,
44 InstancePointersSetSP printed_instance_pointers) {
45 m_cached_valobj = nullptr;
46 m_stream = s;
47 m_options = options;
48 m_ptr_depth = ptr_depth;
49 m_curr_depth = curr_depth;
50 assert(m_stream && "cannot print to a NULL Stream");
51 m_should_print = eLazyBoolCalculate;
52 m_is_nil = eLazyBoolCalculate;
53 m_is_uninit = eLazyBoolCalculate;
54 m_is_ptr = eLazyBoolCalculate;
55 m_is_ref = eLazyBoolCalculate;
56 m_is_aggregate = eLazyBoolCalculate;
57 m_is_instance_ptr = eLazyBoolCalculate;
58 m_summary_formatter = {nullptr, false};
59 m_value.assign(s: "");
60 m_summary.assign(s: "");
61 m_error.assign(s: "");
62 m_val_summary_ok = false;
63 m_printed_instance_pointers =
64 printed_instance_pointers
65 ? printed_instance_pointers
66 : InstancePointersSetSP(new InstancePointersSet());
67 SetupMostSpecializedValue();
68}
69
70bool ValueObjectPrinter::PrintValueObject() {
71 // If the incoming ValueObject is in an error state, the best we're going to
72 // get out of it is its type. But if we don't even have that, just print
73 // the error and exit early.
74 if (m_orig_valobj.GetError().Fail() &&
75 !m_orig_valobj.GetCompilerType().IsValid()) {
76 m_stream->Printf(format: "Error: '%s'", m_orig_valobj.GetError().AsCString());
77 return true;
78 }
79
80 if (ShouldPrintValueObject()) {
81 PrintLocationIfNeeded();
82 m_stream->Indent();
83
84 PrintDecl();
85 }
86
87 bool value_printed = false;
88 bool summary_printed = false;
89
90 m_val_summary_ok =
91 PrintValueAndSummaryIfNeeded(value_printed, summary_printed);
92
93 if (m_val_summary_ok)
94 PrintChildrenIfNeeded(value_printed, summary_printed);
95 else
96 m_stream->EOL();
97
98 return true;
99}
100
101ValueObject &ValueObjectPrinter::GetMostSpecializedValue() {
102 assert(m_cached_valobj && "ValueObjectPrinter must have a valid ValueObject");
103 return *m_cached_valobj;
104}
105
106void ValueObjectPrinter::SetupMostSpecializedValue() {
107 bool update_success = m_orig_valobj.UpdateValueIfNeeded(update_format: true);
108 // If we can't find anything better, we'll fall back on the original
109 // ValueObject.
110 m_cached_valobj = &m_orig_valobj;
111 if (update_success) {
112 if (m_orig_valobj.IsDynamic()) {
113 if (m_options.m_use_dynamic == eNoDynamicValues) {
114 ValueObject *static_value = m_orig_valobj.GetStaticValue().get();
115 if (static_value)
116 m_cached_valobj = static_value;
117 }
118 } else {
119 if (m_options.m_use_dynamic != eNoDynamicValues) {
120 ValueObject *dynamic_value =
121 m_orig_valobj.GetDynamicValue(valueType: m_options.m_use_dynamic).get();
122 if (dynamic_value)
123 m_cached_valobj = dynamic_value;
124 }
125 }
126
127 if (m_cached_valobj->IsSynthetic()) {
128 if (!m_options.m_use_synthetic) {
129 ValueObject *non_synthetic =
130 m_cached_valobj->GetNonSyntheticValue().get();
131 if (non_synthetic)
132 m_cached_valobj = non_synthetic;
133 }
134 } else {
135 if (m_options.m_use_synthetic) {
136 ValueObject *synthetic = m_cached_valobj->GetSyntheticValue().get();
137 if (synthetic)
138 m_cached_valobj = synthetic;
139 }
140 }
141 }
142 m_compiler_type = m_cached_valobj->GetCompilerType();
143 m_type_flags = m_compiler_type.GetTypeInfo();
144 assert(m_cached_valobj &&
145 "SetupMostSpecialized value must compute a valid ValueObject");
146}
147
148const char *ValueObjectPrinter::GetDescriptionForDisplay() {
149 ValueObject &valobj = GetMostSpecializedValue();
150 const char *str = valobj.GetObjectDescription();
151 if (!str)
152 str = valobj.GetSummaryAsCString();
153 if (!str)
154 str = valobj.GetValueAsCString();
155 return str;
156}
157
158const char *ValueObjectPrinter::GetRootNameForDisplay() {
159 const char *root_valobj_name =
160 m_options.m_root_valobj_name.empty()
161 ? GetMostSpecializedValue().GetName().AsCString()
162 : m_options.m_root_valobj_name.c_str();
163 return root_valobj_name ? root_valobj_name : "";
164}
165
166bool ValueObjectPrinter::ShouldPrintValueObject() {
167 if (m_should_print == eLazyBoolCalculate)
168 m_should_print =
169 (!m_options.m_flat_output || m_type_flags.Test(bit: eTypeHasValue))
170 ? eLazyBoolYes
171 : eLazyBoolNo;
172 return m_should_print == eLazyBoolYes;
173}
174
175bool ValueObjectPrinter::IsNil() {
176 if (m_is_nil == eLazyBoolCalculate)
177 m_is_nil =
178 GetMostSpecializedValue().IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
179 return m_is_nil == eLazyBoolYes;
180}
181
182bool ValueObjectPrinter::IsUninitialized() {
183 if (m_is_uninit == eLazyBoolCalculate)
184 m_is_uninit = GetMostSpecializedValue().IsUninitializedReference()
185 ? eLazyBoolYes
186 : eLazyBoolNo;
187 return m_is_uninit == eLazyBoolYes;
188}
189
190bool ValueObjectPrinter::IsPtr() {
191 if (m_is_ptr == eLazyBoolCalculate)
192 m_is_ptr = m_type_flags.Test(bit: eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
193 return m_is_ptr == eLazyBoolYes;
194}
195
196bool ValueObjectPrinter::IsRef() {
197 if (m_is_ref == eLazyBoolCalculate)
198 m_is_ref = m_type_flags.Test(bit: eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
199 return m_is_ref == eLazyBoolYes;
200}
201
202bool ValueObjectPrinter::IsAggregate() {
203 if (m_is_aggregate == eLazyBoolCalculate)
204 m_is_aggregate =
205 m_type_flags.Test(bit: eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
206 return m_is_aggregate == eLazyBoolYes;
207}
208
209bool ValueObjectPrinter::IsInstancePointer() {
210 // you need to do this check on the value's clang type
211 ValueObject &valobj = GetMostSpecializedValue();
212 if (m_is_instance_ptr == eLazyBoolCalculate)
213 m_is_instance_ptr = (valobj.GetValue().GetCompilerType().GetTypeInfo() &
214 eTypeInstanceIsPointer) != 0
215 ? eLazyBoolYes
216 : eLazyBoolNo;
217 if ((eLazyBoolYes == m_is_instance_ptr) && valobj.IsBaseClass())
218 m_is_instance_ptr = eLazyBoolNo;
219 return m_is_instance_ptr == eLazyBoolYes;
220}
221
222bool ValueObjectPrinter::PrintLocationIfNeeded() {
223 if (m_options.m_show_location) {
224 m_stream->Printf(format: "%s: ", GetMostSpecializedValue().GetLocationAsCString());
225 return true;
226 }
227 return false;
228}
229
230void ValueObjectPrinter::PrintDecl() {
231 bool show_type = true;
232 // if we are at the root-level and been asked to hide the root's type, then
233 // hide it
234 if (m_curr_depth == 0 && m_options.m_hide_root_type)
235 show_type = false;
236 else
237 // otherwise decide according to the usual rules (asked to show types -
238 // always at the root level)
239 show_type = m_options.m_show_types ||
240 (m_curr_depth == 0 && !m_options.m_flat_output);
241
242 StreamString typeName;
243 // Figure out which ValueObject we're acting on
244 ValueObject &valobj = GetMostSpecializedValue();
245
246 // always show the type at the root level if it is invalid
247 if (show_type) {
248 // Some ValueObjects don't have types (like registers sets). Only print the
249 // type if there is one to print
250 ConstString type_name;
251 if (m_compiler_type.IsValid()) {
252 type_name = m_options.m_use_type_display_name
253 ? valobj.GetDisplayTypeName()
254 : valobj.GetQualifiedTypeName();
255 } else {
256 // only show an invalid type name if the user explicitly triggered
257 // show_type
258 if (m_options.m_show_types)
259 type_name = ConstString("<invalid type>");
260 }
261
262 if (type_name) {
263 std::string type_name_str(type_name.GetCString());
264 if (m_options.m_hide_pointer_value) {
265 for (auto iter = type_name_str.find(s: " *"); iter != std::string::npos;
266 iter = type_name_str.find(s: " *")) {
267 type_name_str.erase(pos: iter, n: 2);
268 }
269 }
270 typeName << type_name_str.c_str();
271 }
272 }
273
274 StreamString varName;
275
276 if (ShouldShowName()) {
277 if (m_options.m_flat_output)
278 valobj.GetExpressionPath(s&: varName);
279 else
280 varName << GetRootNameForDisplay();
281 }
282
283 bool decl_printed = false;
284 if (!m_options.m_decl_printing_helper) {
285 // if the user didn't give us a custom helper, pick one based upon the
286 // language, either the one that this printer is bound to, or the preferred
287 // one for the ValueObject
288 lldb::LanguageType lang_type =
289 (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
290 ? valobj.GetPreferredDisplayLanguage()
291 : m_options.m_varformat_language;
292 if (Language *lang_plugin = Language::FindPlugin(language: lang_type)) {
293 m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
294 }
295 }
296
297 if (m_options.m_decl_printing_helper) {
298 ConstString type_name_cstr(typeName.GetString());
299 ConstString var_name_cstr(varName.GetString());
300
301 DumpValueObjectOptions decl_print_options = m_options;
302 // Pass printing helpers an option object that indicates whether the name
303 // should be shown or hidden.
304 decl_print_options.SetHideName(!ShouldShowName());
305
306 StreamString dest_stream;
307 if (m_options.m_decl_printing_helper(type_name_cstr, var_name_cstr,
308 decl_print_options, dest_stream)) {
309 decl_printed = true;
310 m_stream->PutCString(cstr: dest_stream.GetString());
311 }
312 }
313
314 // if the helper failed, or there is none, do a default thing
315 if (!decl_printed) {
316 if (!typeName.Empty())
317 m_stream->Printf(format: "(%s) ", typeName.GetData());
318 if (!varName.Empty())
319 m_stream->Printf(format: "%s =", varName.GetData());
320 else if (ShouldShowName())
321 m_stream->Printf(format: " =");
322 }
323}
324
325bool ValueObjectPrinter::CheckScopeIfNeeded() {
326 if (m_options.m_scope_already_checked)
327 return true;
328 return GetMostSpecializedValue().IsInScope();
329}
330
331TypeSummaryImpl *ValueObjectPrinter::GetSummaryFormatter(bool null_if_omitted) {
332 if (!m_summary_formatter.second) {
333 TypeSummaryImpl *entry =
334 m_options.m_summary_sp
335 ? m_options.m_summary_sp.get()
336 : GetMostSpecializedValue().GetSummaryFormat().get();
337
338 if (m_options.m_omit_summary_depth > 0)
339 entry = nullptr;
340 m_summary_formatter.first = entry;
341 m_summary_formatter.second = true;
342 }
343 if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
344 return nullptr;
345 return m_summary_formatter.first;
346}
347
348static bool IsPointerValue(const CompilerType &type) {
349 Flags type_flags(type.GetTypeInfo());
350 if (type_flags.AnySet(mask: eTypeInstanceIsPointer | eTypeIsPointer))
351 return type_flags.AllClear(mask: eTypeIsBuiltIn);
352 return false;
353}
354
355void ValueObjectPrinter::GetValueSummaryError(std::string &value,
356 std::string &summary,
357 std::string &error) {
358 lldb::Format format = m_options.m_format;
359 ValueObject &valobj = GetMostSpecializedValue();
360 // if I am printing synthetized elements, apply the format to those elements
361 // only
362 if (m_options.m_pointer_as_array)
363 valobj.GetValueAsCString(format: lldb::eFormatDefault, destination&: value);
364 else if (format != eFormatDefault && format != valobj.GetFormat())
365 valobj.GetValueAsCString(format, destination&: value);
366 else {
367 const char *val_cstr = valobj.GetValueAsCString();
368 if (val_cstr)
369 value.assign(s: val_cstr);
370 }
371 const char *err_cstr = valobj.GetError().AsCString();
372 if (err_cstr)
373 error.assign(s: err_cstr);
374
375 if (!ShouldPrintValueObject())
376 return;
377
378 if (IsNil()) {
379 lldb::LanguageType lang_type =
380 (m_options.m_varformat_language == lldb::eLanguageTypeUnknown)
381 ? valobj.GetPreferredDisplayLanguage()
382 : m_options.m_varformat_language;
383 if (Language *lang_plugin = Language::FindPlugin(language: lang_type)) {
384 summary.assign(str: lang_plugin->GetNilReferenceSummaryString().str());
385 } else {
386 // We treat C as the fallback language rather than as a separate Language
387 // plugin.
388 summary.assign(s: "NULL");
389 }
390 } else if (IsUninitialized()) {
391 summary.assign(s: "<uninitialized>");
392 } else if (m_options.m_omit_summary_depth == 0) {
393 TypeSummaryImpl *entry = GetSummaryFormatter();
394 if (entry) {
395 valobj.GetSummaryAsCString(summary_ptr: entry, destination&: summary,
396 lang: m_options.m_varformat_language);
397 } else {
398 const char *sum_cstr =
399 valobj.GetSummaryAsCString(lang: m_options.m_varformat_language);
400 if (sum_cstr)
401 summary.assign(s: sum_cstr);
402 }
403 }
404}
405
406bool ValueObjectPrinter::PrintValueAndSummaryIfNeeded(bool &value_printed,
407 bool &summary_printed) {
408 bool error_printed = false;
409 if (ShouldPrintValueObject()) {
410 if (!CheckScopeIfNeeded())
411 m_error.assign(s: "out of scope");
412 if (m_error.empty()) {
413 GetValueSummaryError(value&: m_value, summary&: m_summary, error&: m_error);
414 }
415 if (m_error.size()) {
416 // we need to support scenarios in which it is actually fine for a value
417 // to have no type but - on the other hand - if we get an error *AND*
418 // have no type, we try to get out gracefully, since most often that
419 // combination means "could not resolve a type" and the default failure
420 // mode is quite ugly
421 if (!m_compiler_type.IsValid()) {
422 m_stream->Printf(format: " <could not resolve type>");
423 return false;
424 }
425
426 error_printed = true;
427 m_stream->Printf(format: " <%s>\n", m_error.c_str());
428 } else {
429 // Make sure we have a value and make sure the summary didn't specify
430 // that the value should not be printed - and do not print the value if
431 // this thing is nil (but show the value if the user passes a format
432 // explicitly)
433 TypeSummaryImpl *entry = GetSummaryFormatter();
434 ValueObject &valobj = GetMostSpecializedValue();
435 const bool has_nil_or_uninitialized_summary =
436 (IsNil() || IsUninitialized()) && !m_summary.empty();
437 if (!has_nil_or_uninitialized_summary && !m_value.empty() &&
438 (entry == nullptr ||
439 (entry->DoesPrintValue(valobj: &valobj) ||
440 m_options.m_format != eFormatDefault) ||
441 m_summary.empty()) &&
442 !m_options.m_hide_value) {
443 if (m_options.m_hide_pointer_value &&
444 IsPointerValue(type: valobj.GetCompilerType())) {
445 } else {
446 if (ShouldShowName())
447 m_stream->PutChar(ch: ' ');
448 m_stream->PutCString(cstr: m_value);
449 value_printed = true;
450 }
451 }
452
453 if (m_summary.size()) {
454 if (ShouldShowName() || value_printed)
455 m_stream->PutChar(ch: ' ');
456 m_stream->PutCString(cstr: m_summary);
457 summary_printed = true;
458 }
459 }
460 }
461 return !error_printed;
462}
463
464bool ValueObjectPrinter::PrintObjectDescriptionIfNeeded(bool value_printed,
465 bool summary_printed) {
466 if (ShouldPrintValueObject()) {
467 // let's avoid the overly verbose no description error for a nil thing
468 if (m_options.m_use_objc && !IsNil() && !IsUninitialized() &&
469 (!m_options.m_pointer_as_array)) {
470 if (!m_options.m_hide_value || ShouldShowName())
471 m_stream->Printf(format: " ");
472 const char *object_desc = nullptr;
473 if (value_printed || summary_printed)
474 object_desc = GetMostSpecializedValue().GetObjectDescription();
475 else
476 object_desc = GetDescriptionForDisplay();
477 if (object_desc && *object_desc) {
478 // If the description already ends with a \n don't add another one.
479 size_t object_end = strlen(s: object_desc) - 1;
480 if (object_desc[object_end] == '\n')
481 m_stream->Printf(format: "%s", object_desc);
482 else
483 m_stream->Printf(format: "%s\n", object_desc);
484 return true;
485 } else if (!value_printed && !summary_printed)
486 return true;
487 else
488 return false;
489 }
490 }
491 return true;
492}
493
494bool DumpValueObjectOptions::PointerDepth::CanAllowExpansion() const {
495 switch (m_mode) {
496 case Mode::Always:
497 case Mode::Default:
498 return m_count > 0;
499 case Mode::Never:
500 return false;
501 }
502 return false;
503}
504
505bool ValueObjectPrinter::ShouldPrintChildren(
506 DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
507 const bool is_ref = IsRef();
508 const bool is_ptr = IsPtr();
509 const bool is_uninit = IsUninitialized();
510
511 if (is_uninit)
512 return false;
513
514 // If we have reached the maximum depth we shouldn't print any more children.
515 if (HasReachedMaximumDepth())
516 return false;
517
518 // if the user has specified an element count, always print children as it is
519 // explicit user demand being honored
520 if (m_options.m_pointer_as_array)
521 return true;
522
523 if (m_options.m_use_objc)
524 return false;
525
526 bool print_children = true;
527 ValueObject &valobj = GetMostSpecializedValue();
528 if (TypeSummaryImpl *type_summary = GetSummaryFormatter())
529 print_children = type_summary->DoesPrintChildren(valobj: &valobj);
530
531 // We will show children for all concrete types. We won't show pointer
532 // contents unless a pointer depth has been specified. We won't reference
533 // contents unless the reference is the root object (depth of zero).
534
535 // Use a new temporary pointer depth in case we override the current
536 // pointer depth below...
537
538 if (is_ptr || is_ref) {
539 // We have a pointer or reference whose value is an address. Make sure
540 // that address is not NULL
541 AddressType ptr_address_type;
542 if (valobj.GetPointerValue(address_type: &ptr_address_type) == 0)
543 return false;
544
545 const bool is_root_level = m_curr_depth == 0;
546
547 if (is_ref && is_root_level && print_children) {
548 // If this is the root object (depth is zero) that we are showing and
549 // it is a reference, and no pointer depth has been supplied print out
550 // what it references. Don't do this at deeper depths otherwise we can
551 // end up with infinite recursion...
552 return true;
553 }
554
555 return curr_ptr_depth.CanAllowExpansion();
556 }
557
558 return print_children || m_summary.empty();
559}
560
561bool ValueObjectPrinter::ShouldExpandEmptyAggregates() {
562 TypeSummaryImpl *entry = GetSummaryFormatter();
563
564 if (!entry)
565 return true;
566
567 return entry->DoesPrintEmptyAggregates();
568}
569
570ValueObject &ValueObjectPrinter::GetValueObjectForChildrenGeneration() {
571 return GetMostSpecializedValue();
572}
573
574void ValueObjectPrinter::PrintChildrenPreamble(bool value_printed,
575 bool summary_printed) {
576 if (m_options.m_flat_output) {
577 if (ShouldPrintValueObject())
578 m_stream->EOL();
579 } else {
580 if (ShouldPrintValueObject()) {
581 if (IsRef()) {
582 m_stream->PutCString(cstr: ": ");
583 } else if (value_printed || summary_printed || ShouldShowName()) {
584 m_stream->PutChar(ch: ' ');
585 }
586 m_stream->PutCString(cstr: "{\n");
587 }
588 m_stream->IndentMore();
589 }
590}
591
592void ValueObjectPrinter::PrintChild(
593 ValueObjectSP child_sp,
594 const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
595 const uint32_t consumed_summary_depth = m_options.m_pointer_as_array ? 0 : 1;
596 const bool does_consume_ptr_depth =
597 ((IsPtr() && !m_options.m_pointer_as_array) || IsRef());
598
599 DumpValueObjectOptions child_options(m_options);
600 child_options.SetFormat(m_options.m_format)
601 .SetSummary()
602 .SetRootValueObjectName();
603 child_options.SetScopeChecked(true)
604 .SetHideName(m_options.m_hide_name)
605 .SetHideValue(m_options.m_hide_value)
606 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1
607 ? child_options.m_omit_summary_depth -
608 consumed_summary_depth
609 : 0)
610 .SetElementCount(0);
611
612 if (child_sp.get()) {
613 auto ptr_depth = curr_ptr_depth;
614 if (does_consume_ptr_depth)
615 ptr_depth = curr_ptr_depth.Decremented();
616
617 ValueObjectPrinter child_printer(*(child_sp.get()), m_stream, child_options,
618 ptr_depth, m_curr_depth + 1,
619 m_printed_instance_pointers);
620 child_printer.PrintValueObject();
621 }
622}
623
624uint32_t ValueObjectPrinter::GetMaxNumChildrenToPrint(bool &print_dotdotdot) {
625 ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
626
627 if (m_options.m_pointer_as_array)
628 return m_options.m_pointer_as_array.m_element_count;
629
630 size_t num_children = synth_valobj.GetNumChildren();
631 print_dotdotdot = false;
632 if (num_children) {
633 const size_t max_num_children = GetMostSpecializedValue()
634 .GetTargetSP()
635 ->GetMaximumNumberOfChildrenToDisplay();
636
637 if (num_children > max_num_children && !m_options.m_ignore_cap) {
638 print_dotdotdot = true;
639 return max_num_children;
640 }
641 }
642 return num_children;
643}
644
645void ValueObjectPrinter::PrintChildrenPostamble(bool print_dotdotdot) {
646 if (!m_options.m_flat_output) {
647 if (print_dotdotdot) {
648 GetMostSpecializedValue()
649 .GetTargetSP()
650 ->GetDebugger()
651 .GetCommandInterpreter()
652 .ChildrenTruncated();
653 m_stream->Indent(s: "...\n");
654 }
655 m_stream->IndentLess();
656 m_stream->Indent(s: "}\n");
657 }
658}
659
660bool ValueObjectPrinter::ShouldPrintEmptyBrackets(bool value_printed,
661 bool summary_printed) {
662 ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
663
664 if (!IsAggregate())
665 return false;
666
667 if (!m_options.m_reveal_empty_aggregates) {
668 if (value_printed || summary_printed)
669 return false;
670 }
671
672 if (synth_valobj.MightHaveChildren())
673 return true;
674
675 if (m_val_summary_ok)
676 return false;
677
678 return true;
679}
680
681static constexpr size_t PhysicalIndexForLogicalIndex(size_t base, size_t stride,
682 size_t logical) {
683 return base + logical * stride;
684}
685
686ValueObjectSP ValueObjectPrinter::GenerateChild(ValueObject &synth_valobj,
687 size_t idx) {
688 if (m_options.m_pointer_as_array) {
689 // if generating pointer-as-array children, use GetSyntheticArrayMember
690 return synth_valobj.GetSyntheticArrayMember(
691 index: PhysicalIndexForLogicalIndex(
692 base: m_options.m_pointer_as_array.m_base_element,
693 stride: m_options.m_pointer_as_array.m_stride, logical: idx),
694 can_create: true);
695 } else {
696 // otherwise, do the usual thing
697 return synth_valobj.GetChildAtIndex(idx);
698 }
699}
700
701void ValueObjectPrinter::PrintChildren(
702 bool value_printed, bool summary_printed,
703 const DumpValueObjectOptions::PointerDepth &curr_ptr_depth) {
704 ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
705
706 bool print_dotdotdot = false;
707 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
708 if (num_children) {
709 bool any_children_printed = false;
710
711 for (size_t idx = 0; idx < num_children; ++idx) {
712 if (ValueObjectSP child_sp = GenerateChild(synth_valobj, idx)) {
713 if (m_options.m_child_printing_decider &&
714 !m_options.m_child_printing_decider(child_sp->GetName()))
715 continue;
716 if (!any_children_printed) {
717 PrintChildrenPreamble(value_printed, summary_printed);
718 any_children_printed = true;
719 }
720 PrintChild(child_sp, curr_ptr_depth);
721 }
722 }
723
724 if (any_children_printed)
725 PrintChildrenPostamble(print_dotdotdot);
726 else {
727 if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
728 if (ShouldPrintValueObject())
729 m_stream->PutCString(cstr: " {}\n");
730 else
731 m_stream->EOL();
732 } else
733 m_stream->EOL();
734 }
735 } else if (ShouldPrintEmptyBrackets(value_printed, summary_printed)) {
736 // Aggregate, no children...
737 if (ShouldPrintValueObject()) {
738 // if it has a synthetic value, then don't print {}, the synthetic
739 // children are probably only being used to vend a value
740 if (GetMostSpecializedValue().DoesProvideSyntheticValue() ||
741 !ShouldExpandEmptyAggregates())
742 m_stream->PutCString(cstr: "\n");
743 else
744 m_stream->PutCString(cstr: " {}\n");
745 }
746 } else {
747 if (ShouldPrintValueObject())
748 m_stream->EOL();
749 }
750}
751
752bool ValueObjectPrinter::PrintChildrenOneLiner(bool hide_names) {
753 ValueObject &synth_valobj = GetValueObjectForChildrenGeneration();
754
755 bool print_dotdotdot = false;
756 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
757
758 if (num_children) {
759 m_stream->PutChar(ch: '(');
760
761 bool did_print_children = false;
762 for (uint32_t idx = 0; idx < num_children; ++idx) {
763 lldb::ValueObjectSP child_sp(synth_valobj.GetChildAtIndex(idx));
764 if (child_sp)
765 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
766 dynValue: m_options.m_use_dynamic, synthValue: m_options.m_use_synthetic);
767 if (child_sp) {
768 if (m_options.m_child_printing_decider &&
769 !m_options.m_child_printing_decider(child_sp->GetName()))
770 continue;
771 if (idx && did_print_children)
772 m_stream->PutCString(cstr: ", ");
773 did_print_children = true;
774 if (!hide_names) {
775 const char *name = child_sp.get()->GetName().AsCString();
776 if (name && *name) {
777 m_stream->PutCString(cstr: name);
778 m_stream->PutCString(cstr: " = ");
779 }
780 }
781 child_sp->DumpPrintableRepresentation(
782 s&: *m_stream, val_obj_display: ValueObject::eValueObjectRepresentationStyleSummary,
783 custom_format: m_options.m_format,
784 special: ValueObject::PrintableRepresentationSpecialCases::eDisable);
785 }
786 }
787
788 if (print_dotdotdot)
789 m_stream->PutCString(cstr: ", ...)");
790 else
791 m_stream->PutChar(ch: ')');
792 }
793 return true;
794}
795
796void ValueObjectPrinter::PrintChildrenIfNeeded(bool value_printed,
797 bool summary_printed) {
798 PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
799 ValueObject &valobj = GetMostSpecializedValue();
800
801 DumpValueObjectOptions::PointerDepth curr_ptr_depth = m_ptr_depth;
802 const bool print_children = ShouldPrintChildren(curr_ptr_depth);
803 const bool print_oneline =
804 (curr_ptr_depth.CanAllowExpansion() || m_options.m_show_types ||
805 !m_options.m_allow_oneliner_mode || m_options.m_flat_output ||
806 (m_options.m_pointer_as_array) || m_options.m_show_location)
807 ? false
808 : DataVisualization::ShouldPrintAsOneLiner(valobj);
809 if (print_children && IsInstancePointer()) {
810 uint64_t instance_ptr_value = valobj.GetValueAsUnsigned(fail_value: 0);
811 if (m_printed_instance_pointers->count(x: instance_ptr_value)) {
812 // We already printed this instance-is-pointer thing, so don't expand it.
813 m_stream->PutCString(cstr: " {...}\n");
814 return;
815 } else {
816 // Remember this guy for future reference.
817 m_printed_instance_pointers->emplace(args&: instance_ptr_value);
818 }
819 }
820
821 if (print_children) {
822 if (print_oneline) {
823 m_stream->PutChar(ch: ' ');
824 PrintChildrenOneLiner(hide_names: false);
825 m_stream->EOL();
826 } else
827 PrintChildren(value_printed, summary_printed, curr_ptr_depth);
828 } else if (HasReachedMaximumDepth() && IsAggregate() &&
829 ShouldPrintValueObject()) {
830 m_stream->PutCString(cstr: "{...}\n");
831 // The maximum child depth has been reached. If `m_max_depth` is the default
832 // (i.e. the user has _not_ customized it), then lldb presents a warning to
833 // the user. The warning tells the user that the limit has been reached, but
834 // more importantly tells them how to expand the limit if desired.
835 if (m_options.m_max_depth_is_default)
836 valobj.GetTargetSP()
837 ->GetDebugger()
838 .GetCommandInterpreter()
839 .SetReachedMaximumDepth();
840 } else
841 m_stream->EOL();
842}
843
844bool ValueObjectPrinter::HasReachedMaximumDepth() {
845 return m_curr_depth >= m_options.m_max_depth;
846}
847
848bool ValueObjectPrinter::ShouldShowName() const {
849 if (m_curr_depth == 0)
850 return !m_options.m_hide_root_name && !m_options.m_hide_name;
851 return !m_options.m_hide_name;
852}
853

source code of lldb/source/DataFormatters/ValueObjectPrinter.cpp