1 //===-- ValueObjectPrinter.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/ValueObjectPrinter.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/DataFormatters/DataVisualization.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Target/Language.h"
21 #include "lldb/Target/Target.h"
24 using namespace lldb_private;
26 ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
31 DumpValueObjectOptions options(*valobj);
32 Init (valobj,s,options,m_options.m_max_ptr_depth,0, nullptr);
36 DumpValueObjectOptions options;
37 Init (valobj,s,options,m_options.m_max_ptr_depth,0, nullptr);
41 ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
43 const DumpValueObjectOptions& options)
45 Init(valobj,s,options,m_options.m_max_ptr_depth,0, nullptr);
48 ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
50 const DumpValueObjectOptions& options,
51 const DumpValueObjectOptions::PointerDepth& ptr_depth,
53 InstancePointersSetSP printed_instance_pointers)
55 Init(valobj,s,options,ptr_depth,curr_depth, printed_instance_pointers);
59 ValueObjectPrinter::Init (ValueObject* valobj,
61 const DumpValueObjectOptions& options,
62 const DumpValueObjectOptions::PointerDepth& ptr_depth,
64 InstancePointersSetSP printed_instance_pointers)
66 m_orig_valobj = valobj;
70 m_ptr_depth = ptr_depth;
71 m_curr_depth = curr_depth;
72 assert (m_orig_valobj && "cannot print a NULL ValueObject");
73 assert (m_stream && "cannot print to a NULL Stream");
74 m_should_print = eLazyBoolCalculate;
75 m_is_nil = eLazyBoolCalculate;
76 m_is_uninit = eLazyBoolCalculate;
77 m_is_ptr = eLazyBoolCalculate;
78 m_is_ref = eLazyBoolCalculate;
79 m_is_aggregate = eLazyBoolCalculate;
80 m_is_instance_ptr = eLazyBoolCalculate;
81 m_summary_formatter = {nullptr,false};
85 m_val_summary_ok = false;
86 m_printed_instance_pointers = printed_instance_pointers ? printed_instance_pointers : InstancePointersSetSP(new InstancePointersSet());
90 ValueObjectPrinter::PrintValueObject ()
92 if (!GetMostSpecializedValue () || m_valobj == nullptr)
95 if (ShouldPrintValueObject())
97 PrintValidationMarkerIfNeeded();
99 PrintLocationIfNeeded();
105 bool value_printed = false;
106 bool summary_printed = false;
108 m_val_summary_ok = PrintValueAndSummaryIfNeeded (value_printed,summary_printed);
110 if (m_val_summary_ok)
111 PrintChildrenIfNeeded (value_printed, summary_printed);
115 PrintValidationErrorIfNeeded();
121 ValueObjectPrinter::GetMostSpecializedValue ()
125 bool update_success = m_orig_valobj->UpdateValueIfNeeded (true);
128 m_valobj = m_orig_valobj;
132 if (m_orig_valobj->IsDynamic())
134 if (m_options.m_use_dynamic == eNoDynamicValues)
136 ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
138 m_valobj = static_value;
140 m_valobj = m_orig_valobj;
143 m_valobj = m_orig_valobj;
147 if (m_options.m_use_dynamic != eNoDynamicValues)
149 ValueObject *dynamic_value = m_orig_valobj->GetDynamicValue(m_options.m_use_dynamic).get();
151 m_valobj = dynamic_value;
153 m_valobj = m_orig_valobj;
156 m_valobj = m_orig_valobj;
159 if (m_valobj->IsSynthetic())
161 if (m_options.m_use_synthetic == false)
163 ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
165 m_valobj = non_synthetic;
170 if (m_options.m_use_synthetic == true)
172 ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
174 m_valobj = synthetic;
178 m_compiler_type = m_valobj->GetCompilerType();
179 m_type_flags = m_compiler_type.GetTypeInfo ();
184 ValueObjectPrinter::GetDescriptionForDisplay ()
186 const char* str = m_valobj->GetObjectDescription();
188 str = m_valobj->GetSummaryAsCString();
190 str = m_valobj->GetValueAsCString();
195 ValueObjectPrinter::GetRootNameForDisplay (const char* if_fail)
197 const char *root_valobj_name = m_options.m_root_valobj_name.empty() ?
198 m_valobj->GetName().AsCString() :
199 m_options.m_root_valobj_name.c_str();
200 return root_valobj_name ? root_valobj_name : if_fail;
204 ValueObjectPrinter::ShouldPrintValueObject ()
206 if (m_should_print == eLazyBoolCalculate)
207 m_should_print = (m_options.m_flat_output == false || m_type_flags.Test (eTypeHasValue)) ? eLazyBoolYes : eLazyBoolNo;
208 return m_should_print == eLazyBoolYes;
212 ValueObjectPrinter::IsNil ()
214 if (m_is_nil == eLazyBoolCalculate)
215 m_is_nil = m_valobj->IsNilReference() ? eLazyBoolYes : eLazyBoolNo;
216 return m_is_nil == eLazyBoolYes;
220 ValueObjectPrinter::IsUninitialized ()
222 if (m_is_uninit == eLazyBoolCalculate)
223 m_is_uninit = m_valobj->IsUninitializedReference() ? eLazyBoolYes : eLazyBoolNo;
224 return m_is_uninit == eLazyBoolYes;
228 ValueObjectPrinter::IsPtr ()
230 if (m_is_ptr == eLazyBoolCalculate)
231 m_is_ptr = m_type_flags.Test (eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
232 return m_is_ptr == eLazyBoolYes;
236 ValueObjectPrinter::IsRef ()
238 if (m_is_ref == eLazyBoolCalculate)
239 m_is_ref = m_type_flags.Test (eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
240 return m_is_ref == eLazyBoolYes;
244 ValueObjectPrinter::IsAggregate ()
246 if (m_is_aggregate == eLazyBoolCalculate)
247 m_is_aggregate = m_type_flags.Test (eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
248 return m_is_aggregate == eLazyBoolYes;
252 ValueObjectPrinter::IsInstancePointer ()
254 // you need to do this check on the value's clang type
255 if (m_is_instance_ptr == eLazyBoolCalculate)
256 m_is_instance_ptr = (m_valobj->GetValue().GetCompilerType().GetTypeInfo() & eTypeInstanceIsPointer) != 0 ? eLazyBoolYes : eLazyBoolNo;
257 if ((eLazyBoolYes == m_is_instance_ptr) && m_valobj->IsBaseClass())
258 m_is_instance_ptr = eLazyBoolNo;
259 return m_is_instance_ptr == eLazyBoolYes;
263 ValueObjectPrinter::PrintLocationIfNeeded ()
265 if (m_options.m_show_location)
267 m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
274 ValueObjectPrinter::PrintDecl ()
276 bool show_type = true;
277 // if we are at the root-level and been asked to hide the root's type, then hide it
278 if (m_curr_depth == 0 && m_options.m_hide_root_type)
281 // otherwise decide according to the usual rules (asked to show types - always at the root level)
282 show_type = m_options.m_show_types || (m_curr_depth == 0 && !m_options.m_flat_output);
284 StreamString typeName;
286 // always show the type at the root level if it is invalid
289 // Some ValueObjects don't have types (like registers sets). Only print
290 // the type if there is one to print
291 ConstString type_name;
292 if (m_compiler_type.IsValid())
294 if (m_options.m_use_type_display_name)
295 type_name = m_valobj->GetDisplayTypeName();
297 type_name = m_valobj->GetQualifiedTypeName();
301 // only show an invalid type name if the user explicitly triggered show_type
302 if (m_options.m_show_types)
303 type_name = ConstString("<invalid type>");
310 std::string type_name_str(type_name.GetCString());
311 if (m_options.m_hide_pointer_value)
313 for(auto iter = type_name_str.find(" *");
314 iter != std::string::npos;
315 iter = type_name_str.find(" *"))
317 type_name_str.erase(iter, 2);
320 typeName.Printf("%s", type_name_str.c_str());
324 StreamString varName;
326 if (m_options.m_flat_output)
328 // If we are showing types, also qualify the C++ base classes
329 const bool qualify_cxx_base_classes = show_type;
330 if (!m_options.m_hide_name)
332 m_valobj->GetExpressionPath(varName, qualify_cxx_base_classes);
335 else if (!m_options.m_hide_name)
337 const char *name_cstr = GetRootNameForDisplay("");
338 varName.Printf ("%s", name_cstr);
341 bool decl_printed = false;
342 if (!m_options.m_decl_printing_helper)
344 // if the user didn't give us a custom helper, pick one based upon the language, either the one that this printer is bound to, or the preferred one for the ValueObject
345 lldb::LanguageType lang_type = (m_options.m_varformat_language == lldb::eLanguageTypeUnknown) ? m_valobj->GetPreferredDisplayLanguage() : m_options.m_varformat_language;
346 if (Language *lang_plugin = Language::FindPlugin(lang_type))
348 m_options.m_decl_printing_helper = lang_plugin->GetDeclPrintingHelper();
352 if (m_options.m_decl_printing_helper)
354 ConstString type_name_cstr(typeName.GetData());
355 ConstString var_name_cstr(varName.GetData());
357 StreamString dest_stream;
358 if (m_options.m_decl_printing_helper (type_name_cstr,
364 m_stream->Printf("%s", dest_stream.GetData());
368 // if the helper failed, or there is none, do a default thing
371 if (typeName.GetSize())
372 m_stream->Printf("(%s) ", typeName.GetData());
373 if (varName.GetSize())
374 m_stream->Printf("%s =", varName.GetData());
375 else if (!m_options.m_hide_name)
376 m_stream->Printf(" =");
381 ValueObjectPrinter::CheckScopeIfNeeded ()
383 if (m_options.m_scope_already_checked)
385 return m_valobj->IsInScope();
389 ValueObjectPrinter::GetSummaryFormatter (bool null_if_omitted)
391 if (m_summary_formatter.second == false)
393 TypeSummaryImpl* entry = m_options.m_summary_sp ? m_options.m_summary_sp.get() : m_valobj->GetSummaryFormat().get();
395 if (m_options.m_omit_summary_depth > 0)
397 m_summary_formatter.first = entry;
398 m_summary_formatter.second = true;
400 if (m_options.m_omit_summary_depth > 0 && null_if_omitted)
402 return m_summary_formatter.first;
406 IsPointerValue (const CompilerType &type)
408 Flags type_flags(type.GetTypeInfo());
409 if (type_flags.AnySet(eTypeInstanceIsPointer | eTypeIsPointer))
410 return type_flags.AllClear(eTypeIsBuiltIn);
415 ValueObjectPrinter::GetValueSummaryError (std::string& value,
416 std::string& summary,
419 if (m_options.m_format != eFormatDefault && m_options.m_format != m_valobj->GetFormat())
421 m_valobj->GetValueAsCString(m_options.m_format,
426 const char* val_cstr = m_valobj->GetValueAsCString();
428 value.assign(val_cstr);
430 const char* err_cstr = m_valobj->GetError().AsCString();
432 error.assign(err_cstr);
434 if (ShouldPrintValueObject())
437 summary.assign("nil");
438 else if (IsUninitialized())
439 summary.assign("<uninitialized>");
440 else if (m_options.m_omit_summary_depth == 0)
442 TypeSummaryImpl* entry = GetSummaryFormatter();
444 m_valobj->GetSummaryAsCString(entry, summary, m_options.m_varformat_language);
447 const char* sum_cstr = m_valobj->GetSummaryAsCString(m_options.m_varformat_language);
449 summary.assign(sum_cstr);
456 ValueObjectPrinter::PrintValueAndSummaryIfNeeded (bool& value_printed,
457 bool& summary_printed)
459 bool error_printed = false;
460 if (ShouldPrintValueObject())
462 if (!CheckScopeIfNeeded())
463 m_error.assign("out of scope");
466 GetValueSummaryError(m_value, m_summary, m_error);
470 // we need to support scenarios in which it is actually fine for a value to have no type
471 // but - on the other hand - if we get an error *AND* have no type, we try to get out
472 // gracefully, since most often that combination means "could not resolve a type"
473 // and the default failure mode is quite ugly
474 if (!m_compiler_type.IsValid())
476 m_stream->Printf(" <could not resolve type>");
480 error_printed = true;
481 m_stream->Printf (" <%s>\n", m_error.c_str());
485 // Make sure we have a value and make sure the summary didn't
486 // specify that the value should not be printed - and do not print
487 // the value if this thing is nil
488 // (but show the value if the user passes a format explicitly)
489 TypeSummaryImpl* entry = GetSummaryFormatter();
490 if (!IsNil() && !IsUninitialized() && !m_value.empty() && (entry == NULL || (entry->DoesPrintValue(m_valobj) || m_options.m_format != eFormatDefault) || m_summary.empty()) && !m_options.m_hide_value)
492 if (m_options.m_hide_pointer_value && IsPointerValue(m_valobj->GetCompilerType())) {}
495 m_stream->Printf(" %s", m_value.c_str());
496 value_printed = true;
500 if (m_summary.size())
502 m_stream->Printf(" %s", m_summary.c_str());
503 summary_printed = true;
507 return !error_printed;
511 ValueObjectPrinter::PrintObjectDescriptionIfNeeded (bool value_printed,
512 bool summary_printed)
514 if (ShouldPrintValueObject())
516 // let's avoid the overly verbose no description error for a nil thing
517 if (m_options.m_use_objc && !IsNil() && !IsUninitialized())
519 if (!m_options.m_hide_value || !m_options.m_hide_name)
520 m_stream->Printf(" ");
521 const char *object_desc = nullptr;
522 if (value_printed || summary_printed)
523 object_desc = m_valobj->GetObjectDescription();
525 object_desc = GetDescriptionForDisplay();
526 if (object_desc && *object_desc)
528 m_stream->Printf("%s\n", object_desc);
531 else if (value_printed == false && summary_printed == false)
541 DumpValueObjectOptions::PointerDepth::CanAllowExpansion (bool is_root,
542 TypeSummaryImpl* entry,
544 const std::string& summary)
549 return (m_count > 0);
554 m_count = std::min<decltype(m_count)>(m_count,1);
556 case Mode::Formatters:
557 if (!entry || entry->DoesPrintChildren(valobj) || summary.empty())
565 DumpValueObjectOptions::PointerDepth::CanAllowExpansion () const
571 case Mode::Formatters:
572 return (m_count > 0);
580 ValueObjectPrinter::ShouldPrintChildren (bool is_failed_description,
581 DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
583 const bool is_ref = IsRef ();
584 const bool is_ptr = IsPtr ();
585 const bool is_uninit = IsUninitialized();
590 TypeSummaryImpl* entry = GetSummaryFormatter();
592 if (m_options.m_use_objc)
595 if (is_failed_description || m_curr_depth < m_options.m_max_depth)
597 // We will show children for all concrete types. We won't show
598 // pointer contents unless a pointer depth has been specified.
599 // We won't reference contents unless the reference is the
600 // root object (depth of zero).
602 // Use a new temporary pointer depth in case we override the
603 // current pointer depth below...
605 if (is_ptr || is_ref)
607 // We have a pointer or reference whose value is an address.
608 // Make sure that address is not NULL
609 AddressType ptr_address_type;
610 if (m_valobj->GetPointerValue (&ptr_address_type) == 0)
613 const bool is_root_level = m_curr_depth == 0;
618 // If this is the root object (depth is zero) that we are showing
619 // and it is a reference, and no pointer depth has been supplied
620 // print out what it references. Don't do this at deeper depths
621 // otherwise we can end up with infinite recursion...
625 return curr_ptr_depth.CanAllowExpansion(false, entry, m_valobj, m_summary);
628 return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
634 ValueObjectPrinter::ShouldExpandEmptyAggregates ()
636 TypeSummaryImpl* entry = GetSummaryFormatter();
641 return entry->DoesPrintEmptyAggregates();
645 ValueObjectPrinter::GetValueObjectForChildrenGeneration ()
651 ValueObjectPrinter::PrintChildrenPreamble ()
653 if (m_options.m_flat_output)
655 if (ShouldPrintValueObject())
660 if (ShouldPrintValueObject())
661 m_stream->PutCString(IsRef () ? ": {\n" : " {\n");
662 m_stream->IndentMore();
667 ValueObjectPrinter::PrintChild (ValueObjectSP child_sp,
668 const DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
670 DumpValueObjectOptions child_options(m_options);
671 child_options.SetFormat(m_options.m_format).SetSummary().SetRootValueObjectName();
672 child_options.SetScopeChecked(true).SetHideName(m_options.m_hide_name).SetHideValue(m_options.m_hide_value)
673 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
677 ValueObjectPrinter child_printer(child_sp.get(),
680 (IsPtr() || IsRef()) ? --curr_ptr_depth : curr_ptr_depth,
682 m_printed_instance_pointers);
683 child_printer.PrintValueObject();
688 ValueObjectPrinter::GetMaxNumChildrenToPrint (bool& print_dotdotdot)
690 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
692 size_t num_children = synth_m_valobj->GetNumChildren();
693 print_dotdotdot = false;
696 const size_t max_num_children = m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
698 if (num_children > max_num_children && !m_options.m_ignore_cap)
700 print_dotdotdot = true;
701 return max_num_children;
708 ValueObjectPrinter::PrintChildrenPostamble (bool print_dotdotdot)
710 if (!m_options.m_flat_output)
714 m_valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
715 m_stream->Indent("...\n");
717 m_stream->IndentLess();
718 m_stream->Indent("}\n");
723 ValueObjectPrinter::ShouldPrintEmptyBrackets (bool value_printed,
724 bool summary_printed)
726 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
731 if (m_options.m_reveal_empty_aggregates == false)
733 if (value_printed || summary_printed)
737 if (synth_m_valobj->MightHaveChildren())
740 if (m_val_summary_ok)
747 ValueObjectPrinter::PrintChildren (bool value_printed,
748 bool summary_printed,
749 const DumpValueObjectOptions::PointerDepth& curr_ptr_depth)
751 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
753 bool print_dotdotdot = false;
754 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
757 bool any_children_printed = false;
759 for (size_t idx=0; idx<num_children; ++idx)
761 ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
764 if (!any_children_printed)
766 PrintChildrenPreamble ();
767 any_children_printed = true;
769 PrintChild (child_sp, curr_ptr_depth);
773 if (any_children_printed)
774 PrintChildrenPostamble (print_dotdotdot);
777 if (ShouldPrintEmptyBrackets(value_printed, summary_printed))
779 if (ShouldPrintValueObject())
780 m_stream->PutCString(" {}\n");
788 else if (ShouldPrintEmptyBrackets(value_printed, summary_printed))
790 // Aggregate, no children...
791 if (ShouldPrintValueObject())
793 // if it has a synthetic value, then don't print {}, the synthetic children are probably only being used to vend a value
794 if (m_valobj->DoesProvideSyntheticValue() || !ShouldExpandEmptyAggregates())
795 m_stream->PutCString( "\n");
797 m_stream->PutCString(" {}\n");
802 if (ShouldPrintValueObject())
808 ValueObjectPrinter::PrintChildrenOneLiner (bool hide_names)
810 if (!GetMostSpecializedValue () || m_valobj == nullptr)
813 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
815 bool print_dotdotdot = false;
816 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
820 m_stream->PutChar('(');
822 for (uint32_t idx=0; idx<num_children; ++idx)
824 lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
826 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(m_options.m_use_dynamic, m_options.m_use_synthetic);
830 m_stream->PutCString(", ");
833 const char* name = child_sp.get()->GetName().AsCString();
836 m_stream->PutCString(name);
837 m_stream->PutCString(" = ");
840 child_sp->DumpPrintableRepresentation(*m_stream,
841 ValueObject::eValueObjectRepresentationStyleSummary,
843 ValueObject::ePrintableRepresentationSpecialCasesDisable);
848 m_stream->PutCString(", ...)");
850 m_stream->PutChar(')');
856 ValueObjectPrinter::PrintChildrenIfNeeded (bool value_printed,
857 bool summary_printed)
859 // this flag controls whether we tried to display a description for this object and failed
860 // if that happens, we want to display the children, if any
861 bool is_failed_description = !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
863 auto curr_ptr_depth = m_ptr_depth;
864 bool print_children = ShouldPrintChildren (is_failed_description,curr_ptr_depth);
865 bool print_oneline = (curr_ptr_depth.CanAllowExpansion() ||
866 m_options.m_show_types ||
867 !m_options.m_allow_oneliner_mode ||
868 m_options.m_flat_output ||
869 m_options.m_show_location) ? false : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
870 bool is_instance_ptr = IsInstancePointer();
871 uint64_t instance_ptr_value = LLDB_INVALID_ADDRESS;
873 if (print_children && is_instance_ptr)
875 instance_ptr_value = m_valobj->GetValueAsUnsigned(0);
876 if (m_printed_instance_pointers->count(instance_ptr_value))
878 // we already printed this instance-is-pointer thing, so don't expand it
879 m_stream->PutCString(" {...}\n");
881 // we're done here - get out fast
885 m_printed_instance_pointers->emplace(instance_ptr_value); // remember this guy for future reference
892 m_stream->PutChar(' ');
893 PrintChildrenOneLiner (false);
897 PrintChildren (value_printed, summary_printed, curr_ptr_depth);
899 else if (m_curr_depth >= m_options.m_max_depth && IsAggregate() && ShouldPrintValueObject())
901 m_stream->PutCString("{...}\n");
908 ValueObjectPrinter::ShouldPrintValidation ()
910 return m_options.m_run_validator;
914 ValueObjectPrinter::PrintValidationMarkerIfNeeded ()
916 if (!ShouldPrintValidation())
919 m_validation = m_valobj->GetValidationStatus();
921 if (TypeValidatorResult::Failure == m_validation.first)
923 m_stream->Printf("! ");
931 ValueObjectPrinter::PrintValidationErrorIfNeeded ()
933 if (!ShouldPrintValidation())
936 if (TypeValidatorResult::Success == m_validation.first)
939 if (m_validation.second.empty())
940 m_validation.second.assign("unknown error");
942 m_stream->Printf(" ! validation error: %s", m_validation.second.c_str());