.. image:: https://raw.githubusercontent.com/lmacken/pyrasite/develop/docs/logo.png
.. image:: https://github.com/lmacken/pyrasite/actions/workflows/ci.yml/badge.svg?branch=develop :target: https://github.com/lmacken/pyrasite/actions/workflows/ci.yml .. image:: https://img.shields.io/pypi/v/pyrasite?style=flat :target: https://pypi.org/project/pyrasite/
用于向正在运行的 Python 进程注入任意代码的工具。
:homepage: https://github.com/lmacken/pyrasite :documentation: https://pyrasite.readthedocs.io/ :download: https://pypi.org/project/pyrasite/ :source: https://github.com/lmacken/pyrasite :issues: https://github.com/lmacken/pyrasite/issues
环境要求
* `gdb <https://www.gnu.org/s/gdb>`_(版本 7.3+)
* 一个 ptrace 策略允许附加到目标进程的 Linux 系统。参见下方的 `ptrace_scope`。
安装
~~~~
::
pipx install pyrasite
或者,如需可选的内存查看器::
pip install "pyrasite[memory-viewer]"
兼容性
~~~~~~
Pyrasite 可在 CPython 3.9 至 3.15 上运行。注入也可跨版本工作,因此你可以在一个 Python 下运行 Pyrasite,并向运行另一个 Python 版本的进程注入。pyrasite 客户端和目标进程都必须为 CPython。
ptrace_scope
近期的 Linux 发行版限制了 ptrace,使得进程只能被其祖先进程附加。如果 /proc/sys/kernel/yama/ptrace_scope 非零,你会看到一条警告。你可以全局允许附加::
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
或者让目标进程通过调用 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) 主动选择加入。在 Fedora 上,SELinux 可能还需要::
sudo setsebool -P deny_ptrace=off
对于多线程程序,pyrasite 通过 Py_AddPendingCall 将载荷排队,使其在已持有 GIL 的线程上运行。如果目标阻塞在 C 代码中且从不执行 Python 字节码,请改用 --inject-method direct。
用法
::
pyrasite <pid> hello.py
pyrasite <pid> - <<'EOF'
import sys
print(sys.version)
EOF
pyrasite <pid> <(echo 'print("hello")')
载荷由 pyrasite 进程读取并嵌入到 gdb 命令中,因此目标进程无需对载荷文件具有读权限。这也意味着管道和进程替换均可正常工作。标准载荷可按名称选择(参见 ``pyrasite --list-payloads``)。
测试
::
pip install -e ".[dev]"
pytest
ruff check .
ruff format --check .
要使用 uv <https://docs.astral.sh/uv/>_ 针对每个受支持的解释器运行测试套件::
uv python install 3.9 3.10 3.11 3.12 3.13 3.14 3.15
tox
要测试从一个解释器向所有其他解释器注入,请在运行 pytest 前将 PYRASITE_TEST_INTERPRETERS 设置为以冒号分隔的解释器路径列表。
pyrasite-gui
图形界面单独维护于
https://github.com/lmacken/pyrasite-gui。
作者
~~~~
由 `Luke Macken <https://twitter.com/lmacken>`_ 创建,并得到 `David Malcolm <https://dmalcolm.livejournal.com>`_ 以及许多其他 `贡献者 <https://github.com/lmacken/pyrasite/contributors>`_ 的帮助。
Logo 由 `Adam Saunders <https://fedorahosted.org/design-team/ticket/214>`_ 设计。
许可证
~~~~~~
代码
^^^^
.. image:: https://www.gnu.org/graphics/gplv3-127x51.png
:target: https://www.gnu.org/licenses/gpl.txt
Logo
^^^^
.. image:: https://creativecommons.org/images/deed/nolaw.png
:target: https://creativecommons.org/publicdomain/zero/1.0/