Tolerate missing debug info in the shared_ptr pretty printer.

Certain fields of shared ptr have virtual functions and therefore
have their debug info homed in libc++. But if libc++ wasn't built
with debug info, the pretty printer would fail.

This patch makes the pretty printer tolerate such conditions and
updates the test harness.

This patch significantly reworks a previous attempt.

This addresses https://bugs.llvm.org/show_bug.cgi?id=48937

Differential Revision: https://reviews.llvm.org/D100610
This commit is contained in:
Sterling Augustine 2021-04-15 18:03:01 -07:00
parent 3b7f6fd26d
commit 55b7061116
3 changed files with 21 additions and 11 deletions

View File

@ -45,7 +45,6 @@ class CheckResult(gdb.Command):
# Ignore the convenience variable name and newline
value = value_str[value_str.find("= ") + 2:-1]
gdb.newest_frame().select()
expectation_val = compare_frame.read_var("expectation")
check_literal = expectation_val.string(encoding="utf-8")
if "PrettyPrintToRegex" in compare_frame.name():

View File

@ -608,25 +608,27 @@ void shared_ptr_test() {
// due to which there is one more count for the pointer. Hence, all the
// following tests are testing with expected count plus 1.
std::shared_ptr<const int> test0 = std::make_shared<const int>(5);
// The python regular expression matcher treats newlines as significant, so
// these regular expressions should be on one line.
ComparePrettyPrintToRegex(
test0,
R"(std::shared_ptr<int> count 2, weak 0 containing = {__ptr_ = 0x[a-f0-9]+})");
R"(std::shared_ptr<int> count [2\?], weak [0\?]( \(libc\+\+ missing debug info\))? containing = {__ptr_ = 0x[a-f0-9]+})");
std::shared_ptr<const int> test1(test0);
ComparePrettyPrintToRegex(
test1,
R"(std::shared_ptr<int> count 3, weak 0 containing = {__ptr_ = 0x[a-f0-9]+})");
R"(std::shared_ptr<int> count [3\?], weak [0\?]( \(libc\+\+ missing debug info\))? containing = {__ptr_ = 0x[a-f0-9]+})");
{
std::weak_ptr<const int> test2 = test1;
ComparePrettyPrintToRegex(
test0,
R"(std::shared_ptr<int> count 3, weak 1 containing = {__ptr_ = 0x[a-f0-9]+})");
R"(std::shared_ptr<int> count [3\?], weak [1\?]( \(libc\+\+ missing debug info\))? containing = {__ptr_ = 0x[a-f0-9]+})");
}
ComparePrettyPrintToRegex(
test0,
R"(std::shared_ptr<int> count 3, weak 0 containing = {__ptr_ = 0x[a-f0-9]+})");
R"(std::shared_ptr<int> count [3\?], weak [0\?]( \(libc\+\+ missing debug info\))? containing = {__ptr_ = 0x[a-f0-9]+})");
std::shared_ptr<const int> test3;
ComparePrettyPrintToChars(test3, "std::shared_ptr is nullptr");

View File

@ -312,12 +312,21 @@ class StdSharedPointerPrinter(object):
return "%s is nullptr" % typename
refcount = self.val["__cntrl_"]
if refcount != 0:
usecount = refcount["__shared_owners_"] + 1
weakcount = refcount["__shared_weak_owners_"]
if usecount == 0:
state = "expired, weak %d" % weakcount
else:
state = "count %d, weak %d" % (usecount, weakcount)
try:
usecount = refcount["__shared_owners_"] + 1
weakcount = refcount["__shared_weak_owners_"]
if usecount == 0:
state = "expired, weak %d" % weakcount
else:
state = "count %d, weak %d" % (usecount, weakcount)
except:
# Debug info for a class with virtual functions is emitted
# in the same place as its key function. That means that
# for std::shared_ptr, __shared_owners_ is emitted into
# into libcxx.[so|a] itself, rather than into the shared_ptr
# instantiation point. So if libcxx.so was built without
# debug info, these fields will be missing.
state = "count ?, weak ? (libc++ missing debug info)"
return "%s<%s> %s containing" % (typename, pointee_type, state)
def __iter__(self):