4
社区成员




文章收录:【Q&A】Python代码调试之解决Segmentation fault (core dumped)问题_python segmentation fault (core dumped)-CSDN博客
定位错误行:
(1) 在代码中写入faulthandler
import faulthandler
# 在import之后直接添加以下启用代码即可
faulthandler.enable()
# 后边正常写你的代码
(2)直接通过命令行来启用,运行时添加-X faulthandler参数即可:
python -X faulthandler your_script.py
进入C语言(so文件)之类的,用gdb调试:
gdb python
(gdb) run /path/to/your_script.py
## wait for segfault ##
(gdb) backtrace
## stack trace of the py code,简写为 bt
追踪产生segmenttation fault的位置及代码函数调用情况:
gdb>bt
这样,一般就可以看到出错的代码是哪一句了,还可以打印出相应变量的数值,进行进一步分析。另外需要注意的是,如果机器上跑很多的应用,生成的core又不知道是哪个应用产生的,可以退出gdb之后通过下列命令进行查看:file core