blob: f335afd1c65f22421674a8e99d0c3aff43352d42 [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.
26* Each string is limited to 100 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 Peterson85198752011-06-17 19:54:52 -050046 Dump the traceback of all threads into *file*. If *all_threads* is ``True``,
47 produce tracebacks for every running thread. Otherwise, dump only the current
48 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
72Dump the tracebacks after a timeout
73-----------------------------------
74
75.. function:: dump_tracebacks_later(timeout, repeat=False, file=sys.stderr, exit=False)
76
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
80 :c:func:`_exit` doesn't flush file buffers.) If the function is called twice,
81 the new call replaces previous parameters and resets the timeout. The timer
82 has a 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
Victor Stinner702624e2011-03-31 03:42:34 +020087.. function:: cancel_dump_tracebacks_later()
Victor Stinner024e37a2011-03-31 01:31:06 +020088
Victor Stinner702624e2011-03-31 03:42:34 +020089 Cancel the last call to :func:`dump_tracebacks_later`.
Victor Stinner024e37a2011-03-31 01:31:06 +020090
91
92Dump the traceback on a user signal
93-----------------------------------
94
Victor Stinner7bba62f2011-05-07 12:43:00 +020095.. function:: register(signum, file=sys.stderr, all_threads=True)
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
99 ``False``, into *file*.
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
Victor Stinner702624e2011-03-31 03:42:34 +0200115:func:`enable`, :func:`dump_tracebacks_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