Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 1 | :mod:`faulthandler` --- Dump the Python traceback |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: faulthandler |
| 5 | :synopsis: Dump the Python traceback. |
| 6 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 7 | This module contains functions to dump Python tracebacks explicitly, on a fault, |
| 8 | after a timeout, or on a user signal. Call :func:`faulthandler.enable` to |
| 9 | install fault handlers for the :const:`SIGSEGV`, :const:`SIGFPE`, |
| 10 | :const:`SIGABRT`, :const:`SIGBUS`, and :const:`SIGILL` signals. You can also |
| 11 | enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment |
| 12 | variable or by using :option:`-X` ``faulthandler`` command line option. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 13 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 14 | The fault handler is compatible with system fault handlers like Apport or the |
| 15 | Windows fault handler. The module uses an alternative stack for signal handlers |
| 16 | if the :c:func:`sigaltstack` function is available. This allows it to dump the |
| 17 | traceback even on a stack overflow. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 18 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 19 | The fault handler is called on catastrophic cases and therefore can only use |
| 20 | signal-safe functions (e.g. it cannot allocate memory on the heap). Because of |
| 21 | this limitation traceback dumping is minimal compared to normal Python |
| 22 | tracebacks: |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 23 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 24 | * Only ASCII is supported. The ``backslashreplace`` error handler is used on |
| 25 | encoding. |
Victor Stinner | 1713f9a | 2012-07-31 03:25:28 +0200 | [diff] [blame] | 26 | * Each string is limited to 500 characters. |
Victor Stinner | 2510d9e | 2011-06-19 16:07:20 +0200 | [diff] [blame] | 27 | * Only the filename, the function name and the line number are |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 28 | displayed. (no source code) |
| 29 | * It is limited to 100 frames and 100 threads. |
Guido van Rossum | 2063aaf | 2013-10-20 19:15:19 -0700 | [diff] [blame] | 30 | * The order is reversed: the most recent call is shown first. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 31 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 32 | By default, the Python traceback is written to :data:`sys.stderr`. To see |
| 33 | tracebacks, applications must be run in the terminal. A log file can |
| 34 | alternatively be passed to :func:`faulthandler.enable`. |
| 35 | |
| 36 | The module is implemented in C, so tracebacks can be dumped on a crash or when |
| 37 | Python is deadlocked. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 38 | |
| 39 | .. versionadded:: 3.3 |
| 40 | |
| 41 | |
| 42 | Dump the traceback |
| 43 | ------------------ |
| 44 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 45 | .. function:: dump_traceback(file=sys.stderr, all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 46 | |
Benjamin Peterson | defe6f6 | 2011-06-19 09:37:18 -0500 | [diff] [blame] | 47 | Dump the tracebacks of all threads into *file*. If *all_threads* is |
| 48 | ``False``, dump only the current thread. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 49 | |
| 50 | |
| 51 | Fault handler state |
| 52 | ------------------- |
| 53 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 54 | .. function:: enable(file=sys.stderr, all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 55 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 56 | Enable the fault handler: install handlers for the :const:`SIGSEGV`, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 57 | :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL` |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 58 | signals to dump the Python traceback. If *all_threads* is ``True``, |
| 59 | produce tracebacks for every running thread. Otherwise, dump only the current |
| 60 | thread. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 61 | |
| 62 | .. function:: disable() |
| 63 | |
| 64 | Disable the fault handler: uninstall the signal handlers installed by |
| 65 | :func:`enable`. |
| 66 | |
| 67 | .. function:: is_enabled() |
| 68 | |
| 69 | Check if the fault handler is enabled. |
| 70 | |
| 71 | |
| 72 | Dump the tracebacks after a timeout |
| 73 | ----------------------------------- |
| 74 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 75 | .. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 76 | |
| 77 | Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 78 | every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call |
| 79 | :c:func:`_exit` with status=1 after dumping the tracebacks. (Note |
Benjamin Peterson | defe6f6 | 2011-06-19 09:37:18 -0500 | [diff] [blame] | 80 | :c:func:`_exit` exits the process immediately, which means it doesn't do any |
| 81 | cleanup like flushing file buffers.) If the function is called twice, the new |
| 82 | call replaces previous parameters and resets the timeout. The timer has a |
| 83 | sub-second resolution. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 84 | |
Benjamin Peterson | 8519875 | 2011-06-17 19:54:52 -0500 | [diff] [blame] | 85 | This function is implemented using a watchdog thread and therefore is not |
| 86 | available if Python is compiled with threads disabled. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 87 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 88 | .. function:: cancel_dump_traceback_later() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 89 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 90 | Cancel the last call to :func:`dump_traceback_later`. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 91 | |
| 92 | |
| 93 | Dump the traceback on a user signal |
| 94 | ----------------------------------- |
| 95 | |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 96 | .. function:: register(signum, file=sys.stderr, all_threads=True, chain=False) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 97 | |
| 98 | Register a user signal: install a handler for the *signum* signal to dump |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 99 | the traceback of all threads, or of the current thread if *all_threads* is |
Victor Stinner | a9a9dab | 2011-07-13 23:39:53 +0200 | [diff] [blame] | 100 | ``False``, into *file*. Call the previous handler if chain is ``True``. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 101 | |
| 102 | Not available on Windows. |
| 103 | |
| 104 | .. function:: unregister(signum) |
| 105 | |
| 106 | Unregister a user signal: uninstall the handler of the *signum* signal |
Victor Stinner | cfa7123 | 2011-04-08 12:48:15 +0200 | [diff] [blame] | 107 | installed by :func:`register`. Return ``True`` if the signal was registered, |
| 108 | ``False`` otherwise. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 109 | |
| 110 | Not available on Windows. |
| 111 | |
| 112 | |
| 113 | File descriptor issue |
| 114 | --------------------- |
| 115 | |
Georg Brandl | deb92b5 | 2012-09-22 08:58:55 +0200 | [diff] [blame] | 116 | :func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 117 | file descriptor of their *file* argument. If the file is closed and its file |
| 118 | descriptor is reused by a new file, or if :func:`os.dup2` is used to replace |
| 119 | the file descriptor, the traceback will be written into a different file. Call |
| 120 | these functions again each time that the file is replaced. |
| 121 | |
| 122 | |
| 123 | Example |
| 124 | ------- |
| 125 | |
| 126 | Example of a segmentation fault on Linux: :: |
| 127 | |
| 128 | $ python -q -X faulthandler |
| 129 | >>> import ctypes |
| 130 | >>> ctypes.string_at(0) |
| 131 | Fatal Python error: Segmentation fault |
| 132 | |
Guido van Rossum | 2063aaf | 2013-10-20 19:15:19 -0700 | [diff] [blame] | 133 | Current thread 0x00007fb899f39700 (most recent call first): |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 134 | File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at |
| 135 | File "<stdin>", line 1 in <module> |
| 136 | Segmentation fault |
| 137 | |