So what is wrong with v3 C++

Jim Blandy jimb@zwingli.cygnus.com
Fri Jun 29 13:40:00 GMT 2001


When it comes to prioritizing problems, there's some risk in a bunch
of non-C++ programmers like me trying to assess which bugs are most
important to fix, since we don't have daily experience showing us
which bugs interfere with our work the most.

I just talked about this on the phone with Ben Kosnik.  He says that
the bug causing him the most trouble is the simple inability to print
his objects.  Troubles with virtual base classes and stepping into
virtual functions are insignificant compared to the frustration of
being unable to see his data.

Here's a simple example that illustrates the problem:

$ cat scope-rtti.cc
#include <stdio.h>

namespace N
{
  struct A
  {
    int x, y;
    virtual int sum (void);
  };
};

int
N::A::sum ()
{
  return x + y;
}


main ()
{
  N::A a;

  a.x = 3;
  a.y = 4;
  printf ("%d\n", a.sum ());
}
$ $Gcc3B/g++ -g scope-rtti.cc -o scope-rtti
$ $D6/gdb/gdb scope-rtti
GNU gdb 2001-06-28-cvs (MI_OUT)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) break 25
Breakpoint 1 at 0x8048813: file scope-rtti.cc, line 25.
(gdb) run
Starting program: /home/jimb/c++v3/play/scope-rtti 

Breakpoint 1, main () at scope-rtti.cc:25
25        printf ("%d\n", a.sum ());
(gdb) print a
$1 = {_vptr.A = 0x8049908, x = 3, y = 4}

By default, GDB prints `a' using its compile-time type.  There's
nothing really ABI-specific going on here, so it works okay.

(gdb) set print object 
(gdb) print a
$2 = can't find class named `N::A', as given by C++ RTTI
(gdb) 

When we say `set print object', we ask GDB to print using `a''s
run-time type.  This is really what you need for debugging, I think:
when the compile-time type is just some base class that defines
nothing but a bunch of virtual functions (as is the case in the bug
report Ben just posted), the compile-time type is practically useless.

Unfortunately, since GDB mishandles the scoping, this feature doesn't
work.  If I remove the `namespace N' from the example, so the type's
name is simply `A' instead of `N::A', everything works fine.

What makes it especially urgent is that, for people working on the
standard C++ library implementation, *every* type is in the `std'
namespace.  So this problem will affect just about every object they
ever want to print.  And of course, ordinary C++ code will use the
standard library pretty frequently, too.

So, if I've understood the situation correctly (all you actual C++
users, please correct me), I think this is probably the first bug we
should fix.

I have no opinion yet on whether we would need to rewrite the dwarf 2
reader to fix this.



More information about the Gdb mailing list