Skip to content
systemcoding.dev
Debugging

GDB Cheat Sheet: The Commands You Use Every Day

A GDB command reference grouped by task, with the short form of each command, an example and the case where that command is the wrong choice.

7 min read
Summarize in AI
Flat illustration of a debugger stopped at a breakpoint beside the register values, with the GNU, LLVM and Arm logos.

This is the reference to keep open in a second window while you learn GDB. It lists the commands you will type most, grouped by what you are trying to do, with the short form where one exists and a sentence on when each is the wrong tool. The commands are for GDB 16.3 and have been stable for many releases, so older versions behave the same unless a note says otherwise. Each group links in spirit to one post in this series that covers it in depth; this page is for recall, not for learning. Where an example needs a program, assume a C program built with gcc -g -O0.

Start and stop

**gdb ./prog** loads the program and its symbols without running it. Use gdb -q to skip the banner. Not for a process that is already running; use -p for that.

**gdb --args ./prog a b** starts GDB with the program's command line arguments set. Not needed if you only type run a b once, which does the same thing for that session.

**run** (r) starts the program, with any arguments you add or the ones stored from last time. If the program is running, GDB asks whether to restart. Not for a program you attached to; it was already running and run would start a second copy.

**start** sets a temporary breakpoint at main and runs to it. It is the quickest way to the first line. Not if main is not where the interesting code begins, such as a program with constructors or a library whose startup you are debugging.

**continue** (c) resumes until the next breakpoint, signal or exit. continue 3 ignores the current breakpoint two more times. Not for stepping a single line; use next.

**kill** ends the process GDB is debugging. Not for a process you attached to unless you mean to end it; use detach.

**quit** (q, or Ctrl-D) leaves GDB, and kills a process GDB started or detaches from one it attached to. Not mid-investigation if you want to keep the session; GDB has no way to save a live process, only gcore for a core file.

Breakpoints

**break LOCATION** (b) stops where the location is reached. The location can be a function, file.c:12, a line number, +2 or *0xADDRESS. Not for something that happens a million times; add a condition or an ignore count.

**tbreak LOCATION** is a breakpoint that deletes itself after one hit. Not when you want to stop every time.

**break LOCATION if EXPR** stops only when the expression is true. Not on a hot path, where evaluating the condition costs a stop per hit.

**info breakpoints** (i b) lists breakpoints, watchpoints and catchpoints with hit counts. Not a way to change them; for that use the commands below.

**disable N, enable N, delete N** turn a breakpoint off, on, or remove it. delete without a number removes all of them. Not delete when you will want it back; disable it.

**condition N EXPR** changes the condition of an existing breakpoint, and ignore N COUNT skips the next COUNT hits. Not for conditions that depend on timing, since GDB's pause changes the timing.

**watch EXPR** stops when the expression's value changes, using a hardware debug register when it can. rwatch stops on reads and awatch on both. Not for a variable that changes constantly, and not for a region larger than the hardware can cover: a large buffer falls back to slow software stepping.

**catch EVENT** stops on an event such as fork, exec, syscall write, throw or signal SIGSEGV. Not a replacement for a breakpoint on your own code.

Stepping

**next** (n) runs one source line, running any call on it as a whole. Not when you want to see inside the call.

**step** (s) runs one source line and enters a function call if the callee has debug information. Not for lines that call library functions without symbols, where it behaves like next.

**finish** (fin) runs until the current function returns and prints the value. Not if the function never returns, such as an infinite loop, where it just waits.

**until** (u) is next that never goes backward, which makes it the way out of a loop. until LINE runs to a line in the current frame. Not for jumping into another function; use advance LOCATION.

**advance LOCATION** runs until the location is reached, or the current function returns. Not as a replacement for a temporary breakpoint when you want to stop in another thread.

**stepi** (si) and **nexti** (ni) run one machine instruction. Not for stepping over source lines, which takes a hundred keystrokes in an optimised function.

Press Enter on an empty line to repeat the last stepping command. Not safe after run or kill, which GDB deliberately does not repeat.

Looking at the program

**print EXPR** (p) shows the value of any C expression, and print/x EXPR in hexadecimal. Not for memory with no type; use x.

**print ARR@N or print *PTR@N** shows N elements starting at a place. Not for ranges larger than set print elements, 200 by default, unless you raise the limit.

**display EXPR** prints an expression after every stop, and undisplay N removes it. Not for expressions with side effects such as function calls that change state.

**info locals and info args** list the locals and arguments of the selected frame. Not to see variables of a caller; select its frame first with up.

**ptype EXPR** shows the full definition of a type or expression, and whatis just the type name. Not a way to see a value.

**x/NFU ADDR** examines memory, with a count, a format and a unit size. x/s shows a string, x/10i $pc ten instructions. Not for values the type system already describes well; print is easier.

**list** (l) shows source around the current line, and list FUNC or list 20,40 shows a place or a range. Not for browsing a large codebase; use your editor.

**info source and info line LOCATION** say which file and line an address or place belongs to. Not for the addresses of variables; use info address.

The call stack

**backtrace** (bt, where) prints the stack, innermost frame first. bt N shows N frames, bt -N the outermost N, and bt full adds locals. Not as the first step when the program hung and has several threads; use thread apply all bt.

**frame N, up, down** select a frame. They do not run anything. Not to change what the program does; they only change which frame expressions use.

**info frame** shows the selected frame's addresses and saved registers. Not for everyday use; it matters when a stack is damaged.

**thread N and info threads** list and switch threads. thread apply all bt runs a command on every thread. Not needed in a single-threaded program.

Registers and instructions

**info registers** prints the general-purpose registers, and info registers rip eflags just those. print $rax uses one in an expression. Not for floating-point values; use info all-registers or print $xmm0.

**disassemble** lists the current function's instructions, and disassemble /s interleaves source. Not for a function you could read in C unless you suspect the compiler.

**set disassembly-flavor intel** switches from AT&T to Intel syntax. Not a persistent change unless you put it in ~/.gdbinit.

Changing things

**set var VAR = VALUE** assigns to a variable in the running program. print VAR = VALUE does the same and shows the result. Not safe on a production process whose invariants you do not know.

**call FUNC(ARGS)** runs a function in the program and prints its result. Not for a function that takes locks the stopped program may hold, which can deadlock.

**return** pops the current frame and returns immediately, optionally with a value. Not for restoring state a function was meant to clean up; it skips the function's remaining code.

**jump LINE** resumes execution at a different line. Not outside the current function, where the stack is wrong.

Processes and files

**attach PID** or gdb -p PID attaches to a running process, and **detach** lets it go. Not for a process you want to keep running if you quit without detaching first, as a habit: type detach.

**info proc mappings** shows the process's memory map. Not for a core file with no live process; there it shows what the core recorded.

**generate-core-file** (gcore) writes a core file of the live process. Not as a substitute for a debugger session when you need to see timing.

**file ./prog, symbol-file FILE and core-file FILE** load a program, symbols or a core. Not needed when you start GDB with the arguments.

Settings

**set pagination off removes the "press return for more" pause, and set confirm off removes the prompts. set print pretty on prints structs one field per line. show** with a setting's name prints it. Not as a replacement for putting the ones you always want in ~/.gdbinit.

**help COMMAND and apropos WORD** document and search commands. Not a substitute for the manual when you need the details of a setting.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.