blob: eb2016a7b9b80218320a3a89bdf7d03dfc755a40 [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
Benjamin Peterson85198752011-06-17 19:54:52 -05009This module contains functions to dump Python tracebacks explicitly, on a fault,
10after a timeout, or on a user signal. Call :func:`faulthandler.enable` to
11install fault handlers for the :const:`SIGSEGV`, :const:`SIGFPE`,
12:const:`SIGABRT`, :const:`SIGBUS`, and :const:`SIGILL` signals. You can also
13enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment
Victor Stinner215ad662014-03-25 12:33:56 +010014variable or by using the :option:`-X` ``faulthandler`` command line option.
Victor Stinner024e37a2011-03-31 01:31:06 +020015
Benjamin Peterson85198752011-06-17 19:54:52 -050016The fault handler is compatible with system fault handlers like Apport or the
17Windows fault handler. The module uses an alternative stack for signal handlers
18if the :c:func:`sigaltstack` function is available. This allows it to dump the
19traceback even on a stack overflow.
Victor Stinner024e37a2011-03-31 01:31:06 +020020
Benjamin Peterson85198752011-06-17 19:54:52 -050021The fault handler is called on catastrophic cases and therefore can only use
22signal-safe functions (e.g. it cannot allocate memory on the heap). Because of
23this limitation traceback dumping is minimal compared to normal Python
24tracebacks:
Victor Stinner024e37a2011-03-31 01:31:06 +020025
Benjamin Peterson85198752011-06-17 19:54:52 -050026* Only ASCII is supported. The ``backslashreplace`` error handler is used on
27 encoding.
Victor Stinner1713f9a2012-07-31 03:25:28 +020028* Each string is limited to 500 characters.
Victor Stinner2510d9e2011-06-19 16:07:20 +020029* Only the filename, the function name and the line number are
Benjamin Peterson85198752011-06-17 19:54:52 -050030 displayed. (no source code)
31* It is limited to 100 frames and 100 threads.
Guido van Rossum2063aaf2013-10-20 19:15:19 -070032* The order is reversed: the most recent call is shown first.
Victor Stinner024e37a2011-03-31 01:31:06 +020033
Benjamin Peterson85198752011-06-17 19:54:52 -050034By default, the Python traceback is written to :data:`sys.stderr`. To see
35tracebacks, applications must be run in the terminal. A log file can
36alternatively be passed to :func:`faulthandler.enable`.
37
38The module is implemented in C, so tracebacks can be dumped on a crash or when
39Python is deadlocked.
Victor Stinner024e37a2011-03-31 01:31:06 +020040
Victor Stinner024e37a2011-03-31 01:31:06 +020041
Victor Stinner215ad662014-03-25 12:33:56 +010042Dumping the traceback
43---------------------
Victor Stinner024e37a2011-03-31 01:31:06 +020044
Victor Stinner7bba62f2011-05-07 12:43:00 +020045.. function:: dump_traceback(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020046
Benjamin Petersondefe6f62011-06-19 09:37:18 -050047 Dump the tracebacks of all threads into *file*. If *all_threads* is
48 ``False``, dump only the current thread.
Victor Stinner024e37a2011-03-31 01:31:06 +020049
50
51Fault handler state
52-------------------
53
Victor Stinner7bba62f2011-05-07 12:43:00 +020054.. function:: enable(file=sys.stderr, all_threads=True)
Victor Stinner024e37a2011-03-31 01:31:06 +020055
Benjamin Peterson85198752011-06-17 19:54:52 -050056 Enable the fault handler: install handlers for the :const:`SIGSEGV`,
Victor Stinnerd727e232011-04-01 12:13:55 +020057 :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL`
Benjamin Peterson85198752011-06-17 19:54:52 -050058 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 Stinner024e37a2011-03-31 01:31:06 +020061
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
Victor Stinner215ad662014-03-25 12:33:56 +010072Dumping the tracebacks after a timeout
73--------------------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +020074
Georg Brandldeb92b52012-09-22 08:58:55 +020075.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
Victor Stinner024e37a2011-03-31 01:31:06 +020076
77 Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
Benjamin Peterson85198752011-06-17 19:54:52 -050078 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 Petersondefe6f62011-06-19 09:37:18 -050080 :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 Stinner024e37a2011-03-31 01:31:06 +020084
Benjamin Peterson85198752011-06-17 19:54:52 -050085 This function is implemented using a watchdog thread and therefore is not
86 available if Python is compiled with threads disabled.
Victor Stinner024e37a2011-03-31 01:31:06 +020087
Georg Brandldeb92b52012-09-22 08:58:55 +020088.. function:: cancel_dump_traceback_later()
Victor Stinner024e37a2011-03-31 01:31:06 +020089
Georg Brandldeb92b52012-09-22 08:58:55 +020090 Cancel the last call to :func:`dump_traceback_later`.
Victor Stinner024e37a2011-03-31 01:31:06 +020091
92
Victor Stinner215ad662014-03-25 12:33:56 +010093Dumping the traceback on a user signal
94--------------------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +020095
Victor Stinnera9a9dab2011-07-13 23:39:53 +020096.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
Victor Stinner024e37a2011-03-31 01:31:06 +020097
98 Register a user signal: install a handler for the *signum* signal to dump
Victor Stinner7bba62f2011-05-07 12:43:00 +020099 the traceback of all threads, or of the current thread if *all_threads* is
Victor Stinnera9a9dab2011-07-13 23:39:53 +0200100 ``False``, into *file*. Call the previous handler if chain is ``True``.
Victor Stinner024e37a2011-03-31 01:31:06 +0200101
102 Not available on Windows.
103
104.. function:: unregister(signum)
105
106 Unregister a user signal: uninstall the handler of the *signum* signal
Victor Stinnercfa71232011-04-08 12:48:15 +0200107 installed by :func:`register`. Return ``True`` if the signal was registered,
108 ``False`` otherwise.
Victor Stinner024e37a2011-03-31 01:31:06 +0200109
110 Not available on Windows.
111
112
Victor Stinner215ad662014-03-25 12:33:56 +0100113Issue with file descriptors
114---------------------------
Victor Stinner024e37a2011-03-31 01:31:06 +0200115
Georg Brandldeb92b52012-09-22 08:58:55 +0200116:func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the
Victor Stinner024e37a2011-03-31 01:31:06 +0200117file descriptor of their *file* argument. If the file is closed and its file
118descriptor is reused by a new file, or if :func:`os.dup2` is used to replace
119the file descriptor, the traceback will be written into a different file. Call
120these functions again each time that the file is replaced.
121
122
123Example
124-------
125
Victor Stinner215ad662014-03-25 12:33:56 +0100126.. highlight:: sh
Victor Stinner024e37a2011-03-31 01:31:06 +0200127
Victor Stinner215ad662014-03-25 12:33:56 +0100128Example of a segmentation fault on Linux with and without enabling the fault
129handler::
130
131 $ python3 -c "import ctypes; ctypes.string_at(0)"
132 Segmentation fault
133
134 $ python3 -q -X faulthandler
Victor Stinner024e37a2011-03-31 01:31:06 +0200135 >>> import ctypes
136 >>> ctypes.string_at(0)
137 Fatal Python error: Segmentation fault
138
Guido van Rossum2063aaf2013-10-20 19:15:19 -0700139 Current thread 0x00007fb899f39700 (most recent call first):
Victor Stinner024e37a2011-03-31 01:31:06 +0200140 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
141 File "<stdin>", line 1 in <module>
142 Segmentation fault
143