Project Description

**DISCLAIMER: This is not an official google project, this is just something I wrote while at Google.**

Pyringe=======

What this is------------

Pyringe is a python debugger capable of attaching to running processes, inspecting their state and even of injecting python code into them while they're running. With pyringe, you can list threads, get tracebacks, inspect locals/globals/builtins of running functions, all without having to prepare your program for it.

Pyringe internally uses gdb to do a lot of its heavy lifting, so you will need a fairly recent build of gdb (version 7.4 onwards, and only if gdb was configured with `--with-python`). You will also need the symbols for whatever build of python you're running. On Fedora, the package you're looking for is `python-debuginfo`, on Debian it's called `python2.7-dbg` (adjust according to version). Arch Linux users: see [issue #5][]. Having [Colorama](https://pypi.python.org/pypi/colorama) will get you output in boldface, but it's optional.

[issue #5]: https://github.com/google/pyringe/issues/5

How do I get it?----------------

Get it from the [Github repo][], [PyPI][], or via pip (`pip install pyringe`).

Short answer: **No, sorry.** Long answer: There's three potentially different versions of python in play here: 1. The version running pyringe 2. The version being debugged 3. The version of `libpythonXX.so` your build of gdb was linked against

`2` Is currently the dealbreaker here. Cpython has changed a bit in the meantime[1], and making all features work while debugging python3 will have to take a back seat for now until the more glaring issues have been taken care of. As for `1` and `3`, the `2to3` tool may be able to handle it automatically. But then, as long as `2` hasn't been taken care of, this isn't really a use case in the first place.

[1] - For example, `pendingbusy` (which is used for injection) has been renamed to `busy` and been given a function-local scope, making it harder to interact with via gdb.

Will this work with PyPy?-------------------------

Unfortunately, no. Since this makes use of some CPython internals and implementation details, only CPython is supported. If you don't know what PyPy or CPython are, you'll probably be fine.

Why not PDB?------------

PDB is great. Use it where applicable! But sometimes it isn't. Like when python itself crashes, gets stuck in some C extension, or you want to inspect data without stopping a program. In such cases, PDB (and all other debuggers that run within the interpreter itself) are next to useless, and without pyringe you'd be left with having to debug using `print` statements. Pyringe is just quite convenient in these cases.

I injected a change to a local var into a function and it's not showing up!---------------------------------------------------------------------------

This is a known limitation. Things like `inject('var = 2')` won't work, but `inject('var[1] = 1337')` should. This is because most of the time, python internally uses a fast path for looking up local variables that doesn't actually perform the dictionary lookup in `locals()`. In general, code you inject into processes with pyringe is very different from a normal python function call.

How do I use it?----------------

You can start the debugger by executing `python -m pyringe`. Alternatively:

```pythonimport pyringepyringe.interact()```

If that reminds you of the code module, good; this is intentional. After starting the debugger, you'll be greeted by what behaves almost like a regular python REPL. Try the following:

The IDs you see here correspond to what `threading.current_thread().ident` would tell you. All debugger functions are just regular python functions that have been exposed to the REPL, so you can do things like the following.

There's limits to how far this proxying of objects goes, and everything that isn't trivial data will show up as strings (like `'<function at remote 0x1d957d0>'`). You can inject python code into running programs. Of course, there are caveats but... see for yourself: