GDB
Table of Contents
1. GDB
1.1. GDB Breakpoint
1.3. GDB Target Arch
1.4. GDB Commands
1.4.1. Debug GCC With GDB
1.4.2. user-defined command
https://sourceware.org/gdb/onlinedocs/gdb/Define.html
打印整个链表:
define plist set var $n = $arg0 while $n p *$n set var $n = $n->next end end
1.4.3. dump-binary-memory
(gdb) dump binary memory ./file <from> <to>
1.4.4. add-symbol-file
$> cat test.c extern void foo(); int main(int argc, char *argv[]) { foo(); } $> cat foo.c void foo() { sleep(100000); printf("hello\n"); } $> gcc -shared -O0 -g foo.c -o libfoo.so $> gcc test.c -L. -lfoo $> LD_LIBRARY_PATH=. ./a.out ... $> mv libfoo.so ~/download $> sudo gdb a.out (gdb) attach 10511 Attaching to program: /home/sunway/a.out, process 10511 warning: Could not load shared library symbols for ./libfoo.so. Do you need "set solib-search-path" or "set sysroot"? Reading symbols from /usr/lib/libc.so.6...(no debugging symbols found)...done. Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done. 0x00007f7dfb79f1b1 in nanosleep () from /usr/lib/libc.so.6 (gdb) info sharedlibrary From To Syms Read Shared Object Library No ./libfoo.so 0x00007f7dfb6fb790 0x00007f7dfb83dc0c Yes (*) /usr/lib/libc.so.6 0x00007f7dfbc95db0 0x00007f7dfbcb23e0 Yes (*) /lib64/ld-linux-x86-64.so.2 (*): Shared library is missing debugging information. $> cat /proc/10511/maps|grep libfoo 7f7dfba93000-7f7dfba94000 r-xp 00000000 08:03 17043246 /home/sunway/download/libfoo.so ... $> readelf -a ~/download/libfoo.so|grep .text [11] .text PROGBITS 0000000000000560 00000560 ~~~~~~~~~~~~~~~~ (gdb) add-symbol-file ~/download/libfoo.so 0x7f7dfba93000+0x560 add symbol table from file "/home/sunway/download/libfoo.so" at .text_addr = 0x7f7dfba93560 (y or n) y Reading symbols from /home/sunway/download/libfoo.so...done. (gdb) bt #0 0x00007f7dfb79f1b1 in nanosleep () from /usr/lib/libc.so.6 #1 0x00007f7dfb79f0ea in sleep () from /usr/lib/libc.so.6 #2 0x00007f7dfba9364d in foo () at foo.c:2 #3 0x00005629c2e4a723 in main ()
1.4.5. call
# 使用 call 来调用函数 (gdb) call print_rtl_single(stdout, insn) # call 的返回值 (gdb) call PATTERN(insn) $10 = (rtx &) @0x7ffff7676920: 0x7ffff778a288 (gdb) p *$10 ... # 可以使用 $ 引用最近的返回值, 即 $10 # 使用 set $xxx 可以保存这个返回值到另一个名字 (gdb) set $pat=$ (gdb) p *$pat
1.4.6. commands
break 后可以用 commands 指定每次在断点处自动执行的命令, 例如打印变量的值
(gdb) break xxx # 假设前面的 break 是 2 (gdb) commands 2 Type commands for breakpoint(s) 1, one per line. End with a line saying just "end". >p /c xxx > >end # 再次指定一个空的 commands, 相当于去除 break 2 的 commands (gdb) commands 2 Type commands for breakpoint(s) 1, one per line. End with a line saying just "end". > >end