1 Ramping up on debugpy
Adam Yoblick редактировал(а) эту страницу 2022-11-02 17:45:05 -05:00
Этот файл содержит неоднозначные символы Юникода!

Этот файл содержит неоднозначные символы Юникода, которые могут быть перепутаны с другими в текущей локали. Если это намеренно, можете спокойно проигнорировать это предупреждение. Используйте кнопку Экранировать, чтобы подсветить эти символы.

I dont think theres any kind of docs on tutorials on debuggers in general that would be particularly helpful here. In my experience, between working on ptvsd/debugpy, the mixed-mode debugger in VS, and on the R debugger in RTVS, it all tends to be very language- and runtime-specific, because they all have different hooks, and different tricks are necessary to implement the same features. So the only thing they really have in common is the debug adapter protocol used to talk to the client: https://microsoft.github.io/debug-adapter-protocol/specification

For Python specifically, I think its best to look at the relevant Python APIs first to get a general idea of how things work. That would be: https://docs.python.org/3/library/sys.html#sys.settrace https://docs.python.org/3/library/sys.html#sys._getframe The built-in “pdb” debugger in the Python standard library can be treated as a sample on how to use all this stuff; its a single-file module, and while its not small, its way smaller than pydevd.

Beyond that I find that I use the Python extensibility / C API docs a lot, mostly as a reference: https://docs.python.org/3/c-api/index.html

Its also important to understand how CPython works under the hood – stuff like frame and code objects. This isnt covered in much detail in the official docs, but there are some good blogs on the subject. Unfortunately, I cant find the series that I used to learn myself, but here are some newer ones that give a decent overview: https://blog.sourcerer.io/python-internals-an-introduction-d14f9f70e583 https://tenthousandmeters.com/tag/python-behind-the-scenes/

Beyond the blogs, the main reference for under-the-hood stuff is CPython source code itself: https://github.com/python/cpython Its C, not even C++, so it tends to be very verbose wrt error and resource management, but overall its easier to follow than most C codebases. The parts that are relevant most often are the aforementioned frame objects (PyFrameObject) and code objects (PyCodeObject), and the bytecode interpreter loop in ceval.c.

Now, with respect to debugpy specifically, the first thing to keep in mind is that its effectively two distinct parts: pydevd, which runs in-process relative to the debuggee, and provides core single-process debugging functionality (breakpoints, stepping, stack traces, variables/watch); and debugpy proper, which runs mostly out-of-process, and handles stuff like launching, output redirection, and process lifetime (debugpy.server is the in-proc part; it wraps pydevd APIs into our own, so that we have control over our API surface). Thus, all the info above about Python debugging APIs and its internals pertains to pydevd.

For debugpy proper, we dont have much developer documentation aside from comments in the code, although I tried to keep them extensive. Theres also a short doc and diagram that describes process management: https://github.com/microsoft/debugpy/blob/main/doc/Subprocess%20debugging.md and a lengthy doc that explains the framework used to write debugpy tests: https://github.com/microsoft/debugpy/blob/main/tests/timeline.md