GDB Debugger Cheat Sheet

Last modified by Xwiki VePa on 2024/02/08 08:16

Most Common GDB commands

At start 
gdb -help                                 print startup help, show switches
gdb object                               normal debug

Following commands after starting gdb

Help
help                                          list command classes

Breakpoints
break main                              set a breakpoint on a function
break 101                                 set a breakpoint on a line number
break basic.c:101                    set breakpoint at file and line (or function)
info breakpoints                     show breakpoints

Running the program
run                                            run the program with current arguments
run                                            args redirection run with args and redirection
cont                                           continue the program
step                                           single step the program; step into functions
next                                           step but step over functions 
CTRL-C                                      SIGINT, stop execution of current program 
attach                                       process-id attach to running program
detach                                      detach from running program
finish                                         finish current function's execution


Stack backtrace
bt                                               print stack backtrace
info                                            locals print automatic variables in frame

Browsing source
list 20                                        list 10 lines around line 20
list                                             1,10 list lines 1 to 10
list main                                    list lines around function 
list basic.c:main                       list from another file basic.c
list -                                            list previous 10 lines

Browsing Data
print expression                      print expression, added to value history
print/x expressionR                print in hex
info locals                                 print local automatics only
ptype name                              print type definition
set variable = expression       assign value

Miscellaneous
RETURN                                     repeat last command
shell command args               execute shell command 
source file                                 load gdb commands from file
quit                                            quit gdb