Also, this reference card
is useful when first learning the gdb commands.
Basic features of a debugger
When you are execute a program that does not behave as you like, you need some way to step through you logic other than just looking at your code. Some things you want to know are:
gcc -g trees.cwhich will create the default a.out executable. To run this under the control of gdb, you type
gdb a.outThis starts up the text interface to the debugger. It's much easier to use gdb under an IDE, but this the real gdb interface.
Starts your program as if you had typed
a.out command-line argumentsor you can do the following
a.out < somefileto pipe a file as standard input to your program
Creates a breakpoint; the program will halt when it gets there. The most common breakpoints are at the beginnings of functions, as in
(gdb) break TraverseThe command break main stops at the beginning of execution. You can also set breakpoints at a particular line in a source file:
Breakpoint 2 at 0x2290: file main.c, line 20
(gdb) break 20When you run your program and it hits a breakpoint, you'll get a message and prompt like this.
Breakpoint 2 at 0x2290: file main.c, line 20
Breakpoint 1, Traverse(head=0x6110, NumNodes=4)
at main.c:16
(gdb)
Removes breakpoint number N. Leave off N to remove all breakpoints. info break gives info about each breakpoint
Provides a brief description of a GDB command or topic. Plain help lists the possible topics
Executes the current line of the program and stops on the next statement to be executed
Like step, however, if the current line of the program contains a function call, it executes the function and stops at the next line.
Keeps doing nexts, without stepping, until reaching the end of the current function
Continues regular execution of the program until a breakpoint is hit or the program stops
Reloads the debugging info. You need to do this if you are debugging under emacs, and you recompile in a different executable. You MUST tell gdb to load the new file, or else you will keep trying to debug the old program, and this will drive you crazy
Produces a backtrace - the chain of function calls that brought the program to its current place. The command backtrace is equivalent
prints the value of E in the current framein the program, where E is a C expression (usually just a variable). display is similar, except every time you execute a next or step, it will print out the expression based on the new variable values
Leave GDB.
If you have any questions, check out the full manual.