You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code扩展开发:如何通过cpp DAP获取变量内存地址?

问题:无法通过VS Code DAP获取C++变量内存地址

我正在开发一款VS Code扩展,尝试向vscode-cpptools的Debug Adapter Protocol(DAP)发送请求获取变量内存地址,但始终失败,报错提示无法在指定栈帧上执行表达式求值。

尝试的代码

for (const variable of variables) {
    // vscode.window.showInformationMessage(variable.name + " " + variable.value+ " "+dscope);
    panel.webview.postMessage({
        command: 'updatecontent',
        pid: "name",
        varattr: dscope,
        name: variable.name,
        value: variable.value,
        valueattr: variable.name + dscope,
        nextinstr: nextinstr,
        lineno: lineno,
    });
    //For pointer handling-------------------------------------------------------------

    if (activedebugsession) {
        // Your code to handle the active debug session
        const expression = "&" + variable;
        try {
            const result = await activedebugsession.customRequest('evaluate', { expression, frameId: 1, context: 'hover' });
            vscode.window.showInformationMessage(">>>>>>>>>>>>>>>" + JSON.stringify(result));
        } catch (err) {
            vscode.window.showInformationMessage("Error in evaluation: " + JSON.stringify(err));
        }
        // ...
    } else {
        vscode.window.showInformationMessage("No active debug session");
    }
}

错误信息

Error in evaluation: {"name":"CodeExpectedError","message":"Cannot evaluate expression on the specified stack frame."}

解决方案

  • 修正frameId获取逻辑:硬编码frameId:1不可靠,需要从当前调试会话的栈帧中获取有效ID。可先调用stackTrace请求获取当前栈帧列表,取第一个有效栈帧的ID。
  • 修复表达式构造错误:variable是变量对象,直接拼接会导致表达式无效,应该使用variable.name构造取地址表达式:&${variable.name}。
  • 确保会话处于暂停状态:只有调试会话在断点命中、暂停执行时,栈帧才有效,才能执行表达式求值。
  • 调整context参数:将context: 'hover'改为context: 'repl',更适合主动发起的求值请求,兼容性更好。

修改后的代码示例

for (const variable of variables) {
    panel.webview.postMessage({
        command: 'updatecontent',
        pid: "name",
        varattr: dscope,
        name: variable.name,
        value: variable.value,
        valueattr: variable.name + dscope,
        nextinstr: nextinstr,
        lineno: lineno,
    });

    if (activedebugsession) {
        try {
            // 获取当前有效栈帧ID
            const stackTraceResp = await activedebugsession.customRequest('stackTrace', { levels: 1 });
            if (!stackTraceResp.stackFrames?.length) {
                vscode.window.showInformationMessage("无有效栈帧");
                continue;
            }
            const frameId = stackTraceResp.stackFrames[0].id;
            // 构造正确的取地址表达式
            const expression = `&${variable.name}`;
            const result = await activedebugsession.customRequest('evaluate', { 
                expression, 
                frameId, 
                context: 'repl' 
            });
            vscode.window.showInformationMessage(JSON.stringify(result));
        } catch (err) {
            vscode.window.showInformationMessage("求值错误: " + JSON.stringify(err));
        }
    } else {
        vscode.window.showInformationMessage("无活跃调试会话");
    }
}

内容的提问来源于stack exchange,提问作者mvuma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:06:33