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 (!GetDynamicValueIfNeeded () || m_valobj == nullptr)
72 if (ShouldPrintValueObject())
74 PrintLocationIfNeeded();
77 bool show_type = PrintTypeIfNeeded();
79 PrintNameIfNeeded(show_type);
82 bool value_printed = false;
83 bool summary_printed = false;
85 bool val_summary_ok = PrintValueAndSummaryIfNeeded (value_printed,summary_printed);
88 PrintChildrenIfNeeded (value_printed, summary_printed);
96 ValueObjectPrinter::GetDynamicValueIfNeeded ()
100 bool update_success = m_orig_valobj->UpdateValueIfNeeded (true);
103 m_valobj = m_orig_valobj;
107 if (m_orig_valobj->IsDynamic())
109 if (options.m_use_dynamic == eNoDynamicValues)
111 ValueObject *static_value = m_orig_valobj->GetStaticValue().get();
113 m_valobj = static_value;
115 m_valobj = m_orig_valobj;
118 m_valobj = m_orig_valobj;
122 if (options.m_use_dynamic != eNoDynamicValues)
124 ValueObject *dynamic_value = m_orig_valobj->GetDynamicValue(options.m_use_dynamic).get();
126 m_valobj = dynamic_value;
128 m_valobj = m_orig_valobj;
131 m_valobj = m_orig_valobj;
134 m_clang_type = m_valobj->GetClangType();
135 m_type_flags = m_clang_type.GetTypeInfo ();
140 ValueObjectPrinter::GetDescriptionForDisplay ()
142 const char* str = m_valobj->GetObjectDescription();
144 str = m_valobj->GetSummaryAsCString();
146 str = m_valobj->GetValueAsCString();
151 ValueObjectPrinter::GetRootNameForDisplay (const char* if_fail)
153 const char *root_valobj_name = options.m_root_valobj_name.empty() ?
154 m_valobj->GetName().AsCString() :
155 options.m_root_valobj_name.c_str();
156 return root_valobj_name ? root_valobj_name : if_fail;
160 ValueObjectPrinter::ShouldPrintValueObject ()
162 if (m_should_print == eLazyBoolCalculate)
163 m_should_print = (options.m_flat_output == false || m_type_flags.Test (ClangASTType::eTypeHasValue)) ? eLazyBoolYes : eLazyBoolNo;
164 return m_should_print == eLazyBoolYes;
168 ValueObjectPrinter::IsNil ()
170 if (m_is_nil == eLazyBoolCalculate)
171 m_is_nil = m_valobj->IsObjCNil() ? eLazyBoolYes : eLazyBoolNo;
172 return m_is_nil == eLazyBoolYes;
176 ValueObjectPrinter::IsPtr ()
178 if (m_is_ptr == eLazyBoolCalculate)
179 m_is_ptr = m_type_flags.Test (ClangASTType::eTypeIsPointer) ? eLazyBoolYes : eLazyBoolNo;
180 return m_is_ptr == eLazyBoolYes;
184 ValueObjectPrinter::IsRef ()
186 if (m_is_ref == eLazyBoolCalculate)
187 m_is_ref = m_type_flags.Test (ClangASTType::eTypeIsReference) ? eLazyBoolYes : eLazyBoolNo;
188 return m_is_ref == eLazyBoolYes;
192 ValueObjectPrinter::IsAggregate ()
194 if (m_is_aggregate == eLazyBoolCalculate)
195 m_is_aggregate = m_type_flags.Test (ClangASTType::eTypeHasChildren) ? eLazyBoolYes : eLazyBoolNo;
196 return m_is_aggregate == eLazyBoolYes;
200 ValueObjectPrinter::PrintLocationIfNeeded ()
202 if (options.m_show_location)
204 m_stream->Printf("%s: ", m_valobj->GetLocationAsCString());
211 ValueObjectPrinter::PrintTypeIfNeeded ()
213 bool show_type = true;
214 // if we are at the root-level and been asked to hide the root's type, then hide it
215 if (m_curr_depth == 0 && options.m_hide_root_type)
218 // otherwise decide according to the usual rules (asked to show types - always at the root level)
219 show_type = options.m_show_types || (m_curr_depth == 0 && !options.m_flat_output);
223 // Some ValueObjects don't have types (like registers sets). Only print
224 // the type if there is one to print
225 ConstString qualified_type_name;
226 if (options.m_be_raw)
227 qualified_type_name = m_valobj->GetQualifiedTypeName();
229 qualified_type_name = m_valobj->GetDisplayTypeName();
230 if (qualified_type_name)
231 m_stream->Printf("(%s) ", qualified_type_name.GetCString());
239 ValueObjectPrinter::PrintNameIfNeeded (bool show_type)
241 if (options.m_flat_output)
243 // If we are showing types, also qualify the C++ base classes
244 const bool qualify_cxx_base_classes = show_type;
245 if (!options.m_hide_name)
247 m_valobj->GetExpressionPath(*m_stream, qualify_cxx_base_classes);
248 m_stream->PutCString(" =");
252 else if (!options.m_hide_name)
254 const char *name_cstr = GetRootNameForDisplay("");
255 m_stream->Printf ("%s =", name_cstr);
262 ValueObjectPrinter::CheckScopeIfNeeded ()
264 if (options.m_scope_already_checked)
266 return m_valobj->IsInScope();
270 ValueObjectPrinter::GetSummaryFormatter ()
272 if (m_summary_formatter.second == false)
274 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : m_valobj->GetSummaryFormat().get();
276 if (options.m_omit_summary_depth > 0)
278 m_summary_formatter.first = entry;
279 m_summary_formatter.second = true;
281 return m_summary_formatter.first;
285 ValueObjectPrinter::GetValueSummaryError (std::string& value,
286 std::string& summary,
289 if (options.m_format != eFormatDefault && options.m_format != m_valobj->GetFormat())
291 m_valobj->GetValueAsCString(options.m_format,
296 const char* val_cstr = m_valobj->GetValueAsCString();
298 value.assign(val_cstr);
300 const char* err_cstr = m_valobj->GetError().AsCString();
302 error.assign(err_cstr);
304 if (ShouldPrintValueObject())
307 summary.assign("nil");
308 else if (options.m_omit_summary_depth == 0)
310 TypeSummaryImpl* entry = GetSummaryFormatter();
312 m_valobj->GetSummaryAsCString(entry, summary);
315 const char* sum_cstr = m_valobj->GetSummaryAsCString();
317 summary.assign(sum_cstr);
324 ValueObjectPrinter::PrintValueAndSummaryIfNeeded (bool& value_printed,
325 bool& summary_printed)
327 bool error_printed = false;
328 if (ShouldPrintValueObject())
330 if (!CheckScopeIfNeeded())
331 m_error.assign("out of scope");
334 GetValueSummaryError(m_value, m_summary, m_error);
338 error_printed = true;
339 m_stream->Printf (" <%s>\n", m_error.c_str());
343 // Make sure we have a value and make sure the summary didn't
344 // specify that the value should not be printed - and do not print
345 // the value if this thing is nil
346 // (but show the value if the user passes a format explicitly)
347 TypeSummaryImpl* entry = GetSummaryFormatter();
348 if (!IsNil() && !m_value.empty() && (entry == NULL || (entry->DoesPrintValue(m_valobj) || options.m_format != eFormatDefault) || m_summary.empty()) && !options.m_hide_value)
350 m_stream->Printf(" %s", m_value.c_str());
351 value_printed = true;
354 if (m_summary.size())
356 m_stream->Printf(" %s", m_summary.c_str());
357 summary_printed = true;
361 return !error_printed;
365 ValueObjectPrinter::PrintObjectDescriptionIfNeeded (bool value_printed,
366 bool summary_printed)
368 if (ShouldPrintValueObject())
370 // let's avoid the overly verbose no description error for a nil thing
371 if (options.m_use_objc && !IsNil())
373 if (!options.m_hide_value || !options.m_hide_name)
374 m_stream->Printf(" ");
375 const char *object_desc = nullptr;
376 if (value_printed || summary_printed)
377 object_desc = m_valobj->GetObjectDescription();
379 object_desc = GetDescriptionForDisplay();
380 if (object_desc && *object_desc)
382 m_stream->Printf("%s\n", object_desc);
385 else if (value_printed == false && summary_printed == false)
395 ValueObjectPrinter::ShouldPrintChildren (bool is_failed_description,
396 uint32_t& curr_ptr_depth)
398 const bool is_ref = IsRef ();
399 const bool is_ptr = IsPtr ();
401 if (is_failed_description || m_curr_depth < options.m_max_depth)
403 // We will show children for all concrete types. We won't show
404 // pointer contents unless a pointer depth has been specified.
405 // We won't reference contents unless the reference is the
406 // root object (depth of zero).
408 // Use a new temporary pointer depth in case we override the
409 // current pointer depth below...
411 if (is_ptr || is_ref)
413 // We have a pointer or reference whose value is an address.
414 // Make sure that address is not NULL
415 AddressType ptr_address_type;
416 if (m_valobj->GetPointerValue (&ptr_address_type) == 0)
419 else if (is_ref && m_curr_depth == 0 && curr_ptr_depth == 0)
421 // If this is the root object (depth is zero) that we are showing
422 // and it is a reference, and no pointer depth has been supplied
423 // print out what it references. Don't do this at deeper depths
424 // otherwise we can end up with infinite recursion...
428 return (curr_ptr_depth > 0);
431 TypeSummaryImpl* entry = GetSummaryFormatter();
433 return (!entry || entry->DoesPrintChildren(m_valobj) || m_summary.empty());
439 ValueObjectPrinter::GetValueObjectForChildrenGeneration ()
441 ValueObjectSP synth_valobj_sp = m_valobj->GetSyntheticValue (options.m_use_synthetic);
442 return (synth_valobj_sp ? synth_valobj_sp.get() : m_valobj);
446 ValueObjectPrinter::PrintChildrenPreamble ()
448 if (options.m_flat_output)
450 if (ShouldPrintValueObject())
455 if (ShouldPrintValueObject())
456 m_stream->PutCString(IsRef () ? ": {\n" : " {\n");
457 m_stream->IndentMore();
462 ValueObjectPrinter::PrintChild (ValueObjectSP child_sp,
463 uint32_t curr_ptr_depth)
465 DumpValueObjectOptions child_options(options);
466 child_options.SetFormat(options.m_format).SetSummary().SetRootValueObjectName();
467 child_options.SetScopeChecked(true).SetHideName(options.m_hide_name).SetHideValue(options.m_hide_value)
468 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
471 ValueObjectPrinter child_printer(child_sp.get(),
474 (IsPtr() || IsRef()) && curr_ptr_depth >= 1 ? curr_ptr_depth - 1 : curr_ptr_depth,
476 child_printer.PrintValueObject();
482 ValueObjectPrinter::GetMaxNumChildrenToPrint (bool& print_dotdotdot)
484 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
486 size_t num_children = synth_m_valobj->GetNumChildren();
487 print_dotdotdot = false;
490 const size_t max_num_children = m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
492 if (num_children > max_num_children && !options.m_ignore_cap)
494 print_dotdotdot = true;
495 return max_num_children;
502 ValueObjectPrinter::PrintChildrenPostamble (bool print_dotdotdot)
504 if (!options.m_flat_output)
508 m_valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
509 m_stream->Indent("...\n");
511 m_stream->IndentLess();
512 m_stream->Indent("}\n");
517 ValueObjectPrinter::PrintChildren (uint32_t curr_ptr_depth)
519 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
521 bool print_dotdotdot = false;
522 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
525 PrintChildrenPreamble ();
527 for (size_t idx=0; idx<num_children; ++idx)
529 ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
530 PrintChild (child_sp, curr_ptr_depth);
533 PrintChildrenPostamble (print_dotdotdot);
535 else if (IsAggregate())
537 // Aggregate, no children...
538 if (ShouldPrintValueObject())
539 m_stream->PutCString(" {}\n");
543 if (ShouldPrintValueObject())
549 ValueObjectPrinter::PrintChildrenOneLiner (bool hide_names)
551 if (!GetDynamicValueIfNeeded () || m_valobj == nullptr)
554 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
556 bool print_dotdotdot = false;
557 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
561 m_stream->PutChar('(');
563 for (uint32_t idx=0; idx<num_children; ++idx)
565 lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
566 lldb::ValueObjectSP child_dyn_sp = child_sp.get() ? child_sp->GetDynamicValue(options.m_use_dynamic) : child_sp;
568 child_sp = child_dyn_sp;
572 m_stream->PutCString(", ");
575 const char* name = child_sp.get()->GetName().AsCString();
578 m_stream->PutCString(name);
579 m_stream->PutCString(" = ");
582 child_sp->DumpPrintableRepresentation(*m_stream,
583 ValueObject::eValueObjectRepresentationStyleSummary,
584 lldb::eFormatInvalid,
585 ValueObject::ePrintableRepresentationSpecialCasesDisable);
590 m_stream->PutCString(", ...)");
592 m_stream->PutChar(')');
598 ValueObjectPrinter::PrintChildrenIfNeeded (bool value_printed,
599 bool summary_printed)
601 // this flag controls whether we tried to display a description for this object and failed
602 // if that happens, we want to display the children, if any
603 bool is_failed_description = !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
605 uint32_t curr_ptr_depth = m_ptr_depth;
606 bool print_children = ShouldPrintChildren (is_failed_description,curr_ptr_depth);
607 bool print_oneline = (curr_ptr_depth > 0 || options.m_show_types || options.m_be_raw) ? false : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
613 m_stream->PutChar(' ');
614 PrintChildrenOneLiner (false);
618 PrintChildren (curr_ptr_depth);
620 else if (m_curr_depth >= options.m_max_depth && IsAggregate() && ShouldPrintValueObject())
622 m_stream->PutCString("{...}\n");