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