5 ms·
Would be nice to know how this actually works; docs dont say much about how the tool injects the code into the running process.
by tbarbugli 12y ago
Would be nice to know how this actually works; docs dont say much about how the tool injects the code into the running process.
- matthewmacleod 12y agoIt basically uses GDB to call eval inside a running Python VM. Pretty simple, but quite useful from time to time.
- BozeWolf 12y agoBut how does it get into that running Python VM?
- coolj 12y agoGDB uses a system call(s) to attach to the running process[1], and python exposes an API that lets you evaluate code in the current VM context[2]. Specifically, https://github.com/lmacken/pyrasite/blob/develop/pyrasite/injector.py#L22-42 https://github.com/lmacken/pyrasite/blob/develop/pyrasite/in... [1] http://aosabook.org/en/gdb.html http://aosabook.org/en/gdb.html [2] https://docs.python.org/2/c-api/veryhigh.html https://docs.python.org/2/c-api/veryhigh.html
- BozeWolf 12y agoExcellent answer to my question. Thanks!
- klibertp 12y agoJust read this file: https://github.com/lmacken/pyrasite/blob/develop/pyrasite/injector.py https://github.com/lmacken/pyrasite/blob/develop/pyrasite/in... it's rather straightforward.
- ionelm 12y agoIt's basically this: attach with GDB, get GIL, run commands (your session), release GIL. This is pretty dangerous as the GIL release is not guaranteed. This is why I wrote https://pypi.python.org/pypi/manhole https://pypi.python.org/pypi/manhole - I couldn't afford hosing up (deadlocking) my processes, and it happen pretty often with pyrasite in my usecases. Have in mind that manhole is not that flexible as it uses different technique.