gdb(1)
GNU Debugger — interactive debugging tool for inspecting and controlling program execution.
Synopsis
gdb [OPTION]... [EXECUTABLE] [CORE]Description
gdb is a powerful interactive debugger that lets you run programs under its control, set breakpoints, inspect variables, and step through code line-by-line. It works with compiled languages like C, C++, and Fortran, and can attach to running processes or analyze core dumps.
Start gdb with a program name to load it, then use interactive commands to set breakpoints, run the program, and examine its state when it stops. Use quit or q to exit.
Common options
| Flag | What it does |
|---|---|
-ex COMMAND | Execute a gdb command on startup (can be repeated) |
-batch | Run in batch mode; exit after processing commands, useful for scripts |
-args PROGRAM ARGS | Pass arguments to the program being debugged |
-x FILE | Execute gdb commands from a file on startup |
-q | Quiet mode; suppress gdb startup messages |
-tui | Start in text user interface mode with a source code window |
-p PID | Attach to a running process by its process ID |
--args | Pass remaining command-line arguments to the executable |
Examples
Start debugging myprogram; gdb loads the executable and waits for commands
gdb ./myprogramLoad myprogram and analyze the core dump file to see where it crashed
gdb ./myprogram core.12345Attach gdb to the running process with PID 1234 (requires appropriate permissions)
gdb -p 1234Start debugging myprogram and pass arg1 and arg2 as command-line arguments
gdb --args ./myprogram arg1 arg2Set a breakpoint at main and automatically run the program on startup
gdb -ex 'break main' -ex 'run' ./myprogramNon-interactively print a backtrace from a core dump and exit
gdb -batch -ex 'bt' -ex 'quit' ./myprogram core