Crusader_Decomp/docs/debugger-mcp-integration-plan.md
Marco 1225a3e737 Refactor and enhance documentation and tooling for Crusader USECODE
- Updated retail-debugger-entry-options.md to clarify the status of the debugger in No Remorse and No Regret, including findings from recent investigations.
- Revised usecode-format-specification.md to improve clarity on opcode behavior and historical naming conventions, with added references to Japanese executable analysis.
- Introduced usecode-jp-dispatch-analysis.md to document findings from the Japanese No Remorse executable, detailing opcode dispatch and search object behavior.
- Added ghidra_to_dosbox_memory.py, a new tool for translating Ghidra segment:offset addresses to DOSBox physical memory offsets, including comprehensive address validation.
- Created test_ghidra_to_dosbox_memory.py to ensure the correctness of address translation logic and selector base loading, with unit tests covering various scenarios.
2026-10-08 17:40:59 +02:00

9 KiB

Remote Debugger Integration Plan

Summary

OPT_DEBUGGER=ON builds DOSBox Automation's native interactive debugger, but it does not make debugger controls available over HTTP or MCP. The current source has no debugger REST routes, advertises features.debugger as false, and the MCP debugger tools are stubs.

This plan covers the work to expose a controlled debugger API and connect it to dosbox-mcp. It separates the REST/API work needed by MCP from an optional browser-facing debugger panel.

Current state

The findings below refer to DOSBox Automation 0.85.1 and dosbox-mcp 0.1.2 in the local source trees.

Area Current state
Native debugger build OPT_DEBUGGER defaults to OFF. When enabled, CMake defines C_DEBUGGER, builds src/debugger, and names the executable dosbox-automation-debugger. OPT_HEAVY_DEBUGGER also enables the debugger and adds heavier instrumentation.
Engine REST API src/webserver/webserver.cpp registers no debugger routes. /api/v1/dosbox/info hard-codes features.debugger to false.
Engine execution loop normal_loop pumps Webserver::Bridge::ProcessRequests(). DEBUG_Loop does not. A remote continue or status request could therefore time out while execution is in the debugger loop unless that loop also services API commands.
Native control surface src/debugger/debugger.h exposes debugger entry/status and breakpoint-check functions, but no REST-oriented continue or single-step interface. The native entry point opens the debugger UI and switches DOSBox to DEBUG_Loop.
MCP registration server.py associates the debug group with the debugger feature. The connection guard rejects calls while the feature is false. Mutating debug tools also require mode = "full".
MCP handlers dosbox_mcp/tools/debug.py defines four tools, but each handler returns a fixed not-available message instead of making an HTTP call. debug_status mentions breakpoints, but there are no MCP tools to add or remove them.

Relevant source locations:

  • DOSBox Automation: CMakeLists.txt, src/debugger/debugger.h, src/debugger/debugger.cpp, src/dosbox.cpp, src/webserver/bridge.h, src/webserver/bridge.cpp, and src/webserver/webserver.cpp.
  • dosbox-mcp: dosbox_mcp/server.py, dosbox_mcp/connection.py, dosbox_mcp/tools/debug.py, and tests/test_server.py.

Proposed scope

Required: REST API and MCP controls

  1. Define the remote control contract. Specify status fields and behavior for pause, continue, and single-step. Decide whether status includes a breakpoint list. Keep the operations bounded and return explicit errors for unsupported builds or invalid state transitions.
  2. Add a debugger control layer in DOSBox Automation. Provide emulation-thread-safe operations for querying state, entering a paused state, continuing, and executing exactly one instruction before pausing again. Do not call GUI-only debugger functions directly from the HTTP worker thread.
  3. Keep API commands alive while paused. Make the debugger loop service the existing webserver command queue, or introduce an equivalent safe dispatch path. Cover timeouts, shutdown, and repeated pause/continue requests.
  4. Add REST handlers and capability reporting. Register authenticated status, pause, continue, and step routes. Advertise features.debugger = true only when the compiled binary has the debugger control API, not merely because the native UI was compiled. Keep it false in builds without that support.
  5. Implement the MCP handlers. Replace the fixed responses in dosbox_mcp/tools/debug.py with calls through Connection.get/post. Define tool schemas and map API errors into useful results. Keep state inspection read-only and keep pause/continue/step behind the existing full capability mode.
  6. Test both capability states. Verify that non-debugger builds fail closed, debugger builds expose working routes, and MCP calls actually reach those routes.

