Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
haypo edited this page Jan 3, 2011 · 17 revisions

Fault handler

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.

Download

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.

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

Clone this wiki locally