-
Notifications
You must be signed in to change notification settings - Fork 26
Home
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.
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
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).
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.
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
- http://bugs.python.org/issue8863 (may 2010): Display Python backtrace on SIGSEGV, SIGFPE and fatal error
- http://bugs.python.org/issue3999 (sept. 2009): Real segmentation fault handler