On Mon, 30 Nov 2026 at 16:41, John David Anglin <[email protected]> wrote:
>
> On 2026-11-30 8:04 a.m., Jonathan Wakely wrote:
> > On Mon, 30 Nov 2026 at 12:55, Jonathan Wakely <[email protected]> wrote:
> >>
> >> 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.  Attempt GDB's native virtual-base cast
> >> (val.cast(ios_base)), which uses DWARF vbase location info. If that
> >> fails, assume the stream has not bee redirected to another streambuf.
> >>
> >> libstdc++-v3/ChangeLog:
> >>
> >>         * python/libstdcxx/v6/printers.py (find_base): New function.
> >>         (StdStringStreamPrinter.__init__): Use find_base to find  the
> >>         std::basic_ios virtual base then check its _M_streambuf member.
> >>         Remove the _was_redirected member and use _streambuf!=None to
> >>         imply redirection.
> >>         (StdStringStreamPrinter.to_string): Check _streambuf instead of
> >>         _was_redirected.
> >>         (StdStringStreamPrinter.display_hint): Likewise.
> >>         * testsuite/libstdc++-prettyprinters/debug.cc: Test redirected
> >>         ostringstream.
> >>         * testsuite/libstdc++-prettyprinters/simple.cc: Likewise.
> >>         * testsuite/libstdc++-prettyprinters/simple11.cc: Likewise.
> >>
> >> Co-authored-by: John David Anglin <[email protected]>
> >> ---
> >>
> >> This seems simpler and more robust. Does it work for hppa-linux-gnu?
>
> I tested the revised change with oops adjustment on hppa-linux-gnu and it
> works.  There are no fails in the prettyprinters.exp testsuite.

Great, thanks for checking it. I've pushed that to trunk now, and will
backport it too.

