# gdb(1) # CLI ```markdown gdb [opts] [prg [-c coredump | -p pid]] gdb [opts] --args prg opts: -p attach to pid -c use -x execute script before prompt -ex execute command before prompt --tty set I/O tty for debugee ``` # Interactive usage ```markdown tty Set as tty for debugee. Make sure nobody reads from target tty, easiest is to spawn a shell and run following in target tty: > while true; do sleep 1024; done set follow-fork-mode Specify which process to follow when debuggee makes a fork(2) syscall. sharedlibrary [] Load symbols of shared libs loaded by debugee. Optionally use to filter libs for symbol loading. break [-qualified] thread Set a breakpoint only for a specific thread. -qualified: Tred as fully qualified symbol (quiet handy to set breakpoints on C symbols in C++ contexts) rbreak Set breakpoints matching , where matching internally is done on: .*.* command [] Define commands to run after breakpoint hit. If is not specified attach command to last created breakpoint. Command block terminated with 'end' token. : Space separates list, eg 'command 2 5-8' to run command for breakpoints: 2,5,6,7,8. info functions [] List functions matching . List all functions if no provided. info variables [] List variables matching . List all variables if no provided. info handle [] Print how to handle . If no specified print for all signals. handle Configure how gdb handles sent to debugee. : stop/nostop Catch signal in gdb and break. print/noprint Print message when gdb catches signal. pass/nopass Pass signal down to debugee. catch signal Create a catchpoint for . ``` # User commands (macros) Gdb allows to create & document user commands as follows: ```markdown define # cmds end document # docu end ``` To get all user commands or documentations one can use: ```markdown help user-defined help ``` # Hooks Gdb allows to create two types of command `hooks` - `hook-` will be run before `` - `hookpost-` will be run after `` ```markdown define hook- # cmds end define hookpost- # cmds end ``` # Examples ## Catch SIGSEGV and execute commands This creates a `catchpoint` for the `SIGSEGV` signal and attached the `command` to it. ```markdown catch signal SIGSEGV command bt c end ``` ## Run `backtrace` on thread 1 (batch mode) ```markdown gdb --batch -ex 'thread 1' -ex 'bt' -p ``` ## Script gdb for automating debugging sessions To script gdb add commands into a file and pass it to gdb via `-x`. For example create `run.gdb`: ```markdown set pagination off break mmap command info reg rdi rsi rdx bt c end #initial drop c ``` This script can be used as: ```markdown gdb --batch -x ./run.gdb -p ``` # Know Bugs ## Workaround `command + finish` bug When using `finish` inside a `command` block, commands after `finish` are not executed. To workaround that bug one can create a wrapper function which calls `finish`. ```markdown define handler bt finish info reg rax end command handler end ```