Search code examples
gdbbreakpoints

GDB Getting the correct stop point when breakpoint set on variable declaration


Given the following code sippet:

1:    int main(int argc, char **argv) {
2:       int i;
3:    
4:       i = i + 1;
5:    
6:       ...
7:    
8:    }

When you set a breakpoint on line 2: in the above code and start debugging in GDB the execution actually stops on line 4:. This is the correct behavior of course BUT is it possible to query GDB for the correct stop point number before debugging... right after the creation of the breakpoint.


Solution

  • (gdb) b 2
    Breakpoint 1 at 0x400547: file main.cpp, line 2.
    

    This is what info b shows:

    (gdb) info b $bpnum
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x0000000000400547 in main(int, char**) at main.cpp:2
    

    and you need to do this for address in the column "Address":

    (gdb) info line *0x0000000000400547
    Line 4 of "main.cpp" starts at address 0x400547 <main(int, char**)+11> and ends at 0x400550 <main(int, char**)+20>.
    

    As you can see the real breakpoint on "Line 4 of main.cpp"