|
| 1 | +# TCP Debug Adapter Integration |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The debug adapter now supports TCP transport in addition to STDIN/STDOUT, enabling debugging of desktop GUI applications like the Avalonia Desktop app. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Library Structure |
| 10 | + |
| 11 | +- **Highbyte.DotNet6502.DebugAdapter** (library) |
| 12 | + - `IDebugAdapterTransport` - Interface for transport implementations |
| 13 | + - `StdioTransport` - STDIN/STDOUT implementation for console apps |
| 14 | + - `TcpTransport` - TCP socket implementation for network connections |
| 15 | + - `TcpDebugAdapterServer` - TCP listener that accepts debug connections |
| 16 | + - `DapProtocol` - Debug Adapter Protocol message handling |
| 17 | + - `DebugAdapterLogic` - Core DAP request/response logic |
| 18 | + - `Ca65DbgParser` - Debug symbol parser |
| 19 | + |
| 20 | +### Console Application |
| 21 | + |
| 22 | +- **Highbyte.DotNet6502.DebugAdapter.ConsoleApp** |
| 23 | + - Uses `StdioTransport` for VSCode extension integration |
| 24 | + - Launched by VSCode extension as a child process |
| 25 | + - Maintains backward compatibility with existing VSCode extension |
| 26 | + |
| 27 | +### Desktop Application Integration |
| 28 | + |
| 29 | +- **Highbyte.DotNet6502.App.Avalonia.Desktop** |
| 30 | + - Integrated `TcpDebugAdapterServer` for TCP-based debugging |
| 31 | + - Accepts `--debug-port <port>` command-line argument |
| 32 | + - Accepts `--debug-wait` flag to wait for debugger connection before starting |
| 33 | + - Runs debug adapter server on background thread |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +### Avalonia Desktop App with TCP Debug Adapter |
| 38 | + |
| 39 | +Start the Avalonia Desktop app with debug adapter enabled: |
| 40 | + |
| 41 | +```bash |
| 42 | +./Highbyte.DotNet6502.App.Avalonia.Desktop --debug-port 6502 |
| 43 | +``` |
| 44 | + |
| 45 | +To wait for the debugger to connect before starting: |
| 46 | + |
| 47 | +```bash |
| 48 | +./Highbyte.DotNet6502.App.Avalonia.Desktop --debug-port 6502 --debug-wait |
| 49 | +``` |
| 50 | + |
| 51 | +Combined with console logging: |
| 52 | + |
| 53 | +```bash |
| 54 | +./Highbyte.DotNet6502.App.Avalonia.Desktop --debug-port 6502 --console-log -l Debug |
| 55 | +``` |
| 56 | + |
| 57 | +### VSCode Configuration |
| 58 | + |
| 59 | +To debug the Avalonia Desktop app from VSCode, you'll need to add a launch configuration that connects to the TCP port: |
| 60 | + |
| 61 | +```json |
| 62 | +{ |
| 63 | + "type": "dotnet6502-debug", |
| 64 | + "request": "attach", |
| 65 | + "name": "Attach to Avalonia Desktop", |
| 66 | + "debugServer": 6502, |
| 67 | + "program": "${workspaceFolder}/samples/Assembler/GenericComputer/snake6502/build/snake6502.prg", |
| 68 | + "stopOnEntry": true, |
| 69 | + "trace": true |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +**Note:** The VSCode extension may need updates to support the `debugServer` configuration property for TCP connections. |
| 74 | + |
| 75 | +## Implementation Details |
| 76 | + |
| 77 | +### TCP Transport |
| 78 | + |
| 79 | +The `TcpTransport` class implements `IDebugAdapterTransport` using a `TcpClient` and `NetworkStream`: |
| 80 | + |
| 81 | +- Reads DAP messages with Content-Length headers |
| 82 | +- Writes DAP messages with proper framing |
| 83 | +- Fires `Disconnected` event when connection is closed |
| 84 | + |
| 85 | +### TCP Debug Adapter Server |
| 86 | + |
| 87 | +The `TcpDebugAdapterServer` class: |
| 88 | + |
| 89 | +- Listens on `IPAddress.Loopback` (localhost only) |
| 90 | +- Accepts a single client connection at a time |
| 91 | +- Fires `ClientConnected` event with a `TcpTransport` instance |
| 92 | +- Supports port 0 for random port assignment |
| 93 | + |
| 94 | +### Avalonia Desktop Integration |
| 95 | + |
| 96 | +The Avalonia Desktop app: |
| 97 | + |
| 98 | +1. Parses `--debug-port` and `--debug-wait` command-line arguments |
| 99 | +2. Creates a `TcpDebugAdapterServer` when debug port is specified |
| 100 | +3. Handles `ClientConnected` event by: |
| 101 | + - Creating `DapProtocol` and `DebugAdapterLogic` instances |
| 102 | + - Starting a message loop on a background thread |
| 103 | + - Logging to a separate file in the temp directory |
| 104 | +4. Optionally waits for debugger connection (30 second timeout) |
| 105 | +5. Continues with normal application startup |
| 106 | + |
| 107 | +### Debug Logging |
| 108 | + |
| 109 | +Debug adapter activity is logged to: |
| 110 | + |
| 111 | +``` |
| 112 | +$TMPDIR/dotnet6502-debugadapter-avalonia-{timestamp}.log |
| 113 | +``` |
| 114 | + |
| 115 | +This log file contains: |
| 116 | +- Connection events |
| 117 | +- DAP messages sent/received |
| 118 | +- Errors and exceptions |
| 119 | + |
| 120 | +## Future Enhancements |
| 121 | + |
| 122 | +1. **VSCode Extension Updates** |
| 123 | + - Add support for `debugServer` configuration property |
| 124 | + - Allow attaching to running desktop applications |
| 125 | + - Provide UI for discovering running instances |
| 126 | + |
| 127 | +2. **Multiple Client Support** |
| 128 | + - Allow multiple simultaneous debug connections |
| 129 | + - Share breakpoints and state across clients |
| 130 | + |
| 131 | +3. **Auto-Discovery** |
| 132 | + - Broadcast availability on local network |
| 133 | + - Allow VSCode to discover running instances |
| 134 | + |
| 135 | +4. **Security** |
| 136 | + - Add authentication token support |
| 137 | + - Support remote debugging with encryption |
| 138 | + |
| 139 | +## Testing |
| 140 | + |
| 141 | +### Manual Testing |
| 142 | + |
| 143 | +1. Start the Avalonia Desktop app with debug port: |
| 144 | + ```bash |
| 145 | + ./Highbyte.DotNet6502.App.Avalonia.Desktop --debug-port 6502 --debug-wait --console-log |
| 146 | + ``` |
| 147 | + |
| 148 | +2. Connect with a TCP client (e.g., `nc`): |
| 149 | + ```bash |
| 150 | + nc localhost 6502 |
| 151 | + ``` |
| 152 | + |
| 153 | +3. Send a DAP initialize request: |
| 154 | + ``` |
| 155 | + Content-Length: 122 |
| 156 | +
|
| 157 | + {"seq":1,"type":"request","command":"initialize","arguments":{"adapterID":"dotnet6502","pathFormat":"path"}} |
| 158 | + ``` |
| 159 | + |
| 160 | +4. Verify response is received |
| 161 | + |
| 162 | +### Automated Testing |
| 163 | + |
| 164 | +Future work: Add integration tests that: |
| 165 | +- Start the desktop app with debug port |
| 166 | +- Connect via TCP |
| 167 | +- Send DAP requests |
| 168 | +- Verify responses |
| 169 | +- Test breakpoint functionality |
0 commit comments