要让vscode支持自定义语言调试,需实现符合DAP协议的调试适配器。1. DAP采用前后端分离架构,VSCode为客户端,调试适配器作为桥梁与实际调试后端通信;2. 适配器通过stdin/stdout或socket接收JSON-rpc格式消息,处理请求并返回响应或事件;3. 使用node.js继承DebugSession类可快速搭建最小适配器,重写initializeRequest和launchRequest等方法;4. 在package.json中注册调试器类型,并在extension.ts中通过registerDebugAdapterDescriptorFactory启动适配器进程;5. 实现setBreakpoints、Threads、stackTrace、variables等关键请求以支持断点、调用栈和变量查看;6. 程序状态变化时发送StoppedEvent等事件通知ui更新。从基础响应入手,逐步实现协议接口,即可构建完整调试功能。

要让 VSCode 支持自定义语言或运行时的调试功能,核心在于实现一个符合 VSCode 调试协议(Debug Adapter Protocol, DAP) 的调试适配器。DAP 是一种标准化通信协议,定义了编辑器(前端)与调试器(后端)之间的 JSON-RPC 消息格式。通过实现 DAP 适配器,你可以将任意调试引擎接入 VSCode,获得断点、变量查看、单步执行等完整调试体验。
理解 DAP 架构与通信机制
DAP 采用前后端分离设计:
- 调试客户端:VSCode 编辑器本身,负责 UI 展示(如调用栈、变量窗口)和用户交互
- 调试适配器:一个独立进程,作为桥梁翻译 VSCode 消息并控制实际调试后端
- 调试后端:真正的语言调试器(如 GDB、V8 Inspector、自定义解释器)
VSCode 通过 stdin/stdout 或 socket 与调试适配器进行 JSON-RPC 通信。每条消息遵循如下结构:
Content-Length: …rnrn{ “type”: “request”, “command”: “launch”, “arguments”: { … }, “seq”: 1 }
适配器需解析请求、处理逻辑,并返回 response 或发送 event(如输出日志、暂停通知)。
搭建最小可用调试适配器
使用 Node.js 实现一个基础适配器:
- 安装依赖:npm install vscode-debugadapter
- 继承 DebugSession 类,重写关键方法
示例代码片段:
const { DebugSession } = require(‘vscode-debugadapter’); class MyDebugAdapter extends DebugSession { constructor() { super(); } protected initializeRequest( response: DebugProtocol.InitializeResponse, args: DebugProtocol.InitializeRequestArguments ): void { response.body = { supportsConfigurationDoneRequest: true, supportsEvaluateForHovers: true }; this.sendResponse(response); } protected launchRequest( response: DebugProtocol.LaunchResponse, args: any ): void { // 启动目标程序逻辑 this.sendResponse(response); // 模拟程序暂停 this.sendEvent(new StoppedEvent(‘entry’, 1)); } } // 启动适配器 DebugSession.run(MyDebugAdapter);
编译后,该脚本可通过 node 直接运行,监听标准输入接收来自 VSCode 的指令。
注册调试器到 VSCode 扩展
在 package.json 中声明调试支持:
“contributes”: { “debuggers”: [ { “type”: “mylang”, “label”: “My Language Debugger”, “languages”: [“mylang”], “configurationAttributes”: { … }, “initialConfigurations”: [ … ] } ] }, “activationEvents”: [ “onDebugResolve:mylang” ]
并在 extension.ts 中激活适配器:
vscode.debug.registerDebugAdapterDescriptorFactory(‘mylang’, { createDebugAdapterDescriptor(session: DebugSession) { return new ExecutableDebugAdapter(‘node’, [‘out/debugAdapter.js’]); } });
当用户启动调试会话时,VSCode 会按配置启动你的适配器进程。
实现关键调试功能
为了让调试器真正可用,需处理以下核心请求:
- setBreakpoints:将用户设置的断点同步到底层引擎
- configurationDone:表示初始化完成,可开始执行
- threads:返回当前所有线程(即使单线程也要返回 dummy thread)
- stackTrace:查询调用栈,用于左侧调用栈面板
- scopes + variables:获取作用域及变量值,支撑 Variables 面板
- continue / next / stepIn:控制程序继续运行
每当程序状态变化(如命中断点),适配器应主动发送 StoppedEvent 或 ContinuedEvent 通知 VSCode 更新界面。
基本上就这些。DAP 协议虽细节繁多,但结构清晰。从响应 initialize 和 launch 开始,逐步实现断点、变量、控制流,即可构建出稳定可用的调试体验。关键是保持消息收发正确、状态同步及时。调试适配器本身不复杂,但需要耐心对接每一个协议接口。


