-
Notifications
You must be signed in to change notification settings - Fork 1k
python-ecosys/debugpy: Add VS Code debugging support for MicroPython. #1022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This implementation provides a Debug Adapter Protocol (DAP) server that enables VS Code to debug MicroPython code with full breakpoint, stepping, and variable inspection capabilities. Features: - Manual breakpoints via debugpy.breakpoint() - Line breakpoints set from VS Code - Stack trace inspection - Variable scopes (locals/globals) - Source code viewing - Stepping (into/over/out) - Non-blocking architecture for MicroPython's single-threaded environment - Conditional debug logging based on VS Code's logToFile setting Implementation highlights: - Uses MicroPython's sys.settrace() for execution monitoring - Handles path mapping between VS Code and MicroPython - Efficient O(n) fibonacci demo (was O(2^n) recursive) - Compatible with MicroPython's limited frame object attributes - Comprehensive DAP protocol support Files: - debugpy/: Core debugging implementation - test_vscode.py: VS Code integration test - VSCODE_TESTING_GUIDE.md: Setup and usage instructions - dap_monitor.py: Protocol debugging utility Usage: ```python import debugpy debugpy.listen() # Start debug server debugpy.debug_this_thread() # Enable tracing debugpy.breakpoint() # Manual breakpoint ``` 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
For those interested in AI coding, this was 95% written by Claude Code (Opus and Sonnet 4) as mentioned in the attributions above. I started prompting the build of this just this morning at 5am from my armchair with my infant asleep in my arms, using Termux on my phone to ssh into my linux box. I had a clone of micropython with my historical work on getting pdb checked out (micropython/micropython#8767 and #499) I also had a copy of the official cpython debugpy package checked out, this is the package used behind the scenes to drive python debugging in VSCode and similar IDE's. From past reviews I knew debugpy relies on threads, the old pydevd network debug engine as well a large RPC server which runs in a thread. I figured some of this could be re-implemented if needed, or replaced with other servers already running on micropython :-) So I started with a
Which produced DEBUGPY_ARCHITECTURE_ANALYSIS.md I then kicked off :
WIthin just 1 hour of armchair vibe coding I had an initial implementation ready to test, along with test scripts and a written plan. Around 10 am I was at my desk and had finished my morning meetings, so started testing it in the background while working on my other "real" projects. It tooks quite a few iterations of testing in vscode for Claude to finish its implementation plan, adding features as it ran test scripts with me hitting the vscode "debug" button in between. Most of these tests failed badly in many different ways, enough that I was quite pessimistic at times because it really looked like it wasn't going to work ..... however I was still able to get other solid work done though during this time (which coincidentally was also using Claude Code; I've had 4 sessions actively on the go today) so I gave Claude a few chances to get it all going after a number of wrong paths were backtracked. After all that though this screen capture was at 2:12 pm (and yes I ate lunch during that time too): It took me a while to realise while reviewing afterwards and cleaning up the git tree that it hasn't actually pulled in |
Thanks Andrew, |
"configurations": [ | ||
{ | ||
"name": "Attach to MicroPython", | ||
"type": "python", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"type": "debugpy",
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, yeah I fixed that in the examples file, missed it here
continue | ||
|
||
try: | ||
value_str = str(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be value_str = repr(value)
. without that strings show without quotes etc.
Thanks @Josverl good to hear it either for you, I still could hardly believe it worked for me! I'd be interested to hear any notes about what was confusing / difficult to get going to feed into docs. I assume some of it was getting paths right to import stuff? And/or compiling with the other features needed? Aka things that'll be better once finished and merged... I will do some testing on hardware too, ensure that does work and document how to get it going. I did think the branches were pretty well rebased up to date, I'll double check. Oh yeah I'll eventually look into getting a useful representation of locals too, even if they end up basically just showing the array of values without names as per the current internal representation. |
1. Build the MicroPython Unix coverage port: | ||
```bash | ||
cd ports/unix | ||
make CFLAGS_EXTRA="-DMICROPY_PY_SYS_SETTRACE=1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this flag MICROPY_PY_SYS_SETTRACE
conflicts with the referenced PR where this is already set unconditionally
I first got in a tangle by
blocked most attempts at building as Building a firmware with "the updates to settrace in micropython/micropython#8767" Matching up the paths vscode / remote paths, was not to difficult. Open questions/ more play time needed :
📚 likely relevant : What is the Debug Adapter Protocol? |
@andrewleech |
This implementation provides a Debug Adapter Protocol (DAP) server that enables VS Code to debug MicroPython code with full breakpoint, stepping, and variable inspection capabilities.
Features:
Implementation highlights:
Files:
Usage:
🤖 Generated with Claude Code
Currently only tested on unix port with updates to settrace in micropython/micropython#8767
Should work on any network enabled device however?