Python Tracing a Segmentation Fault

How to debug a Python segmentation fault?

I got to this question because of the Segmentation fault, but not on exit, just in general, and I found that nothing else helped as effectively as faulthandler. It's part of Python 3.3, and you can install in 2.7 using pip.

python tracing a segmentation fault

Here's a way to output the filename and line number of every line of Python your code runs:

import sys

def trace(frame, event, arg):
print("%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno))
return trace

def test():
print("Line 8")
print("Line 9")

sys.settrace(trace)
test()

Output:

call, test.py:7
line, test.py:8
Line 8
line, test.py:9
Line 9
return, test.py:9

(You'd probably want to write the trace output to a file, of course.)

Catching segfault with debugger in Python

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 :-)

How to recover from Segmentation fault in Python?

Call the third module in another process, so it doesn't crash the main one when segfault occurs.



Related Topics



Leave a reply



Submit