>
> >>
> >> Just define a find_base function which walks the base classes looking
> >> for one of the right name, then use that in the stringstream printer to
> >> find the base class, and cast to it.
> >>
> >>  libstdc++-v3/python/libstdcxx/v6/printers.py  | 38 ++++++++++++-------
> >>  .../libstdc++-prettyprinters/debug.cc         |  3 ++
> >>  .../libstdc++-prettyprinters/simple.cc        |  3 ++
> >>  .../libstdc++-prettyprinters/simple11.cc      |  3 ++
> >>  4 files changed, 34 insertions(+), 13 deletions(-)
> >>
> >> diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py 
> >> b/libstdc++-v3/python/libstdcxx/v6/printers.py
> >> index 8927b7c4fd86..08c1649d7322 100644
> >> --- a/libstdc++-v3/python/libstdcxx/v6/printers.py
> >> +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
> >> @@ -105,8 +105,6 @@ else:
> >>  # Starting with the type ORIG, search for the member type NAME.  This
> >>  # handles searching upward through superclasses.  This is needed to
> >>  # work around http://sourceware.org/bugzilla/show_bug.cgi?id=13615.
> >> -
> >> -
> >>  def find_type(orig, name):
> >>      typ = orig.strip_typedefs()
> >>      while True:
> >> @@ -125,6 +123,18 @@ def find_type(orig, name):
> >>          else:
> >>              raise ValueError("Cannot find type %s::%s" % (str(orig), 
> >> name))
> >>
> >> +# Similar to find_type but searches all base classes of ORIG for base 
> >> NAME.
> >> +def find_base(orig, name):
> >> +    typ = orig.strip_typedefs()
> >> +    for f in typ.fields():
> >> +        if f.is_base_class:
> >> +            if '::{}<'.format(name) in f.type.name:
> >> +                return f.type
> >> +            else:
> >> +                t = find_base(f.type, name)
> >> +                if t is not None:
> >
> > Oops, find_base never returns None, it just raises ValueError when the
> > base isn't found. So if that happens here then we abandon the whole
> > function and do not keep searching in any other bases.
> >
> >> +                    return t;
> >> +    raise ValueError('Cannot find base class {} of {}'.format(name, orig))
> >
> > It should be:
> >
> > def find_base(orig, name):
> >     typ = orig.strip_typedefs()
> >     for f in typ.fields():
> >         if f.is_base_class:
> >             if '::{}<'.format(name) in f.type.name:
> >                 return f.type
> >             else:
> >                 try:
> >                     return find_base(f.type, name)
> >                 except ValueError:
> >                     pass
> >     raise ValueError('Cannot find base class {} of {}'.format(name, orig))
> >
> >
> >
> >>
> >>  _versioned_namespace = '__8::'
> >>
> >> @@ -1145,7 +1155,6 @@ class StdStringBufPrinter(printer_base):
> >>      def display_hint(self):
> >>          return 'string'
> >>
> >> -
> >>  class StdStringStreamPrinter(printer_base):
> >>      """Print a std::basic_stringstream."""
> >>
> >> @@ -1153,23 +1162,26 @@ 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()')
> >> -        self._was_redirected = self._streambuf != 
> >> val['_M_stringbuf'].address
> >> +        # Locate the virtual base std::basic_ios to read _M_streambuf
> >> +        # and see if it has been set to something other than _M_stringbuf.
> >> +        self._streambuf = None
> >> +        try:
> >> +            # Get the gdb.Type for the std::basic_ios base class.
> >> +            ios = find_base(val.type, 'basic_ios')
> >> +            streambuf = val.cast(ios)['_M_streambuf']
> >> +            if streambuf != val['_M_stringbuf'].address:
> >> +                self._streambuf = streambuf
> >> +        except (gdb.error, IndexError):
> >> +            pass
> >>
> >>      def to_string(self):
> >> -        if self._was_redirected:
> >> +        if self._streambuf:
> >>              return "%s redirected to %s" % (
> >>                  self._typename, self._streambuf.dereference())
> >>          return self._val['_M_stringbuf']
> >>
> >>      def display_hint(self):
> >> -        if self._was_redirected:
> >> +        if self._streambuf:
> >>              return None
> >>          return 'string'
> >>
> >> diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc 
> >> b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
> >> index 07db1bbdba04..beefd658fdb4 100644
> >> --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
> >> +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
> >> @@ -124,6 +124,9 @@ main()
> >>    std::stringstream redirected("xxx");
> >>    
> >> static_cast<std::basic_ios<std::stringstream::char_type>&>(redirected).rdbuf(sstream.rdbuf());
> >>  // { dg-final { regexp-test redirected {std::.*stringstream redirected to 
> >> .*} } }
> >> +  std::ostringstream redirected2("xxx");
> >> +  
> >> static_cast<std::basic_ios<std::ostringstream::char_type>&>(redirected2).rdbuf(sstream.rdbuf());
> >> +// { dg-final { regexp-test redirected2 {std::.*ostringstream redirected 
> >> to .*} } }
> >>
> >>    std::cout << "\n";
> >>    return 0;                    // Mark SPOT
> >> diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc 
> >> b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
> >> index c9a7e50dd596..835af8ec5570 100644
> >> --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
> >> +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple.cc
> >> @@ -177,6 +177,9 @@ main()
> >>    std::stringstream redirected("xxx");
> >>    
> >> static_cast<std::basic_ios<std::stringstream::char_type>&>(redirected).rdbuf(sstream.rdbuf());
> >>  // { dg-final { regexp-test redirected {std::.*stringstream redirected to 
> >> .*} } }
> >> +  std::ostringstream redirected2("xxx");
> >> +  
> >> static_cast<std::basic_ios<std::ostringstream::char_type>&>(redirected2).rdbuf(sstream.rdbuf());
> >> +// { dg-final { regexp-test redirected2 {std::.*ostringstream redirected 
> >> to .*} } }
> >>
> >>    std::cout << "\n";
> >>    return 0;                    // Mark SPOT
> >> diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc 
> >> b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
> >> index e0ca1d5c3eb3..474d77afe5b8 100644
> >> --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
> >> +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/simple11.cc
> >> @@ -170,6 +170,9 @@ main()
> >>    std::stringstream redirected("xxx");
> >>    
> >> static_cast<std::basic_ios<std::stringstream::char_type>&>(redirected).rdbuf(sstream.rdbuf());
> >>  // { dg-final { regexp-test redirected {std::.*stringstream redirected to 
> >> .*} } }
> >> +  std::ostringstream redirected2("xxx");
> >> +  
> >> static_cast<std::basic_ios<std::ostringstream::char_type>&>(redirected2).rdbuf(sstream.rdbuf());
> >> +// { dg-final { regexp-test redirected2 {std::.*ostringstream redirected 
> >> to .*} } }
> >>
> >>    std::cout << "\n";
> >>    return 0;                    // Mark SPOT
> >> --
> >> 2.56.1
> >>
> >
>
>
> --
> John David Anglin  [email protected]
>

Reply via email to