Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to debug a pyside segmentation fault in a virtualenv?

I am building a Qt app with pyside and I have random segmentation faults. I installed faulthandler and the callstack is clear if uncomplete :

  File "/home/lionel/Libs/python/ProcessGraph/process_graph/qtgraph.py", line 247 in itemChange
  File "/home/lionel/Libs/python/ProcessGraph/process_graph/qtgraph.py", line 268 in mousePressEvent
  File "/home/lionel/Libs/python/ProcessGraph/process_graph/qtgraph.py", line 87 in mousePressEvent
  File "main.py", line 219 in main
  File "main.py", line 223 in <module>

The crash is always there (in mousePressEvent) but I don't see what I can do from there except launching gdb.

I have installed python-gdb and tried to launch : gdb -ex r --args python-dbg main.py but It doesn't run. So I am kind of stuck. Here the stack :

python2.7-dbg: ../Objects/object.c:65: _Py_AddToAllObjects: Assertion `(op->_ob_prev == ((void *)0)) == (op->_ob_next == ((void *)0))' failed.

Program received signal SIGABRT, Aborted.
0x00007ffff6f09037 in raise ()
   from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0  0x00007ffff6f09037 in raise ()
   from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff6f0c698 in abort ()
   from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007ffff6f01e03 in ?? ()
   from /lib/x86_64-linux-gnu/libc.so.6
#3  0x00007ffff6f01eb2 in __assert_fail ()
   from /lib/x86_64-linux-gnu/libc.so.6
#4  0x0000000000488f38 in _Py_AddToAllObjects (
    op=<unknown at remote 0x7ffff578aa20>, force=0)
    at ../Objects/object.c:65
#5  0x00000000004bbde5 in PyType_Ready (
    type=0x7ffff578aa20 <SbkEnumType_Type>)
    at ../Objects/typeobject.c:3985
#6  0x00007ffff5567e50 in Shiboken::init() ()
   from /usr/lib/x86_64-linux-gnu/libshiboken-python2.7.so.1.1
#7  0x00007ffff5577b79 in Shiboken::Module::create(char const*, void*) ()
   from /usr/lib/x86_64-linux-gnu/libshiboken-python2.7.so.1.1
#8  0x00007ffff0c98fb9 in initQtCore ()
   from /usr/lib/python2.7/dist-packages/PySide/QtCore.so
#9  0x0000000000549771 in _PyImport_LoadDynamicModule
    (name=0xc37900 "PySide.QtCore", 
    pathname=0xc38930 "/usr/lib/python2.7/dist-packages/PySide/QtCore.so", fp=0xc3a980)
    at ../Python/importdl.c:53
#10 0x0000000000545984 in load_module (
    name=0xc37900 "PySide.QtCore", fp=0xc3a980, 
    pathname=0xc38930 "/usr/lib/python2.7/dist-packages/PySide/QtCore.so", type=3, loader=0x0)
    at ../Python/import.c:1915
#11 0x0000000000547ade in import_submodule (
    mod=<module at remote 0xbe2af0>, 
    subname=0xc37907 "QtCore", 
    fullname=0xc37900 "PySide.QtCore")
    at ../Python/import.c:2700
#12 0x00000000005470ae in load_next (
    mod=<module at remote 0xbe2af0>, 
    altmod=<module at remote 0xbe2af0>, 
    p_name=0x7fffffffcee8, 
    buf=0xc37900 "PySide.QtCore", 
    p_buflen=0x7fffffffcf00)
    at ../Python/import.c:2515
#13 0x000000000054632e in import_module_level (
    name=0x0, 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, fromlist=['__doc__'], 
    level=0) at ../Python/import.c:2232
#14 0x0000000000546805 in PyImport_ImportModuleLevel (
    name=0xc86c3c "PySide.QtCore", 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, fromlist=['__doc__'], 
    level=0) at ../Python/import.c:2288
#15 0x000000000051208b in builtin___import__ (
    self=0x0, 
    args=('PySide.QtCore', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ['__doc__'], 0), kwds=0x0) at ../Python/bltinmodule.c:49
---Type <return> to continue, or q <return> to quit---
#16 0x0000000000487ae4 in PyCFunction_Call (
    func=<built-in function __import__>, 
    arg=('PySide.QtCore', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ['__doc__'], 0), kw=0x0) at ../Objects/methodobject.c:85
#17 0x0000000000427737 in PyObject_Call (
    func=<built-in function __import__>, 
    arg=('PySide.QtCore', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ['__doc__'], 0), kw=0x0) at ../Objects/abstract.c:2529
#18 0x000000000042788d in call_function_tail (
    callable=<built-in function __import__>, 
    args=('PySide.QtCore', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ['__doc__'], 0)) at ../Objects/abstract.c:2561
#19 0x00000000004279f9 in PyObject_CallFunction (
    callable=<built-in function __import__>, 
    format=0x6412f7 "OOOOi")
    at ../Objects/abstract.c:2585
#20 0x00000000005483e5 in PyImport_Import (
    module_name='PySide.QtCore')
    at ../Python/import.c:2886
#21 0x00000000005460cc in PyImport_ImportModule (
    name=0x7ffff6796c67 "PySide.QtCore")
    at ../Python/import.c:2129
#22 0x00007ffff5577b38 in Shiboken::Module::import(char const*) ()
   from /usr/lib/x86_64-linux-gnu/libshiboken-python2.7.so.1.1
#23 0x00007ffff65a7cd6 in initQtGui ()
   from /usr/lib/python2.7/dist-packages/PySide/QtGui.so
#24 0x0000000000549771 in _PyImport_LoadDynamicModule
    (name=0xc209a0 "PySide.QtGui", 
    pathname=0xc235a0 "/usr/lib/python2.7/dist-packages/PySide/QtGui.so", fp=0xc09660)
    at ../Python/importdl.c:53
#25 0x0000000000545984 in load_module (
    name=0xc209a0 "PySide.QtGui", fp=0xc09660, 
    pathname=0xc235a0 "/usr/lib/python2.7/dist-packages/PySide/QtGui.so", type=3, loader=0x0)
    at ../Python/import.c:1915
#26 0x0000000000547ade in import_submodule (
    mod=<module at remote 0xbe2af0>, 
    subname=0xbe4a94 "QtGui", 
    fullname=0xc209a0 "PySide.QtGui")
    at ../Python/import.c:2700
#27 0x000000000054766c in ensure_fromlist (
    mod=<module at remote 0xbe2af0>, 
    fromlist=('QtGui',), buf=0xc209a0 "PySide.QtGui", 
    buflen=6, recursive=0) at ../Python/import.c:2606
#28 0x000000000054672a in import_module_level (
    name=0x0, 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, fromlist=('QtGui',), 
    level=-1) at ../Python/import.c:2269
#29 0x0000000000546805 in PyImport_ImportModuleLevel (
    name=0xbe4a34 "PySide", 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7f---Type <return> to continue, or q <return> to quit---
cd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, fromlist=('QtGui',), 
    level=-1) at ../Python/import.c:2288
#30 0x000000000051208b in builtin___import__ (
    self=0x0, 
    args=('PySide', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ('QtGui',)), kwds=0x0) at ../Python/bltinmodule.c:49
#31 0x0000000000487ae4 in PyCFunction_Call (
    func=<built-in function __import__>, 
    arg=('PySide', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ('QtGui',)), 
    kw=0x0) at ../Objects/methodobject.c:85
#32 0x0000000000427737 in PyObject_Call (
    func=<built-in function __import__>, 
    arg=('PySide', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ('QtGui',)), 
    kw=0x0) at ../Objects/abstract.c:2529
#33 0x0000000000527948 in PyEval_CallObjectWithKeywords (func=<built-in function __import__>, 
    arg=('PySide', {'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, {...}, ('QtGui',)), 
    kw=0x0) at ../Python/ceval.c:3890
#34 0x0000000000521c54 in PyEval_EvalFrameEx (
    f=Frame 0xc092f0, for file main.py, line 7, in <module> (), throwflag=0) at ../Python/ceval.c:2333
#35 0x0000000000525989 in PyEval_EvalCodeEx (
    co=0xbc2510, 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, args=0x0, argcount=0, 
    kws=0x0, kwcount=0, defs=0x0, defcount=0, 
    closure=0x0) at ../Python/ceval.c:3253
#36 0x000000000051b9f6 in PyEval_EvalCode (
    co=0xbc2510, 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}) at ../Python/ceval.c:667
#37 0x000000000055842d in run_mod (mod=0xc1c1a8, 
    filename=0x7fffffffe1a8 "main.py", 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, flags=0x7fffffffdc10, 
    arena=0xb7f8a0) at ../Python/pythonrun.c:1365
#38 0x00000000005583b3 in PyRun_FileExFlags (
    fp=0xc05370, filename=0x7fffffffe1a8 "main.py", 
    start=257, 
    globals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, 
---Type <return> to continue, or q <return> to quit---
    locals={'__builtins__': <module at remote 0x7ffff7fcd4d8>, '__file__': 'main.py', '__package__': None, 'sys': <module at remote 0x7ffff7fcd610>, '__name__': '__main__', '__doc__': None}, closeit=1, 
    flags=0x7fffffffdc10)
    at ../Python/pythonrun.c:1351
#39 0x0000000000556b70 in PyRun_SimpleFileExFlags (
    fp=0xc05370, filename=0x7fffffffe1a8 "main.py", 
    closeit=1, flags=0x7fffffffdc10)
    at ../Python/pythonrun.c:943
#40 0x0000000000556201 in PyRun_AnyFileExFlags (
    fp=0xc05370, filename=0x7fffffffe1a8 "main.py", 
    closeit=1, flags=0x7fffffffdc10)
    at ../Python/pythonrun.c:747
#41 0x000000000057203a in Py_Main (argc=2, 
    argv=0x7fffffffde28) at ../Modules/main.c:640
#42 0x0000000000417edc in main (argc=2, 
    argv=0x7fffffffde28) at ../Modules/python.c:23
like image 643
LBarret Avatar asked Oct 02 '13 20:10

LBarret


People also ask

What causes segmentation fault in Python?

Segfaults are caused by a program trying to read or write an illegal memory location.


1 Answers

There's a trick, you have to start system Python interpreter that corresponds to your virtualenv.

Also you have to make sure you are using debug build of Python.

Here's how I managed to get gdb with python features on Ubuntu:

# must use debug build of Python
sudo apt-get install gdb python2.7-dbg

# newest kernels disallow tracing/debugging by default (why on earth?)    
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

# create virtualenv with debug Python build
virtualenv -p /usr/bin/python2.7-dbg ~/labarret

# start virtualenv    
. ~/labarret/bin/activate

# pip install your-libs-and-deps

# now start **system** interpreter that corresponds to virtualenv
# make sure virtualenv libs/modules are loaded via environment
PYTHONHOME=~/labarret/ gdb --args /usr/bin/python2.7-dbg your-code.py
like image 86
Dima Tisnek Avatar answered Nov 14 '22 23:11

Dima Tisnek