Capturing segfault using a debugger in Python

I want to debug a Python program that often gets stuck.

Basically, my program launches a spyne server that accepts SOAP requests. My program has multithreading, and sometimes a client, which I use to achieve its timeouts.

I tried snippet debuggers like PUDB, PDB, WINPDB, PYSTUCK, but I couldn’t exclude them, they actually got stuck too (CTRL + C doesn't work ...)

The best I achieved was from GDB with the following command:

 gdb -ex r --args python myscript.py

GDB manages to catch an exception, but does not display useful information:

 Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffd7fff700 (LWP 22573)]
0x000000000057656d in PyEval_EvalCodeEx ()
(gdb) info threads 
  Id   Target Id         Frame 
* 15   Thread 0x7fffd7fff700 (LWP 22573) "python" 0x000000000057656d in PyEval_EvalCodeEx ()
  7    Thread 0x7fffecc2c700 (LWP 22277) "python" 0x00007ffff6998653 in select () at ../sysdeps/unix/syscall-template.S:82
  6    Thread 0x7fffed42d700 (LWP 22276) "python" 0x00007ffff6998653 in select () at ../sysdeps/unix/syscall-template.S:82
  5    Thread 0x7fffedc2e700 (LWP 22271) "python" sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:86
  4    Thread 0x7fffee42f700 (LWP 22270) "python" sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:86
  3    Thread 0x7fffef9e8700 (LWP 22261) "python" 0x00007ffff6993933 in __GI___poll (fds=<optimized out>, nfds=<optimized out>, 
    timeout=<optimized out>) at ../sysdeps/unix/sysv/linux/poll.c:87
  2    Thread 0x7ffff613f700 (LWP 21988) "python" 0x00007ffff7bcc04d in accept () at ../sysdeps/unix/syscall-template.S:82
  1    Thread 0x7ffff7fd6700 (LWP 20970) "python" 0x00007ffff6998653 in select () at ../sysdeps/unix/syscall-template.S:82
(gdb) bt
#0  0x000000000057656d in PyEval_EvalCodeEx ()
#1  0x0000000000577ab0 in function_call.15039 ()
#2  0x00000000004d91b6 in PyObject_Call ()
#3  0x000000000054d8a5 in PyEval_EvalFrameEx ()
#4  0x000000000054c272 in PyEval_EvalFrameEx ()
#5  0x000000000054c272 in PyEval_EvalFrameEx ()
#6  0x000000000054c272 in PyEval_EvalFrameEx ()
#7  0x000000000054c272 in PyEval_EvalFrameEx ()
#8  0x000000000054c272 in PyEval_EvalFrameEx ()

I installed the python2.7-dbg package to enable the py-bt command, but this is no more useful:

(gdb) py-bt 
#7 (unable to read python frame information)
#8 (unable to read python frame information)
#16 (unable to read python frame information)
#17 (unable to read python frame information)
#18 (unable to read python frame information)
#27 (unable to read python frame information)
#28 (unable to read python frame information)
#32 (unable to read python frame information)
#33 (unable to read python frame information)
#34 (unable to read python frame information)

I read somewhere because Python has no debugging symbols, then I tried the following

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

, , :

ImportError: /usr/lib/python2.7/dist-packages/lxml/etree.so: undefined symbol: Py_InitModule4_64
ImportError: /usr/lib/python2.7/dist-packages/apt_pkg.so: undefined symbol: Py_InitModule4_64

....

: Python: Python 2.7 : Ubuntu 12.04 : Spyne (ex SoapLib) Pyro , . Pyro,

+5
3

,

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

(. ), lxml python-dbg. , , , :

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

: http://hustoknow.blogspot.fr/2013/06/why-your-python-program-cant-start-when.html

:-)

+2

gdb? python -m pdb myscript.py. , gdb hardcode .

+2

/, python faulthandler . ( ), , , python .

:

import faulthandler


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

, Python, , , , . :

  • ASCII. backslashreplace .

  • 500 .

  • , . ( )

  • It is limited to 100 frames and 100 threads.

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

0
source

All Articles