![]() 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> |
||
---|---|---|
.. | ||
debugpy | ||
README.md | ||
dap_monitor.py | ||
demo.py | ||
development_guide.md | ||
manifest.py | ||
test_vscode.py | ||
vscode_launch_example.json |
README.md
MicroPython debugpy
A minimal implementation of debugpy for MicroPython, enabling remote debugging such as VS Code debugging support.
Features
- Debug Adapter Protocol (DAP) support for VS Code integration
- Basic debugging operations:
- Breakpoints
- Step over/into/out
- Stack trace inspection
- Variable inspection (globals, locals generally not supported)
- Expression evaluation
- Pause/continue execution
Requirements
- MicroPython with
sys.settrace
support (enabled withMICROPY_PY_SYS_SETTRACE
) - Socket support for network communication
- JSON support for DAP message parsing
Usage
Basic Usage
import debugpy
# Start listening for debugger connections
host, port = debugpy.listen() # Default: 127.0.0.1:5678
print(f"Debugger listening on {host}:{port}")
# Enable debugging for current thread
debugpy.debug_this_thread()
# Your code here...
def my_function():
x = 10
y = 20
result = x + y # Set breakpoint here in VS Code
return result
result = my_function()
print(f"Result: {result}")
# Manual breakpoint
debugpy.breakpoint()
VS Code Configuration
Create a .vscode/launch.json
file in your project:
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to MicroPython",
"type": "python",
"request": "attach",
"connect": {
"host": "127.0.0.1",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": false
}
]
}
Testing
-
Build the MicroPython Unix coverage port:
cd ports/unix make CFLAGS_EXTRA="-DMICROPY_PY_SYS_SETTRACE=1"
-
Run the test script:
cd lib/micropython-lib/python-ecosys/debugpy ../../../../ports/unix/build-coverage/micropython test_debugpy.py
-
In VS Code, open the debugpy folder and press F5 to attach the debugger
-
Set breakpoints in the test script and observe debugging functionality
API Reference
debugpy.listen(port=5678, host="127.0.0.1")
Start listening for debugger connections.
Parameters:
port
: Port number to listen on (default: 5678)host
: Host address to bind to (default: "127.0.0.1")
Returns: Tuple of (host, port) actually used
debugpy.debug_this_thread()
Enable debugging for the current thread by installing the trace function.
debugpy.breakpoint()
Trigger a manual breakpoint that will pause execution if a debugger is attached.
debugpy.wait_for_client()
Wait for the debugger client to connect and initialize.
debugpy.is_client_connected()
Check if a debugger client is currently connected.
Returns: Boolean indicating connection status
debugpy.disconnect()
Disconnect from the debugger client and clean up resources.
Architecture
The implementation consists of several key components:
- Public API (
public_api.py
): Main entry points for users - Debug Session (
server/debug_session.py
): Handles DAP protocol communication - PDB Adapter (
server/pdb_adapter.py
): Bridges DAP and MicroPython's trace system - Messaging (
common/messaging.py
): JSON message handling for DAP - Constants (
common/constants.py
): DAP protocol constants
Limitations
This is a minimal implementation with the following limitations:
- Single-threaded debugging only
- No conditional breakpoints
- No function breakpoints
- Limited variable inspection (no nested object expansion)
- No step back functionality
- No hot code reloading
- Simplified stepping implementation
Compatibility
Tested with:
- MicroPython Unix port
- VS Code with Python/debugpy extension
- CPython 3.x (for comparison)
Contributing
This implementation provides a foundation for MicroPython debugging. Contributions are welcome to add:
- Conditional breakpoint support
- Better variable inspection
- Multi-threading support
- Performance optimizations
- Additional DAP features
License
MIT License - see the MicroPython project license for details.