gdb is a utility for debugging and executing programs.
In order to be able to debug a program written in C or C++, it has to be
compiled with the -g option:
cc -g -o filename filename.c...
g++ -g -o filename filename.cc...
To start off gdb, type gdb filename at the command line. A few messages are printed, and then you are left at the (gdb) prompt:
% gdb filename <various messages> (gdb)Some of the important and most often used commands at the gdb prompt:
Used to set a breakpoint at the sourceline or the function. In the case of the sourceline, execution is stopped before any code on the line is executed. In the case of the function, execution stops when the function is entered.
Start execution of the program. If breakpoints are set, execution stops when the sourceline or function is reached. Otherwise, the program runs to completion. gdb prints a message stating the status of the program on termination.
Continue execution from where it stopped.
Kill execution of the program begin run. Typically used to prepare to re-start the program from the beginning.
Execute the next n source lines. The default value for n is 1. This command steps into functions.
Same as step, but the command steps past the fuctions.
Deletes all breakpoints
Deletes any breakpoints set on the sourceline or at the entry of function.
Print a backtrace of all the active functions on the stack.
Print the value of expression. The contents of variables in the program can be viewed through this command.
Print 10 lines centered at sourceline or starting from the beginning of function. By itself, print 10 more lines.
Display the set of commands available in gdb.
The commands in the file .gdbinit are executed as gdb initializes. gdb executes (if present) the file in the home directory. Then, this process is repeated using the current working directory. For more information on gdb, run the info (see the man page) facility from the shell prompt, then use the m command to enter the gdb documentation. You may also look at the man pages and gdb's help system.