GDB Crib Sheet
Compilation
Ensure the source is compiled with debug information as such:-
gcc -o test -g -Wall ./test.c
Getting started
gdb ./test
set args 1 + 3
break main
r ← run
start ← starts with a break point.
start takes you to main but inserts a temp break point.
Changing views
ctl-x a show tui
ctl-x 2 show assembler
ctl-x a clear
ctl l → clear gdb screen
set listsize 100
list ← set code around present point.
list func ← will center around function.
General commands
break test.c:45 if 1 > 20 ← conditional breakpoint.
watch counter → will execute until watch point is changed.
rwatch counter → will print when it is read
bt <- show bt of execution stack
up/down move up and down call stack.
frame shows present position in call stack
file ./test will re load a file that has changed, updating the debug info.
Call a function : call add(1,2)
Alternative runnnig
sudo gdb -p <pid> will attach to a running process.
Cores
Load a core file
gdb -c core.xxx
then load symbol table if you have the file, and the code was not compiled with -g
cores in linux are often in /var/lib/systemd/coredump
or use cordumpctl to show where stored, if not available, try enabling
ulimit -c unlimited (See online things may have changed since systemd)
Redirection
Redirecting output to another terminal.
Find terminal by typing tty → /dev/pts/1
in gdb then type tty /dev/pts/1
More Break Points Things
set b func1
p $pc
p value
end← will run after every hit.
Set a variable
set var i=30
Gdbserver
gsbserver 192.168.1.1:2345 –attach 6683 & ← on sash/bash of machine
on machine with code, ie nfs share.
target remote 192.168.1.2:2345
best to define in local .gdbinit but it is not a built with -g , load ./execute
define tgtr
target remote 192.168.1.2:2345
display/i $pc
end
Using from emacs
In emacs if you want to add a link to a cross compiled gdb add this to your .emacs file
(defvar gud-gdb-history ‘(“/toolkit/crosscompiler/bin/xxx-gdb”))
Other useful debugging tools: ddd
Allows graphic view of gdb, but also good data viewer.
Other tools:-
ltrace ← view calls to libs strace ← view system calls
valgrind –leak-check=full ./executable
cordumpctl debug will start debug on last generated core file. In c++ you can put virtual function in objects and confirm a function is called from which object by typing
info vtbl obj as typing whatis obj would only show you the parent class not any derived classes.