![]() |
Chapter 6 | ![]() |
The first thing we need to do though (as in all debugging) is take a guess where our program is failing. Though gdb gives us a good hint, it may not always be accurate, the error may have occured in a previous frame, or even in previously executed code in the same function. It'd be nice to examine each potential spot if we don't know which is the culprit, and gdb's breakpoints allow you to do this.
Breakpoints are your way of telling gdb "I want to know what's happening there, so even though you haven't crashed, drop me into command mode so I can look around!" It's very useful, especially for non-crash bugs. In effect, your program will 'pause' at that spot. To examine them though, we're going to need a more complicated program than gdbex1. Cut and paste this code into a file called gdbex2.c:
#include <stdio.h> #include <stdlib.h> int bubblesort(int *array, int size); int main(int argc,char *argv[]) { int array[100]; int i; srand(1); for(i=0;i<100;i++) { array[i] = rand() % 300; } bubblesort(array,100); for(i=0;i<100;i++) { printf("%d\n",array[i]); } } int bubblesort(int *array, int size) { int i,j; int temp; for(i=0;i < size -1;i++) { for(j=0;j<size-2;j++) { if(array[j] > array[j+1]) { temp=array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } }Compile it with the command 'gcc -g -o gdbex2 gdbex2.c'. Notice that it doesn't contain any (crashing) errors. Try running this through gdb, and you'll notice it runs merrily on its way and exits without a problem.
That's great, except you really wanted to look at the array before it went into bubblesort(), so that way you know for a fact that it's working correctly and sorting. Go ahead and load gdbex2 into gdb, but don't type 'run' yet. Instead, we're going to set a breakpoint with the command 'break'.
(gdb) break gdbex2.c:15 Breakpoint 1 at 0x80484f4: file gdbex2.c, line 15. (gdb) |
Now, we have a breakpoint set on line 15, in the file gdbex2.c. If you only have one file, you don't have to specify the file name, but since that's rarely the case I've included the full argument here.
Go ahead and enter the 'run' command. Here's the output I get:
(gdb) run Starting program: /home/dughi/gdbexamples/gdbex2 Breakpoint 1, main (argc=1, argv=0xbffffd04) at gdbex2.c:16 16 bubblesort(array,100); (gdb) |
You'll notice that the program stopped (more or less) right where I wanted it to. When setting a breakpoint, you can't stop directly on a blank line or comment (like line 15!). Instead, it will advance to the next piece of functional code, and stop right before it's executed. Now, if I wanted to, I could use the 'print' command to look through the contents of the variable array[].
You may want to continue the program after you've hit a breakpoint, and there are several ways to do this. The simplest way is with the command 'continue'. This will let your program proceed from where ever it has stopped. There will be more on this later in section 6.1.4.4 - Stepping through code.
![]() |
Index | ![]() |
6.1.3.3 Examining the Stack | 6.1.4.2 Advanced Breakpoints |