Installation

Example

Using test suite as an example for debugging with gdb:

$ gdb ./testsuite.bin

This starts program testsuite.bin and halts execution at its entry point (usually the main function entry point).

First, let’s switch to a more useful view. In gdb, enter <CTRL-X> 2. This switches to a TUI (terminal user interface, a GUI in the terminal) consisting of three view sections: the source code at the top with assembly below, and the gdb command line at the bottom.

Or, write the content of this file to ~/.gdbinit for colored output and overall prettier gdb (incompatible with TUI apparently).

Searching for gdb dotfiles on github gives lots of other coder’s gdb tweaks like this one.

Now, enable pretty printing for displaying values:

(gdb) set print pretty on

You will find the integrated help system of gdb very useful:

(gdb) help
# display help for a command category like "running":
(gdb) help running
# display help for a specific command:
(gdb) help next

To debug a specific function like vector__push_back, add a break point at the function’s entry point using the gdb command break.

You can use auto-completion to choose from all functions named ’vector__’ like:

(gdb) break vector__<TAB><TAB> (select a function name) <ENTER>

To run the program, use the gdb command run. Execution will halt at the first break point.

(gdb) run

To continue execution in single operations step-by-step, use next or n:

(gdb) n

To repeat the last command, just hit <ENTER>.

Show the value of a variable with print (ensure that pretty-printing is enabled as described above):