Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DebugSession.customRequest to run an arbitrary gdb command and get results

I am using VSCode debug APIs from an extension. My goal is to read CPU registers for a C++ program from a gdb/lldb debugger during a debug session. I also have similar requests for other things (reading memory, disassembly, etc.) that current debuggers don't provide

I have the following in my extension...I can provide a full test case if needed. Yes, I know I have a bug here in that after the first await, the session may not be active anymore and I don't have full error checking shown.

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

I want the results of gdb somehow provided back to my extension. The results are being printed to the Debug Console instead and I get an empty string. I have also tried setting up an event handler vscode.debug.onDidReceiveDebugSessionCustomEvent for custom commands but nothing fires. Is there a better way?

Maybe the first argument to customRequest should not be 'evaluate' but I don't see anything else in the DebugProtocol spec. to send a custom command to the actual debugger.

like image 608
bluefox Avatar asked Nov 21 '25 11:11

bluefox


1 Answers

Same issue! Here is my solution:

  1. Register a "DebugAdapterTracker" (I believe you already know about it);
  2. In "onDidSendMessage(message: any)" of the tracker, be aware of the "event" messages, especially when "message.event === "output"";
  3. Check the "message.body.output", surprise!

It seems instead of a response with proper values, the DA directly triggers an event to print on the debug console.

like image 93
shiheng Avatar answered Nov 23 '25 00:11

shiheng