Some Notes on Debugging C Using gdb

If you want to use gdb to debug a C program, be sure to compile the program using the -g option to the gcc compiler command. For example

gcc -g -o myprog myprog.c

The -g option tells the compiler to include information needed by the debugger in the compiled program (such as line numbers and names for variables).

To run the program under the control of the debugger, run gdb and give the name of the executable program as a command-line argument:

gdb myprog

The executable should be in the current directory where you are working. You will get the prompt "(gdb)" from the debugger. You can enter gdb commands at this prompt. The program does not start running automatically; you need to start it with gdb's run command. Often, however, you will want to set some breakpoints before you do that...

Debugging Concepts

You are probably familiar with the Java debugger in Eclipse. gdb can do much the same thing for a C program, on the command line. A debugger allows you to control the execution of a program by setting breakpoints (places in the source code where the program will pause automatically) and watchpoints (which cause the program to paused when the value of a variable or expression changes). A breakpoint can be conditional, meaning that the execution will pause only if some specified condition evaluates to true.

When the program is paused, it is possible to check the values of variables and expressions and even to set the value of a variable. You can view information about the function call stack. You can resume execution, or you can execute the program one step at a time.

The rest of this document discusses gdb commands to do all these things. Remember that these commands are given at the (gdb) prompt while the debugger is running. The most common commands have one or two letter equivalents (such as "c", "s", and "n" for "continue", "step", and "next"). Any command can be abbreviated to the shortest unambiguous prefix.

Program Execution

Here and in the rest of this document, <location> can be a line number in the current source code file or a function name, or it can be of the form file_name:line_number to mean a line number in the specified file.

Breakpoints and Watchpoints

A watchpoint is actually a kind of breakpoint. The info break command will list watchpoints as well as breakpoints, including watchpoint numbers. Watchpoint numbers can be used with the disable, enable, and delete commands.

A watchpoint will automatically be deleted if the expression contains local variables, and one of those variables goes out of scope.

View Information and Set Values

Note that it is possible to print arrays using the print command. If the array A has been statically allocated, such as "int A[10]", then print A will print a list containing all of the elements of A. However, often you are working with pointers instead of arrays, and in that case, gdb doesn't know how long the array is. If p is a pointer, and you want to treat it as an array of 10 elements, you can say print *p@10. This will print out the 10 items starting with *p.