1
votes

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.

2

2 Answers

2
votes
(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"

0
votes

Probably you are looking for

info b

(which is short for info breakpoints) and gives the number in the first "column".

If you want to break when the variable gets modified, use a watch point instead. GDB will always break at the nearest executable code line after the breakpoint (if set by line). A variable declaration isn't executable code and therefore the assignment on the next (non-empty) line will be used.

If what you are looking for is to know at exactly which line it stops, I think there is no such thing on the GDB prompt. However, you can still use a watch point and customize what happens on your break and watch points using commands.


From the comment skwllsp wrote, here's how it would look:

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

Which says there is one breakpoint (1), which is enabled and it shows you the line, too. Disposition (Disp) and enabled (Enb) are useful, but please read the manual for a proper description. I can also warmly recommend the book "Art of Debugging" from Nostarch.