]> git.ipfire.org Git - thirdparty/gcc.git/commit
Add 'num_children' method to relevant pretty-printers
authorTom Tromey <tromey@adacore.com>
Mon, 3 Nov 2025 21:02:22 +0000 (14:02 -0700)
committerTom Tromey <tromey@adacore.com>
Fri, 14 Nov 2025 19:10:16 +0000 (12:10 -0700)
commitde124ffe1439d63e4531f8aaf3641bb8f4260c33
tree4dafa2795cb6170e9a7f8179beab015d931dc1a1
parentf94a73e0fcedf7a05b04aa12beee57b9a82859a5
Add 'num_children' method to relevant pretty-printers

A user pointed out that, in DAP mode, gdb would hang while trying to
display a certain vector.  See

    https://sourceware.org/bugzilla/show_bug.cgi?id=33594

This is caused by a combination of things: the vector is
uninitialized, DAP requires a count of the number of children of a
variable, and libstdc++ printers don't implement the 'num_children'
method, so gdb tries to count children by iterating.

In this case, the vector has a nonsensical size:

    (gdb) p myVector
    $1 = std::vector of length -34979931, capacity -33992726

This patch adds a 'num_children' method to a subset of the
pretty-printers, in particular ones where I thought the length might
be arbitrarily large and susceptible to being garbage when the object
isn't initialized.

I've also specifically added a check to the vector printer for the
case where the length is negative.

These container printers could be further improved by adding the
'child' method, allowing random access to child objects.  However I
haven't done that here.

libstdc++-v3/ChangeLog

* python/libstdcxx/v6/printers.py (StdVectorPrinter._bounds):
New method.
(StdVectorPrinter.to_string): Use it.
(StdVectorPrinter.num_children): New method.
(StdStackOrQueuePrinter.num_children): New method.
(StdMapPrinter.num_children): New method.
(StdSetPrinter.num_children): New method.
(StdDequePrinter._size): New method.
(StdDequePrinter.to_string): Use it.
(StdDequePrinter.num_children): New method.
(Tr1UnorderedSetPrinter.num_children): New method.
(Tr1UnorderedMapPrinter.num_children): New method.
(StdSpanPrinter.num_children): New method.
libstdc++-v3/python/libstdcxx/v6/printers.py