Under Construction
This page is currently being developed for the 2025 iteration of the course.
The information or content on this page may change, or be outdated.
Keep checking back regularly to see the most up to date information.
gdb Cheatsheet#
Launching GDB#
Using GDB#
After you have launched GDB successfully, you can run the following commands to start, control, inspect and stop the program you are debugging.
-
Command Syntax Behaviour Frame Selection frame n Select a stack frame, where Frame 0 is the innermost (currently executing) Frame, Frame 1 is the frame that called Frame 0, Frame 2 is the frame that called Frame 1 and so on. The highest numbered Frame in the stack trace is the main function. f n up {n} Move up n frames. If n is omitted, n defaults to 1. Positive values of n will move to the outermost frames. down {n} Move down n frames. If n is omitted, n defaults to 1. Positive values of n will move to the innermost frames. Viewing Backtraces backtrace Print a backtrace of the entire stack, one line per frame for all frames in the stack. backtrace n Print a backtrace of the innermost n frames backtrace -n Print a backtrace of the outermost n frames Frame Information info frame Display information about the currently selected stack frame. info args Display arguments of the currently selected frame info locals Display variables local to the currently selected frame -
Command Syntax Behaviour Starting & Stopping Programs run Run your program under GDB kill Kill the process running under GDB Ctrl + C ... Managing Program Arguments set args strings Set the arguments to be used when your program is next ran (using the run command). strings is a list of string arguments. show args strings Show the arguments passed to the program when it is run. -
Command Syntax Behaviour Resuming Execution continue Continue program execution until the next breakpoint is hit. c Stepping (Source Code) step {count} Continue running your program until control reaches a different source line, then stop it and return control to GDB.
If count is included, GDB will step count times before stopping. If count is omitted, defaults to 1.s {count} next {count} Continue running your program until control reaches a different source line in the innermost stack frame . If the current frame contains a call to another function, it will execute the entire function before stopping.
If count is included, GDB will step count times before stopping. If count is omitted, defaults to 1.n {count} finish Continue running until just after the function in the currently selected stack frame returns. fin Stepping (Machine Instructions) stepi {count} Execute one machine instruction, then stop and return to the debugger.
Optional argument count behaves identically as described in source code stepping.si {count} nexti {count} Execute one machine instruction, skipping over function calls. This is similar to how next operates.
Optional argument count behaves identically as described in source code stepping.ni {count} -
Command Syntax Behaviour Examing Memory x addr Print memory starting at address addr x{/nfu} addr Print n counts of u unit size starting at address addr with the display format specified by f.
The possible values for f and u are explained below. -
Command Syntax Behaviour Inspecting Variables info locals Show all local variables of the current stack frame. print {/f} expr Computes the value of the expression expr and prints the result. expr can be any valid C expression, such as the name of variable in your program. The optional parameter /f is one of the format specifiers described in Format Specifiers. p {/f} expr Inspecting Registers info registers Display a list of integer registers and their values in the current stack frame. info r info register reg Display the value of register reg in the current stack frame. info r reg
Format Specifiers#
| Format Specifier | Output Format |
|---|---|
| x | Hexadecimal |
| d | Decimal |
| u | Unsigned Decimal |
| o | Octal |
| t | Binary |
| c | Character Constant |
| f | Floating Point |
| s | Null-terminated string |
Unit Sizes#
| Unit Size Specifier | Meaning |
|---|---|
| b | Bytes |
| h | Halwords (2 bytes) |
| w | Words (4 bytes) |
| g | Giant Words (8 bytes) |
Other Resources#
This is not an exhaustive cheatsheet. GDB is a very complex debugger with a lot of additional functionality we haven’t explained here. The commands listed above will be those you need to use most frequently.
If you would like to learn more about GDB, you can check out the GDB Documentation. This is a very detailed guide to GDB.
lldb#
If you are using macOS, you will have to use a different debugger - lldb.
Both debuggers offer similar functionality, but you will have to learn an alternate
command syntax. Fortunately, the lldb documentation has a page dedicated to
listing the lldb equivalent of many of the above gdb commands
available here.