0

I have a caller.py which repeatedly calls routines from some_c_thing.so, which was created from some_c_thing.c. When I run it, it segfaults - is there a way for me to detect which line of c code is segfaulting?

wcai
  • 169
  • 1
  • 14
  • 1
    While I expect that this question is answerable in its current state, it would help the rest of us if you provided sample code for both so that it is easier to test any solutions we come up with. – merlin2011 Jun 29 '14 at 06:42

3 Answers3

1

This might work:

  1. make sure the native library is compiled with debug symbols (-g switch for gcc).

  2. Run python under gdb and let it crash:

    gdb --args python caller.py
    run # tell gdb to run the program
    # script runs and crashes
    bt # print backtrace, which should show the crashing line
    

If crash happens in the native library code, then this should reveal the line.

If native library code just corrupts something or violates some postconditions, and crash happens in Python interpreter's code, then this will not be helpful. In that case your options are code review, adding debug prints (first step would be to just log entry and exit of each C function to detect which is the last C function called before crash, then adding more fine-grained logging for variable values etc), and finally using debugger to see what happens by using the usual debugger techniques (breakpoints, stepping, watches...).

hyde
  • 60,639
  • 21
  • 115
  • 176
0

Take Python and the .so file(s) out of the equation. See what params are being passed, if any, and call the routines from a debugger capable of stepping through C code and binaries.

Here is a link to an article describing a simple C debugging process, in case you're not familiar with debugging C (command line interface). Here is another link on using NetBeans to debug C. Also using Eclipse...

This could help: gdb: break in shared library loaded by python (might also turn out to be a dupe)

Community
  • 1
  • 1
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
-1

segfault... Check if the number of variables or the types of variables you passed to that c function (in .so) are correct. If not aligned, usually it's a segfault.

Edmund
  • 149
  • 1
  • 3