An Introduction to GDB in Emacs

An Introduction to GDB in Emacs


GDB is the GNU Debugger. You can run it from the command line, or preferrably, from within Emacs. You can find details about using it from the command line here.

Here's the fastest way to get started:

Start emacs
Open up the .c file you want to debug. Split the window into two halves by typing:
C-x 2
Starting gdb in Emacs
Make sure your executable is compiled with the -g option. Type in
M-x gdb
You'll be prompted for the name of an executable. You should now have "windows," one for your gdb interaction, and the other showing your source file.

Breakpoints
Whenever the computer reaches a breakpoint, it pauses and gives you the debugger prompt just before it executes the line. Using Emacs makes setting breakpoints a snap.

Set a breakpoint by going to the source line of the program you're debugging, and typing:

C-x [space]
(That's the space bar.) GDB will tell you that you've just set a breakpoint.

You can also break on function entry by typing "b functionName." Typing "d 1" deletes breakpoint 1. Typing "disable 1" disables breakpoint 1. You can enable this breakpoint again by typing "enable 1". You can delete all breakpoints by typing "d."

Running your program in gdb
Once gdb is up, simply type "run" or "r" (most commands can be abbreviated to their first letter) to start your program. If your program takes command line arguments, simply add them after the run command.

Examining Data
Once you've gotten to where you want to start looking at variables, you'll generally use "print" (or just "p"):
print a
prints out the value of variable a, assuming a is valid in the current scope.
p (char) a
casts a to a char before printing it out.
print f(123)
execute any function f with arguments 123, and print its return value. This can have side effects.
x 0xfaf47
print out the contents of a given memory address.
display x
print out the value of x whenever the program is stopped.

Tracing
Step through a program using the step (execute line by line, entering any function calls) and next (execute line by line, skipping over function calls). If you're debugging inside of Emacs, gdb will draw an arrow before the to-be-executed line of your source file. If you step into functions, gdb will do its best to open up the source file responsible for that function.

continue resumes normal execution of the program until a breakpoint is hit.

finish resumes normal execution of the program until the end of this procedure or a breakpoint is hit (which ever comes first).

You can also set conditional breakpoints aand watchpoints. Try "help breakpoints" for more info.

Detecting segfaults
No brainer - once your code starts running and if a segfault occurs, the debugger alerts you and gives you a debugger prompt. Type in bt (the "backtrace" command). This prints out the current stack and you can immediately see where the segfault occurred.

Quitting
Type "quit".

This is by no means a complete listing, but it's what you'll use most of the time.


Orignal version by brd@cs.washington.edu
Modified by wolman@cs.washington.edu, Autumn 1997.