Suggested initial route shape, subject to contract review:

Method and route Purpose
GET /api/v1/debugger/status Report enabled/paused state and current instruction location; include breakpoints only if list support is implemented.
POST /api/v1/debugger/pause Request a pause at a defined instruction boundary.
POST /api/v1/debugger/continue Resume from a debugger pause.
POST /api/v1/debugger/step Execute one instruction and return to the paused state.

The engine should expose these routes in its OpenAPI schema and local API explorer. Preserve bearer-token authentication and loopback defaults.

Browser-facing debugger panel

The REST API is the integration surface MCP needs. A browser panel is a separate client and can be delivered after the API works. If "web interface" means a human-operated page in addition to the Swagger/API explorer, add a page that shows paused/running state and current instruction, and provides pause, continue, and step controls. Reuse the REST routes rather than creating browser-only engine behavior. Add breakpoint display or editing only if that scope is approved.

Breakpoint management

The native debugger has internal breakpoint machinery, but the current MCP surface does not offer breakpoint management. Choose one of these before implementation:

  • Keep the first release to pause, continue, step, and status; remove the promise of a breakpoint list from the debug_status description unless the API can supply it.
  • Add list/add/remove breakpoint endpoints and corresponding MCP tools. Validate address forms and breakpoint types, and test normal and heavy-debugger breakpoint variants.

OPT_HEAVY_DEBUGGER=ON should be optional for the first release. Enable it only if the planned features need its extra instrumentation, such as memory access breakpoints.

Build and verification

The Windows preset documented by the repository can build the native debugger variant:

cmake --preset debug-windows-vs2022 -DOPT_DEBUGGER=ON
cmake --build --preset debug-windows-vs2022

This validates the native debugger build only. It does not implement the REST API or MCP handlers. Use -DOPT_HEAVY_DEBUGGER=ON only when the heavier instrumentation is required. Point a separate test launcher at the resulting dosbox-automation-debugger binary; do not replace the installed game binary as part of initial validation.

Work estimates

Estimates are focused engineer-days for someone familiar with both codebases, using the existing CMake preset and test frameworks. They exclude environment setup, review latency, release packaging, and unexpected native debugger defects.

Work item Deliverable Estimate
Contract and behavior decisions Route schemas, pause/step semantics, disabled-build behavior 0.5-1 day
Engine control layer Safe debugger state, pause, continue, and single-step operations 2-4 days
Engine REST integration Routes, capability reporting, command-queue servicing while paused, OpenAPI output 1.5-3 days
MCP implementation Real handlers, schemas, error handling, feature and mode behavior 1-2 days
Engine and MCP tests Unit/API tests for both build states and tool calls 1.5-3 days
End-to-end validation and docs Build, live pause/step/continue test, API and MCP docs 1-2 days
Required API + MCP total Items above 7.5-15 days
Browser debugger panel Status, instruction display, and controls using the REST API +2-4 days
Breakpoint add/remove support Engine API, MCP tools, validation, and tests +2-4 days

A first release with both the API/MCP path and a basic browser panel is roughly 9.5-19 engineer-days. Adding breakpoint editing brings the range to about 11.5-23 engineer-days. These are planning ranges, not a commitment; the pause/step control layer and servicing requests during debugger execution are the main uncertainties.

Acceptance criteria

  • A build without the debugger API advertises debugger: false; MCP calls fail with a clear unsupported-feature result.
  • A debugger-enabled build advertises the feature only when its debugger routes are registered and operational.
  • Pause reaches a stable instruction boundary; status reports paused state and a useful instruction location.
  • Continue resumes execution, and step advances one instruction then pauses again.
  • API and MCP requests continue to complete while the emulator is paused; no remote control call hangs waiting for the normal loop.
  • Non-mutating status remains available in read-only mode; pause, continue, and step require full mode.
  • All API requests retain bearer-token authentication and existing loopback security defaults.
  • Tests cover disabled and enabled builds, invalid transitions, timeout/shutdown behavior, and successful MCP-to-engine calls.

Documentation follow-up

When the routes or user-visible controls are implemented, update the DOSBox Automation REST API reference and generated OpenAPI surface, the dosbox-mcp tool reference, and any browser-panel help. Keep the capability meaning explicit: it reports usable remote debugger support, not just the presence of the native debugger UI.