Python: 使用 faulthandler 模块获取运行中进程的 traceback 信息

Python 3.3 新增了一个 faulthandler 模块,可以输出运行中进程的 traceback 信息,可以用来作为调试运行中的 Python 进程的一种手段。

特点及限制

模块本身是使用 C 实现的,所以有如下特点和限制:

特点

  • 在 Segmentation fault 或者 Python 死锁时仍然可以输出 traceback 信息

限制

  • 只能用于信号安全的函数
  • 只支持 ASCII 字符。程序使用 backslashreplace 作为编码时的 error handler
  • 每个字符串最长只能包含 500 个字符
  • 只显示文件名,函数名和行号(不显示源码)
  • 最大支持 100 个 frame 和 100 个线程
  • 显示顺序是翻转的:最近的调用显示在前面

安装

Python 3.3 自带了这个模块,Python 2.x 可以通过一下方式安装:

pip install faulthandler

使用示例

手动输出 traceback 信息

测试函数:

from threading import Thread
import time


def do(x):
    x = x * 3
    time.sleep(x * 60)


def main():
    threads = []
    for x in range(1, 3):
        t = Thread(target=do, args=(x,))
        t.start()
    for x in threads:
        x.join()

使用 faulthandler 输出所有线程的 traceback 信息:

In [1]: import test

In [2]: test.main()

In [3]: import faulthandler

In [4]: faulthandler.dump_traceback()
Thread 0x0000700009d66000 (most recent call first):
  File "/Users/xxx/tmp/test_faulthandler/test.py", line 7 in do
  File "/xxx/lib/python3.6/threading.py", line 864 in run
  File "/xxx/lib/python3.6/threading.py", line 916 in _bootstrap_inner
  File "/xxx/lib/python3.6/threading.py", line 884 in _bootstrap

Thread 0x0000700009863000 (most recent call first):
  File "/Users/xxx/tmp/test_faulthandler/test.py", line 7 in do
  File "/xxx/lib/python3.6/threading.py", line 864 in run
  File "/xxx/lib/python3.6/threading.py", line 916 in _bootstrap_inner
  File "/xxx/lib/python3.6/threading.py", line 884 in _bootstrap

Thread 0x0000700009360000 (most recent call first):
  File "/xxx/lib/python3.6/threading.py", line 295 in wait
  File "/xxx/lib/python3.6/threading.py", line 551 in wait
  File "/xxx/lib/python3.6/site-packages/IPython/core/history.py", line 834 in run
  File "/xxx/lib/python3.6/site-packages/IPython/core/history.py", line 60 in needs_sqlite
  File "<decorator-gen-23>", line 2 in run
  File "/xxx/lib/python3.6/threading.py", line 916 in _bootstrap_inner
  File "/xxx/lib/python3.6/threading.py", line 884 in _bootstrap

Current thread 0x00007fffe03873c0 (most recent call first):
  File "<ipython-input-4-6b8299023d33>", line 1 in <module>
  File "/xxx/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2881 in run_code
  File "/xxx/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2827 in run_ast_nodes
  File "/xxx/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2717 in run_cell
  File "/xxx/lib/python3.6/site-packages/IPython/terminal/interactiveshell.py", line 446 in interact
  File "/xxx/lib/python3.6/site-packages/IPython/terminal/interactiveshell.py", line 455 in mainloop
  File "/xxx/lib/python3.6/site-packages/IPython/terminal/ipapp.py", line 348 in start
  File "/xxx/lib/python3.6/site-packages/traitlets/config/application.py", line 658 in launch_instance
  File "/xxx/lib/python3.6/site-packages/IPython/__init__.py", line 119 in start_ipython
  File "/xxx/bin/ipython", line 11 in <module>

In [5]:

可以通过 all_threads=False 参数指定只输出当前线程, file=sys.stderr 参数指定输出到哪里(默认是标准错误), 详见 faulthandler 官方文档。

通过信号自动输出 traceback 信息

给前面的测试函数增加信号处理,增加通过 USR1 信号输出 traceback 信息的功能:

import faulthandler
import signal
from threading import Thread
import time


def do(x):
    x = x * 3
    time.sleep(x * 60)


def main():
    threads = []
    for x in range(1, 3):
        t = Thread(target=do, args=(x,))
        t.start()
    for x in threads:
        x.join()


if __name__ == '__main__':
    faulthandler.register(signal.SIGUSR1)
    main()

运行程序,然后就可以通过向进程发送 USR1 信号来获取运行中进程的 traceback 信息了:

$ python test.py &
[1] 21924
$ kill -s USR1 21924
Thread 0x0000700001c03000 (most recent call first):
  File "test.py", line 9 in do
  File "/xxx/lib/python3.6/threading.py", line 864 in run
  File "/xxx/lib/python3.6/threading.py", line 916 in _bootstrap_inner
  File "/xxx/lib/python3.6/threading.py", line 884 in _bootstrap

Thread 0x0000700001700000 (most recent call first):
  File "test.py", line 9 in do
  File "/xxx/lib/python3.6/threading.py", line 864 in run
  File "/xxx/lib/python3.6/threading.py", line 916 in _bootstrap_inner
  File "/xxx/lib/python3.6/threading.py", line 884 in _bootstrap

Current thread 0x00007fffe03873c0 (most recent call first):
  File "/xxx/lib/python3.6/threading.py", line 1072 in _wait_for_tstate_lock
  File "/xxx/lib/python3.6/threading.py", line 1056 in join
  File "/xxx/lib/python3.6/threading.py", line 1290 in _shutdown

更多用法以及其他的信息请阅读 官方文档

有任何意见欢迎在下方留言或者加我微信交流


Comments

comments powered by Disqus