如何使用 DebugSession.customRequest 运行任意 gdb 命令并获取结果

blu*_*fox 3 typescript visual-studio-code

我正在使用扩展中的 VSCode 调试 API。我的目标是在调试会话期间从 gdb/lldb 调试器读取 C++ 程序的 CPU 寄存器。我对当前调试器不提供的其他事情(读取内存、反汇编等)也有类似的请求

我的扩展中有以下内容...如果需要,我可以提供完整的测试用例。是的,我知道我这里有一个错误,在第一次等待之后,会话可能不再处于活动状态,并且我没有显示完整的错误检查。

static async getRegisters() {
    const session = vscode.debug.activeDebugSession;
    if (session && text) {      // Make sure we still have a session
        // The following gets me the right result
        const sTrace = await session.customRequest('stackTrace', { threadId: 1 });
        const frameId = sTrace.stackFrames[0].id; 

        // The following does execute but the results are printed to debug console
        // rather than returning the result
        // I tried many variations of arguments and contexts types
        const text = '-exec -data-list-register-names';
        const arg : DebugProtocol.EvaluateArguments = {expression: text, frameId: frameId, context:'hover'};
        session.customRequest('evaluate', arg).then((response) => {
            console.log(response.result); 
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望 gdb 的结果以某种方式返回给我的扩展。结果被打印到调试控制台,但我得到一个空字符串。我还尝试为自定义命令设置事件处理程序vscode.debug.onDidReceiveDebugSessionCustomEvent,但没有任何触发。有没有更好的办法?

也许第一个参数customRequest不应该是'evaluate',但我在 DebugProtocol 规范中没有看到任何其他内容将自定义命令发送到实际的调试器。

小智 6

同样的问题!这是我的解决方案:

  1. 注册一个“DebugAdapterTracker”(相信你已经知道了);
  2. 在跟踪器的“onDidSendMessage(message:any)”中,请注意“event”消息,尤其是当“message.event ===“output””时;
  3. 检查“message.body.output”,惊喜!

DA 似乎没有提供正确值的响应,而是直接触发一个事件并在调试控制台上打印。