skip to navigation
skip to content

faulthandler 1.0

Display the Python backtrace on a crash

Latest Version: 3.0

Fault handler for SIGSEGV, SIGFPE, SIGBUS and SIGILL signals: display the Python backtrace and restore the previous handler. Allocate an alternate stack for this handler, if sigaltstack() is available, to be able to allocate memory on the stack, even on stack overflow.

Just import the module to enable the fault handler.

Website: https://github.com/haypo/faulthandler/wiki/

Example

Example of a segmentation fault on Linux:

$ python
>>> import faulthandler
>>> faulthandler.isenabled()
True
>>> faulthandler.sigsegv()
Fatal Python error: Segmentation fault

Traceback (most recent call first):
  File "<stdin>", line 1 in <module>
Segmentation fault

Installation

To install faulthandler module, type the following command:

python setup.py install

Then you can test your setup using the following command:

python tests.py

You need a C compiler (eg. gcc) and Python headers to build the faulthandler module. Eg. on Fedora, you have to install python-devel package (sudo yum install python-devel).

faulthandler module API

Fault handler state:

  • enable(): enable the fault handler (it is enabled by default)
  • disable(): disable the fault handler
  • isenabled(): get the status of the fault handler

Functions to test the fault handler:

  • sigbus(): raise a SIGBUS signal (Bus error)
  • sigfpe(): raise a SIGFPE signal (Floating point exception), do a division by zero
  • sigill(): raise a SIGILL signal (Illegal instruction)
  • sigsegv(): raise a SIGSEGV signal (Segmentation fault), read memory from NULL (address 0)

sigbus() and sigill() are not available on all operation systems.

The version can be read in the “version” attribute: use “version >> 8” to get the major version, and “version & 255” to get the minor version.

Changelog

Version 1.0 (2010-12-24)

  • First public release

Status

  • 2010-12-24: Tested with Python 2.6, 3.1 and 3.2 on Debian Sid
  • 2010-12-24: Tested with Python 2.6 and 3.1 on Windows XP

Similar projects

Application fault handlers:

  • The GNU libc has a fault handler in debug/segfault.c
  • XEmacs has a fault handler displaying the Lisp backtrace
  • RPy has a fault handler

System-wide fault handlers:

  • Ubuntu uses Apport: https://wiki.ubuntu.com/Apport
  • The Linux kernel logs also segfaults into /var/log/kern.log (and /var/log/syslog). /proc/sys/kernel/core_pattern contols how coredumps are created.
  • Windows opens a popup on a fatal error asking if the error should be reported to Microsoft

See also

 
File Type Py Version Uploaded on Size
faulthandler-1.0.tar.gz (md5) Source 2010-12-24 7KB
faulthandler-1.0.win32-py2.6.exe (md5) MS Windows installer 2.6 2010-12-24 201KB
faulthandler-1.0.win32-py2.7.exe (md5) MS Windows installer 2.7 2010-12-24 201KB
faulthandler-1.0.win32-py3.1.exe (md5) MS Windows installer 3.1 2010-12-24 201KB