Persistence of Breakpoints

We said "much later on" above to make the point that you should not exit GDB during your debugging session. For example, when you find and fix one bug, but other bugs remain, you should not exit and then re-enter GDB to use the new version of your program. That would be unnecessary trouble, and more importantly, you would have to re-enter your breakpoints.

If you do not exit GDB when you change and recompile your code, the next time you issue GDB's run command, GDB will sense that your code has changed and automatically reload the new version.

However, note that your breakpoints may "move." For instance, consider the following simple program:

1  main()
2  {  int x,y;
3     x = 1;
4     y = 2;
5  }

We compile, enter GDB, and set a breakpoint at line 4:

(gdb) l
1       main()
2       {  int x,y;
3          x = 1;
4          y = 2;
5       }
(gdb) b 4
Breakpoint 1 at 0x804830b: file a.c, line 4.
(gdb) r
Starting program: /usr/home/matloff/Tmp/tmp1/a.out

Breakpoint 1, main () at a.c:4
4          y = 2;

All well and good. But suppose you now add a source line:

1  main()
2  {  int x,y;
3     x = 1;
4     x++;
5     y = 2;
6  }

Then recompile—again, keep in mind that you have not left GDB—and again issue the GDB run command:

(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
'/usr/home/matloff/Tmp/tmp1/a.out' has changed; re-reading symbols.

Starting program: /usr/home/matloff/Tmp/tmp1/a.out

Breakpoint 1, main () at a.c:4
4          x++;

GDB did reload the new code, but the breakpoint has seemingly shifted from the statement

y = 2;

to

x++;

If you take a closer look, you'll see that the breakpoint actually has not moved at all; it had been at line 4, and it is still at line 4. But that line no longer contains the statement at which you had originally set your breakpoint. Thus, you will need to move the breakpoint by deleting this one and setting a new one. (In DDD, you can do this much more easily; see "Moving" Breakpoints in DDD.)

Eventually, your current debugging session will end, say, because it's time to eat, sleep, or relax. If you don't normally keep your computer running continuously, you will need to exit your debugger. Is there any way to save your breakpoints?

For GDB and DDD, the answer is yes, to some extent. You can place your breakpoints in a .gdbinit startup file in the directory where you have your source code (or the directory from which you invoke GDB).

If you are in Eclipse, you are in luck, because all of your breakpoints will be automatically saved and restored in your next Eclipse session.