Core Dump
- linux下core dump【总结】 - Rabbit_Dale - 博客园
- gdb调试coredump(使用篇) - 知乎
ulimit -culimit -c unlimited
GDB
Ref:
- GDB 命令表_子曰小玖的博客-CSDN博客_gdb命令
- 100-gdb-tips/index.md at master · hellogcc/100-gdb-tips · GitHub
- 使用GDB 文本用户界面(GDB-TUI)进行调试 - aij - 博客园
结合vscode的界面使用GDB
在vscode界面调试过程中使用GDB命令:
- 在右下Console窗口,切换到“DEBUG CONSOLE”:输入
-exec <gdb command>,如-exec info registers - 在诸如此类对GDB的调用基于GDB的MI接口; GDB 的MI接口 - foo__hack - 博客园
其他技巧:
- Breakpoints — Deactivate Breakpoints;用来暂时禁用所有断点,而不用手动把所有断点全取消;
基本命令
gdb [(file)(kill)(list)(next)(step)(run)(start)(quit)(help)(watch)(print [(/f)('{type'})] [<::globalVar>])(break <line#>)(make)(shell)([(forward-search)(reverse-search)(search)] [])(examine)(display)(info)]# 常用set args "--gtest_filter=CCC.ccc" # 指定运行gtest用例set print pretty on # 按格式打印信息tui enable # 进入命令行用户界面b $fileName:$lineNumb $funcName # 打函数断点b $lineNum # 打当前函数内的行号断点n # next 下一行,遇到函数跳过s # step 下一行,遇到函数进入# 启动file $fileName # 加载可执行文件r # run 开始运行程序/重新启动程序# 浏览代码list $lineStart,$lineEnd # 显示代码# 断点b $fileName:$lineNum # break 打断点delete $b_id # 删除指定编号的断点clear $lineNum # 删除指定位置的断点disable $b_id # 禁用断点enable $count $b_id # 启用断点(特定次数)disable/enable # 禁用/启用所有断点info breakpoints # 查看所有断点info breakpoint [$brkPtNum] # 查看断点,可以指定断点编号info watchpoint [$brkPtNum] # 查看观察断点# 打印信息set print pretty on # 格式化输出结构体bt # (在coredump之后)打印backtraceup # 向main函数移动down # 远离main函数移动info locals # 打印栈帧局部变量info args # 打印函数参数ptype $Type # 打印类型定义whatis $Expr # 打印表达式类型
常用命令
# 在pthread互斥锁上加断点-exec break pthread_mutex_lock-exec break pthread_mutex_unlock# 其他工具ptree -p $tidhtop # 查看tree视图# 查看内存x/4ub 0xf725cf40
查看进程地址空间(和各个段的装载情况)
- c - GDB: Listing all mapped memory regions for a crashed process - Stack Overflow
- 辅助:使用objdump和nm;GDB调试之段信息 | 田宇的个人博客
info files # 可以查看装载的各个段maintenance info sections # 也可以info proc map # 或者 info proc mappings 查看进程映射空间,可以看到堆和栈、# 再用objdump -h打印# 也可以使用cat /proc/$pid/maps进行
使用自定义command
gdb可以自定义命令,然后像函数那样使用;比如打印一个链表;
不过暂时没找到在vscoce / MI接口下这样使用的正确方法;define adderset var $cur = $arg0set var $num = 0while ($num < 100)print *$curset $cur = $cur->nextset $num = $num + 1endprint $numend
查看容器
查看内存
- gdb查看内存地址和栈中的值—查看虚函数表、函数地址_张同光 (Tongguang Zhang):Hello everyone !-CSDN博客_gdb查看内存地址和栈中的值
format={s[tring]i[nstruction]x[ hexadecimal]d[ecimal]u[nsigned hex]o[ctal]t[winned binary]a[ hex variable]c[har]f[loat]}byteNumAs1Addr={b[yte 1Byte]h[alfword 2Byte]w[ord 4Byte]g[iant 8Byte]}x/${addrLen}${format}${byteNumAs1Addr} ${addr}
多线程调试
# 线程info threads # 查看当前运行的线程thread $tid # 切换到指定线程
Valgrind
# 内存泄漏调试valgrind --tool=memcheck ./a > log.txt 2>&1valgrind --tool=memcheck --leak-check=yes ./a > log.txt 2>&1# 线程死锁调试valgrind --tool=helgrind ./a > log.txt 2>&1 #valgrind --tool=drd --exclusive-threshold=30 ./a > log.txt 2>&1# drd, 分析死锁;记录持有锁超过30ms的情况valgrind --tool=drd --trace-mutex=yes ./a > log.txt 2>&1# drd显示上锁和解锁情况;# massifvalgrind --leak-check=full --show-leak-kinds=all ./xxxsudo apt install massif-visualizervalgrind --tool=massif ./xxx
ASAN/UBSAN/XXXSanitizer
- 地址消毒fsanitizer选项细节探究 - 简书 ```bash export ASAN_OPTIONS=halt_on_error=0
export ASAN_OPTIONS=alloc_dealloc_mismatch=0
export ASAN_OPTIONS=alloc_dealloc_mismatch=0:alloc_dealloc_mismatch=0 # 多个选项 ```
生成compile_commands.json
- 生成compile_commands.json文件_denglin12315的博客-CSDN博客
- 配置vscode使用compile_commands.json实现跳转 vscode使用compile_commands.json - TruthHell - 博客园
