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()
+                        if f.is_base_class][0]
+            if ios_base.name and 'basic_iostream' in ios_base.name:
+                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).
+                base_addr = val.cast(basetype).address
+                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())
+                self._streambuf = ios_addr.dereference()['_M_streambuf']
+        except (gdb.error, IndexError):
+            pass
+
+        if self._streambuf is None:
+            self._streambuf = val['_M_stringbuf'].address
+
         self._was_redirected = self._streambuf != val['_M_stringbuf'].address
 
     def to_string(self):

Attachment: signature.asc
Description: PGP signature

Reply via email to