| 1 |
Remote Debugging |
| 2 |
================= |
| 3 |
|
| 4 |
PyPy has an experimental remote debugging facility built into the virtual |
| 5 |
machine. It can be used to inject arbitrary Python code into another PyPy |
| 6 |
process. So far, it works only under Linux. This mechanism is strongly inspired |
| 7 |
by `PEP 768`_. |
| 8 |
|
| 9 |
.. _`PEP 768`: https://peps.python.org/pep-0768/ |
| 10 |
|
| 11 |
From the commandline |
| 12 |
--------------------- |
| 13 |
|
| 14 |
To directly execute Python code in another PyPy process, you can run this command:: |
| 15 |
|
| 16 |
pypy -m _pypy_remote_debug <pid> -c <code> |
| 17 |
|
| 18 |
This will cause the other PyPy process with process id ``pid`` the to execute |
| 19 |
the ``code`` given. |
| 20 |
|
| 21 |
Alternatively you can pass a script file:: |
| 22 |
|
| 23 |
pypy -m _pypy_remote_debug <pid> <filename> |
| 24 |
|
| 25 |
If the other process is currently blocked by a system call or running a |
| 26 |
long-running computation in a C extension, it will not be interrupted. In this |
| 27 |
case the script will be executed once that is finished. |
| 28 |
|
| 29 |
For example, to cause the other process to dump its (RPython-level) heap, you |
| 30 |
can run:: |
| 31 |
|
| 32 |
pypy -m _pypy_remote_debug -c "import gc; gc.dump_rpy_heap('/tmp/rpy-heap-dump')" |
| 33 |
|
| 34 |
API |
| 35 |
--- |
| 36 |
|
| 37 |
To execute code remotely from Python code, there is the function |
| 38 |
``__pypy__.remote_exec(pid, filename, wait=True)``. |
| 39 |
|
| 40 |
Security |
| 41 |
--------- |
| 42 |
|
| 43 |
This feature is implemented by writing to the heap memory of another process. |
| 44 |
Under Linux, it uses the ``process_vm_writev`` function. This function is |
| 45 |
disabled by default on most hardened Linux distributions by Yama_. Therefore |
| 46 |
you might need sudo to run the above commands. |
| 47 |
|
| 48 |
The VM will raise an ``remote_exec`` audit event before starting the execution |
| 49 |
of remote code, with the to-be-executed code as a ``str`` as the argument. |
| 50 |
|
| 51 |
.. _Yama: https://www.kernel.org/doc/html/v4.15/admin-guide/LSM/Yama.html#ptrace-scope |
| 52 |
|
| 53 |
The feature can be disable by running PyPy with the ``-X disable-remote-debug`` |
| 54 |
option, or setting the ``PYTHON_DISABLE_REMOTE_DEBUG`` environment variable. |