Skip to content

Commit a05db4d

Browse files
committed
[lldb][Formatters] Add shared/weak count to libstdc++ std::shared_ptr summary (llvm#147166)
Depends on llvm#147165 This adds weak/strong counts to the std::shared_ptr summary of the libstdcxx formatters. We already do this for libcxx. This will make it easier to consolidate the tests into a generic one (see llvm#147141). (cherry picked from commit 40fb90e)
1 parent 7845ee2 commit a05db4d

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "LibCxx.h"
1111

1212
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
13+
#include "lldb/DataFormatters/FormattersHelpers.h"
1314
#include "lldb/DataFormatters/StringPrinter.h"
1415
#include "lldb/DataFormatters/VectorIterator.h"
1516
#include "lldb/Target/Target.h"
@@ -450,29 +451,37 @@ bool lldb_private::formatters::LibStdcppSmartPointerSummaryProvider(
450451
if (!ptr_sp)
451452
return false;
452453

453-
ValueObjectSP usecount_sp(
454-
valobj_sp->GetChildAtNamePath({"_M_refcount", "_M_pi", "_M_use_count"}));
455-
if (!usecount_sp)
454+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
455+
456+
ValueObjectSP pi_sp = valobj_sp->GetChildAtNamePath({"_M_refcount", "_M_pi"});
457+
if (!pi_sp)
456458
return false;
457459

458-
if (ptr_sp->GetValueAsUnsigned(0) == 0 ||
459-
usecount_sp->GetValueAsUnsigned(0) == 0) {
460-
stream.Printf("nullptr");
460+
bool success;
461+
uint64_t pi_addr = pi_sp->GetValueAsUnsigned(0, &success);
462+
// Empty control field. We're done.
463+
if (!success || pi_addr == 0)
461464
return true;
465+
466+
int64_t shared_count = 0;
467+
if (auto count_sp = pi_sp->GetChildMemberWithName("_M_use_count")) {
468+
bool success;
469+
shared_count = count_sp->GetValueAsSigned(0, &success);
470+
if (!success)
471+
return false;
472+
473+
stream.Printf(" strong=%" PRId64, shared_count);
462474
}
463475

464-
Status error;
465-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
466-
if (pointee_sp && error.Success()) {
467-
if (pointee_sp->DumpPrintableRepresentation(
468-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
469-
lldb::eFormatInvalid,
470-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
471-
false)) {
472-
return true;
473-
}
476+
// _M_weak_count is the number of weak references + (_M_use_count != 0).
477+
if (auto weak_count_sp = pi_sp->GetChildMemberWithName("_M_weak_count")) {
478+
bool success;
479+
int64_t count = weak_count_sp->GetValueAsUnsigned(0, &success);
480+
if (!success)
481+
return false;
482+
483+
stream.Printf(" weak=%" PRId64, count - (shared_count != 0));
474484
}
475485

476-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
477486
return true;
478487
}

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libstdcpp/smart_ptr/TestDataFormatterStdSmartPtr.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Test lldb data formatter subsystem.
33
"""
44

5-
65
import lldb
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
@@ -48,5 +47,7 @@ def test_with_run_command(self):
4847
self.expect("frame variable ssp", substrs=["ssp = nullptr"])
4948

5049
self.expect("frame variable nwp", substrs=["nwp = nullptr"])
51-
self.expect("frame variable iwp", substrs=["iwp = nullptr"])
52-
self.expect("frame variable swp", substrs=["swp = nullptr"])
50+
51+
# FIXME: these weak_ptr's should also be reset to nullptr.
52+
self.expect("frame variable iwp", substrs=["iwp = ", "strong=0 weak=1"])
53+
self.expect("frame variable swp", substrs=["swp = ", "strong=0 weak=1"])

0 commit comments

Comments
 (0)