GDB (Gnu DeBugger)


Overview

GDB is an immensely useful tool to help you debug your C and assembly programs.

  • It lets you insert breakpoints into your programs so that you can stop execution and examine the contents of memory and registers.
  • It supports single-stepping your program one line of source code at a time.
  • It leads to much more productive debugging than just using print statements (e.g. printf).

The time you spend getting familiar with GDB will be an excellent investment.


Resources

    • A handy dandy guide to the most commonly used GDB commands. Useful to have open while using GDB (and going through the other resources here).
    • This very useful video shows you how to get started with GDB.
    • Looking for more details? Find them here thanks to Norman Matloff at UC Davis.

Examples

Here are some code samples for playing with GDB along with some example commands you can try:


Text User Interface

Text User Interface (TUI) mode of GDB can nicely show your code and the value of registers as you debug!
Its use is entirely optional for 351, but we wanted you to be aware of it as it may help some of you.

Opening TUI Mode

Most TUI commands will automatically start TUI mode, but you can also explicitly open GDB in TUI mode using:

[attu]$ gdb -tui <filename>

Layout Views

Of particular interest, you can bring up the disassembly and registers view using:

(gdb) layout asm
(gdb) layout regs

Note that you want to do them in that order so that the registers window is on top. Then as you execute instructions (stepi or si for assembly instructions), the assembly view will highlight the next instruction (not executed yet) and the registers view will highlight any changed registers.

Formatting Issues

Unfortunately, there are some annoying formatting issues that sometimes pop up while using TUI mode. If things start to look weird, run the following command to set things straight:

(gdb) refresh

You can also refresh the window by typing Ctrl-l.

Navigating within TUI

You may notice that up-arrow and down-arrow to cycle through commands no longer works. You can use focus cmd to move the TUI focus to the command window, which will fix this. Alternately, if you prefer to keep focus elsewhere, Ctrl-p and Ctrl-n will toggle backward and forward through commands, respectively.