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