GDB Quick Reference for Most Used Commands

Andrew O

Andrew O

3 min read Tags: Linux

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:

  • run or r - Start the program
  • quit or q - Quit GDB
  • break or b - Set a breakpoint
  • continue or c - Continue execution
  • next or n - Step over
  • step or s - Step into
  • Enter key - Repeat last command
  • set or s - Change value of variable (super useful)
  • print or p - Print value of variable
  • backtrace or bt - Print backtrace
  • list or l - 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 or r - Start the program
  • run <args> - Start the program with arguments
  • run < <file> - Start the program with input from file

Quitting GDB

Note: You can also use Ctrl + D to quit GDB

  • quit or q - Quit GDB

Breakpoints

  • break <function> - Set a breakpoint at function
  • break <file>:<function> - Set a breakpoint at function in specified file
  • break <line> - Set a breakpoint at line number in current file
  • break <file>:<line> - Set a breakpoint at line number in specified file
  • break - List all breakpoints

Running the program

Note: continue will continue the program until the next breakpoint

  • continue or c - Continue execution
  • next or n - Step over
  • step or s - Step into

Printing values

Note: sometimes it is easier to backtrace (bt) to see the values of variables at the current point

  • print <variable> or p <variable> - Print value of variable
  • print <expression> - Print value of expression
  • print *<pointer> - Print value of pointer

Backtrace

  • backtrace or bt - Print backtrace
  • backtrace full or bt full - Print backtrace with arguments and local variables

Listing source code

  • list or l - List source code
  • list <function> - List source code of function
  • list <file>:<line> - List source code around line number in specified file

Displaying source code

  • display <variable> - Display value of variable after each step
  • undisplay <display number> - Remove display

Changing values

  • set <variable> = <value> - Change value of variable
  • set $<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

/../assets/images/posts/cl2.webp
Hiking Checklist (Overnight & Multi-day)

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

Andrew O

Andrew O

2 min read
/../assets/images/posts/cl1.webp
Hiking Checklist (Day Trip)

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

Andrew O

Andrew O

2 min read
/../assets/images/posts/mace1.webp
DIY 3D-Printed Mace/Gada

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

Andrew O

Andrew O

6 min read

Leave a comment

Need help or want to collaborate?

Contact for any inquiries, I'll get back within 24 hours.