javascriptvscode-extensionsvscode-debuggervscode-api

VS code API: How to get thread id on which debugger is paused in multithread program


I am currently building a VS code extension to visualize C# variables and datatables. And I have successfully achieved this in single threaded application by calling these in sequence:

  1. Thread request
  2. StackTrace request
  3. Scope request
  4. Variable request via DAP(Debug Adapter Protocol).

This is what i have done for single threaded C# application in javascript.

// Get Active Debug Session
const session = vscode.debug.activeDebugSession;

// Get Thread
const threadResponse = await session.customRequest('threads');
const threadId = threadResponse.threads[0].id;

// Get StackFrame
const stackResponse = await session.customRequest('stackTrace', { threadId: threadId, startFrame: 0 });
const frameId = stackResponse.stackFrames[0].id;

// Get Scope
const scopeResponse = await session.customRequest('scopes', { frameId: frameId });
const variableReference = scopeResponse.scopes[0].variablesReference;

// Get Variable
const variableResponse = await session.customRequest('variables', { variablesReference: variableReference });
const variables = variableResponse.variables;

Solution

  • You can check this out: https://code.visualstudio.com/updates/v1_90#_debug-stack-focus-api.

    The new activeStackItem interface can directly return the currently focused thread or stack frame:

    const activeStackItem = vscode.debug.activeStackItem;
    let frameId = undefined
    if (activeStackItem instanceof vscode.DebugStackFrame) {
        frameId = activeStackItem.frameId;
    }