This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: gdb breakpoint problem

On 12 June 2007 12:20, Daniel Jacobowitz wrote:

> On Mon, Jun 11, 2007 at 11:33:15PM -0400, wrote:
>> The breakpoints set at line 22, 23, 24 are all ignored. However, the
>> breakpoints set at line 16, 17, 18 are all good to break the program.
>> What's the difference? They are doing the same thing. Why gdb treated
>> them differently? Thanks for your comments.
> That's a known bug in GDB - it has trouble with the way GCC generates
> constructors for C++ classes.

  Specifically, it's down to the fact that GCC emits two constructor functions
which are called in different circumstances, but they both have the same name
and function signature (when unmangled - there is a difference in the mangled
names) so GDB is unable to distinguish them.

  NB that you can work around this; here's your testcase again:

/tmp/dosfs $ cat test.cpp
/* Test.cpp -- Test C++ program to be debugged with ddd */
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

class Test{
                void donothing();

void Test::donothing(){
        std::string s = "abc"; // line 16
        s = s+s; // line 17
        std::cout<<s<<std::endl; // line 18

        std::string s="abc"; // line 22
        s = s+s; // line 23
        std::cout<<s<<std::endl; // line 24

int main(int argc, char *argv[]){
        Test test;
        return 0;

/tmp/dosfs $ c++ -g -O0 Test.cpp -o test

  Here we see how there are two constructor functions with different mangled
names but they both unmangle to the same thing:

/tmp/dosfs $ nm ./test.exe | grep Test
004016e6 t __GLOBAL__D__ZN4Test9donothingEv
004016ca t __GLOBAL__I__ZN4Test9donothingEv
00401150 T __ZN4Test9donothingEv
004014a2 T __ZN4TestC1Ev
004012f4 T __ZN4TestC2Ev
/tmp/dosfs $ c++filt

  You can use the underlying mangled names to set breakpoints (remember to
remove the first leading underscore when trying to use a symbol shown by 'nm'
as the C equivalent in gdb):

/tmp/dosfs $ gdb ./test.exe
GNU gdb
Copyright (C) 2006 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-cygwin"...
(gdb) break _ZN4TestC1Ev
Breakpoint 1 at 0x4014a8: file Test.cpp, line 20.
(gdb) break _ZN4TestC2Ev
Breakpoint 2 at 0x40132c: file Test.cpp, line 21.
(gdb) r
Starting program: /tmp/dosfs/test.exe
Loaded symbols for /win/c/WINDOWS/system32/ntdll.dll
Loaded symbols for /win/c/WINDOWS/system32/kernel32.dll
Loaded symbols for /usr/bin/cygwin1.dll
Loaded symbols for /win/c/WINDOWS/system32/advapi32.dll
Loaded symbols for /win/c/WINDOWS/system32/rpcrt4.dll

Breakpoint 1, Test::Test (this=0x23ccb7) at Test.cpp:21
21              std::string s="abc"; // line 22
(gdb) c

Program exited normally.

Can't think of a witty .sigline today....

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]