如何在 vscode 扩展中监听调试适配器协议事件?

How can I listen for debug adapter protocol events in a vscode extension?

我知道我可以使用 vscode.debug.activeDebugSession?.customRequest(command) 向调试适配器发送自定义请求。但是,如果我想监听诸如 Stopped Event?

之类的事件怎么办?

对于自定义事件,使用这个:

vscode.debug.onDidReceiveDebugSessionCustomEvent(event => {
    if(event.event == 'stopped') {
        // ...
    }
})

对于 vscode 拦截的事件,您可能需要检查 this answer

The solution for this is called a DebugAdapterTracker.

vscode.debug.registerDebugAdapterTrackerFactory('*', {
  createDebugAdapterTracker(session: DebugSession) {
    return {
      onWillReceiveMessage: m => console.log(`> ${JSON.stringify(m, undefined, 2)}`),
      onDidSendMessage: m => console.log(`< ${JSON.stringify(m, undefined, 2)}`)
    };
  }
});

https://code.visualstudio.com/updates/v1_30#_extension-authoring

Look for "Finalized Debug Adapter Tracker API". It was originally created for Live Share debugging.