blob: 59274c1dd7ec354a8b0094e71fcd281c1fa63173 [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
Georg Brandldf48b972014-03-24 09:06:18 +01007.. versionadded:: 3.3
8
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009----------------
10
Benjamin Peterson85198752011-06-17 19:54:52 -050011This module contains functions to dump Python tracebacks explicitly, on a fault,
12after a timeout, or on a user signal. Call :func:`faulthandler.enable` to
13install fault handlers for the :const:`SIGSEGV`, :const:`SIGFPE`,
14:const:`SIGABRT`, :const:`SIGBUS`, and :const:`SIGILL` signals. You can also
15enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment
Victor Stinner215ad662014-03-25 12:33:56 +010016variable or by using the :option:`-X` ``faulthandler`` command line option.
Victor Stinner024e37a2011-03-31 01:31:06 +020017
Benjamin Peterson85198752011-06-17 19:54:52 -050018The fault handler is compatible with system fault handlers like Apport or the
19Windows fault handler. The module uses an alternative stack for signal handlers
20if the :c:func:`sigaltstack` function is available. This allows it to dump the
21traceback even on a stack overflow.
Victor Stinner024e37a2011-03-31 01:31:06 +020022
Benjamin Peterson85198752011-06-17 19:54:52 -050023The fault handler is called on catastrophic cases and therefore can only use
24signal-safe functions (e.g. it cannot allocate memory on the heap). Because of
25this limitation traceback dumping is minimal compared to normal Python
26tracebacks:
Victor Stinner024e37a2011-03-31 01:31:06 +020027
Benjamin Peterson85198752011-06-17 19:54:52 -050028* Only ASCII is supported. The ``backslashreplace`` error handler is used on
29 encoding.
Victor Stinner1713f9a2012-07-31 03:25:28 +020030* Each string is limited to 500 characters.
Victor Stinner2510d9e2011-06-19 16:07:20 +020031* Only the filename, the function name and the line number are
Benjamin Peterson85198752011-06-17 19:54:52 -050032 displayed. (no source code)
33* It is limited to 100 frames and 100 threads.
Guido van Rossum2063aaf2013-10-20 19:15:19 -070034* The order is reversed: the most recent call is shown first.
Victor Stinner024e37a2011-03-31 01:31:06 +020035
Benjamin Peterson85198752011-06-17 19:54:52 -050036By default, the Python traceback is written to :data:`sys.stderr`. To see
37tracebacks, applications must be run in the terminal. A log file can
38alternatively be passed to :func:`faulthandler.enable`.
39
40The module is implemented in C, so tracebacks can be dumped on a crash or when
41Python is deadlocked.
Victor Stinner024e37a2011-03-31 01:31:06 +020042
Victor Stinnerb9783d22020-01-24 10:22:18 +010043The :ref:`Python Development Mode <devmode>` calls :func:`faulthandler.enable`
44at Python startup.
45
Victor Stinner024e37a2011-03-31 01:31:06 +020046
Victor Stinner215ad662014-03-25 12:33:56 +010047Dumping the traceback
48---------------------
Victor Stinner024e37a2011-03-31 01:31:06 +020049
Victor Stinner7bba62f2011-05-07 12:43:00 +020050.. function:: dump_traceback(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020051
Benjamin Petersondefe6f62011-06-19 09:37:18 -050052 Dump the tracebacks of all threads into *file*. If *all_threads* is
53 ``False``, dump only the current thread.
Victor Stinner024e37a2011-03-31 01:31:06 +020054
Victor Stinner95bb7142015-03-12 15:32:03 +010055 .. versionchanged:: 3.5
56 Added support for passing file descriptor to this function.
57
Victor Stinner024e37a2011-03-31 01:31:06 +020058
59Fault handler state
60-------------------
61
Victor Stinner7bba62f2011-05-07 12:43:00 +020062.. function:: enable(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020063
Benjamin Peterson85198752011-06-17 19:54:52 -050064 Enable the fault handler: install handlers for the :const:`SIGSEGV`,
Victor Stinnerd727e232011-04-01 12:13:55 +020065 :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL`
Benjamin Peterson85198752011-06-17 19:54:52 -050066 signals to dump the Python traceback. If *all_threads* is ``True``,
67 produce tracebacks for every running thread. Otherwise, dump only the current
68 thread.
Victor Stinner024e37a2011-03-31 01:31:06 +020069
Victor Stinner95bb7142015-03-12 15:32:03 +010070 The *file* must be kept open until the fault handler is disabled: see
71 :ref:`issue with file descriptors <faulthandler-fd>`.
72
73 .. versionchanged:: 3.5
74 Added support for passing file descriptor to this function.
75
Victor Stinner404cdc52016-03-23 10:39:17 +010076 .. versionchanged:: 3.6
77 On Windows, a handler for Windows exception is also installed.
78
Victor Stinner024e37a2011-03-31 01:31:06 +020079.. function:: disable()
80
81 Disable the fault handler: uninstall the signal handlers installed by
82 :func:`enable`.
83
84.. function:: is_enabled()
85
86 Check if the fault handler is enabled.
87
88
Victor Stinner215ad662014-03-25 12:33:56 +010089Dumping the tracebacks after a timeout
90--------------------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +020091
Georg Brandldeb92b52012-09-22 08:58:55 +020092.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
Victor Stinner024e37a2011-03-31 01:31:06 +020093
94 Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
Benjamin Peterson85198752011-06-17 19:54:52 -050095 every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call
96 :c:func:`_exit` with status=1 after dumping the tracebacks. (Note
Benjamin Petersondefe6f62011-06-19 09:37:18 -050097 :c:func:`_exit` exits the process immediately, which means it doesn't do any
98 cleanup like flushing file buffers.) If the function is called twice, the new
99 call replaces previous parameters and resets the timeout. The timer has a
100 sub-second resolution.
Victor Stinner024e37a2011-03-31 01:31:06 +0200101
Victor Stinner95bb7142015-03-12 15:32:03 +0100102 The *file* must be kept open until the traceback is dumped or
103 :func:`cancel_dump_traceback_later` is called: see :ref:`issue with file
104 descriptors <faulthandler-fd>`.
105
Victor Stinner0a963fb2019-09-18 14:15:10 +0200106 This function is implemented using a watchdog thread.
107
108 .. versionchanged:: 3.7
109 This function is now always available.
Victor Stinner024e37a2011-03-31 01:31:06 +0200110
Victor Stinner95bb7142015-03-12 15:32:03 +0100111 .. versionchanged:: 3.5
112 Added support for passing file descriptor to this function.
113
Georg Brandldeb92b52012-09-22 08:58:55 +0200114.. function:: cancel_dump_traceback_later()
Victor Stinner024e37a2011-03-31 01:31:06 +0200115
Georg Brandldeb92b52012-09-22 08:58:55 +0200116 Cancel the last call to :func:`dump_traceback_later`.
Victor Stinner024e37a2011-03-31 01:31:06 +0200117
118
Victor Stinner215ad662014-03-25 12:33:56 +0100119Dumping the traceback on a user signal
120--------------------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +0200121
Victor Stinnera9a9dab2011-07-13 23:39:53 +0200122.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
Victor Stinner024e37a2011-03-31 01:31:06 +0200123
124 Register a user signal: install a handler for the *signum* signal to dump
Victor Stinner7bba62f2011-05-07 12:43:00 +0200125 the traceback of all threads, or of the current thread if *all_threads* is
Victor Stinnera9a9dab2011-07-13 23:39:53 +0200126 ``False``, into *file*. Call the previous handler if chain is ``True``.
Victor Stinner024e37a2011-03-31 01:31:06 +0200127
Victor Stinner95bb7142015-03-12 15:32:03 +0100128 The *file* must be kept open until the signal is unregistered by
129 :func:`unregister`: see :ref:`issue with file descriptors <faulthandler-fd>`.
130
Victor Stinner024e37a2011-03-31 01:31:06 +0200131 Not available on Windows.
132
Victor Stinner95bb7142015-03-12 15:32:03 +0100133 .. versionchanged:: 3.5
134 Added support for passing file descriptor to this function.
135
Victor Stinner024e37a2011-03-31 01:31:06 +0200136.. function:: unregister(signum)
137
138 Unregister a user signal: uninstall the handler of the *signum* signal
Victor Stinnercfa71232011-04-08 12:48:15 +0200139 installed by :func:`register`. Return ``True`` if the signal was registered,
140 ``False`` otherwise.
Victor Stinner024e37a2011-03-31 01:31:06 +0200141
142 Not available on Windows.
143
144
Victor Stinner95bb7142015-03-12 15:32:03 +0100145.. _faulthandler-fd:
146
Victor Stinner215ad662014-03-25 12:33:56 +0100147Issue with file descriptors
148---------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +0200149
Georg Brandldeb92b52012-09-22 08:58:55 +0200150:func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the
Victor Stinner024e37a2011-03-31 01:31:06 +0200151file descriptor of their *file* argument. If the file is closed and its file
152descriptor is reused by a new file, or if :func:`os.dup2` is used to replace
153the file descriptor, the traceback will be written into a different file. Call
154these functions again each time that the file is replaced.
155
156
157Example
158-------
159
Victor Stinner215ad662014-03-25 12:33:56 +0100160Example of a segmentation fault on Linux with and without enabling the fault
Serhiy Storchaka46936d52018-04-08 19:18:04 +0300161handler:
162
163.. code-block:: shell-session
Victor Stinner215ad662014-03-25 12:33:56 +0100164
165 $ python3 -c "import ctypes; ctypes.string_at(0)"
166 Segmentation fault
167
168 $ python3 -q -X faulthandler
Victor Stinner024e37a2011-03-31 01:31:06 +0200169 >>> import ctypes
170 >>> ctypes.string_at(0)
171 Fatal Python error: Segmentation fault
172
Guido van Rossum2063aaf2013-10-20 19:15:19 -0700173 Current thread 0x00007fb899f39700 (most recent call first):
Victor Stinner024e37a2011-03-31 01:31:06 +0200174 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
175 File "<stdin>", line 1 in <module>
176 Segmentation fault
177