File: ~rlt/cs240/general/gdb -- gdb cheat sheet

Copyright 1988 Paul English (, edited by eoneil by permission

gdb is the GNU source level debugger. To use gdb, you must make sure you compile your programs with the `-g' flag, e.g., gcc -g subs.c

To run gdb on a program, just enter `gdb progname' (or M-x gdb in emacs). Note that most commands can be abbreviated to the initial character. Pressing RETURN repeats the last command in a useful way. This is often done with `list', for example.

Here are the basic gdb commands:

r   	    - run program
r small     - run program, and give it "small" as an argument.
r < infile  - run program, redirect input from file infile.

b main	    - set a breakpoint in function main().
b 36	    - break at line 36 of current source file.
i b	    - info breakpoints. Lists all breakpoints now set up.
d	    - delete all breakpoints (d 1 for just #1).

n   	    - execute next line of code.
s	    - like next, but steps inside function calls.
c	    - continue.  Run from this point until breakpoint or program exit.

p foo	    - print variable foo
p/x foo	    - print foo in hex (/o = octal, /d = decimal)
i lo	    - info locals: values of all local vars, current function
i var	    - info variables: values of global/static vars
i s	    - info stack: calls made to get to this execution point.
x 0x20034   - examine memory address 0x20034
x/s 0x20034 - examine memory, addr 0x20034, as a string  (also x/f

l	    - list source code (list 18 = code near line 18, list main, etc.)
i fun	    - list of all functions in the program.

h	    - top-level help, lists topics
h b	    - help on break command
h breakp    - help on topic of breakpoints

q   	    - quit gdb