Core Dump

Ref:

结合vscode的界面使用GDB

在vscode界面调试过程中使用GDB命令:

  • 在右下Console窗口,切换到“DEBUG CONSOLE”:输入-exec <gdb command>,如-exec info registers
  • 在诸如此类对GDB的调用基于GDB的MI接口; GDB 的MI接口 - foo__hack - 博客园

其他技巧:

  • Breakpoints — Deactivate Breakpoints;用来暂时禁用所有断点,而不用手动把所有断点全取消;

基本命令

  1. gdb [
  2. (file)(kill)(list)(next)(step)(run)(start)(quit)(help)
  3. (watch)(print [(/f)('{type'})] [<::globalVar>])
  4. (break <line#>)(make)(shell)
  5. ([(forward-search)(reverse-search)(search)] [])
  6. (examine)(display)
  7. (info)
  8. ]
  9. # 常用
  10. set args "--gtest_filter=CCC.ccc" # 指定运行gtest用例
  11. set print pretty on # 按格式打印信息
  12. tui enable # 进入命令行用户界面
  13. b $fileName:$lineNum
  14. b $funcName # 打函数断点
  15. b $lineNum # 打当前函数内的行号断点
  16. n # next 下一行,遇到函数跳过
  17. s # step 下一行,遇到函数进入
  18. # 启动
  19. file $fileName # 加载可执行文件
  20. r # run 开始运行程序/重新启动程序
  21. # 浏览代码
  22. list $lineStart,$lineEnd # 显示代码
  23. # 断点
  24. b $fileName:$lineNum # break 打断点
  25. delete $b_id # 删除指定编号的断点
  26. clear $lineNum # 删除指定位置的断点
  27. disable $b_id # 禁用断点
  28. enable $count $b_id # 启用断点(特定次数)
  29. disable/enable # 禁用/启用所有断点
  30. info breakpoints # 查看所有断点
  31. info breakpoint [$brkPtNum] # 查看断点,可以指定断点编号
  32. info watchpoint [$brkPtNum] # 查看观察断点
  33. # 打印信息
  34. set print pretty on # 格式化输出结构体
  35. bt # (在coredump之后)打印backtrace
  36. up # 向main函数移动
  37. down # 远离main函数移动
  38. info locals # 打印栈帧局部变量
  39. info args # 打印函数参数
  40. ptype $Type # 打印类型定义
  41. whatis $Expr # 打印表达式类型

查看进程地址空间

常用命令

  1. # 在pthread互斥锁上加断点
  2. -exec break pthread_mutex_lock
  3. -exec break pthread_mutex_unlock
  4. # 其他工具
  5. ptree -p $tid
  6. htop # 查看tree视图
  7. # 查看内存
  8. x/4ub 0xf725cf40

查看进程地址空间(和各个段的装载情况)

  • c - GDB: Listing all mapped memory regions for a crashed process - Stack Overflow
  • 辅助:使用objdump和nm;GDB调试之段信息 | 田宇的个人博客
    1. info files # 可以查看装载的各个段
    2. maintenance info sections # 也可以
    3. info proc map # 或者 info proc mappings 查看进程映射空间,可以看到堆和栈、
    4. # 再用objdump -h打印
    5. # 也可以使用cat /proc/$pid/maps进行

    使用自定义command

    gdb可以自定义命令,然后像函数那样使用;比如打印一个链表;
    不过暂时没找到在vscoce / MI接口下这样使用的正确方法;
    1. define adder
    2. set var $cur = $arg0
    3. set var $num = 0
    4. while ($num < 100)
    5. print *$cur
    6. set $cur = $cur->next
    7. set $num = $num + 1
    8. end
    9. print $num
    10. end

    查看容器

GDB打印STL容器内容 - linyx - 博客园

查看内存

多线程调试

  1. # 线程
  2. info threads # 查看当前运行的线程
  3. thread $tid # 切换到指定线程

Valgrind

  1. # 内存泄漏调试
  2. valgrind --tool=memcheck ./a > log.txt 2>&1
  3. valgrind --tool=memcheck --leak-check=yes ./a > log.txt 2>&1
  4. # 线程死锁调试
  5. valgrind --tool=helgrind ./a > log.txt 2>&1 #
  6. valgrind --tool=drd --exclusive-threshold=30 ./a > log.txt 2>&1
  7. # drd, 分析死锁;记录持有锁超过30ms的情况
  8. valgrind --tool=drd --trace-mutex=yes ./a > log.txt 2>&1
  9. # drd显示上锁和解锁情况;
  10. # massif
  11. valgrind --leak-check=full --show-leak-kinds=all ./xxx
  12. sudo apt install massif-visualizer
  13. valgrind --tool=massif ./xxx

ASAN/UBSAN/XXXSanitizer

clang之AddressSanitizer

export ASAN_OPTIONS=alloc_dealloc_mismatch=0

export ASAN_OPTIONS=alloc_dealloc_mismatch=0:alloc_dealloc_mismatch=0 # 多个选项 ```

生成compile_commands.json