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 | |
| 7 | This module contains functions to dump the Python traceback explicitly, on a |
| 8 | fault, after a timeout or on a user signal. Call :func:`faulthandler.enable` to |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 9 | install fault handlers for :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, |
| 10 | :const:`SIGBUS` and :const:`SIGILL` signals. You can also enable them at |
| 11 | startup by setting the :envvar:`PYTHONFAULTHANDLER` environment variable or by |
| 12 | using :option:`-X` ``faulthandler`` command line option. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 13 | |
| 14 | The fault handler is compatible with system fault handlers like Apport or |
| 15 | the Windows fault handler. The module uses an alternative stack for signal |
| 16 | handlers, if the :c:func:`sigaltstack` function is available, to be able to |
| 17 | dump the traceback even on a stack overflow. |
| 18 | |
| 19 | The fault handler is called on catastrophic cases and so can only use |
| 20 | signal-safe functions (e.g. it cannot allocate memory on the heap). That's why |
| 21 | the traceback is limited: only support ASCII encoding (use the |
| 22 | ``backslashreplace`` error handler), limit each string to 100 characters, don't |
| 23 | print the source code (only the filename, the function name and the line |
| 24 | number), limit to 100 frames and 100 threads. |
| 25 | |
| 26 | By default, the Python traceback is written to :data:`sys.stderr`. Start your |
| 27 | graphical applications in a terminal and run your server in foreground to see |
| 28 | the traceback, or specify a log file to :func:`faulthandler.enable()`. |
| 29 | |
| 30 | The module is implemented in C to be able to dump a traceback on a crash or |
| 31 | when Python is blocked (e.g. deadlock). |
| 32 | |
| 33 | .. versionadded:: 3.3 |
| 34 | |
| 35 | |
| 36 | Dump the traceback |
| 37 | ------------------ |
| 38 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 39 | .. function:: dump_traceback(file=sys.stderr, all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 40 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 41 | Dump the traceback of all threads, or of the current thread if *all_threads* |
| 42 | is ``False``, into *file*. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 43 | |
| 44 | |
| 45 | Fault handler state |
| 46 | ------------------- |
| 47 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 48 | .. function:: enable(file=sys.stderr, all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 49 | |
| 50 | Enable the fault handler: install handlers for :const:`SIGSEGV`, |
Victor Stinner | d727e23 | 2011-04-01 12:13:55 +0200 | [diff] [blame] | 51 | :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL` |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 52 | signals to dump the Python traceback. It dumps the traceback of the all |
| 53 | threads, or of the current thread if *all_threads* is ``False``, into |
| 54 | *file*. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 55 | |
| 56 | .. function:: disable() |
| 57 | |
| 58 | Disable the fault handler: uninstall the signal handlers installed by |
| 59 | :func:`enable`. |
| 60 | |
| 61 | .. function:: is_enabled() |
| 62 | |
| 63 | Check if the fault handler is enabled. |
| 64 | |
| 65 | |
| 66 | Dump the tracebacks after a timeout |
| 67 | ----------------------------------- |
| 68 | |
| 69 | .. function:: dump_tracebacks_later(timeout, repeat=False, file=sys.stderr, exit=False) |
| 70 | |
| 71 | Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or |
| 72 | each *timeout* seconds if *repeat* is ``True``. If *exit* is True, call |
Georg Brandl | 7098685 | 2011-04-01 09:19:57 +0200 | [diff] [blame] | 73 | :c:func:`_exit` with status=1 after dumping the tracebacks to terminate |
| 74 | immediatly the process, which is not safe. For example, :c:func:`_exit` |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 75 | doesn't flush file buffers. If the function is called twice, the new call |
| 76 | replaces previous parameters (resets the timeout). The timer has a |
| 77 | sub-second resolution. |
| 78 | |
| 79 | This function is implemented using a watchdog thread, and therefore is |
| 80 | not available if Python is compiled with threads disabled. |
| 81 | |
Victor Stinner | 702624e | 2011-03-31 03:42:34 +0200 | [diff] [blame] | 82 | .. function:: cancel_dump_tracebacks_later() |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 83 | |
Victor Stinner | 702624e | 2011-03-31 03:42:34 +0200 | [diff] [blame] | 84 | Cancel the last call to :func:`dump_tracebacks_later`. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 85 | |
| 86 | |
| 87 | Dump the traceback on a user signal |
| 88 | ----------------------------------- |
| 89 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 90 | .. function:: register(signum, file=sys.stderr, all_threads=True) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 91 | |
| 92 | 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] | 93 | the traceback of all threads, or of the current thread if *all_threads* is |
| 94 | ``False``, into *file*. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 95 | |
| 96 | Not available on Windows. |
| 97 | |
| 98 | .. function:: unregister(signum) |
| 99 | |
| 100 | Unregister a user signal: uninstall the handler of the *signum* signal |
Victor Stinner | cfa7123 | 2011-04-08 12:48:15 +0200 | [diff] [blame] | 101 | installed by :func:`register`. Return ``True`` if the signal was registered, |
| 102 | ``False`` otherwise. |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 103 | |
| 104 | Not available on Windows. |
| 105 | |
| 106 | |
| 107 | File descriptor issue |
| 108 | --------------------- |
| 109 | |
Victor Stinner | 702624e | 2011-03-31 03:42:34 +0200 | [diff] [blame] | 110 | :func:`enable`, :func:`dump_tracebacks_later` and :func:`register` keep the |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 111 | file descriptor of their *file* argument. If the file is closed and its file |
| 112 | descriptor is reused by a new file, or if :func:`os.dup2` is used to replace |
| 113 | the file descriptor, the traceback will be written into a different file. Call |
| 114 | these functions again each time that the file is replaced. |
| 115 | |
| 116 | |
| 117 | Example |
| 118 | ------- |
| 119 | |
| 120 | Example of a segmentation fault on Linux: :: |
| 121 | |
| 122 | $ python -q -X faulthandler |
| 123 | >>> import ctypes |
| 124 | >>> ctypes.string_at(0) |
| 125 | Fatal Python error: Segmentation fault |
| 126 | |
Victor Stinner | 7bba62f | 2011-05-07 12:43:00 +0200 | [diff] [blame] | 127 | Current thread 0x00007fb899f39700: |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 128 | File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at |
| 129 | File "<stdin>", line 1 in <module> |
| 130 | Segmentation fault |
| 131 | |