使用gdb查看python段错误(Segmentation fa

2019-11-27  本文已影响0人  愤愤的有痣青年

公司有个业务是使用python脚本调用.so文件,但是有时候so文件内部发生错误,python就直接崩溃了,无任何提示信息,所以很不方便找错误原因.

使用gdb可以看到更详细的一些信息,其使用方式如下:

# ulimit -c
0
# cat /proc/sys/kernel/core_pattern
core

ulimit -c是查看创建的核心转储的最大大小,这里为0,是需要修改的,可以将其改成不限制大小的unlimited.
cat /proc/sys/kernel/core_pattern 这一步我的理解是查看到时候生成的缓存文件存储名称,这里为core,表示其会在当前目录下生成一个名为core的缓存文件,但是为了使其更加通用,可以修改一下其路径和名称格式.

# ulimit -c unlimited
# mkdir /var/cores
# echo "/var/cores/core.%e.%p" > /proc/sys/kernel/core_pattern
# python3 run.py
Segmentation fault
# ls  /var/cores
core.celery.31796

可以看到在var/cores目录下生成了一个core.python.31796文件,此时可以在刚才的运行目录下执行,下面的which前面是`符号,不是单引号

# gdb `which python ` /var/cores/core.python.31796
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/panso/venv/bin/python...(no debugging symbols found)...done.

warning: core file may not match specified executable file.
[New LWP 31796]
[New LWP 31798]
[New LWP 31797]
[New LWP 31801]
[New LWP 31799]
[New LWP 31800]
[New LWP 31802]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/home/panso/venv/bin/python3 /home/panso/venv/bin/celery -A algos worker --logl'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f89d8700960 in mkl_pds_lp64_ladj_mod_pardiso () from /usr/lib/libmkl_intel_thread.so
[Current thread is 1 (Thread 0x7f89f5953700 (LWP 31796))]

此时可以看到项目最终是在#0 0x00007f89d8700960 in mkl_pds_lp64_ladj_mod_pardiso () from /usr/lib/libmkl_intel_thread.so时发生了错误,这个时候可以输入bt查看更多,其从上到下是错误从底层到最外层的顺序.
若执行的时候没有gdb,可以执行apt-get install gdb安装.

还有就是,若查看的时候错误地方是#0 0x00000000005406df in ?? ()这样的没有具体函数名的情况,这是因为so文件在编译时候没有链接符号到文件里面,需要在使用gcc编译的时候加上-g

上一篇下一篇

猜你喜欢

热点阅读