My quick gdb cheatsheet

04/01/2024

Here lies a *very* short list of the most basic commands one could need to use gdb (gnu debugger). + small setup guide

The cool thing about having a blog is that you have an excuse to write down things you are certain you’ll eventually forget, such as gdb commands/settings.

Tested with gdb 12.1

Making gdb feel faster/nicer

Create the following files in your ~/ folder

~/.gdbinit

# Make gdb create/update a .gdb_history file  when
# it gets closed so that past commands don't get lost
# > Don't forget to put it in the .gitignore!
set history save on

# Remove annoying duplicate commands
set history remove-duplicates 1

# Simple alias
alias e = exit

# Disable quit application confirmation
define hook-quit
    set confirm off
end

~/.gdbearlyinit

# Remove copyright notice on startup
set startup-quietly on

GDB commands

Call gdb with:

gdb executable_name

To actually run your code:

run

Set a breakpoint

Set a breakpoint when a function with name func gets called:

break func

Variables can be printed, together with their address:

print variable_name

Print memory contents:

x/10x variable_name
^ ^ ^
| | |
| | - Format, (x - hexadecimal, s - string)
| - How many 4 byte chunks
- Display memory contents

Next and step

Once gdb stops at a breakpoint you can execute the following lines of code with:

next: stays at the same level, doesn’t go inside of the implementation of a function if it gets called

There is a default alias and it’s pretty handy: n

step: actually goes inside a function if it gets called


And here are the absolute basics! Good luck with your debugging!

All posts
davespace.xyz – 2024