blob: 3c336215fc22c99c501f7f1600b3c5cd99b6064b [file] [log] [blame]
Victor Stinner024e37a2011-03-31 01:31:06 +02001:mod:`faulthandler` --- Dump the Python traceback
2=================================================
3
4.. module:: faulthandler
5 :synopsis: Dump the Python traceback.
6
Benjamin Peterson85198752011-06-17 19:54:52 -05007This module contains functions to dump Python tracebacks explicitly, on a fault,
8after a timeout, or on a user signal. Call :func:`faulthandler.enable` to
9install fault handlers for the :const:`SIGSEGV`, :const:`SIGFPE`,
10:const:`SIGABRT`, :const:`SIGBUS`, and :const:`SIGILL` signals. You can also
11enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment
12variable or by using :option:`-X` ``faulthandler`` command line option.
Victor Stinner024e37a2011-03-31 01:31:06 +020013
Benjamin Peterson85198752011-06-17 19:54:52 -050014The fault handler is compatible with system fault handlers like Apport or the
15Windows fault handler. The module uses an alternative stack for signal handlers
16if the :c:func:`sigaltstack` function is available. This allows it to dump the
17traceback even on a stack overflow.
Victor Stinner024e37a2011-03-31 01:31:06 +020018
Benjamin Peterson85198752011-06-17 19:54:52 -050019The fault handler is called on catastrophic cases and therefore can only use
20signal-safe functions (e.g. it cannot allocate memory on the heap). Because of
21this limitation traceback dumping is minimal compared to normal Python
22tracebacks:
Victor Stinner024e37a2011-03-31 01:31:06 +020023
Benjamin Peterson85198752011-06-17 19:54:52 -050024* Only ASCII is supported. The ``backslashreplace`` error handler is used on
25 encoding.
Victor Stinner1713f9a2012-07-31 03:25:28 +020026* Each string is limited to 500 characters.
Victor Stinner2510d9e2011-06-19 16:07:20 +020027* Only the filename, the function name and the line number are
Benjamin Peterson85198752011-06-17 19:54:52 -050028 displayed. (no source code)
29* It is limited to 100 frames and 100 threads.
Victor Stinner024e37a2011-03-31 01:31:06 +020030
Benjamin Peterson85198752011-06-17 19:54:52 -050031By default, the Python traceback is written to :data:`sys.stderr`. To see
32tracebacks, applications must be run in the terminal. A log file can
33alternatively be passed to :func:`faulthandler.enable`.
34
35The module is implemented in C, so tracebacks can be dumped on a crash or when
36Python is deadlocked.
Victor Stinner024e37a2011-03-31 01:31:06 +020037
38.. versionadded:: 3.3
39
40
41Dump the traceback
42------------------
43
Victor Stinner7bba62f2011-05-07 12:43:00 +020044.. function:: dump_traceback(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020045
Benjamin Petersondefe6f62011-06-19 09:37:18 -050046 Dump the tracebacks of all threads into *file*. If *all_threads* is
47 ``False``, dump only the current thread.
Victor Stinner024e37a2011-03-31 01:31:06 +020048
49
50Fault handler state
51-------------------
52
Victor Stinner7bba62f2011-05-07 12:43:00 +020053.. function:: enable(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020054
Benjamin Peterson85198752011-06-17 19:54:52 -050055 Enable the fault handler: install handlers for the :const:`SIGSEGV`,
Victor Stinnerd727e232011-04-01 12:13:55 +020056 :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL`
Benjamin Peterson85198752011-06-17 19:54:52 -050057 signals to dump the Python traceback. If *all_threads* is ``True``,
58 produce tracebacks for every running thread. Otherwise, dump only the current
59 thread.
Victor Stinner024e37a2011-03-31 01:31:06 +020060
61.. function:: disable()
62
63 Disable the fault handler: uninstall the signal handlers installed by
64 :func:`enable`.
65
66.. function:: is_enabled()
67
68 Check if the fault handler is enabled.
69
70
71Dump the tracebacks after a timeout
72-----------------------------------
73
Georg Brandldeb92b52012-09-22 08:58:55 +020074.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
Victor Stinner024e37a2011-03-31 01:31:06 +020075
76 Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
Benjamin Peterson85198752011-06-17 19:54:52 -050077 every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call
78 :c:func:`_exit` with status=1 after dumping the tracebacks. (Note
Benjamin Petersondefe6f62011-06-19 09:37:18 -050079 :c:func:`_exit` exits the process immediately, which means it doesn't do any
80 cleanup like flushing file buffers.) If the function is called twice, the new
81 call replaces previous parameters and resets the timeout. The timer has a
82 sub-second resolution.
Victor Stinner024e37a2011-03-31 01:31:06 +020083
Benjamin Peterson85198752011-06-17 19:54:52 -050084 This function is implemented using a watchdog thread and therefore is not
85 available if Python is compiled with threads disabled.
Victor Stinner024e37a2011-03-31 01:31:06 +020086
Georg Brandldeb92b52012-09-22 08:58:55 +020087.. function:: cancel_dump_traceback_later()
Victor Stinner024e37a2011-03-31 01:31:06 +020088
Georg Brandldeb92b52012-09-22 08:58:55 +020089 Cancel the last call to :func:`dump_traceback_later`.
Victor Stinner024e37a2011-03-31 01:31:06 +020090
91
92Dump the traceback on a user signal
93-----------------------------------
94
Victor Stinnera9a9dab2011-07-13 23:39:53 +020095.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
Victor Stinner024e37a2011-03-31 01:31:06 +020096
97 Register a user signal: install a handler for the *signum* signal to dump
Victor Stinner7bba62f2011-05-07 12:43:00 +020098 the traceback of all threads, or of the current thread if *all_threads* is
Victor Stinnera9a9dab2011-07-13 23:39:53 +020099 ``False``, into *file*. Call the previous handler if chain is ``True``.
Victor Stinner024e37a2011-03-31 01:31:06 +0200100
101 Not available on Windows.
102
103.. function:: unregister(signum)
104
105 Unregister a user signal: uninstall the handler of the *signum* signal
Victor Stinnercfa71232011-04-08 12:48:15 +0200106 installed by :func:`register`. Return ``True`` if the signal was registered,
107 ``False`` otherwise.
Victor Stinner024e37a2011-03-31 01:31:06 +0200108
109 Not available on Windows.
110
111
112File descriptor issue
113---------------------
114
Georg Brandldeb92b52012-09-22 08:58:55 +0200115:func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the
Victor Stinner024e37a2011-03-31 01:31:06 +0200116file descriptor of their *file* argument. If the file is closed and its file
117descriptor is reused by a new file, or if :func:`os.dup2` is used to replace
118the file descriptor, the traceback will be written into a different file. Call
119these functions again each time that the file is replaced.
120
121
122Example
123-------
124
125Example of a segmentation fault on Linux: ::
126
127 $ python -q -X faulthandler
128 >>> import ctypes
129 >>> ctypes.string_at(0)
130 Fatal Python error: Segmentation fault
131
Victor Stinner7bba62f2011-05-07 12:43:00 +0200132 Current thread 0x00007fb899f39700:
Victor Stinner024e37a2011-03-31 01:31:06 +0200133 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
134 File "<stdin>", line 1 in <module>
135 Segmentation fault
136