• faulthandler —- Dump the Python traceback
    • Dumping the traceback
    • Fault handler state
    • Dumping the tracebacks after a timeout
    • Dumping the traceback on a user signal
    • Issue with file descriptors
    • 示例

    faulthandler —- Dump the Python traceback

    3.3 新版功能.


    This module contains functions to dump Python tracebacks explicitly, on a fault,after a timeout, or on a user signal. Call faulthandler.enable() toinstall fault handlers for the SIGSEGV, SIGFPE,SIGABRT, SIGBUS, and SIGILL signals. You can alsoenable them at startup by setting the PYTHONFAULTHANDLER environmentvariable or by using the -X faulthandler command line option.

    The fault handler is compatible with system fault handlers like Apport or theWindows fault handler. The module uses an alternative stack for signal handlersif the sigaltstack() function is available. This allows it to dump thetraceback even on a stack overflow.

    The fault handler is called on catastrophic cases and therefore can only usesignal-safe functions (e.g. it cannot allocate memory on the heap). Because ofthis limitation traceback dumping is minimal compared to normal Pythontracebacks:

    • Only ASCII is supported. The backslashreplace error handler is used onencoding.

    • Each string is limited to 500 characters.

    • Only the filename, the function name and the line number aredisplayed. (no source code)

    • It is limited to 100 frames and 100 threads.

    • The order is reversed: the most recent call is shown first.

    By default, the Python traceback is written to sys.stderr. To seetracebacks, applications must be run in the terminal. A log file canalternatively be passed to faulthandler.enable().

    The module is implemented in C, so tracebacks can be dumped on a crash or whenPython is deadlocked.

    Dumping the traceback

    • faulthandler.dumptraceback(_file=sys.stderr, all_threads=True)
    • Dump the tracebacks of all threads into file. If all_threads isFalse, dump only the current thread.

    在 3.5 版更改: Added support for passing file descriptor to this function.

    Fault handler state

    • faulthandler.enable(file=sys.stderr, all_threads=True)
    • Enable the fault handler: install handlers for the SIGSEGV,SIGFPE, SIGABRT, SIGBUS and SIGILLsignals to dump the Python traceback. If all_threads is True,produce tracebacks for every running thread. Otherwise, dump only the currentthread.

    The file must be kept open until the fault handler is disabled: seeissue with file descriptors.

    在 3.5 版更改: Added support for passing file descriptor to this function.

    在 3.6 版更改: On Windows, a handler for Windows exception is also installed.

    • faulthandler.disable()
    • Disable the fault handler: uninstall the signal handlers installed byenable().

    • faulthandler.is_enabled()

    • Check if the fault handler is enabled.

    Dumping the tracebacks after a timeout

    • faulthandler.dumptraceback_later(_timeout, repeat=False, file=sys.stderr, exit=False)
    • Dump the tracebacks of all threads, after a timeout of timeout seconds, orevery timeout seconds if repeat is True. If exit is True, call_exit() with status=1 after dumping the tracebacks. (Note_exit() exits the process immediately, which means it doesn't do anycleanup like flushing file buffers.) If the function is called twice, the newcall replaces previous parameters and resets the timeout. The timer has asub-second resolution.

    The file must be kept open until the traceback is dumped orcancel_dump_traceback_later() is called: see issue with filedescriptors.

    This function is implemented using a watchdog thread.

    在 3.7 版更改: This function is now always available.

    在 3.5 版更改: Added support for passing file descriptor to this function.

    • faulthandler.cancel_dump_traceback_later()
    • Cancel the last call to dump_traceback_later().

    Dumping the traceback on a user signal

    • faulthandler.register(signum, file=sys.stderr, all_threads=True, chain=False)
    • Register a user signal: install a handler for the signum signal to dumpthe traceback of all threads, or of the current thread if all_threads isFalse, into file. Call the previous handler if chain is True.

    The file must be kept open until the signal is unregistered byunregister(): see issue with file descriptors.

    Not available on Windows.

    在 3.5 版更改: Added support for passing file descriptor to this function.

    • faulthandler.unregister(signum)
    • Unregister a user signal: uninstall the handler of the signum signalinstalled by register(). Return True if the signal was registered,False otherwise.

    Not available on Windows.

    Issue with file descriptors

    enable(), dump_traceback_later() and register() keep thefile descriptor of their file argument. If the file is closed and its filedescriptor is reused by a new file, or if os.dup2() is used to replacethe file descriptor, the traceback will be written into a different file. Callthese functions again each time that the file is replaced.

    示例

    Example of a segmentation fault on Linux with and without enabling the faulthandler:

    1. $ python3 -c "import ctypes; ctypes.string_at(0)"
    2. Segmentation fault
    3.  
    4. $ python3 -q -X faulthandler
    5. >>> import ctypes
    6. >>> ctypes.string_at(0)
    7. Fatal Python error: Segmentation fault
    8.  
    9. Current thread 0x00007fb899f39700 (most recent call first):
    10. File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
    11. File "<stdin>", line 1 in <module>
    12. Segmentation fault