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(m_valobj->GetQualifiedTypeName());
226 if (qualified_type_name)
227 m_stream->Printf("(%s) ", qualified_type_name.GetCString());
235 ValueObjectPrinter::PrintNameIfNeeded (bool show_type)
237 if (options.m_flat_output)
239 // If we are showing types, also qualify the C++ base classes
240 const bool qualify_cxx_base_classes = show_type;
241 if (!options.m_hide_name)
243 m_valobj->GetExpressionPath(*m_stream, qualify_cxx_base_classes);
244 m_stream->PutCString(" =");
248 else if (!options.m_hide_name)
250 const char *name_cstr = GetRootNameForDisplay("");
251 m_stream->Printf ("%s =", name_cstr);
258 ValueObjectPrinter::CheckScopeIfNeeded ()
260 if (options.m_scope_already_checked)
262 return m_valobj->IsInScope();
266 ValueObjectPrinter::GetSummaryFormatter ()
268 if (m_summary_formatter.second == false)
270 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : m_valobj->GetSummaryFormat().get();
272 if (options.m_omit_summary_depth > 0)
274 m_summary_formatter.first = entry;
275 m_summary_formatter.second = true;
277 return m_summary_formatter.first;
281 ValueObjectPrinter::GetValueSummaryError (std::string& value,
282 std::string& summary,
285 if (options.m_format != eFormatDefault && options.m_format != m_valobj->GetFormat())
287 m_valobj->GetValueAsCString(options.m_format,
292 const char* val_cstr = m_valobj->GetValueAsCString();
294 value.assign(val_cstr);
296 const char* err_cstr = m_valobj->GetError().AsCString();
298 error.assign(err_cstr);
300 if (ShouldPrintValueObject())
303 summary.assign("nil");
304 else if (options.m_omit_summary_depth == 0)
306 TypeSummaryImpl* entry = GetSummaryFormatter();
308 m_valobj->GetSummaryAsCString(entry, summary);
311 const char* sum_cstr = m_valobj->GetSummaryAsCString();
313 summary.assign(sum_cstr);
320 ValueObjectPrinter::PrintValueAndSummaryIfNeeded (bool& value_printed,
321 bool& summary_printed)
323 bool error_printed = false;
324 if (ShouldPrintValueObject())
326 if (!CheckScopeIfNeeded())
327 m_error.assign("out of scope");
330 GetValueSummaryError(m_value, m_summary, m_error);
334 error_printed = true;
335 m_stream->Printf (" <%s>\n", m_error.c_str());
339 // Make sure we have a value and make sure the summary didn't
340 // specify that the value should not be printed - and do not print
341 // the value if this thing is nil
342 // (but show the value if the user passes a format explicitly)
343 TypeSummaryImpl* entry = GetSummaryFormatter();
344 if (!IsNil() && !m_value.empty() && (entry == NULL || (entry->DoesPrintValue() || options.m_format != eFormatDefault) || m_summary.empty()) && !options.m_hide_value)
346 m_stream->Printf(" %s", m_value.c_str());
347 value_printed = true;
350 if (m_summary.size())
352 m_stream->Printf(" %s", m_summary.c_str());
353 summary_printed = true;
357 return !error_printed;
361 ValueObjectPrinter::PrintObjectDescriptionIfNeeded (bool value_printed,
362 bool summary_printed)
364 if (ShouldPrintValueObject())
366 // let's avoid the overly verbose no description error for a nil thing
367 if (options.m_use_objc && !IsNil())
369 if (!options.m_hide_value || !options.m_hide_name)
370 m_stream->Printf(" ");
371 const char *object_desc = nullptr;
372 if (value_printed || summary_printed)
373 object_desc = m_valobj->GetObjectDescription();
375 object_desc = GetDescriptionForDisplay();
376 if (object_desc && *object_desc)
378 m_stream->Printf("%s\n", object_desc);
381 else if (value_printed == false && summary_printed == false)
391 ValueObjectPrinter::ShouldPrintChildren (bool is_failed_description,
392 uint32_t& curr_ptr_depth)
394 const bool is_ref = IsRef ();
395 const bool is_ptr = IsPtr ();
397 if (is_failed_description || m_curr_depth < options.m_max_depth)
399 // We will show children for all concrete types. We won't show
400 // pointer contents unless a pointer depth has been specified.
401 // We won't reference contents unless the reference is the
402 // root object (depth of zero).
404 // Use a new temporary pointer depth in case we override the
405 // current pointer depth below...
407 if (is_ptr || is_ref)
409 // We have a pointer or reference whose value is an address.
410 // Make sure that address is not NULL
411 AddressType ptr_address_type;
412 if (m_valobj->GetPointerValue (&ptr_address_type) == 0)
415 else if (is_ref && m_curr_depth == 0 && curr_ptr_depth == 0)
417 // If this is the root object (depth is zero) that we are showing
418 // and it is a reference, and no pointer depth has been supplied
419 // print out what it references. Don't do this at deeper depths
420 // otherwise we can end up with infinite recursion...
424 return (curr_ptr_depth > 0);
427 TypeSummaryImpl* entry = GetSummaryFormatter();
429 return (!entry || entry->DoesPrintChildren() || m_summary.empty());
435 ValueObjectPrinter::GetValueObjectForChildrenGeneration ()
437 ValueObjectSP synth_valobj_sp = m_valobj->GetSyntheticValue (options.m_use_synthetic);
438 return (synth_valobj_sp ? synth_valobj_sp.get() : m_valobj);
442 ValueObjectPrinter::PrintChildrenPreamble ()
444 if (options.m_flat_output)
446 if (ShouldPrintValueObject())
451 if (ShouldPrintValueObject())
452 m_stream->PutCString(IsRef () ? ": {\n" : " {\n");
453 m_stream->IndentMore();
458 ValueObjectPrinter::PrintChild (ValueObjectSP child_sp,
459 uint32_t curr_ptr_depth)
461 DumpValueObjectOptions child_options(options);
462 child_options.SetFormat(options.m_format).SetSummary().SetRootValueObjectName();
463 child_options.SetScopeChecked(true).SetHideName(options.m_hide_name).SetHideValue(options.m_hide_value)
464 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
467 ValueObjectPrinter child_printer(child_sp.get(),
470 (IsPtr() || IsRef()) && curr_ptr_depth >= 1 ? curr_ptr_depth - 1 : curr_ptr_depth,
472 child_printer.PrintValueObject();
478 ValueObjectPrinter::GetMaxNumChildrenToPrint (bool& print_dotdotdot)
480 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
482 size_t num_children = synth_m_valobj->GetNumChildren();
483 print_dotdotdot = false;
486 const size_t max_num_children = m_valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
488 if (num_children > max_num_children && !options.m_ignore_cap)
490 print_dotdotdot = true;
491 return max_num_children;
498 ValueObjectPrinter::PrintChildrenPostamble (bool print_dotdotdot)
500 if (!options.m_flat_output)
504 m_valobj->GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
505 m_stream->Indent("...\n");
507 m_stream->IndentLess();
508 m_stream->Indent("}\n");
513 ValueObjectPrinter::PrintChildren (uint32_t curr_ptr_depth)
515 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
517 bool print_dotdotdot = false;
518 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
521 PrintChildrenPreamble ();
523 for (size_t idx=0; idx<num_children; ++idx)
525 ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
526 PrintChild (child_sp, curr_ptr_depth);
529 PrintChildrenPostamble (print_dotdotdot);
531 else if (IsAggregate())
533 // Aggregate, no children...
534 if (ShouldPrintValueObject())
535 m_stream->PutCString(" {}\n");
539 if (ShouldPrintValueObject())
545 ValueObjectPrinter::PrintChildrenOneLiner (bool hide_names)
547 if (!GetDynamicValueIfNeeded () || m_valobj == nullptr)
550 ValueObject* synth_m_valobj = GetValueObjectForChildrenGeneration();
552 bool print_dotdotdot = false;
553 size_t num_children = GetMaxNumChildrenToPrint(print_dotdotdot);
557 m_stream->PutChar('(');
559 for (uint32_t idx=0; idx<num_children; ++idx)
561 lldb::ValueObjectSP child_sp(synth_m_valobj->GetChildAtIndex(idx, true));
562 lldb::ValueObjectSP child_dyn_sp = child_sp.get() ? child_sp->GetDynamicValue(options.m_use_dynamic) : child_sp;
564 child_sp = child_dyn_sp;
568 m_stream->PutCString(", ");
571 const char* name = child_sp.get()->GetName().AsCString();
574 m_stream->PutCString(name);
575 m_stream->PutCString(" = ");
578 child_sp->DumpPrintableRepresentation(*m_stream,
579 ValueObject::eValueObjectRepresentationStyleSummary,
580 lldb::eFormatInvalid,
581 ValueObject::ePrintableRepresentationSpecialCasesDisable);
586 m_stream->PutCString(", ...)");
588 m_stream->PutChar(')');
594 ValueObjectPrinter::PrintChildrenIfNeeded (bool value_printed,
595 bool summary_printed)
597 // this flag controls whether we tried to display a description for this object and failed
598 // if that happens, we want to display the children, if any
599 bool is_failed_description = !PrintObjectDescriptionIfNeeded(value_printed, summary_printed);
601 uint32_t curr_ptr_depth = m_ptr_depth;
602 bool print_children = ShouldPrintChildren (is_failed_description,curr_ptr_depth);
603 bool print_oneline = (curr_ptr_depth > 0 || options.m_show_types || options.m_be_raw) ? false : DataVisualization::ShouldPrintAsOneLiner(*m_valobj);
609 m_stream->PutChar(' ');
610 PrintChildrenOneLiner (false);
614 PrintChildren (curr_ptr_depth);
616 else if (m_curr_depth >= options.m_max_depth && IsAggregate() && ShouldPrintValueObject())
618 m_stream->PutCString("{...}\n");