8.2 Debuggers

8.2 Debuggers

The standard debugger on Linux systems is gdb, with DDD as a graphical user frontend. To enable full debugging in your programs, you must run the compiler with the -g option to write a symbol table and other debugging information into the executable. To start gdb on an executable named program, run this command:

gdb program

You should get a (gdb) prompt. To run program with the command-line arguments options, type this at the (gdb) prompt:

run options

If the program works, it should start, run, and exit as normal. However, if there's a problem, gdb stops, prints a stack trace and the failed source code, and throws you back to the (gdb) prompt. Because the source code fragment often gives you a hint about the problem, you often want to print the value of a particular variable that the trouble may be related to (print also works for arrays and C structures):

print variable

If you want to make gdb stop the program at any point in the original source code, use the breakpoint feature. In the following command, file is a source code file, and line_num is the line number in that file where gdb should stop:

break file:line_num

To tell gdb to continue executing the program, use this command:


To clear a breakpoint, type this:

clear file:line_num

This section provides only the briefest introduction to the debugger. gdb comes with a very extensive manual that you can read online, print, or buy as Debugging with GDB [Stallman/Pesch/Shebs].


If you're interested in rooting out memory problems and running profiling test, try Valgrind (http://valgrind.kde.org/).