On Sun, 29 Nov 2026 at 13:53 -0500, John David Anglin wrote:
Tested on hppa-unknown-linux-gnu.
Okay?
Dave
---
libstdc++: Avoid inferior call in StdStringStreamPrinter
Evaluating '$__stream->rdbuf()' inside StdStringStreamPrinter.__init__
performs an inferior function call through the PLT/vtable. This fails
with SIGSEGV on targets where GDB inferior calls through function
descriptors or PLT stubs are unreliable (such as hppa-linux-gnu), and
prevents pretty-printing stringstreams when inspecting core files.
Read _M_streambuf directly from the std::basic_ios virtual base
subobject instead. First attempt GDB's native virtual-base cast
(val.cast(ios_base)), which uses DWARF vbase location info. If that
fails, compute the std::basic_ios address using the vbase offset stored
at vptr[-3] in GCC's vtable layout (see gcc/cp/class.cc, where vbase
offsets precede offset-to-top at vptr[-2] and the RTTI pointer at
vptr[-1] in the Itanium C++ ABI used across all GCC targets). If both
fail, fall back to val['_M_stringbuf'].address.
libstdc++-v3/ChangeLog:
* python/libstdcxx/v6/printers.py (StdStringStreamPrinter.__init__):
Read _M_streambuf from the std::basic_ios virtual base instead of
calling rdbuf() in the inferior.
diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py
b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 1bba377513c..dfe1f48696e 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -1153,13 +1153,38 @@ class StdStringStreamPrinter(printer_base):
self._val = val
self._typename = typename
- # Check if the stream was redirected. This is essentially:
- # val['_M_streambuf'] != val['_M_stringbuf'].address
- # However, GDB can't resolve the virtual inheritance, so we do that
- # manually.
- basetype = [f.type for f in val.type.fields() if f.is_base_class][0]
- gdb.set_convenience_variable('__stream', val.cast(basetype).address)
- self._streambuf = gdb.parse_and_eval('$__stream->rdbuf()')
+ # Locate the virtual base std::basic_ios to read _M_streambuf
+ # without executing an inferior function call ($__stream->rdbuf()).
+ self._streambuf = None
+ try:
+ basetype = [f.type for f in val.type.fields()
+ if f.is_base_class][0]
+ ios_base = [f.type for f in basetype.fields()
'ios_base' is a very confusing name, because I think what you're
looking for is the 'basic_ios' base class. But that type has another
base class of type 'std::ios_base'. So calling the variable for the
basic_ios base class ios_base makes it look like you're finding the
std::ios_base base class!
+ if f.is_base_class][0]
+ if ios_base.name and 'basic_iostream' in ios_base.name:
This condition is never true, because we already went past the
basic_iostream base class (if it was present at all in this
hierarchy).
I think this check should be done earlier, after we find basetype.
The relevant hierarchies are:
basic_istringstream -> basic_istream -> basic_ios
basic_ostringstream -> basic_ostream -> basic_ios
basic_stringstream -> basic_iostream -> {basic_istream,basic_ostream} ->
basic_ios
So only the third case has an extra level, but the basic_iostream base
is the first base. So I think we want to rename the variable and move
the check for basic_iostream earlier:
# Locate the virtual base std::basic_ios to read _M_streambuf
# without executing an inferior function call ($__stream->rdbuf()).
self._streambuf = None
try:
# Get the gdb.Type for the first base class, which should be
# one of basic_istream, basic_ostream, or basic_iostream.
basetype = [f.type for f in val.type.fields()
if f.is_base_class][0]
# basic_stringstream has an extra base class of type basic_iostream
# so find its first base class, which will be basic_istream.
if basetype.name and '::basic_iostream<' in basetype.name:
basetype = [f.type for f in basetype.fields()
if f.is_base_class][0]
# Get the gdb.Type for the std::basic_ios base class.
basic_ios = [f.type for f in basetype.fields()
if f.is_base_class][0]
And I think the last check would be more robust if it checked the name
too, because that virtual base might not be the first if the
stringstream in self.val is itself a base class of something else
which has other virtual bases. So:
# Get the gdb.Type for the std::basic_ios base class.
basic_ios = [f.type for f in basetype.fields()
if f.is_base_class and '::basic_ios<' in f.type.name][0]
+ ios_base = [f.type for f in ios_base.fields()
+ if f.is_base_class][0]
+ try:
+ self._streambuf = val.cast(ios_base)['_M_streambuf']
+ except gdb.error:
+ # Fall back to the vbase offset at vptr[-3] in GCC's vtable
+ # layout (see gcc/cp/class.cc).
Do we even need this fallback now? If we manually traverse the base
classes as above, then I think it should work without needing this
ABI-specific black magic.
+ base_addr = val.cast(basetype).address
In the original patch, at this point 'basetype' was the first base
class, which is either basic_istream, basic_ostream, or
basic_iostream. In no case is it the basic_ios virtual base. And
'ios_base' was either basic_ios or basic_istream.
+ vptr_type = gdb.lookup_type('void').pointer().pointer()
+ ptrdiff_type = (gdb.lookup_type('long long')
+ if vptr_type.sizeof == 8
+ else gdb.lookup_type('long'))
+ vptr = base_addr.cast(vptr_type).dereference()
+ vbase_offset = vptr.cast(ptrdiff_type.pointer())[-3]
+ ios_addr = (base_addr.cast(ptrdiff_type)
+ + vbase_offset).cast(ios_base.pointer())
The .cast(ios_base.pointer()) assumes ios_base was basic_ios, but
that's not necessarily true. So I don't think this black magic was
even correct.
If I change the try: above to raise an exception so that the except:
fallback using vptr[-3] is used, the tests fail.
I would prefer not to use it at all.
+ self._streambuf = ios_addr.dereference()['_M_streambuf']
+ except (gdb.error, IndexError):
+ pass
+
+ if self._streambuf is None:
+ self._streambuf = val['_M_stringbuf'].address
Is this final fallback useful? It assumes no redirection, but there's
no reason that has to be true.
+
self._was_redirected = self._streambuf != val['_M_stringbuf'].address
def to_string(self):