From c8529af1769235f46b1149a78a6712f61c905c21 Mon Sep 17 00:00:00 2001 From: johannst Date: Fri, 13 Mar 2020 21:58:49 +0100 Subject: added mdbook + migrated first chapter as test (gdb) --- .gitignore | 3 +- book.toml | 9 ++++ gdb.txt | 146 ------------------------------------------------------ src/SUMMARY.md | 3 ++ src/gdb.md | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 148 deletions(-) create mode 100644 book.toml delete mode 100644 gdb.txt create mode 100644 src/SUMMARY.md create mode 100644 src/gdb.md diff --git a/.gitignore b/.gitignore index 5a1407e..7585238 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -tags -*.nopub +book diff --git a/book.toml b/book.toml new file mode 100644 index 0000000..305776d --- /dev/null +++ b/book.toml @@ -0,0 +1,9 @@ +[book] +authors = ["johannst"] +language = "en" +multilingual = false +src = "src" +title = "Notes" + +[output.html] +default-theme = "ayu" diff --git a/gdb.txt b/gdb.txt deleted file mode 100644 index f064cb5..0000000 --- a/gdb.txt +++ /dev/null @@ -1,146 +0,0 @@ -# gdb --------------------------------------------------------------------------------- - -# toc ------- - |gdb| - |opts| - |prompt| - |user_commands| - |hooks| - |flows| - -# gdb(1) *gdb* -========= - gdb [opts] [exe [coredump | pid]] - gdb [opts] --args exe - opts: *opts* - -p attach to pid - -x execute script before prompt - -ex execute command before prompt - --tty set I/O tty for debugee (see *prompt* for details) - - - prompt: *prompt* - tty set as tty for debugee. make sure nobody - reads from target tty, easiest is to spawn a shell - and run - > while true; do sleep 1024; done - - set follow-fork-mode - specify which process to follow on fork(2) - - sharedlibrary [regex] - load symbols of shared lib, if REGEX then only symbols - for matching libs - - break [-qualified] thread - set a breakpoint only on a specific thread - -qualified: sym must be fully qualified (quiet handy - to set breakpoints on C symbols in C++ contexts) - - rbreak set breakpoints based on symbols matching regex - is internally expanded to .*.* - so 'rbreak foo' matches barfoobar() - - command [bp_list] define commands to run after breakpoint hit if - BP_LIST not supplied attach command to last - created bp - - BP_LIST: space separates list, eg 'command 2 5-8' - to run command for bp 2,5,6,7,8 - - info functions [regex] - list functions according to REGEX, if REGEX empty, - list all - - info variables [regex] - list variables according to REGEX, if REGEX empty, - list all - - info handle [signal] - list bevahior for SIGNAL, if SIGNAL empty list all signals - - handle signal - configure how gdb handles SIGNAL which is ment for the debugee - : - stop/nostop catch signal in gdb and break - print/noprint print message when gdb catches signal - pass/nopass pass signal down to debugee - noignore/ignore synonyms for - - catch signal - create a catchpoint for SIGNAL - - - user commands: *user_commands* - define defines user command to be run - # cmds in prompt or script - end - - document define documentation for cmd - # docu - end - - help user-defined list user defined commands - help list documentation for command - - - hooks: *hooks* - define hook- run commands defined in hook before - # cmds executing - end - - define hookpost- run commands defined in hookpost after - # cmds executing - end - - - flows: *flows* - # catch SIGSEGV and execute some actions once it happends - - script: - catch signal SIGSEGV - command - bt - c - end - - # quickly execute gdb command on running process, eg get backtrace from - # thread 1 - - cmd: - $> gdb -ex 'thread 1' -ex 'bt' -p - - # script gdb for automating debugging sessions - - script: run.gdb - set pagination off - - break mmap - command - info reg rdi rsi rdx - bt - c - end - - #initial drop - c - - - cmd: - $> gdb -p -x ./run.gdb --batch &> run.log - - # workaround command + finish bug - # issue: when using finish in a command block, actions after finish will not - # be executed - - script: - define handler - bt - finish - info reg rax - end - - command - handler - end - --------------------------------------------------------------------------------- -vim:ft=help:sts=2:et:tw=80:cc=80:fo+=t - diff --git a/src/SUMMARY.md b/src/SUMMARY.md new file mode 100644 index 0000000..f31e217 --- /dev/null +++ b/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +- [gdb](./gdb.md) diff --git a/src/gdb.md b/src/gdb.md new file mode 100644 index 0000000..c11a85d --- /dev/null +++ b/src/gdb.md @@ -0,0 +1,153 @@ +# 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) + +```markdown + define + # cmds + end + + document + # docu + end + + help user-defined List user defined commands. + help List documentation for command . +``` + +# Hooks + +Gdb allows to create two types of command `hooks` which will be either executed +before or after a certain command. + +```markdown + define hook- Run commands defined in hook before + # cmds executing . + end + + define hookpost- Run commands defined in hookpost after + # cmds executing . + end +``` + +# Flows + +## Catch SIGSEGV and execute commands on occurrence +```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 +```markdown +# run.gdb + 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 -p -x ./run.gdb --batch &> run.log +``` + +## Workaround `command + finish` bug +When using `finish` action inside a `command` block, actions after `finish` are +not executed anymore. 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 +``` -- cgit v1.2.3