Skip to content

Commit 40fb90e

Browse files
authored
[lldb][Formatters] Add shared/weak count to libstdc++ std::shared_ptr summary (#147166)
Depends on #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 #147141).
1 parent 8763245 commit 40fb90e

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"
@@ -330,29 +331,37 @@ bool lldb_private::formatters::LibStdcppSmartPointerSummaryProvider(
330331
if (!ptr_sp)
331332
return false;
332333

333-
ValueObjectSP usecount_sp(
334-
valobj_sp->GetChildAtNamePath({"_M_refcount", "_M_pi", "_M_use_count"}));
335-
if (!usecount_sp)
334+
DumpCxxSmartPtrPointerSummary(stream, *ptr_sp, options);
335+
336+
ValueObjectSP pi_sp = valobj_sp->GetChildAtNamePath({"_M_refcount", "_M_pi"});
337+
if (!pi_sp)
336338
return false;
337339

338-
if (ptr_sp->GetValueAsUnsigned(0) == 0 ||
339-
usecount_sp->GetValueAsUnsigned(0) == 0) {
340-
stream.Printf("nullptr");
340+
bool success;
341+
uint64_t pi_addr = pi_sp->GetValueAsUnsigned(0, &success);
342+
// Empty control field. We're done.
343+
if (!success || pi_addr == 0)
341344
return true;
345+
346+
int64_t shared_count = 0;
347+
if (auto count_sp = pi_sp->GetChildMemberWithName("_M_use_count")) {
348+
bool success;
349+
shared_count = count_sp->GetValueAsSigned(0, &success);
350+
if (!success)
351+
return false;
352+
353+
stream.Printf(" strong=%" PRId64, shared_count);
342354
}
343355

344-
Status error;
345-
ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
346-
if (pointee_sp && error.Success()) {
347-
if (pointee_sp->DumpPrintableRepresentation(
348-
stream, ValueObject::eValueObjectRepresentationStyleSummary,
349-
lldb::eFormatInvalid,
350-
ValueObject::PrintableRepresentationSpecialCases::eDisable,
351-
false)) {
352-
return true;
353-
}
356+
// _M_weak_count is the number of weak references + (_M_use_count != 0).
357+
if (auto weak_count_sp = pi_sp->GetChildMemberWithName("_M_weak_count")) {
358+
bool success;
359+
int64_t count = weak_count_sp->GetValueAsUnsigned(0, &success);
360+
if (!success)
361+
return false;
362+
363+
stream.Printf(" weak=%" PRId64, count - (shared_count != 0));
354364
}
355365

356-
stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
357366
return true;
358367
}

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)