2024-05-08 16:34:40 -03:00
|
|
|
:mod:`!faulthandler` --- Dump the Python traceback
|
|
|
|
==================================================
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
.. module:: faulthandler
|
|
|
|
:synopsis: Dump the Python traceback.
|
|
|
|
|
2014-03-24 05:06:18 -03:00
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
----------------
|
|
|
|
|
2011-06-17 21:54:52 -03:00
|
|
|
This module contains functions to dump Python tracebacks explicitly, on a fault,
|
|
|
|
after a timeout, or on a user signal. Call :func:`faulthandler.enable` to
|
2024-04-28 15:06:45 -03:00
|
|
|
install fault handlers for the :const:`~signal.SIGSEGV`,
|
|
|
|
:const:`~signal.SIGFPE`, :const:`~signal.SIGABRT`, :const:`~signal.SIGBUS`, and
|
|
|
|
:const:`~signal.SIGILL` signals. You can also
|
2011-06-17 21:54:52 -03:00
|
|
|
enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment
|
2014-03-25 08:33:56 -03:00
|
|
|
variable or by using the :option:`-X` ``faulthandler`` command line option.
|
2011-06-17 21:54:52 -03:00
|
|
|
|
|
|
|
The fault handler is compatible with system fault handlers like Apport or the
|
|
|
|
Windows fault handler. The module uses an alternative stack for signal handlers
|
2024-04-28 15:06:45 -03:00
|
|
|
if the :c:func:`!sigaltstack` function is available. This allows it to dump the
|
2011-06-17 21:54:52 -03:00
|
|
|
traceback even on a stack overflow.
|
|
|
|
|
|
|
|
The fault handler is called on catastrophic cases and therefore can only use
|
|
|
|
signal-safe functions (e.g. it cannot allocate memory on the heap). Because of
|
|
|
|
this limitation traceback dumping is minimal compared to normal Python
|
|
|
|
tracebacks:
|
|
|
|
|
|
|
|
* Only ASCII is supported. The ``backslashreplace`` error handler is used on
|
|
|
|
encoding.
|
2012-07-30 22:25:28 -03:00
|
|
|
* Each string is limited to 500 characters.
|
2011-06-19 11:07:20 -03:00
|
|
|
* Only the filename, the function name and the line number are
|
2011-06-17 21:54:52 -03:00
|
|
|
displayed. (no source code)
|
|
|
|
* It is limited to 100 frames and 100 threads.
|
2013-10-20 23:15:19 -03:00
|
|
|
* The order is reversed: the most recent call is shown first.
|
2011-06-17 21:54:52 -03:00
|
|
|
|
|
|
|
By default, the Python traceback is written to :data:`sys.stderr`. To see
|
|
|
|
tracebacks, applications must be run in the terminal. A log file can
|
|
|
|
alternatively be passed to :func:`faulthandler.enable`.
|
|
|
|
|
|
|
|
The module is implemented in C, so tracebacks can be dumped on a crash or when
|
|
|
|
Python is deadlocked.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2020-01-24 05:22:18 -04:00
|
|
|
The :ref:`Python Development Mode <devmode>` calls :func:`faulthandler.enable`
|
|
|
|
at Python startup.
|
|
|
|
|
2023-02-03 23:49:29 -04:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Module :mod:`pdb`
|
|
|
|
Interactive source code debugger for Python programs.
|
|
|
|
|
|
|
|
Module :mod:`traceback`
|
|
|
|
Standard interface to extract, format and print stack traces of Python programs.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2014-03-25 08:33:56 -03:00
|
|
|
Dumping the traceback
|
|
|
|
---------------------
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2011-05-07 07:43:00 -03:00
|
|
|
.. function:: dump_traceback(file=sys.stderr, all_threads=True)
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2011-06-19 11:37:18 -03:00
|
|
|
Dump the tracebacks of all threads into *file*. If *all_threads* is
|
|
|
|
``False``, dump only the current thread.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2023-02-03 23:49:29 -04:00
|
|
|
.. seealso:: :func:`traceback.print_tb`, which can be used to print a traceback object.
|
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
.. versionchanged:: 3.5
|
|
|
|
Added support for passing file descriptor to this function.
|
|
|
|
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
Fault handler state
|
|
|
|
-------------------
|
|
|
|
|
2011-05-07 07:43:00 -03:00
|
|
|
.. function:: enable(file=sys.stderr, all_threads=True)
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2024-04-28 15:06:45 -03:00
|
|
|
Enable the fault handler: install handlers for the :const:`~signal.SIGSEGV`,
|
|
|
|
:const:`~signal.SIGFPE`, :const:`~signal.SIGABRT`, :const:`~signal.SIGBUS`
|
|
|
|
and :const:`~signal.SIGILL`
|
2011-06-17 21:54:52 -03:00
|
|
|
signals to dump the Python traceback. If *all_threads* is ``True``,
|
|
|
|
produce tracebacks for every running thread. Otherwise, dump only the current
|
|
|
|
thread.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
The *file* must be kept open until the fault handler is disabled: see
|
|
|
|
:ref:`issue with file descriptors <faulthandler-fd>`.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
Added support for passing file descriptor to this function.
|
|
|
|
|
2016-03-23 06:39:17 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
On Windows, a handler for Windows exception is also installed.
|
|
|
|
|
2021-06-21 08:15:40 -03:00
|
|
|
.. versionchanged:: 3.10
|
|
|
|
The dump now mentions if a garbage collector collection is running
|
|
|
|
if *all_threads* is true.
|
|
|
|
|
2011-03-30 20:31:06 -03:00
|
|
|
.. function:: disable()
|
|
|
|
|
|
|
|
Disable the fault handler: uninstall the signal handlers installed by
|
|
|
|
:func:`enable`.
|
|
|
|
|
|
|
|
.. function:: is_enabled()
|
|
|
|
|
|
|
|
Check if the fault handler is enabled.
|
|
|
|
|
|
|
|
|
2014-03-25 08:33:56 -03:00
|
|
|
Dumping the tracebacks after a timeout
|
|
|
|
--------------------------------------
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2012-09-22 03:58:55 -03:00
|
|
|
.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
|
2011-06-17 21:54:52 -03:00
|
|
|
every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call
|
2024-04-28 15:06:45 -03:00
|
|
|
:c:func:`!_exit` with status=1 after dumping the tracebacks. (Note
|
|
|
|
:c:func:`!_exit` exits the process immediately, which means it doesn't do any
|
2011-06-19 11:37:18 -03:00
|
|
|
cleanup like flushing file buffers.) If the function is called twice, the new
|
|
|
|
call replaces previous parameters and resets the timeout. The timer has a
|
|
|
|
sub-second resolution.
|
2011-06-17 21:54:52 -03:00
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
The *file* must be kept open until the traceback is dumped or
|
|
|
|
:func:`cancel_dump_traceback_later` is called: see :ref:`issue with file
|
|
|
|
descriptors <faulthandler-fd>`.
|
|
|
|
|
2019-09-18 09:15:10 -03:00
|
|
|
This function is implemented using a watchdog thread.
|
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
.. versionchanged:: 3.5
|
|
|
|
Added support for passing file descriptor to this function.
|
|
|
|
|
2024-03-07 04:05:03 -04:00
|
|
|
.. versionchanged:: 3.7
|
|
|
|
This function is now always available.
|
|
|
|
|
2012-09-22 03:58:55 -03:00
|
|
|
.. function:: cancel_dump_traceback_later()
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2012-09-22 03:58:55 -03:00
|
|
|
Cancel the last call to :func:`dump_traceback_later`.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
|
2014-03-25 08:33:56 -03:00
|
|
|
Dumping the traceback on a user signal
|
|
|
|
--------------------------------------
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2011-07-13 18:39:53 -03:00
|
|
|
.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
Register a user signal: install a handler for the *signum* signal to dump
|
2011-05-07 07:43:00 -03:00
|
|
|
the traceback of all threads, or of the current thread if *all_threads* is
|
2011-07-13 18:39:53 -03:00
|
|
|
``False``, into *file*. Call the previous handler if chain is ``True``.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
The *file* must be kept open until the signal is unregistered by
|
|
|
|
:func:`unregister`: see :ref:`issue with file descriptors <faulthandler-fd>`.
|
|
|
|
|
2011-03-30 20:31:06 -03:00
|
|
|
Not available on Windows.
|
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
.. versionchanged:: 3.5
|
|
|
|
Added support for passing file descriptor to this function.
|
|
|
|
|
2011-03-30 20:31:06 -03:00
|
|
|
.. function:: unregister(signum)
|
|
|
|
|
|
|
|
Unregister a user signal: uninstall the handler of the *signum* signal
|
2011-04-08 07:48:15 -03:00
|
|
|
installed by :func:`register`. Return ``True`` if the signal was registered,
|
|
|
|
``False`` otherwise.
|
2011-03-30 20:31:06 -03:00
|
|
|
|
|
|
|
Not available on Windows.
|
|
|
|
|
|
|
|
|
2015-03-12 11:32:03 -03:00
|
|
|
.. _faulthandler-fd:
|
|
|
|
|
2014-03-25 08:33:56 -03:00
|
|
|
Issue with file descriptors
|
|
|
|
---------------------------
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2012-09-22 03:58:55 -03:00
|
|
|
:func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the
|
2011-03-30 20:31:06 -03:00
|
|
|
file descriptor of their *file* argument. If the file is closed and its file
|
|
|
|
descriptor is reused by a new file, or if :func:`os.dup2` is used to replace
|
|
|
|
the file descriptor, the traceback will be written into a different file. Call
|
|
|
|
these functions again each time that the file is replaced.
|
|
|
|
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
2014-03-25 08:33:56 -03:00
|
|
|
Example of a segmentation fault on Linux with and without enabling the fault
|
2018-04-08 13:18:04 -03:00
|
|
|
handler:
|
|
|
|
|
|
|
|
.. code-block:: shell-session
|
2014-03-25 08:33:56 -03:00
|
|
|
|
2023-01-11 06:05:41 -04:00
|
|
|
$ python -c "import ctypes; ctypes.string_at(0)"
|
2014-03-25 08:33:56 -03:00
|
|
|
Segmentation fault
|
2011-03-30 20:31:06 -03:00
|
|
|
|
2023-01-11 06:05:41 -04:00
|
|
|
$ python -q -X faulthandler
|
2011-03-30 20:31:06 -03:00
|
|
|
>>> import ctypes
|
|
|
|
>>> ctypes.string_at(0)
|
|
|
|
Fatal Python error: Segmentation fault
|
|
|
|
|
2013-10-20 23:15:19 -03:00
|
|
|
Current thread 0x00007fb899f39700 (most recent call first):
|
2011-03-30 20:31:06 -03:00
|
|
|
File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
|
|
|
|
File "<stdin>", line 1 in <module>
|
|
|
|
Segmentation fault
|