Skip to content

Visual Studio Code, rebuilt in Rust

Rust
8.9 KB
Raw Blame

Debug

Debugging is the breakpoint and watch model, the sessions that speak the debug adapter protocol (DAP), and the Run and Debug views. This file goes one level under ARCHITECTURE.md, section "Extension host bridge", and under extension-host.md. Upstream is src/vs/workbench/contrib/debug and src/vs/workbench/api/browser/mainThreadDebugService.ts. The decisions per feature (hover, inline values, memory, extension host debugging) are in port/notes.md, "Milestone 16: debug and testing".

What is where

Data without a window, in app/src/debug.rs and its submodules:

  • app/src/debug.rs::DebugModel: breakpoints of every kind and the watch expressions, debugModel.ts DebugModel without the sessions. It also holds the storage round trip of debugStorage.ts (storage_entries, from_storage) and the breakpoint DTOs for the extension host (dtos).
  • app/src/debug/session.rs::Session: one session's threads, frames, capabilities and its DAP request bookkeeping (debugSession.ts and the request state of rawDebugSession.ts). The same file converts paths as debugUtils.ts does.
  • app/src/debug/configuration.rs: the debuggers extensions contribute, launch.json as Launch, and the ${command:...} and ${input:...} references of a configuration.
  • app/src/debug/disassembly.rs::Disassembly: the instruction list of the Disassembly view, merged by address.
  • app/src/debug/ansi.rs::parse: output text split into styled runs (debugANSIHandling.ts).

The service and the views, in app/src/workbench:

  • app/src/workbench/debug_service.rs::DebugService: the state of debugService.ts, debugViewModel.ts and debugAdapterManager.ts in one struct: the model, the sessions, the focus, the variables cache, the REPL. It is the field debug of app/src/workbench.rs::Workbench. The file's impl Workbench block is the protocol: start, stop, restart, events, breakpoints sent to sessions.
  • app/src/workbench/debug_view.rs: the Run and Debug views. debug_console.rs, debug_toolbar.rs, breakpoint_widget.rs, inline_breakpoints.rs, debug_hover.rs, debug_inline_values.rs, disassembly_view.rs, debug_memory.rs and debug_visualizers.rs beside it each name the upstream files they port in their //! header.
  • app/src/workbench/debug_commands.rs: the commands, and debug_context, the context keys of debug.ts.
  • app/src/workbench/debug_task_runner.rs and app/src/workbench/debug_dynamic.rs: preLaunchTask and postDebugTask, and dynamic configurations.

Who owns the adapter

The extension host owns every debug adapter. Inline, executable and server adapters all run behind ExtHostDebugService, as on VS Code desktop, and the app starts no adapter process. The app owns the session: it decides which DAP request goes out and when, and it holds all state.

The path of a message:

  • App to adapter: app/src/workbench/debug_service.rs::dap_request gives the request its sequence number (app/src/debug/session.rs::request), converts paths, and sends $sendDAMessage with the session's handle through app/src/host_sync.rs::HostSync. It returns a receiver for the response.
  • Adapter to app: the host calls MainThreadDebugService.$acceptDAMessage. app/src/main_thread_commands.rs::route forwards the whole proxy (path 3 of extension-host.md), and app/src/workbench/debug_service.rs::handle_debug_request hands the message to accept_adapter_message. A response resolves its pending request, an event goes to accept_adapter_event, a reverse request (runInTerminal, startDebugging, launchVSCode) to accept_reverse_request.
  • The adapter ends: $acceptDAExit or $acceptDAError reach adapter_exited, and app/src/debug/session.rs::cancel_pending fails every request still waiting.

The host addresses a session by handle, the workbench by id. app/src/workbench/debug_service.rs::session_by_handle is the one lookup from a handle to its session, and it finds only running sessions.

How state reaches the views

