![]() When breakpoints are hit, VS Code was opening read-only copies of source files instead of the original workspace files due to path mismatches between VS Code's absolute paths and MicroPython's runtime paths. Changes: - Add path mapping dictionary to track VS Code path <-> runtime path relationships - Enhance breakpoint matching to handle relative paths and basename matches - Update stack trace reporting to use mapped VS Code paths - Add debug logging for path mapping diagnostics - Fix VS Code launch configuration (debugpy -> python, enable logging) This ensures VS Code correctly opens the original editable source files when debugging, rather than creating read-only temporary copies. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au> |
||
---|---|---|
.. | ||
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.