GDB quick reference, based on the most used commands
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:
run
orr
- Start the programquit
orq
- Quit GDBbreak
orb
- Set a breakpointcontinue
orc
- Continue executionnext
orn
- Step overstep
ors
- Step into- Enter key - Repeat last command
set
ors
- Change value of variable (super useful)print
orp
- Print value of variablebacktrace
orbt
- Print backtracelist
orl
- List source code
Detailed list
Here's expanded list of commands with more details.
Starting the program
Note:
r
will start the program with the last used arguments, from the beginning. Useful if you want to restart the program with the same arguments.
run
orr
- 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 + D
to quit GDB
quit
orq
- 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:
continue
will continue the program until the next breakpoint
continue
orc
- Continue executionnext
orn
- Step overstep
ors
- 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
backtrace
orbt
- Print backtracebacktrace full
orbt full
- Print backtrace with arguments and local variables
Listing source code
list
orl
- 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!
Best regards,
Andrew