I am putting together a small debugger for python so I can basically attach in the middle of the program and pause the code or break when some variable changes or step one line, etc… basically pdb but with attaching (so more like gdb assuming pystack isn’t working).
For a single thread, one can do sys.settrace(), for a multithreaded program, assuming no threads have started, one can do threading.settrace(), but the problem is that you are stuck debugging the whole thing (or doing it at a specific point in a specific thread) as far as I know.
I was wondering if there is a way to do something like sys.settrace() in python for a particular different thread that has already started?
ok, it doesn’t seem quite possible to do this.
The closest I got was hearing about thread2 (killable threads), which calls
PyThreadState_SetAsyncExc, which raises an exception, and the constructor is called in the other thread, which is rather useless.My solution is going to not use sys.settrace at all, but instead just use
sys.setcheckintervalto set it to1for when I leave my debugging thread, and back to some huge number (sys.maxintmaybe) when I enter the debug thread (along with grabbing the GIL). Along with looking atsys._current_frames().This is nearly as good as sys.settrace, maybe slower, but can be run against any thread at any time for any duration.
edit:
First – I’ve found out that doing setcheckinterval is insufficient for stopping other threads, though it can be done for particular python versions to some extent by setting the global _Py_Ticker (in ceval.c and not a static global, so you can use ctypes) and making sure you don’t release the GIL in your code.
w.r.t. the original problem, I looked thru the c code in CPython and found a way to traverse the thread state objects and adjust them to include a tracefunc and traceobj; you can see how I do it in my debugger here — https://github.com/bobfrank/pydebug/blob/master/debug.py#L291