python tracing a segmentation fault python tracing a segmentation fault c c

python tracing a segmentation fault


If you are on linux, run python under gdb

gdb python(gdb) run /path/to/script.py## wait for segfault ##(gdb) backtrace## stack trace of the c code


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

import sysdef trace(frame, event, arg):    print("%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno))    return tracedef test():    print("Line 8")    print("Line 9")sys.settrace(trace)test()

Output:

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

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


Segfaults from C extensions are very frequently a result of not incrementing a reference count when you create a new reference to an object. That makes them very hard to track down as the segfault occurs only after the last reference is removed from the object, and even then often only when some other object is being allocated.

You don't say how much C extension code you have written so far, but if you're just starting out consider whether you can use either ctypes or Cython. Ctypes may not be flexible enough for your needs, but you should be able to link to just about any C library with Cython and have all the reference counts maintained for you automatically.

That isn't always sufficient: if your Python objects and any underlying C objects have different lifetimes you can still get problems, but it does simplify things considerably.