GDB 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/in...[1] http://aosabook.org/en/gdb.html
[2] https://docs.python.org/2/c-api/veryhigh.html