GDB Tutorial

Apr 14, 2026

Tags: gdb
Categories: tutorial


GDB , the GNU Project debugger, is a useful debugger for many languages (C, C++, Fortran, etc). Here I keep a set of useful commands for my records.

Using GDB

To use GDB, compile the program with debugging information. For standard C++ program, add -g flag when compiling, i.e. something like

g++ -g -o myprogram myprogram.cpp

For AMReX related application codes like Castro, compile with DEBUG=TRUE, i.e.

make DEBUG=TRUE

Now launch GDB with compiled program as the following.

gdb myprogram

Now inside gdb, set breakpoints to tell GDB where to pause. You can either set by line number of function name

(gdb) break main         # Break at the beginning of the main function
(gdb) break 42           # Break at line 42
(gdb) break file.cpp:55  # Break at line 55 of a specific file

Now run the program via run command. If the program needs command-line arguments, pass them after run, i.e. inputs in this case.

(gdb) run inputs

The program should stop at your breakpoints, use commands like print to debug.

Commands

Here are a set of useful commands that I keep track of: