跳转到主要内容

pystuck.py 是一个用于分析挂起的 Python 程序(或只是硬核调试)的工具。为了调试一个 Python 程序(因此,调试对象),在启动时在任何位置添加此行:import pystuck; pystuck.run_server()。这个脚本是客户端,一旦调用,它就会连接到调试对象并打印调试对象的线程堆栈跟踪(适用于大多数情况)。此外,它还打开了一个带有 rpyc 连接的 ipython 提示符,该连接提供对调试对象的模块的访问(适用于检查变量)。

项目描述

pystuck

pystuck.py 是一个用于分析挂起的 Python 程序(或只是硬核调试)的工具。

pystuck 目前有两个主要功能

  1. 打印所有运行线程的堆栈跟踪。

  2. 在不中断程序的情况下远程检查模块和变量。

在调试脚本中:import pystuck; pystuck.run_server()

调用客户端:从 shell 中调用 pystuck。

依赖项

  • IPython

  • rpyc(3.2.3,可能与其他版本兼容)

使用说明

pystuck 在没有客户端连接时不会消耗资源。

run_server 函数所做的一切就是启动一个线程,该线程在 accept 上阻塞(等待客户端连接),因此它可以用于生产环境。

在生产环境中使用 pystuck 有两个缺点

  1. 修改变量和模块不是线程安全的。

  2. 潜在的安全漏洞 - 没有任何东西可以阻止未授权的用户连接到受权的运行中的 Python 进程并使用远程访问做任何事情。

安装

pip install pystuck

示例

Python 在做什么?!

test.py 挂起了,你不想知道它在哪吗?

import pystuck; pystuck.run_server().

while True:
    with lock: # could block
         sock.recv(1024) # could block

从 shell 中运行 pystuck 显示有趣的内容

% pystuck
<_MainThread(MainThread, started -1215396160)>
  File "test.py", line 9, in <module>
    with lock: # could block

...

它挂起等待锁!实际上它打印了两个与 pystuck 相关的线程,忽略它们。

谁拥有了锁?!

# it seldom happens that a thread doesn't release the lock or is stuck while holding it.
# we want to know which thread... bear with me now.
rlock.acquire()

再次调用pystuck

% pystuck
<_MainThread(MainThread, started -1215396160)>
  File "test.py", line 9, in <module>
    with lock: # could block

<Thread(Thread-1, started -1219450000)>
  File "test.py", line 12, in <module>
    do_math()

<Thread(Thread-2, started -1219540000)>
  File "test.py", line 14, in <module>
    foo()
  File "test.py", line 20, in foo
    do_network()

use the 'modules' dictionary to access remote modules (like 'os', or '__main__')

In [0]: modules['sys']._current_frames() # gets a mapping between a thread to its frame (top of stack)
Out[0]: {-1215396160: <frame object at 0x8a07154>,
         -1219450000: <frame object at 0x8a29154>,
         -1219540000: <frame object at 0x8b39154>}

In [1]: _[-1215396160] # get the stuck thread's frame
Out[1]: <frame object at 0x8a07154>

In [2]: _.f_locals # local variables of the function
Out[2]: {'lock': <_RLock owner=-1219450000 count=1>}

In [3]: # our stuck thread is probably waiting for thread 1219450000 to finish do_math.. figures

Pyrc能为您做什么

Pyrc是一个用于在进程之间通信Python对象和过程调用的库。以下是您可以对已运行的服务器执行的某些操作。

% pystuck

(stacks appear here...)

In [1]: modules['sys'].stdout = file("/tmp/log.txt", "w") # tunnel the script's stdout to log

In [2]: modules['__main__'].global_var = 100 # change and inspect variables in the __main__ module (the name of the script when invoked like this: python script.py)

In [3]: socket = modules['socket'].socket() # create a socket object opened by the server script!

用法

usage: pystuck [-h] [--no-stacks] [--no-ipython] [host] [port]

pystuck.py is a utility for analyzing stuck python programs (or just hardcore debugging).

in order to debug a python program (hence, the debugee),
add this line anywhere at startup: import pystuck; pystuck.run_server().

this script is the client, once invoked it connects to the debuggee
and prints the debugee's threads stack traces (good for most cases).
in addition, it opens an ipython prompt with an rpyc connection that provides
access to the debuggee's modules (good for inspecting variables).
positional arguments:

  host          server address (default: 127.0.0.1)
  port          server port (default: 6666)

optional arguments:
  -h, --help    show this help message and exit
  --no-stacks   don't print the debugee's threads and stacks
  --no-ipython  don't open an ipython prompt for debugging

项目详情


下载文件

下载您平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

pystuck-0.8.5.tar.gz (5.6 kB 查看哈希值)

上传时间

由以下组织支持