VS Code:如何从扩展中访问调试变量? [英] VS Code: How to access debug variables from within extension?

查看:28
本文介绍了VS Code:如何从扩展中访问调试变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Visual Studio 代码编写扩展,我想在其中评估 javascript 调试会话的当前变量.这些变量通常在打开 VARIABLES 部分下的调试窗格时显示.请参阅随附的屏幕截图.

I am writing an extension for visual studio code where I want to evaluate the current variables of a javascript debug session. These variables are normally shown when one have open the debug pane under the section VARIABLES. See the attached screenshot.

我想在用户右键单击编辑器时访问这些变量,但我不知道如何.

I want to get access to these variables when the user right clicks the editor, but I don't know how.

我当前的扩展设置是这样的:在 package.json 中,我已经注册了一个菜单贡献和一个命令:

My current extension setting for this is like that: in the package.json I have registered a menu contribution along with a command:

"contributes": {
    "menus": {
        "editor/context": [{
            "command": "extension.showVariables",
            "group": "navigation"
        }]
    }
}

在我的 extension.ts 中,我注册了这样的命令:

In my extension.ts I register the command like that:

export function activate(context: vscode.ExtensionContext) {

    let disposable = vscode.commands.registerCommand('extension.showVariables', () => {

        // TODO: let variables = vscode.debug.activeDebugSession.variables.toString();

        vscode.window.showInformationMessage(variables);
    });
}

我试图通过 vscode.debug.activeDebugSession 获取它们,但是这里没有变量的 API.我还尝试为 vscode.debug.onDidReceiveDebugSessionCustomEvent 注册一个事件处理程序,但我不知道在哪里搜索调试变量.

I have tried to get them through vscode.debug.activeDebugSession but there is no API for variables here. I also tried to register an event handler for vscode.debug.onDidReceiveDebugSessionCustomEvent but I can't figure out where to search for the debug variables.

甚至可以在 vs 扩展中访问这些变量还是我需要实现自己的调试器?

Is it even possible to access these variables in an vs extension or do I need to implement my own debugger?

推荐答案

我已经设法访问局部变量,尽管这不是通用的解决方案 - 它可能只能在单线程调试器中工作.如果你知道更好的方法,请回答或评论.

I have managed to get access to the local variables although this is not a general solution - it may only work in a single threaded debugger. If you know any better way, please answer or comment.

比如说,调试器中断了一个具有局部变量 car 的方法.

Say, the debugger breaks in a method that has a local variable car.

为了获取 car 的值,我在活动调试会话中使用了 customRequest 方法:

To get the value of car, I am using the customRequest method on the active debug session:

const session = vscode.debug.activeDebugSession;
const response = await session.customRequest('evaluate', { expression: 'car', frameId: frameId });
const car = response.result;

为了获得 frameId,我使用了另一个 customRequest 调用:

To get the frameId, I use another call of customRequest:

const session = vscode.debug.activeDebugSession;
const response = await session.customRequest('stackTrace', { threadId: 1 })
const frameId = response.stackFrames[0].id;

<小时>

为了在我的扩展中获得一个真正的汽车对象(不是字符串表示),我将 "JSON.stringify(car)" 作为表达式在 evaluate customRequest 中传递.然后,我可以使用 JSON.parse(response.result).


To get a real car object (not a string representation) in my extension, I pass "JSON.stringify(car)" as expression in the evaluate customRequest. Then, I can use JSON.parse(response.result).

要获取所有作用域、堆栈和变量,请查看调试会话 APIDebugProtocol.

To get all scopes, stacks and variables, have a look at the Debug Session API and the specification of the DebugProtocol.

这篇关于VS Code:如何从扩展中访问调试变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