Catching segfault with debugger in Python

坚强是说给别人听的谎言 提交于 2019-12-05 11:35:24

Have you tried the built-in gdb module? I mean python -m pdb myscript.py. On top of that you can import gdb and hardcode some breakpoints.

I've managed to get a better backtrace by running

gdb -ex r --args python-dbg myscript.py

I've resolved the symbol issues (cf above) by recompiling the package lxml with python-dbg. I had some troubles doing that but it finally worked following the steps:

pip install lxml --download-cache myDir
# for newer pip, use : pip install lxml --download myDir --no-use-wheel
cd myDir
tar -xvf lxml-4.2.1.tar.gz
cd lxml-4.2.1
sudo apt-get install libxslt-dev
sudo apt-get install gcc
sudo apt-get install python-dev
sudo apt-get install python-dbg
sudo python-dbg setup.py install

The following post helped a lot: http://hustoknow.blogspot.fr/2013/06/why-your-python-program-cant-start-when.html

Now I just have to understand the backtrace :-)

I've tracked down hard to find fatal errors/segfaults using python's faulthandler from the standard library. It will create a traceback (recent call first) showing which line of code was being executed when python takes a dump.

ex:

import faulthandler


with open("fault_handler.log", "w") as fobj:
    faulthandler.enable(fobj)
    your_function_to_debug()

It's somewhat more limited than standard python tracebacks, but it's more than sufficient to at least point you in the right direction. From the docs:

  • Only ASCII is supported. The backslashreplace error handler is used on encoding.

  • Each string is limited to 500 characters.

  • Only the filename, the function name and the line number are displayed. (no source code)

  • It is limited to 100 frames and 100 threads.

  • The order is reversed: the most recent call is shown first.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!