blob: 7b106bc68b779da3ecf31f6871816fe39e49091a [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
7This module contains functions to dump the Python traceback explicitly, on a
8fault, after a timeout or on a user signal. Call :func:`faulthandler.enable` to
9install fault handlers for :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGBUS`
10and :const:`SIGILL` signals. You can also enable them at startup by setting the
11:envvar:`PYTHONFAULTHANDLER` environment variable or by using :option:`-X`
12``faulthandler`` command line option.
13
14The fault handler is compatible with system fault handlers like Apport or
15the Windows fault handler. The module uses an alternative stack for signal
16handlers, if the :c:func:`sigaltstack` function is available, to be able to
17dump the traceback even on a stack overflow.
18
19The fault handler is called on catastrophic cases and so can only use
20signal-safe functions (e.g. it cannot allocate memory on the heap). That's why
21the traceback is limited: only support ASCII encoding (use the
22``backslashreplace`` error handler), limit each string to 100 characters, don't
23print the source code (only the filename, the function name and the line
24number), limit to 100 frames and 100 threads.
25
26By default, the Python traceback is written to :data:`sys.stderr`. Start your
27graphical applications in a terminal and run your server in foreground to see
28the traceback, or specify a log file to :func:`faulthandler.enable()`.
29
30The module is implemented in C to be able to dump a traceback on a crash or
31when Python is blocked (e.g. deadlock).
32
33.. versionadded:: 3.3
34
35
36Dump the traceback
37------------------
38
39.. function:: dump_traceback(file=sys.stderr, all_threads=False)
40
41 Dump the traceback of the current thread, or of all threads if *all_threads*
42 is ``True``, into *file*.
43
44
45Fault handler state
46-------------------
47
48.. function:: enable(file=sys.stderr, all_threads=False)
49
50 Enable the fault handler: install handlers for :const:`SIGSEGV`,
51 :const:`SIGFPE`, :const:`SIGBUS` and :const:`SIGILL` signals to dump the
52 Python traceback. It dumps the traceback of the current thread, or all
53 threads if *all_threads* is ``True``, into *file*.
54
55.. function:: disable()
56
57 Disable the fault handler: uninstall the signal handlers installed by
58 :func:`enable`.
59
60.. function:: is_enabled()
61
62 Check if the fault handler is enabled.
63
64
65Dump the tracebacks after a timeout
66-----------------------------------
67
68.. function:: dump_tracebacks_later(timeout, repeat=False, file=sys.stderr, exit=False)
69
70 Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
71 each *timeout* seconds if *repeat* is ``True``. If *exit* is True, call
Georg Brandl70986852011-04-01 09:19:57 +020072 :c:func:`_exit` with status=1 after dumping the tracebacks to terminate
73 immediatly the process, which is not safe. For example, :c:func:`_exit`
Victor Stinner024e37a2011-03-31 01:31:06 +020074 doesn't flush file buffers. If the function is called twice, the new call
75 replaces previous parameters (resets the timeout). The timer has a
76 sub-second resolution.
77
78 This function is implemented using a watchdog thread, and therefore is
79 not available if Python is compiled with threads disabled.
80
Victor Stinner702624e2011-03-31 03:42:34 +020081.. function:: cancel_dump_tracebacks_later()
Victor Stinner024e37a2011-03-31 01:31:06 +020082
Victor Stinner702624e2011-03-31 03:42:34 +020083 Cancel the last call to :func:`dump_tracebacks_later`.
Victor Stinner024e37a2011-03-31 01:31:06 +020084
85
86Dump the traceback on a user signal
87-----------------------------------
88
89.. function:: register(signum, file=sys.stderr, all_threads=False)
90
91 Register a user signal: install a handler for the *signum* signal to dump
92 the traceback of the current thread, or of all threads if *all_threads* is
93 ``True``, into *file*.
94
95 Not available on Windows.
96
97.. function:: unregister(signum)
98
99 Unregister a user signal: uninstall the handler of the *signum* signal
100 installed by :func:`register`.
101
102 Not available on Windows.
103
104
105File descriptor issue
106---------------------
107
Victor Stinner702624e2011-03-31 03:42:34 +0200108:func:`enable`, :func:`dump_tracebacks_later` and :func:`register` keep the
Victor Stinner024e37a2011-03-31 01:31:06 +0200109file descriptor of their *file* argument. If the file is closed and its file
110descriptor is reused by a new file, or if :func:`os.dup2` is used to replace
111the file descriptor, the traceback will be written into a different file. Call
112these functions again each time that the file is replaced.
113
114
115Example
116-------
117
118Example of a segmentation fault on Linux: ::
119
120 $ python -q -X faulthandler
121 >>> import ctypes
122 >>> ctypes.string_at(0)
123 Fatal Python error: Segmentation fault
124
125 Traceback (most recent call first):
126 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
127 File "<stdin>", line 1 in <module>
128 Segmentation fault
129