Prerequisites
- Make sure to install the
gdb
package - Make sure to compile with
-g -gdb3
and-O0
Starting
Load the binary with
gdb ./hellband
Setting breakpoints and watches
break <filename>:<line>
watch <variable>
(breaks on write access)rwatch <variable>
(breaks on read access)awatch <variable>
(breaks on read and write access)
Querying variables
info variables
to list "All global and static variable names".info locals
to list "Local variables of current stack frame" (names and values)info args
to list "Arguments of the current stack frame" (names and values).print <variable>
to show a variablevariable
explore <variable>
to inspect a variablevariable
Call stack
bt
to show all frames aka the the call stackselect-frame <n>
to select frame numbern
from the call stack
Program flow
continue
to run the program till the end or the next breakpointstep
to execute the whole line at oncenext
to execute the next atomic part of the linefinish
to return to previous frame
Getting fancy
- Consider installing https://github.com/cyrus-and/gdb-dashboard
- Having 2 ssh sessions with one running in gdb
dashboard -output <tty>
can be gold c
is shorthand forcontinue
frame
is shorthand forselect-frame
p
is shorthand forprint
Debugging a stuck game
- Open two sessions
- Get one session stuck within a gdb run
- Find the hellband/gdb session with
ps -al
- Send from the other session a SIGSEGV with
kill -s SIGSEGV <pid>