The Science of Debugging
Chapter 6


6.5.2 Advanced Breakpoints

We've already covered the standard way to use breakpoints, but there's a few advanced techniques that come in handy. The first we'll cover is the ability to set a breakpoint on a function. I'm sure you'll immediately realize the convenience of being able to just specify a function name instead of having to look up line numbers. I can replace the breakpoint above with that strategy;

(gdb) break bubblesort
Breakpoint 2 at 0x80485a7: file gdbex2.c, line 28.
(gdb)






As you can see, it actually calls the function, but stops before it's executed the first line, which is good enough for me.

Next we'll cover how to disable breakpoints. Why would you want to disable a breakpoint? Well, lets pretend that our bubblesort function's inner loop starts mucking up when i is equal to 50. If I put a breakpoint in the loop, I'll have to retype 'continue' 50 times before I get to the section I want, and that's assuming I don't miscount. Let's see how I would use the 'ignore' command to skip the first 49 iterations:

(gdb) break gdbex2.c:31
Breakpoint 3 at 0x80485fa: file gdbex2.c, line 31.
(gdb) info breakpoints
Num Type            Disp Enb Address       What
1       breakpoint   keep y     0x080484f4 in main at gdbex2.c:15
2       breakpoint   keep y     0x080485a7 in main at gdbex2.c:28
3       breakpoint   keep y     0x080485fa in main at gdbex2.c:31
(gdb) ignore 3 49
Will ignore next 49 crossings of breakpoint 3.
(gdb) run
Starting program: /home/dughi/gdbexamples/gdbex2

Breakpoint 1, main (argc=1, argv=0xbffffd04) at gdbex2.c:16
16       bubblesort(array,100);
(gdb) continue
Continuing.
BR> Breakpoint 2, bubblesort (array=0xbffffb28, size=100) at gdbex2.c:28
28       for(i=0;i < size -1;i++) {
(gdb) continue
Continuing.

Breakpoint 3, bubblesort (array=0xbffffb28, size=100) at gdbex2.c:31
31              temp=array[j];
(gdb) print j
$8 = 51
(gdb)





























Now, to explain what it was I actually did...

You can see I created a new breakpoint with the 'break' command - that's a break at the start of the for loop within bubblesort(). The next command is our old friend 'info'. One of the many arguments it takes is 'breakpoints' and obviously enough, it gives us information about which breakpoints exist. I used this to see which checkpoint had which number even though we can see the breakpoint number that's assigned when we set a breakpoint. If you're setting many of these, it's easier to just double check before you run a command).

Using this information, I filled out the syntax of the 'ignore' command ; ignore <breakpoint> <number of times before it becomes active again>.

Another common usage of breakpoints is a one-time only breakpoint. You can use the command 'tbreak' to achieve this end. Aside from only working once, it is identical to 'break' in all aspects.

I'm starting to feel like I'm squeezing alot into this section, but there is just one more thing to say on the subject of breakpoints; how to turn them off. The command is called 'disable' and it takes a breakpoint number. I'd show you an example of how it works, but it should be fairly obvious.



Index
6.1.4.1 Breakpoints 6.1.4.3 Watchpoints