On Fri, 27 Nov 2026 at 19:33, John David Anglin <[email protected]> wrote:
>
> Tested on hppa-unknown-linux-gnu with no regressions.
>
> Okay?
>
> Dave
> ---
>
> libstdc++: Fix StdStringStreamPrinter and hh_mm_ss printer on hppa
>
> On targets where GDB inferior function calls through PLT/vtables fail
> (such as hppa-linux-gnu), evaluating '$__stream->rdbuf()' inside
> StdStringStreamPrinter.__init__ triggers a SIGSEGV in the inferior.
> Additionally, accessing the static constexpr member
> std::chrono::hh_mm_ss::fractional_width raises 'gdb.error: value has
> been optimized out' when DWARF omits a materializable constant value.

This is two separate issues and should be two patches really.
The second one is PR 128817 which links to a GDB bug that causes the
incorrect "optimized out" claim (the test is compiled with -O0 so it
shouldn't be optimized out). But if we can make it work anyway, that's
nice.

> Locate the virtual base std::basic_ios directly (via GDB's cast or the
> Itanium C++ ABI vbase offset at vptr[-3]) to read _M_streambuf without

Do we use that ABI on all targets? Or at least all where GDB runs?

> executing code in the inferior, and fall back to deriving
> fractional_width from the Duration template argument's period when
> val['fractional_width'] cannot be evaluated directly.
>
> 2026-11-27  John David Anglin  <[email protected]>
>
> 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.
>         (StdChronoCalendarPrinter.to_string): Derive fractional_width from
>         the Duration template argument if val['fractional_width'] raises
>         gdb.error.
>
> diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py 
> b/libstdc++-v3/python/libstdcxx/v6/printers.py
> index 1bba377513c..2375ac6925d 100644
> --- a/libstdc++-v3/python/libstdcxx/v6/printers.py
> +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
> @@ -1153,13 +1153,34 @@ 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:
> +                base_addr = val.cast(basetype).address
> +                vptr_type = gdb.lookup_type('void').pointer().pointer()
> +                ptrdiff_type = 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):
> @@ -2251,9 +2272,20 @@ class StdChronoCalendarPrinter(printer_base):
>              return '{}/{}/{}'.format(y, m, val['_M_wdl'])
>          if typ.startswith('std::chrono::hh_mm_ss'):
>              fract = ''
> -            if val['fractional_width'] != 0:
> +            try:
> +                width = int(val['fractional_width'])
> +            except gdb.error:
> +                # static constexpr fractional_width may be optimized out
> +                dur = val.type.strip_typedefs().template_argument(0)
> +                period = (dur.strip_typedefs()
> +                          .template_argument(1).strip_typedefs())
> +                num = int(period.template_argument(0))
> +                den = int(period.template_argument(1))
> +                width = next((w for w in range(19)
> +                              if (num * (10 ** w)) % den == 0), 6)
> +            if width != 0:
>                  fract = '.{:0{}d}'.format(int(val['_M_ss']['_M_r']),
> -                                          int(val['fractional_width']))
> +                                          width)
>              h = int(val['_M_h']['__r'])
>              m = int(val['_M_m']['__r'])
>              s = int(val['_M_s']['__r'])

Reply via email to