Some gdb Notes

William T Krieger... Oct 2005

Enjoy this ever-so-brief overview of gdb, the Gnu debugger.

The official documentation is at:

http://www.gnu.org/software/gdb/documentation/

With a tiny learning curve, gdb will allow you to:

  • execute your program one line at a time
  • set breakpoints, specific points in your program where you want to stop
  • inspect register values
  • inspect memory values

Compiling for gdb

To use gdb, you need to tell gcc that you want to debug your program using the -g option, like this:

gcc -g test.s
gdb a.exe

Steering gdb

OK, so you've assembled using -g, you've cranked up gdb, and now what...

Well, you can issue commands to gdb to steer it in the direction that you want it to go. You can enter help and gdb will list all the groups of commands that it understands. Here is a list of the commands that I think you are most likely to use:

Command Shortcut Description
help <topic> h List help on any command or group. With no parameter, gdb lists all the command groups it has.
file <exec> f Load an executable file into gdb
list <num> l List code at the current line number, or specify a number
run r Run the executable until completion or the next breakpoint
next n Execute the next instruction
step s Execute the next instruction, stepping into function calls
break <place> b Pause execution at the specified "place"... it can be either a function name or a line number
info registers i reg List the value of all registers
x/<fmt> <addr> x Examine memory location... this is a complex, but useful, debugging command... see below for more!
quit q Quit

OK, a quick note on the "x" command to examine the values at memory locations. Usually, your memory location will come from some register, like -12(%ebp) or something. You'll want to use the memory location listed as the register's value when using the "x" command in the debugger. For example:

  • x/d 0xFF56A3... displays the int (d is for decimal) at memory location FF56A3. You must prepend the "0x", signifying that a hexadecimal value is being entered.

You can also use /x and /c for examining hexadecimal and character values. For more information, please enter "help x" into gdb.

BTW, I have gotten gdb to crash on occasion. If it happens, try and set a different breakpoint to avoid the problem.

Getting more help

Googling this:

gdb "assembly code" tutorial 

led me to these online gdb tutorials (like this one, but with more details and examples):