GDB Quick Reference for Most Used Commands

Andrew O

Table of Contents
Introduction
GDB is a powerful command line tool that can be used to debug programs written in C, C++, and other languages.
Despite its popularity, GDB can be quite intimidating for new users and especially for people just starting with C/C++. The existing guides and tutorials provide a lot of information, but they can be a bit too verbose for beginners. With this post I aim to provide a quick tutorial/reference for the most commonly used GDB commands for myself and others.
For the below commands, I also recommend to use shortcuts to optimize the workflow, e.g. use r instead of run, b instead of break, etc.
Commands
Quick list, sorted by usage
You probably don’t have all day to read the entire post, so here is a quick list of commands sorted by usage:
runorr- Start the programquitorq- Quit GDBbreakorb- Set a breakpointcontinueorc- Continue executionnextorn- Step overstepors- Step into- Enter key - Repeat last command
setors- Change value of variable (super useful)printorp- Print value of variablebacktraceorbt- Print backtracelistorl- List source code
Detailed list
Here’s expanded list of commands with more details.
Starting the program
Note:
rwill start the program with the last used arguments, from the beginning. Useful if you want to restart the program with the same arguments.
runorr- Start the programrun <args>- Start the program with argumentsrun < <file>- Start the program with input from file
Quitting GDB
Note: You can also use
Ctrl + Dto quit GDB
quitorq- Quit GDB
Breakpoints
break <function>- Set a breakpoint at functionbreak <file>:<function>- Set a breakpoint at function in specified filebreak <line>- Set a breakpoint at line number in current filebreak <file>:<line>- Set a breakpoint at line number in specified filebreak- List all breakpoints
Running the program
Note:
continuewill continue the program until the next breakpoint
continueorc- Continue executionnextorn- Step overstepors- Step into
Printing values
Note: sometimes it is easier to backtrace (
bt) to see the values of variables at the current point
print <variable>orp <variable>- Print value of variableprint <expression>- Print value of expressionprint *<pointer>- Print value of pointer
Backtrace
backtraceorbt- Print backtracebacktrace fullorbt full- Print backtrace with arguments and local variables
Listing source code
listorl- List source codelist <function>- List source code of functionlist <file>:<line>- List source code around line number in specified file
Displaying source code
display <variable>- Display value of variable after each stepundisplay <display number>- Remove display
Changing values
set <variable> = <value>- Change value of variableset $<register> = <value>- Change value of register
Conclusion
Hopefully this helped you get started or get a refresher on GDB. If you have any questions or suggestions, feel free to contact me. Don’t forget to drink water and take breaks while debugging!
Feel free to leave a comment below, and have a good day!
Andrew
Recent Posts

A plug-and-play checklist for an overnight or multi-day hike

Andrew O

A plug-and-play checklist for a day hike, tried and tested

Andrew O

DIY 3D-Printed Mace/Gada for your home gym, filled with concrete and metal filler

Andrew O
Leave a comment
Need help or want to collaborate?
Contact for any inquiries, I'll get back within 24 hours.
