Michael Hilus
Michael Hilus

Reputation: 1828

VS Code: How to access debug variables from within extension?

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.

Debug variables pane in visual studio code

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

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"
        }]
    }
}

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);
    });
}

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.

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

Upvotes: 6

Views: 6753

Answers (2)

Adrian
Adrian

Reputation: 1989

You have to talk to the debug adapter using the debug adapter protocol directly. You do it with vscode.debug.activeDebugSession.customRequest(command: string, args?: any) (Ref)

This function receives 2 parameters: command and args. Check out this resource to find all possible values of those parameters. One example is the 'evaluate' command that Michael Hilus uses in his answer:

enter image description here

If you want to get the variables in a multi threaded debug session, you must do these requests in this order

  1. Threads Request: Get the thread ids
  2. StackTrace Request: Get the frame ids
  3. Scopes Request: Get the variablesReference
  4. Variables Request: Finally, get the variable names with their values. If a variable is an object you might want to use Variables Request again with the variablesReference of the variable with an object value.

PS: It's kind of hard to find what you want in the DAP specification, so here is a tip:

  1. Go to the 'Types' section in the right menu and find what you want. For instance, Breakpoints.
  2. Ctrl+F and search ': Breakpoint'
  3. Take a look at all matches in the response section of each request.

In the case of variablesReference I had to search variablesReference: number to find it in the responses of Evaluate Request, Scope (type) and Variable (also type).

Upvotes: 4

Michael Hilus
Michael Hilus

Reputation: 1828

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.

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

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;

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;

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).

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

Upvotes: 2

Related Questions