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/Debugger.h"
17 #include "lldb/DataFormatters/DataVisualization.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Target/Target.h"
22 using namespace lldb_private;
24 ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
26 const DumpValueObjectOptions& options)
28 Init(valobj,s,options,options.m_max_ptr_depth,0);
31 ValueObjectPrinter::ValueObjectPrinter (ValueObject* valobj,
33 const DumpValueObjectOptions& options,
37 Init(valobj,s,options,ptr_depth,curr_depth);
41 ValueObjectPrinter::Init (ValueObject* valobj,
43 const DumpValueObjectOptions& options,
47 m_orig_valobj = valobj;
50 this->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_ptr = eLazyBoolCalculate;
58 m_is_ref = eLazyBoolCalculate;
59 m_is_aggregate = eLazyBoolCalculate;
60 m_summary_formatter = {nullptr,false};
67 ValueObjectPrinter::PrintValueObject ()
69 if (!GetMostSpecializedValue () || m_valobj == nullptr)
72 if (ShouldPrintValueObject())
74 PrintValidationMarkerIfNeeded();
76 PrintLocationIfNeeded();
79 bool show_type = PrintTypeIfNeeded();
81 PrintNameIfNeeded(show_type);
84 bool value_printed = false;
85 bool summary_printed = false;
87 bool val_summary_ok = PrintValueAndSummaryIfNeeded (value_printed,summary_printed);
90 PrintChildrenIfNeeded (value_printed, summary_printed);
94 PrintValidationErrorIfNeeded();
100 ValueObjectPrinter::GetMostSpecializedValue ()
104 bool update_success = m_orig_valobj->UpdateValueIfNeeded (true);
107 m_valobj = m_orig_valobj;
111 if (m_orig_valobj->IsDynamic())
113 if (options.m_use_dynamic == eNoDynamicValues)
115 ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
117 m_valobj = static_value;
119 m_valobj = m_orig_valobj;
122 m_valobj = m_orig_valobj;
126 if (options.m_use_dynamic != eNoDynamicValues)
128 ValueObject *dynamic_value = m_orig_valobj->GetDynamicValue(options.m_use_dynamic).get();
130 m_valobj = dynamic_value;
132 m_valobj = m_orig_valobj;
135 m_valobj = m_orig_valobj;
138 if (m_valobj->IsSynthetic())
140 if (options.m_use_synthetic == false)
142 ValueObject *non_synthetic = m_valobj->GetNonSyntheticValue().get();
144 m_valobj = non_synthetic;
149 if (options.m_use_synthetic == true)
151 ValueObject *synthetic = m_valobj->GetSyntheticValue().get();
153 m_valobj = synthetic;
157 m_clang_type = m_valobj->GetClangType();
158 m_type_flags = m_clang_type.GetTypeInfo ();
163 ValueObjectPrinter::GetDescriptionForDisplay ()
165 const char* str = m_valobj->GetObjectDescription();
167 str = m_valobj->GetSummaryAsCString();
169 str = m_valobj->GetValueAsCString();
174 ValueObjectPrinter::GetRootNameForDisplay (const char* if_fail)
176 const char *root_valobj_name = options.m_root_valobj_name.empty() ?
177 m_valobj->GetName().AsCString() :
178 options.m_root_valobj_name.c_str();
179 return root_valobj_name ? root_valobj_name : if_fail;
183 ValueObjectPrinter::ShouldPrintValueObject ()
185 if (m_should_print == eLazyBoolCalculate)
186 m_should_print = (options.m_flat_output == false || m_type_flags.Test (eTypeHasValue)) ? eLazyBoolYes : eLazyBoolNo;
187 return m_should_print == eLazyBoolYes;
191 ValueObjectPrinter::IsNil ()
193 if (m_is_nil == eLazyBoolCalculate)
194 m_is_nil = m_valobj->IsObjCNil() ? eLazyBoolYes : eLazyBoolNo;
195 return m_is_nil == eLazyBoolYes;
199 ValueObjectPrinter::IsPtr ()
201 if (m_is_ptr == eLazyBoolCalculate)
202 m_is_ptr = m_type_flags.Test (eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
203 return m_is_ptr == eLazyBoolYes;
207 ValueObjectPrinter::IsRef ()
209 if (m_is_ref == eLazyBoolCalculate)
210 m_is_ref = m_type_flags.Test (eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
211 return m_is_ref == eLazyBoolYes;
215 ValueObjectPrinter::IsAggregate ()
217 if (m_is_aggregate == eLazyBoolCalculate)
218 m_is_aggregate = m_type_flags.Test (eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
219 return m_is_aggregate == eLazyBoolYes;
223 ValueObjectPrinter::PrintLocationIfNeeded ()
225 if (options.m_show_location)
227 m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
234 ValueObjectPrinter::PrintTypeIfNeeded ()
236 bool show_type = true;
237 // if we are at the root-level and been asked to hide the root's type, then hide it
238 if (m_curr_depth == 0 && options.m_hide_root_type)
241 // otherwise decide according to the usual rules (asked to show types - always at the root level)
242 show_type = options.m_show_types || (m_curr_depth == 0 && !options.m_flat_output);
246 // Some ValueObjects don't have types (like registers sets). Only print
247 // the type if there is one to print
248 ConstString type_name;
249 if (options.m_use_type_display_name)
250 type_name = m_valobj->GetDisplayTypeName();
252 type_name = m_valobj->GetQualifiedTypeName();
254 m_stream->Printf("(%s) ", type_name.GetCString());
262 ValueObjectPrinter::PrintNameIfNeeded (bool show_type)
264 if (options.m_flat_output)
266 // If we are showing types, also qualify the C++ base classes
267 const bool qualify_cxx_base_classes = show_type;
268 if (!options.m_hide_name)
270 m_valobj->GetExpressionPath(*m_stream, qualify_cxx_base_classes);
271 m_stream->PutCString(" =");
275 else if (!options.m_hide_name)
277 const char *name_cstr = GetRootNameForDisplay("");
278 m_stream->Printf ("%s =", name_cstr);
285 ValueObjectPrinter::CheckScopeIfNeeded ()
287 if (options.m_scope_already_checked)
289 return m_valobj->IsInScope();
293 ValueObjectPrinter::GetSummaryFormatter ()
295 if (m_summary_formatter.second == false)
297 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : m_valobj->GetSummaryFormat().get();
299 if (options.m_omit_summary_depth > 0)
301 m_summary_formatter.first = entry;
302 m_summary_formatter.second = true;
304 return m_summary_formatter.first;
308 ValueObjectPrinter::GetValueSummaryError (std::string& value,
309 std::string& summary,
312 if (options.m_format != eFormatDefault && options.m_format != m_valobj->GetFormat())
314 m_valobj->GetValueAsCString(options.m_format,
319 const char* val_cstr = m_valobj->GetValueAsCString();
321 value.assign(val_cstr);
323 const char* err_cstr = m_valobj->GetError().AsCString();
325 error.assign(err_cstr);
327 if (ShouldPrintValueObject())
330 summary.assign("nil");
331 else if (options.m_omit_summary_depth == 0)
333 TypeSummaryImpl* entry = GetSummaryFormatter();
335 m_valobj->GetSummaryAsCString(entry, summary);
338 const char* sum_cstr = m_valobj->GetSummaryAsCString();
340 summary.assign(sum_cstr);
347 ValueObjectPrinter::PrintValueAndSummaryIfNeeded (bool& value_printed,
348 bool& summary_printed)
350 bool error_printed = false;
351 if (ShouldPrintValueObject())
353 if (!CheckScopeIfNeeded())
354 m_error.assign("out of scope");
357 GetValueSummaryError(m_value, m_summary, m_error);
361 error_printed = true;
362 m_stream->Printf (" <%s>\n", m_error.c_str());
366 // Make sure we have a value and make sure the summary didn't
367 // specify that the value should not be printed - and do not print
368 // the value if this thing is nil
369 // (but show the value if the user passes a format explicitly)
370 TypeSummaryImpl* entry = GetSummaryFormatter();
371 if (!IsNil() && !m_value.empty() && (entry == NULL || (entry->DoesPrintValue(m_valobj) || options.m_format != eFormatDefault) || m_summary.empty()) && !options.m_hide_value)
373 m_stream->Printf(" %s", m_value.c_str());
374 value_printed = true;
377 if (m_summary.size())
379 m_stream->Printf(" %s", m_summary.c_str());
380 summary_printed = true;
384 return !error_printed;
388 ValueObjectPrinter::PrintObjectDescriptionIfNeeded (bool value_printed,
389 bool summary_printed)
391 if (ShouldPrintValueObject())
393 // let's avoid the overly verbose no description error for a nil thing
394 if (options.m_use_objc && !IsNil())
396 if (!options.m_hide_value || !options.m_hide_name)
397 m_stream->Printf(" ");
398 const char *object_desc = nullptr;
399 if (value_printed || summary_printed)
400 object_desc = m_valobj->GetObjectDescription();
402 object_desc = GetDescriptionForDisplay();
403 if (object_desc && *object_desc)
405 m_stream->Printf("%s\n", object_desc);
408 else if (value_printed == false && summary_printed == false)
418 ValueObjectPrinter::ShouldPrintChildren (bool is_failed_description,
419 uint32_t& curr_ptr_depth)
421 const bool is_ref = IsRef ();
422 const bool is_ptr = IsPtr ();
424 if (is_failed_description || m_curr_depth < options.m_max_depth)
426 // We will show children for all concrete types. We won't show
427 // pointer contents unless a pointer depth has been specified.
428 // We won't reference contents unless the reference is the
429 // root object (depth of zero).
431 // Use a new temporary pointer depth in case we override the
432 // current pointer depth below...
434 if (is_ptr || is_ref)
436 // We have a pointer or reference whose value is an address.
437 // Make sure that address is not NULL
438 AddressType ptr_address_type;
439 if (m_valobj->GetPointerValue (&ptr_address_type) == 0)
442 else if (is_ref && m_curr_depth == 0 && curr_ptr_depth == 0)
444 // If this is the root object (depth is zero) that we are showing
445 // and it is a reference, and no pointer depth has been supplied
446 // print out what it references. Don't do this at deeper depths
447 // otherwise we can end up with infinite recursion...
451 return (curr_ptr_depth > 0);
454 TypeSummaryImpl* entry = GetSummaryFormatter();
456 return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
462 ValueObjectPrinter::GetValueObjectForChildrenGeneration ()
468 ValueObjectPrinter::PrintChildrenPreamble ()
470 if (options.m_flat_output)
472 if (ShouldPrintValueObject())
477 if (ShouldPrintValueObject())
478 m_stream->PutCString(IsRef () ? ": {\n" : " {\n");
479 m_stream->IndentMore();
484 ValueObjectPrinter::PrintChild (ValueObjectSP child_sp,
485 uint32_t curr_ptr_depth)
487 DumpValueObjectOptions child_options(options);
488 child_options.SetFormat(options.m_format).SetSummary().SetRootValueObjectName();
489 child_options.SetScopeChecked(true).SetHideName(options.m_hide_name).SetHideValue(options.m_hide_value)
490 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
493 ValueObjectPrinter child_printer(child_sp.get(),
496 (IsPtr() || IsRef()) && curr_ptr_depth >= 1 ? curr_ptr_depth - 1 : curr_ptr_depth,
498 child_printer.PrintValueObject();
504 ValueObjectPrinter::GetMaxNumChildrenToPrint (bool& print_dotdotdot)
506 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
508 size_t num_children = synth_m_valobj->GetNumChildren();
509 print_dotdotdot = false;
512 const size_t max_num_children = m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
514 if (num_children > max_num_children && !options.m_ignore_cap)
516 print_dotdotdot = true;
517 return max_num_children;
524 ValueObjectPrinter::PrintChildrenPostamble (bool print_dotdotdot)
526 if (!options.m_flat_output)
530 m_valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
531 m_stream->Indent("...\n");
533 m_stream->IndentLess();
534 m_stream->Indent("}\n");
539 ValueObjectPrinter::PrintChildren (uint32_t curr_ptr_depth)
541 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
543 bool print_dotdotdot = false;
544 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
547 PrintChildrenPreamble ();
549 for (size_t idx=0; idx<num_children; ++idx)
551 ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
552 PrintChild (child_sp, curr_ptr_depth);
555 PrintChildrenPostamble (print_dotdotdot);
557 else if (IsAggregate())
559 // Aggregate, no children...
560 if (ShouldPrintValueObject())
562 // if it has a synthetic value, then don't print {}, the synthetic children are probably only being used to vend a value
563 if (m_valobj->DoesProvideSyntheticValue())
564 m_stream->PutCString( "\n");
566 m_stream->PutCString(" {}\n");
571 if (ShouldPrintValueObject())
577 ValueObjectPrinter::PrintChildrenOneLiner (bool hide_names)
579 if (!GetMostSpecializedValue () || m_valobj == nullptr)
582 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
584 bool print_dotdotdot = false;
585 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
589 m_stream->PutChar('(');
591 for (uint32_t idx=0; idx<num_children; ++idx)
593 lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
595 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(options.m_use_dynamic, options.m_use_synthetic);
599 m_stream->PutCString(", ");
602 const char* name = child_sp.get()->GetName().AsCString();
605 m_stream->PutCString(name);
606 m_stream->PutCString(" = ");
609 child_sp->DumpPrintableRepresentation(*m_stream,
610 ValueObject::eValueObjectRepresentationStyleSummary,
611 lldb::eFormatInvalid,
612 ValueObject::ePrintableRepresentationSpecialCasesDisable);
617 m_stream->PutCString(", ...)");
619 m_stream->PutChar(')');
625 ValueObjectPrinter::PrintChildrenIfNeeded (bool value_printed,
626 bool summary_printed)
628 // this flag controls whether we tried to display a description for this object and failed
629 // if that happens, we want to display the children, if any
630 bool is_failed_description = !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
632 uint32_t curr_ptr_depth = m_ptr_depth;
633 bool print_children = ShouldPrintChildren (is_failed_description,curr_ptr_depth);
634 bool print_oneline = (curr_ptr_depth > 0 || options.m_show_types || !options.m_allow_oneliner_mode) ? false : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
640 m_stream->PutChar(' ');
641 PrintChildrenOneLiner (false);
645 PrintChildren (curr_ptr_depth);
647 else if (m_curr_depth >= options.m_max_depth && IsAggregate() && ShouldPrintValueObject())
649 m_stream->PutCString("{...}\n");
656 ValueObjectPrinter::ShouldPrintValidation ()
658 return options.m_run_validator;
662 ValueObjectPrinter::PrintValidationMarkerIfNeeded ()
664 if (!ShouldPrintValidation())
667 m_validation = m_valobj->GetValidationStatus();
669 if (TypeValidatorResult::Failure == m_validation.first)
671 m_stream->Printf("! ");
679 ValueObjectPrinter::PrintValidationErrorIfNeeded ()
681 if (!ShouldPrintValidation())
684 if (TypeValidatorResult::Success == m_validation.first)
687 if (m_validation.second.empty())
688 m_validation.second.assign("unknown error");
690 m_stream->Printf(" ! validation error: %s", m_validation.second.c_str());