spenceforce / pdb-attach

A python debugger that can attach to running processes.
BSD 3-Clause "New" or "Revised" License
33 stars 1 forks source link

Feature request (idea): An API for client side scripting #12

Closed balint-bg closed 3 years ago

balint-bg commented 3 years ago

I suspect this might go against the main design idea behind pdb_attach (no accident it has attach in its name :) ) but I was wondering if it's possible to debug a running Python module in a scripted fashion.

I'd like to have (1) trigger a script, so that in (2) I can run this script that would ask pdb_attach to e.g. talk to the running Python app, and get the stacktraces for threads, the frames, etc. Is it possible?

(to be more clear: I know I can manually attach to this hanging process via python -m pdb_attach <PID> 50000 but that provides an interactive debugger. I'd like a non-interactive way to get debug info. This can be a shell command, a python script, anything that can get info from the hung process and save/print this info for me)

spenceforce commented 3 years ago

That is a great idea and I'll need to think on it a bit. In the meantime you may be able to adapt the ideas in test/test_end_to_end.py to send commands via a script using the subprocess module instead of interactively.

balint-bg commented 3 years ago

Oh I see if you run pdb_attach in a Subprocess, and leave the stdin open with a PIPE then you will actually get the stdin of the interactive debug shell, so you can just pipe in python commands to the debugger shell, including detach. That's brilliant. Definitely will get me what I want. If you just break out that functionality from the end_to_end test and turn it into a function, then add a cmd line flag to the main executable and call that function if the flag is given (after flag validation etc etc) that might just work :) Thanks so much!!!

spenceforce commented 3 years ago

I'm glad that will get the job done for you in the meantime. I've been thinking about breaking the __main__.py script into an API, which would be easier to work with for your needs instead of having to pipe in commands to a subprocess.

spenceforce commented 3 years ago

I released a new version with some basic functionality for client side scripting. See pdb_attach/client.py to see if that covers your needs.