https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95547

            Bug ID: 95547
           Summary: gdb pretty-printing of std::unique_ptr less helpful
                    than it could be
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: terra at gnome dot org
  Target Milestone: ---

Created attachment 48680
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48680&action=edit
Preprocessed ttt.C

C++ smart-pointer pretty-printing in gdb isn't as helpful as it could
be.  This is in contrast to the container printing which I absolute
love.  Example:

#include <memory>
#include <optional>

void baz (std::optional<int> p) { abort(); }
void bar (std::shared_ptr<int> p) { baz (std::make_optional<int> (*p)); }
void foo (std::unique_ptr<int> p) { bar (std::make_shared<int> (*p)); }
int main() { foo (std::make_unique<int>(14)); }



welinder@CarbonX1:~$ g++ -std=c++17 -Wall -g -O0 ttt.C
welinder@CarbonX1:~$ gdb ./a.out 
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
[...]
(gdb) r
Starting program: /home/welinder/a.out 

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) where
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff7483801 in __GI_abort () at abort.c:79
#2  0x0000555555554d11 in baz (p=std::optional<int> = {...}) at ttt.C:4
#3  0x0000555555554d56 in bar (p=std::shared_ptr<int> (use count 1, weak count
0) = {...}) at ttt.C:5
#4  0x0000555555554db3 in foo (p=std::unique_ptr<int> = {...}) at ttt.C:6
#5  0x0000555555554e35 in main () at ttt.C:7


Note that other than the reference counts for shared_ptr there is
nothing useful printed for the parameters in the above.  (Where "useful"
for me excludes static information like the the types, YMMV; types
aren't printed for, say, plain pointers and ints.)

I realize that the situation is better for the more verbose answer to
"print p", but I don't see the need to take up 3 lines for a pointer.

I would like to see something like

For unique_ptr:
p=0x12345678   or   p=nullptr

For shared_ptr:
p=0x12345678/1  or  p=0x12345678/2/1  or  p=nullptr
(the middle one when weak pointers are present.)

For optional:
p={14}  or  p=-


This is with a fairly old compiler, but same behaviour observed with gcc10.

welinder@CarbonX1:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

Reply via email to