1 //===-- LibCxx.cpp ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/ScopeExit.h"
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/FormatEntity.h"
14 #include "lldb/Core/ValueObject.h"
15 #include "lldb/Core/ValueObjectConstResult.h"
16 #include "lldb/DataFormatters/FormattersHelpers.h"
17 #include "lldb/DataFormatters/StringPrinter.h"
18 #include "lldb/DataFormatters/TypeSummary.h"
19 #include "lldb/DataFormatters/VectorIterator.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ProcessStructReader.h"
22 #include "lldb/Target/SectionLoadList.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/DataBufferHeap.h"
25 #include "lldb/Utility/Endian.h"
26 #include "lldb/Utility/Status.h"
27 #include "lldb/Utility/Stream.h"
29 #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
32 using namespace lldb_private;
33 using namespace lldb_private::formatters;
35 bool lldb_private::formatters::LibcxxOptionalSummaryProvider(
36 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
37 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
41 // An optional either contains a value or not, the member __engaged_ is
42 // a bool flag, it is true if the optional has a value and false otherwise.
43 ValueObjectSP engaged_sp(
44 valobj_sp->GetChildMemberWithName(ConstString("__engaged_"), true));
49 llvm::StringRef engaged_as_cstring(
50 engaged_sp->GetValueAsUnsigned(0) == 1 ? "true" : "false");
52 stream.Printf(" Has Value=%s ", engaged_as_cstring.data());
57 bool lldb_private::formatters::LibcxxFunctionSummaryProvider(
58 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
60 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
65 ExecutionContext exe_ctx(valobj_sp->GetExecutionContextRef());
66 Process *process = exe_ctx.GetProcessPtr();
68 if (process == nullptr)
71 CPPLanguageRuntime *cpp_runtime = CPPLanguageRuntime::Get(*process);
76 CPPLanguageRuntime::LibCppStdFunctionCallableInfo callable_info =
77 cpp_runtime->FindLibCppStdFunctionCallableInfo(valobj_sp);
79 switch (callable_info.callable_case) {
80 case CPPLanguageRuntime::LibCppStdFunctionCallableCase::Invalid:
81 stream.Printf(" __f_ = %" PRIu64, callable_info.member__f_pointer_value);
84 case CPPLanguageRuntime::LibCppStdFunctionCallableCase::Lambda:
86 " Lambda in File %s at Line %u",
87 callable_info.callable_line_entry.file.GetFilename().GetCString(),
88 callable_info.callable_line_entry.line);
90 case CPPLanguageRuntime::LibCppStdFunctionCallableCase::CallableObject:
92 " Function in File %s at Line %u",
93 callable_info.callable_line_entry.file.GetFilename().GetCString(),
94 callable_info.callable_line_entry.line);
96 case CPPLanguageRuntime::LibCppStdFunctionCallableCase::FreeOrMemberFunction:
97 stream.Printf(" Function = %s ",
98 callable_info.callable_symbol.GetName().GetCString());
105 bool lldb_private::formatters::LibcxxSmartPointerSummaryProvider(
106 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
107 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
110 ValueObjectSP ptr_sp(
111 valobj_sp->GetChildMemberWithName(ConstString("__ptr_"), true));
112 ValueObjectSP count_sp(valobj_sp->GetChildAtNamePath(
113 {ConstString("__cntrl_"), ConstString("__shared_owners_")}));
114 ValueObjectSP weakcount_sp(valobj_sp->GetChildAtNamePath(
115 {ConstString("__cntrl_"), ConstString("__shared_weak_owners_")}));
120 if (ptr_sp->GetValueAsUnsigned(0) == 0) {
121 stream.Printf("nullptr");
124 bool print_pointee = false;
126 ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
127 if (pointee_sp && error.Success()) {
128 if (pointee_sp->DumpPrintableRepresentation(
129 stream, ValueObject::eValueObjectRepresentationStyleSummary,
130 lldb::eFormatInvalid,
131 ValueObject::PrintableRepresentationSpecialCases::eDisable,
133 print_pointee = true;
136 stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
140 stream.Printf(" strong=%" PRIu64, 1 + count_sp->GetValueAsUnsigned(0));
143 stream.Printf(" weak=%" PRIu64, 1 + weakcount_sp->GetValueAsUnsigned(0));
149 (lldb) fr var ibeg --raw --ptr-depth 1
150 (std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::pair<int,
151 std::__1::basic_string<char, std::__1::char_traits<char>,
152 std::__1::allocator<char> > >, std::__1::__tree_node<std::__1::pair<int,
153 std::__1::basic_string<char, std::__1::char_traits<char>,
154 std::__1::allocator<char> > >, void *> *, long> >) ibeg = {
156 __ptr_ = 0x0000000100103870 {
157 std::__1::__tree_node_base<void *> = {
158 std::__1::__tree_end_node<std::__1::__tree_node_base<void *> *> = {
159 __left_ = 0x0000000000000000
161 __right_ = 0x0000000000000000
162 __parent_ = 0x00000001001038b0
167 second = { std::string }
170 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::
171 LibCxxMapIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
172 : SyntheticChildrenFrontEnd(*valobj_sp), m_pair_ptr(), m_pair_sp() {
177 bool lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::Update() {
179 m_pair_ptr = nullptr;
181 ValueObjectSP valobj_sp = m_backend.GetSP();
185 TargetSP target_sp(valobj_sp->GetTargetSP());
193 static ConstString g___i_("__i_");
195 // this must be a ValueObject* because it is a child of the ValueObject we
196 // are producing children for it if were a ValueObjectSP, we would end up
197 // with a loop (iterator -> synthetic -> child -> parent == iterator) and
198 // that would in turn leak memory by never allowing the ValueObjects to die
199 // and free their memory
200 m_pair_ptr = valobj_sp
201 ->GetValueForExpressionPath(
202 ".__i_.__ptr_->__value_", nullptr, nullptr,
203 ValueObject::GetValueForExpressionPathOptions()
204 .DontCheckDotVsArrowSyntax()
205 .SetSyntheticChildrenTraversal(
206 ValueObject::GetValueForExpressionPathOptions::
207 SyntheticChildrenTraversal::None),
212 m_pair_ptr = valobj_sp
213 ->GetValueForExpressionPath(
214 ".__i_.__ptr_", nullptr, nullptr,
215 ValueObject::GetValueForExpressionPathOptions()
216 .DontCheckDotVsArrowSyntax()
217 .SetSyntheticChildrenTraversal(
218 ValueObject::GetValueForExpressionPathOptions::
219 SyntheticChildrenTraversal::None),
223 auto __i_(valobj_sp->GetChildMemberWithName(g___i_, true));
225 m_pair_ptr = nullptr;
228 CompilerType pair_type(
229 __i_->GetCompilerType().GetTypeTemplateArgument(0));
231 uint64_t bit_offset_ptr;
232 uint32_t bitfield_bit_size_ptr;
233 bool is_bitfield_ptr;
234 pair_type = pair_type.GetFieldAtIndex(
235 0, name, &bit_offset_ptr, &bitfield_bit_size_ptr, &is_bitfield_ptr);
237 m_pair_ptr = nullptr;
241 auto addr(m_pair_ptr->GetValueAsUnsigned(LLDB_INVALID_ADDRESS));
242 m_pair_ptr = nullptr;
243 if (addr && addr != LLDB_INVALID_ADDRESS) {
244 ClangASTContext *ast_ctx =
245 llvm::dyn_cast_or_null<ClangASTContext>(pair_type.GetTypeSystem());
248 CompilerType tree_node_type = ast_ctx->CreateStructForIdentifier(
251 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
253 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
255 ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
256 {"cw", ast_ctx->GetBasicType(lldb::eBasicTypeBool)},
257 {"payload", pair_type}});
258 llvm::Optional<uint64_t> size = tree_node_type.GetByteSize(nullptr);
261 DataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
262 ProcessSP process_sp(target_sp->GetProcessSP());
264 process_sp->ReadMemory(addr, buffer_sp->GetBytes(),
265 buffer_sp->GetByteSize(), error);
268 DataExtractor extractor(buffer_sp, process_sp->GetByteOrder(),
269 process_sp->GetAddressByteSize());
270 auto pair_sp = CreateValueObjectFromData(
271 "pair", extractor, valobj_sp->GetExecutionContextRef(),
274 m_pair_sp = pair_sp->GetChildAtIndex(4, true);
282 size_t lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::
283 CalculateNumChildren() {
288 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::GetChildAtIndex(
291 return m_pair_ptr->GetChildAtIndex(idx, true);
293 return m_pair_sp->GetChildAtIndex(idx, true);
294 return lldb::ValueObjectSP();
297 bool lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::
298 MightHaveChildren() {
302 size_t lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::
303 GetIndexOfChildWithName(ConstString name) {
306 if (name == "second")
311 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::
312 ~LibCxxMapIteratorSyntheticFrontEnd() {
313 // this will be deleted when its parent dies (since it's a child object)
314 // delete m_pair_ptr;
317 SyntheticChildrenFrontEnd *
318 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEndCreator(
319 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
320 return (valobj_sp ? new LibCxxMapIteratorSyntheticFrontEnd(valobj_sp)
325 (lldb) fr var ibeg --raw --ptr-depth 1 -T
326 (std::__1::__wrap_iter<int *>) ibeg = {
327 (std::__1::__wrap_iter<int *>::iterator_type) __i = 0x00000001001037a0 {
333 SyntheticChildrenFrontEnd *
334 lldb_private::formatters::LibCxxVectorIteratorSyntheticFrontEndCreator(
335 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
336 static ConstString g_item_name;
338 g_item_name.SetCString("__i");
340 ? new VectorIteratorSyntheticFrontEnd(valobj_sp, g_item_name)
344 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
345 LibcxxSharedPtrSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
346 : SyntheticChildrenFrontEnd(*valobj_sp), m_cntrl(nullptr), m_count_sp(),
347 m_weak_count_sp(), m_ptr_size(0), m_byte_order(lldb::eByteOrderInvalid) {
352 size_t lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
353 CalculateNumChildren() {
354 return (m_cntrl ? 1 : 0);
358 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex(
361 return lldb::ValueObjectSP();
363 ValueObjectSP valobj_sp = m_backend.GetSP();
365 return lldb::ValueObjectSP();
368 return valobj_sp->GetChildMemberWithName(ConstString("__ptr_"), true);
371 return lldb::ValueObjectSP();
375 ValueObjectSP shared_owners_sp(m_cntrl->GetChildMemberWithName(
376 ConstString("__shared_owners_"), true));
377 if (!shared_owners_sp)
378 return lldb::ValueObjectSP();
379 uint64_t count = 1 + shared_owners_sp->GetValueAsUnsigned(0);
380 DataExtractor data(&count, 8, m_byte_order, m_ptr_size);
381 m_count_sp = CreateValueObjectFromData(
382 "count", data, valobj_sp->GetExecutionContextRef(),
383 shared_owners_sp->GetCompilerType());
386 } else /* if (idx == 2) */
388 if (!m_weak_count_sp) {
389 ValueObjectSP shared_weak_owners_sp(m_cntrl->GetChildMemberWithName(
390 ConstString("__shared_weak_owners_"), true));
391 if (!shared_weak_owners_sp)
392 return lldb::ValueObjectSP();
393 uint64_t count = 1 + shared_weak_owners_sp->GetValueAsUnsigned(0);
394 DataExtractor data(&count, 8, m_byte_order, m_ptr_size);
395 m_weak_count_sp = CreateValueObjectFromData(
396 "count", data, valobj_sp->GetExecutionContextRef(),
397 shared_weak_owners_sp->GetCompilerType());
399 return m_weak_count_sp;
403 bool lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update() {
405 m_weak_count_sp.reset();
408 ValueObjectSP valobj_sp = m_backend.GetSP();
412 TargetSP target_sp(valobj_sp->GetTargetSP());
416 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
417 m_ptr_size = target_sp->GetArchitecture().GetAddressByteSize();
419 lldb::ValueObjectSP cntrl_sp(
420 valobj_sp->GetChildMemberWithName(ConstString("__cntrl_"), true));
422 m_cntrl = cntrl_sp.get(); // need to store the raw pointer to avoid a circular
427 bool lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
428 MightHaveChildren() {
432 size_t lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
433 GetIndexOfChildWithName(ConstString name) {
434 if (name == "__ptr_")
438 if (name == "weak_count")
443 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::
444 ~LibcxxSharedPtrSyntheticFrontEnd() = default;
446 SyntheticChildrenFrontEnd *
447 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEndCreator(
448 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
449 return (valobj_sp ? new LibcxxSharedPtrSyntheticFrontEnd(valobj_sp)
453 bool lldb_private::formatters::LibcxxContainerSummaryProvider(
454 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
455 if (valobj.IsPointerType()) {
456 uint64_t value = valobj.GetValueAsUnsigned(0);
459 stream.Printf("0x%016" PRIx64 " ", value);
461 return FormatEntity::FormatStringRef("size=${svar%#}", stream, nullptr,
462 nullptr, nullptr, &valobj, false, false);
465 // the field layout in a libc++ string (cap, side, data or data, size, cap)
466 enum LibcxxStringLayoutMode {
467 eLibcxxStringLayoutModeCSD = 0,
468 eLibcxxStringLayoutModeDSC = 1,
469 eLibcxxStringLayoutModeInvalid = 0xffff
472 // this function abstracts away the layout and mode details of a libc++ string
473 // and returns the address of the data and the size ready for callers to
475 static bool ExtractLibcxxStringInfo(ValueObject &valobj,
476 ValueObjectSP &location_sp,
478 ValueObjectSP D(valobj.GetChildAtIndexPath({0, 0, 0, 0}));
482 ValueObjectSP layout_decider(
483 D->GetChildAtIndexPath(llvm::ArrayRef<size_t>({0, 0})));
485 // this child should exist
489 ConstString g_data_name("__data_");
490 ConstString g_size_name("__size_");
491 bool short_mode = false; // this means the string is in short-mode and the
492 // data is stored inline
493 LibcxxStringLayoutMode layout = (layout_decider->GetName() == g_data_name)
494 ? eLibcxxStringLayoutModeDSC
495 : eLibcxxStringLayoutModeCSD;
496 uint64_t size_mode_value = 0;
498 if (layout == eLibcxxStringLayoutModeDSC) {
499 ValueObjectSP size_mode(D->GetChildAtIndexPath({1, 1, 0}));
503 if (size_mode->GetName() != g_size_name) {
504 // we are hitting the padding structure, move along
505 size_mode = D->GetChildAtIndexPath({1, 1, 1});
510 size_mode_value = (size_mode->GetValueAsUnsigned(0));
511 short_mode = ((size_mode_value & 0x80) == 0);
513 ValueObjectSP size_mode(D->GetChildAtIndexPath({1, 0, 0}));
517 size_mode_value = (size_mode->GetValueAsUnsigned(0));
518 short_mode = ((size_mode_value & 1) == 0);
522 ValueObjectSP s(D->GetChildAtIndex(1, true));
525 location_sp = s->GetChildAtIndex(
526 (layout == eLibcxxStringLayoutModeDSC) ? 0 : 1, true);
527 size = (layout == eLibcxxStringLayoutModeDSC)
529 : ((size_mode_value >> 1) % 256);
530 return (location_sp.get() != nullptr);
532 ValueObjectSP l(D->GetChildAtIndex(0, true));
535 // we can use the layout_decider object as the data pointer
536 location_sp = (layout == eLibcxxStringLayoutModeDSC)
538 : l->GetChildAtIndex(2, true);
539 ValueObjectSP size_vo(l->GetChildAtIndex(1, true));
540 if (!size_vo || !location_sp)
542 size = size_vo->GetValueAsUnsigned(0);
547 bool lldb_private::formatters::LibcxxWStringSummaryProvider(
548 ValueObject &valobj, Stream &stream,
549 const TypeSummaryOptions &summary_options) {
551 ValueObjectSP location_sp;
552 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
555 stream.Printf("L\"\"");
561 DataExtractor extractor;
563 StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj);
565 if (summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryCapped) {
566 const auto max_size = valobj.GetTargetSP()->GetMaximumSizeOfStringSummary();
567 if (size > max_size) {
569 options.SetIsTruncated(true);
572 location_sp->GetPointeeData(extractor, 0, size);
574 // std::wstring::size() is measured in 'characters', not bytes
575 ClangASTContext *ast_context =
576 ClangASTContext::GetScratch(*valobj.GetTargetSP());
581 ast_context->GetBasicType(lldb::eBasicTypeWChar).GetByteSize(nullptr);
585 options.SetData(extractor);
586 options.SetStream(&stream);
587 options.SetPrefixToken("L");
588 options.SetQuote('"');
589 options.SetSourceSize(size);
590 options.SetBinaryZeroIsTerminator(false);
592 switch (*wchar_t_size) {
594 StringPrinter::ReadBufferAndDumpToStream<
595 lldb_private::formatters::StringPrinter::StringElementType::UTF8>(
600 lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStream<
601 lldb_private::formatters::StringPrinter::StringElementType::UTF16>(
606 lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStream<
607 lldb_private::formatters::StringPrinter::StringElementType::UTF32>(
612 stream.Printf("size for wchar_t is not valid");
619 template <StringPrinter::StringElementType element_type>
620 bool LibcxxStringSummaryProvider(ValueObject &valobj, Stream &stream,
621 const TypeSummaryOptions &summary_options,
622 std::string prefix_token = "") {
624 ValueObjectSP location_sp;
626 if (!ExtractLibcxxStringInfo(valobj, location_sp, size))
630 stream.Printf("\"\"");
637 StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj);
639 DataExtractor extractor;
640 if (summary_options.GetCapping() == TypeSummaryCapping::eTypeSummaryCapped) {
641 const auto max_size = valobj.GetTargetSP()->GetMaximumSizeOfStringSummary();
642 if (size > max_size) {
644 options.SetIsTruncated(true);
647 location_sp->GetPointeeData(extractor, 0, size);
649 options.SetData(extractor);
650 options.SetStream(&stream);
652 if (prefix_token.empty())
653 options.SetPrefixToken(nullptr);
655 options.SetPrefixToken(prefix_token);
657 options.SetQuote('"');
658 options.SetSourceSize(size);
659 options.SetBinaryZeroIsTerminator(false);
660 StringPrinter::ReadBufferAndDumpToStream<element_type>(options);
665 bool lldb_private::formatters::LibcxxStringSummaryProviderASCII(
666 ValueObject &valobj, Stream &stream,
667 const TypeSummaryOptions &summary_options) {
668 return LibcxxStringSummaryProvider<StringPrinter::StringElementType::ASCII>(
669 valobj, stream, summary_options);
672 bool lldb_private::formatters::LibcxxStringSummaryProviderUTF16(
673 ValueObject &valobj, Stream &stream,
674 const TypeSummaryOptions &summary_options) {
675 return LibcxxStringSummaryProvider<StringPrinter::StringElementType::UTF16>(
676 valobj, stream, summary_options, "u");
679 bool lldb_private::formatters::LibcxxStringSummaryProviderUTF32(
680 ValueObject &valobj, Stream &stream,
681 const TypeSummaryOptions &summary_options) {
682 return LibcxxStringSummaryProvider<StringPrinter::StringElementType::UTF32>(
683 valobj, stream, summary_options, "U");