gdb has several multi-line commands. Once you finish inputting a multi-line command, use end to issue it. You can also exit out of the multiline input with Ctrl-C.
Vanilla GDB Usage
I compiled a list of commands that I have found useful.
Command-line
If the program segfaults but doesn’t dump corefile, consider running ulimit -c unlimited to bypass corefile size limits, or add <your-username> - core unlimited in /etc/security/limits.conf to persist this setting.
Common Configuration
Place the following in ~/.gdbinit or /etc/gdb/gdbinit to persist across debugging sessions.
Place the following in .gdbearlyinit
Disassembly
Breakpoints
Watchpoint
A watchpoint can be define to monitor the value of an expression (function calls allowed probably) and pause when the value changes.
Execution
Reversible Debugging
Reversible debugging is a powerful feature that allows you to run the program backwards, but you must begin recording process state first.
Inspect Memory
For x:
num is 1 by default
size could be
b (bytes)
h (half word / two bytes)
w (word / four bytes - default)
g (giant word / eight bytes)
Notes
Note that gdb assumes the memory to be integers of the size you requested, so be aware of little endianness!
GDB’s word size (32-bit) does not match convention for x86 (16-bit).
Naturally, size does not matter when fmt is not s or i.
The order of size and fmt does not matter, since the sets of letters used are disjoint.
Note: Often times, gef and pwndbg’s telescope command is helpful equivalent of examining pointer-sized hex values (e.g., telescope $rsp 20 is the same as x/20gx $rsp, but also automatically and recursively dereference valid pointers for you)
Program / Memory Info
Backtrace
Find byte sequence in memory
Alternatively, to find offset of a string in a library, e.g., libc, you can use grep -Rabo /bin/sh /lib/libc.so.6.
Python
Source
For debug builds (e.g., gcc -g):
TUI
Use Ctrl-X A to enter TUI mode. The default view is source code.
Use Ctrl-L to rerender the view (e.g., in case some printf statements from the debugged binary mess up the UI).
Use Ctrl-X 2 to add a second window.
Use the same keybind to switch between views (e.g., registers, source code, etc).
tui reg float to show floating registers
Ctrl-<P/N> to view previous commands (up and down arrows now scroll the source code view)