多线程程序在linux上调式的新手教程,本人已踩过坑
每次看到Segmentation fault (core dumped),都很头痛,特别是多线程之间互相调用,记录下在linux上调式过程
ulimit -c unlimited
ulimit -c
cat /proc/sys/kernel/core_pattern
ubuntu 22 coredump想要放的位置
echo "<desired-file-path>/<desired-file-name>" > /proc/sys/kernel/core_pattern
wsl ubuntu 22
//指定文件
sudo sysctl -w kernel.core_pattern=<desired-file-path>/<desired-file-name>
//或者保持默认, 哪里段错误,哪里出现core文件
sudo sysctl -w kernel.core_pattern=core
然后就会有这个core文件
gdb使用
gdb <binary-file> <core-dump-file>
gdb <binary-file> <core-dump-file>
我这个是多线程程序,切换到对应线程
info thread
thread id
然后会看到一堆狗屎,gdb里面啥也没有 ,具体的函数内代码行和局部变量信息由于没有可用的符号表而未能显示,这时候需要在makefile里面重新编译程序并包含 -g 选项以保留符号信息
gcc -g -o simulator source.c
至此就能看到出问题的函数
查看那个获取导致崩溃的函数调用序列,还能看到看到局部变量的值
bt full
list my_function
print 变量
看到是currentProcess是空指针 ,非法访问空指针导致段错误
至此问题排查完毕。