GDB The GNU Project Debugger

General GDB notes and commands reference


Last Updated: December 31, 2018 by Pepe Sandoval



Want to show support?

If you find the information in this page useful and want to show your support, you can make a donation

Use PayPal

This will help me create more stuff and fix the existent content...


### GDB - GDB (The **G**NU **D**e**b**ugger) is a general purpose debugger

GDB cheat sheet and useful stuff

  • You can hit enter to execute the previously executed command
  • Auto complete and command history using arrow keys usually works fine
  • Make sure to add -DDEBUG and -ggdb options when compiling your code
  • Basic usage: (1) run gdb, (2) set breakpoints then (3) run program
    1. gdb <app_name_here>
    2. (gdb) break <file_name_here>:<line_number_here>
      • b <function_name>
    3. (gdb) run
      • r <app command-line arguments>

Create a .in command file

  • Call commands file: gdb --command <gdb_commands_file_here.in>
  • .in commands file example:
# You can add commends to the filelike this
file ./<bin_executable_app_here>
break main
directory ./.
set print pretty on
set print union on
set print array on
set print array-indexes on
set args -t 60

GDB Commands

All these commands ca be executed once gdb is already running

Running and stepping
  1. run or r: Run the currently opened executable
  2. step or s: Execute next code line entering function calls
  3. next or n: Execute next code line skipping function calls
  4. finish: Continue running until the function that is currently being executed returns
  5. until <line_number>: Run until a line number in the current context
  6. until <file_name>:<line_number>: Run until a line number in the given file
  7. continue: Continue program execution
  8. quit or q: quits/exits GDB
Breaks
  1. break <file_name>:<line_number>: Set breakpoint in a certain line number of a certain file
    • Ex. break MyClass.cpp:50
  2. info breakpoints : Shows status and info of current breaks/breakpoints.
  3. enable/disable <break_number>: Enables or disables a breakpoint
  4. delete <break_number>: Removes a breakpoint
Display, print and other
  1. print <variable_name> or p: Prints variable contents, the variable can be of any type
    • Ex. print MyVariable
    • Ex. p/x MyVariable : print in hex
  2. set var <variable_name>=<value>: Set variable to the given value.
    • Ex. set var MyVariable=0x10
  3. backtrace or bt: Prints function backtrace/callstack
  4. display <variable_name/expression>: Adds variable/expression to the items that are displayed every time execution stops
    • Ex. display i
  5. info display: Shows list and status of display items
  6. info locals: Shows list of local variables
  7. delete display <display_num>: Removes variable/expression from the items that are displayed every time execution stops
Want to show support?

If you find the information in this page useful and want to show your support, you can make a donation

Use PayPal

This will help me create more stuff and fix the existent content...