GDB
启动时不显示提示信息:alias gdb="gdb -q"
调试前执行初始化配置,默认读取用户目录下的 .gdbinit 文件(~/.gdbinit),也可用 -x 指定。https://github.com/gdbinit/Gdbinit
directory /usr/src/musl-libc layout src # -tui set pagination off set confirm off set print pretty on set print object on set print array-indexes on #set history filename ~/.gdb_history #set history save on python import sys import gdb end
gdb cheat sheet:https://sourceware.org/gdb/download/onlinedocs/refcard.pdf & https://darkdust.net/files/GDB Cheat Sheet.pdf
file:加载需要调试的程序、kill/k:终止正在调试的程序run/r:启动程序(r arg1 arg2)、start:设置 main 断点并启动程序、continue/c:继续执行程序,直到下一个断点、starti:从程序的第一条机器指令开始执行
next/n:单步、step/s:单步(进入函数)、nexti/ni:单步机器指令、stepi/si:单步机器指令(进入函数内部)
break/b:设置断点、delete/d:删除断点、disable/dis:禁用断点、enable/en:启用断点
print/p:打印变量值、ptype:打印类型信息、info/i:查看信息(i break、i reg、i thread、i proc mappings、i locals、i args 等)、list/l 显示源码片段、where 显示当前堆栈信息、backtrace/bt:查看调用栈、frame/f:切换调用栈帧
set:修改变量值或配置选项
watch:设置观察点、condition/cond:设置断点条件、shell:执行系统命令(shell ls -l /)
handle SIGUSR1 SIGSEGV nostop:忽略某些信号
快捷键:https://sourceware.org/gdb/current/onlinedocs/gdb.html/TUI.html
Enter:执行上一条命令Ctrl + l:刷新屏幕
Ctrl + x 后,a:进入或退出 TUI 模式、o:更改活动窗口(focus src/cmd)、1:使用只有一个窗口的 TUI 布局、2:使用至少两个窗口的 TUI 布局
Ctrl + p:上一条历史命令,Ctrl + n:下一历史条命令
Ctrl + r:进入反向搜索历史命令
Record-and-Replay:GDB 会记录程序的每一条指令执行,并将执行过程保存在一个文件中,以便后续回放和分析
starti record full # record full save /path/to/record_full.log # record full load /path/to/record_full.log n rn s rs info record record stop
调试 musl
wget https://musl.libc.org/releases/musl-1.2.4.tar.gz tar -zxf musl-1.2.4.tar.gz && cd musl-1.2.4 make distclean && ./configure --enable-debug make -j8 && sudo make install cd ~ && /usr/local/musl/bin/musl-gcc ~/main.c -g3 -static gdb a.out
Debian
https://wiki.debian.org/Debuginfod https://www.debian.org/doc/manuals/debian-reference/ch12.zh-cn.html#_debug & https://wiki.debian.org/DebugPackage
https://wiki.debian.org/musl & https://packages.debian.org/search?keywords=musl
# echo "deb http://deb.debian.org/debian-debug $(lsb_release -cs)-debug main" | sudo tee /etc/apt/sources.list.d/debian-debug.list # http://mirrors.aliyun.com/debian-debug echo "deb http://mirrors.volces.com/debian-debug bookworm-debug main" | sudo tee /etc/apt/sources.list.d/debian-debug.list sudo apt update sudo apt install -y musl musl-dev musl-tools musl-dbgsym apt source musl musl-gcc -g src/main.c # gdb `find ./musl-1.2.3/ -type d | xargs printf " -d %s"` -q -tui a.out gdb `find ./musl-1.2.3/ -type d -printf '-d %p '` -q -tui a.out
https://deb.debian.org/debian-debug/pool/main/m/musl & http://debug.mirrors.debian.org/debian-debug/pool/main/m/musl & https://mirrors.tuna.tsinghua.edu.cn/help/debian & https://developer.volcengine.com/mirror
Ubuntu
https://documentation.ubuntu.com/server/explanation/debugging/debug-symbol-packages/#getting-dbgsym-ddeb-packages & http://ddebs.ubuntu.com/pool/universe/m/musl sudo dpkg -i xxx.ddeb
调试多线程:https://sourceware.org/gdb/current/onlinedocs/gdb.html/Threads.html
show scheduler-locking // 显示线程的 scheduler-locking 状态 set scheduler-locking on // 锁定线程,只有当前线程或指定线程可以运行 info threads
UI
https://www.gnu.org/software/ddd/
https://wiki.gnome.org/Apps/Nemiver/
https://sourceware.org/gdb/documentation
https://eli.thegreenplace.net/tag/debuggers
https://jyywiki.cn/OS/2023/build/lect8.ipynb.html