Re: How to setup a breakpoint on constructor

Michael Elizabeth Chastain
2004-07-15T10:30:18Z

> Is there anything different in the constructor in comparision to normal
> function since I cannot break the program in there?

Yes, this is a long-standing problem with gdb and gcc 3.X.

See the PROBLEMS file:

gdb/1091: Constructor breakpoints ignored
gdb/1193: g++ 3.3 creates multiple constructors: gdb 5.3 can't set breakpoints

When gcc 3.x compiles a C++ constructor or C++ destructor, it generates
2 or 3 different versions of the object code. These versions have
unique mangled names (they have to, in order for linking to work), but
they have identical source code names, which leads to a great deal of
confusion. Specifically, if you set a breakpoint in a constructor or a
destructor, gdb will put a breakpoint in one of the versions, but your
program may execute the other version. This makes it impossible to set
breakpoints reliably in constructors or destructors.

gcc 3.x generates these multiple object code functions in order to
implement virtual base classes. gcc 2.x generated just one object code
function with a hidden parameter, but gcc 3.x conforms to a multi-vendor
ABI for C++ which requires multiple object code functions.

Things you can try:

. modify your program so that the constructors that you want to
breakpoint call some function that is not a constructor, and break
on that.

. run 'nm a.out | c++filt' to find the symbols in your program.
break on the absolute address: "break *0x01234567". this is
very crude (1960's technique) but it does work.

. use nm, c++filt, and 'strip -N' to strip out symbols for
not-in-charge constructors. This is scriptable, if someone
wants to write a little script.

It's a hard problem.

Michael C