There is no event emitter. The views are impl Workbench methods that read self.debug in render, and whoever changes debug state calls cx.notify(). Three functions bundle the follow-up work, use them instead of a bare field write:

  • app/src/workbench/debug_service.rs::breakpoints_changed: stores the model, sends $acceptBreakpointsDelta to the host, sends the breakpoints to every session again when send_to_sessions is set, and refreshes the Breakpoints hint. A change marked session_only goes to neither. The DebugModel methods that add, remove, update or enable breakpoints return a app/src/debug.rs::BreakpointsChange for it; the three that set exception breakpoints per session do not.
  • app/src/workbench/debug_service.rs::set_debug_focus: the way to move the focus to a session, thread or frame. It tells the host and reveals the frame. Three functions write focus without it, to drop the frame of a resumed thread or to leave an ended session: threads_continued, session_ended and app/src/workbench/debug_commands.rs::resume_thread.
  • app/src/workbench/debug_service.rs::update_debug_views: fetches the shown variable rows and watch values again, each keeping its rows until the reply.

A flow that waits on the adapter (a start, a restart, a lazy variable) is an async function that takes a WeakEntity<Workbench> and re-enters through app/src/workbench/debug_service.rs::on_workbench for each step. It holds no borrow of the workbench across an await.

Invariants

  • Message order at session start. launch_or_attach calls $startDASession so the host creates the adapter, then sends initialize, then launch or attach. An initialized event that arrives with the initialize response sets initialized_event_pending and is handled only after the launch request went out, because js-debug rejects setBreakpoints before launch. session_initialized then sends all breakpoints, waits for their answers, sends configurationDone if the adapter supports it, and fetches the threads.
  • One sequence counter per session. Requests and the responses to reverse requests both take the next number (app/src/debug/session.rs::number).
  • Paths convert at one boundary. Toward the adapter to_adapter_paths turns source paths into UriComponents; from it from_adapter_paths turns them back. Inside the app a source is a path.
  • Lines. A breakpoint's line is 1-based in the model and in DAP, and 0-based in the DTO the host gets (debug::tests::dtos_carry_zero_based_lines).
  • Cancellation tokens. A host method whose last parameter is a CancellationToken gets its token when the call goes out through app/src/host_sync.rs::call_cancellable, or through call with an entry in app/src/main_thread.rs::TOKEN_METHODS. Without either the extension gets undefined as its token.

Traps

  • A handler in handle_debug_request that needs the adapter's answer returns Outcome::Pending with the receiver. Do not wait in the handler: it runs on the main thread.
  • app/src/workbench/debug_memory.rs serves the vscode-debug-memory file system from the workbench while the session thread waits, and its answer needs a readMemory round trip to the host. This works only because exthost/src/session.rs::ask_workbench keeps sending the workbench's queued calls while it waits. port/notes.md ("View Binary Data") has the case.
  • sessions holds running sessions, inactive_sessions the ended ones upstream keeps as State.Inactive. app/src/workbench/debug_service.rs::session finds only running ones; use sessions_including_inactive for a view that lists both.
  • app/src/workbench/extension_host_restart.rs::stop_extension_host clears the host's commands, language features, SCM model, webview views and markers. It does not clear providers or adapter_types, and no other code in native/crates clears them on a restart: the new host's $registerDebugTypes replaces adapter_types, and its $registerDebugConfigurationProvider calls add to the providers of the old host. A running session vetoes a restart (stop_extension_hosts), and the user can answer Restart Anyway.
  • An extensionHost session needs a V8 inspector, so the host it debugs runs on Node, not Bun (port/notes.md, "Extension host debugging").

Check a change

  • port/tools/cargo-test -p app debug:: runs the model, storage, session and path tests (upstream's breakpoints.test.ts and debugUtils.test.ts cases).
  • port/tools/cargo-test -p app debug_service and port/tools/cargo-test -p app debug_view run the service and view tests, most of them in a test window.
  • MOCHA_GREP="vscode API - debug" port/tools/api-tests runs upstream's API tests for MainThreadDebugService.
  • port/tools/studio --lane 'FOLDER=port/fixtures/debug-node FILE=app.js port/tools/ui-pair /tmp/out port/fixtures/ui-pair/m16/breakpoint.json' compares a real session against the reference app: the script sets a breakpoint on line 5 of the open file and runs Launch app.js. port/fixtures/ui-pair/debug holds the scripts of more cases, one per file.