GDB note
Shorthand aliases
cl |
continue |
|
cl |
clear |
Clear breakpoints in location |
b |
break |
Set a breakpoint |
bt |
backtrace |
|
d |
delete |
Delete breakpoints/watchpoints/catchpoints by its number |
l |
list |
Print current line of source code |
rb |
rbreak |
Set a breakpoint using a regular expression |
r |
run |
Run your program |
u |
until |
|
! |
shell |
Run shell command |
Frontmost commands
rb regex Set breakpoints on all functions matching the regular expression regex
e.g.: rb ^ds will set breakpoints to all functions starting with dsrb filename.c:. Set breakpoints on all functions in the filename.cb open if strcmp($rdi,"~/error.log") == 0 Break when opening ~/error.log fileb read if $rdi == 12 When read a file with descriptior 12cl filename:linenum Removes all breakpoints at the specified line
clear function Removes all breakpoints from the functiond breakpoint_number Delete one breakpoint whose number is breakpoint_numberset filename-display <relative|absolute|basename>set follow-fork-mode <parent|child|ask> Set following mode when perform a fork()set follow-exec-mode <new|same> Lets the process bound to the same/new inferior when performing execset print elements 0 Don't limit when printingwin Show source code in a windows (if available)set $myvar_main_addr = &main Set convinience variable
set $mydigit = 0show convenience or show conv # Show convinience variablesshow environment or show envbt <num> Trace last u <num> or until <num> # Run to wa <variable> or watch <variable> # Catch a variable changecommands [list] Use afrer the b command to invoke other gdb commands. See more
e.g.:
b foo if i!=43
commands
silent
printf "i=%d\n",i
sum+=u
cont
end
! or shell To run a command in shell.
e.g.: (gdb) !ls -alh (gdb) !pwd (gdb) !whoami
The convenience variables $_shell_exitcode and $_shell_exitsignal can be used to examine the exit status of the last shell command launched by shell, make, pipe and |
To show stdout in another terminal get another's treminal number$ tty
/dev/pts/1
and type it in gdb's console(gdb) tty /dev/pts/1.
Or it can be done like this:gdb -tty <TTY_PATH> --args ./my_progtam <arguments> TTY_PATH is like /dev/pts/1
call foo() or p foo() To call a function from your program
info proc <files><stat><status> See process info in gdb more orinfo proc to get PID and then shell ll /proc/<PID>/fd
Coloring prompt: type of add to ~/.gdbinitset prompt \033[0;34m(gdb) \033[0m 0;32m is also a nice color
set print address off Can be done to make bt nice-looking
set detach-on-fork off Retain control over forked processes, including nested forksinfo infe Print a list of all inferiorsinfe <N> Switch from one fork to another
generate-core-file Produce a core file from your program
$ gdb -c <core_file> Read the core filesharedlibrary Load symbols form shared librariessymbol-file </path/to/binary> Read symbols from the binary moreset solib-search-path /path/to/libs Set path to libs (do before loading the core)
core /path/to/core/dump Load core during runnig (after setting solib-search-path)
.gdbinit
My ~/.gdbinit:
# b foobar - don\'t try setting breakpoints from here - they won\'t work.
set follow-fork-mode child
set follow-exec-mode same
set detach-on-fork off
set print address off
set print elements 0
set prompt \033[0;34m(gdb) \033[0m
set confirm off
define pp
set follow-fork-mode parent
end
define cc
set follow-fork-mode child
end
define ii
info inferiors
end
define i1
inferior 1
end
Links
https://sourceware.org/gdb/onlinedocs/gdb/
https://jvns.ca/blog/2018/01/04/how-does-gdb-call-functions/