$linuxjunkies
>

gdb(1)

GNU Debugger — interactive debugging tool for inspecting and controlling program execution.

UbuntuDebianFedoraArch

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

FlagWhat it does
-ex COMMANDExecute a gdb command on startup (can be repeated)
-batchRun in batch mode; exit after processing commands, useful for scripts
-args PROGRAM ARGSPass arguments to the program being debugged
-x FILEExecute gdb commands from a file on startup
-qQuiet mode; suppress gdb startup messages
-tuiStart in text user interface mode with a source code window
-p PIDAttach to a running process by its process ID
--argsPass remaining command-line arguments to the executable

Examples

Start debugging myprogram; gdb loads the executable and waits for commands

gdb ./myprogram

Load myprogram and analyze the core dump file to see where it crashed

gdb ./myprogram core.12345

Attach gdb to the running process with PID 1234 (requires appropriate permissions)

gdb -p 1234

Start debugging myprogram and pass arg1 and arg2 as command-line arguments

gdb --args ./myprogram arg1 arg2

Set a breakpoint at main and automatically run the program on startup

gdb -ex 'break main' -ex 'run' ./myprogram

Non-interactively print a backtrace from a core dump and exit

gdb -batch -ex 'bt' -ex 'quit' ./myprogram core

Related commands