node.js - VSC 不会在正确的断点处中断

标签 node.js visual-studio-code javascript-debugger

我正在尝试在我自己的调试器之上实现 NodeJS 调试 API 服务器。我想使用任何能够调试 Node 的客户端来通过我自己的调试器进行调试。我使用 Visual Studio Code 作为测试调试客户端,并且能够实现足够的消息交换来附加、设置断点并通知 VSC 脚本在断点处暂停。问题是,Visual Studio Code 将“暂停”图标更改为“播放”以指示执行已暂停,但它似乎不了解代码停止在哪个断点上。没有突出显示断点。并且它不会从服务器请求帧或堆栈跟踪。在上次 VSC 代码更新之前,它还会在堆栈跟踪窗口顶部显示执行已暂停的消息,在上次更新之后,该消息不会显示,但执行仍暂停。

这是服务器和VSC之间的消息交换日志(从断点请求开始):

Request:
 {
    "command": "setbreakpoint",
    "arguments": {
        "line": 4,
        "column": 0,
        "type": "scriptRegExp",
        "target": "^(.*[\\/\\\\])?\\/Users\\/me\\/Documents\\/github\\/test\\/test\\.js$"
    },
    "type": "request",
    "seq": 8
}
Response:
{
    "seq": 9,
    "type": "response",
    "success": true,
    "running": true,
    "request_seq": 8,
    "command": "setbreakpoint",
    "body": {
        "type": "scriptRegExp",
        "breakpoint": 1,
        "script_regexp": "^(.*[\\/\\\\])?\\/Users\\/me\\/Documents\\/github\\/test\\/test\\.js$",
        "line": 4,
        "column": 0,
        "actual_locations": [
            {
                "line": 4,
                "column": 4,
                "script_id": 42
            }
        ]
    }
}
Response:
 {
    "type": "event",
    "event": "break",
    "body": {
        "sourceLine": 4,
        "sourceColumn": 4,
        "sourceLineText": "    var product = Product.get(params.pid.stringValue);",
        "breakpoints": [
            1
        ],
        "script": {
            "id": 42,
            "name": "/Users/me/Documents/github/test/test.js",
            "lineOffset": 0,
            "columnOffset": 0,
            "lineCount": 458
        }
    }
}
Request:
{
    "command": "threads",
    "type": "request",
    "seq": 10
}
Response:
{
    "seq": 11,
    "type": "response",
    "success": true,
    "running": false,
    "request_seq": 10,
    "command": "threads",
    "body": {
        "totalThreads": 1,
        "threads": [
            {
                "current": true,
                "id": 4
            }
        ]
    }
}

此后,VSC 不再发送任何请求。我做错了什么吗?也许有人可以向我指出一些用于验证 VSC Node 调试的单元测试的位置?

此外,尽管 Node 响应了线程请求,但 NodeJS 调试器 API 规范中似乎并未包含线程请求。这是新功能,还是只是未记录的功能?

最佳答案

以下部分来自 https://code.visualstudio.com/docs/extensionAPI/api-debugging解释了命中断点时会发生什么:

“每当程序停止时(在程序入口处,因为遇到断点、发生异常或用户请求暂停执行),调试适配器必须发送具有适当原因和线程 ID 的停止事件。收到后,VS Code 将请求给定线程的堆栈跟踪(堆栈帧列表)。如果用户随后深入堆栈帧,VS Code 首先请求堆栈帧的范围,然后请求范围的变量。如果变量本身是结构化的,VS Code 会请求其属性通过附加变量请求。这导致以下层次结构:...“

调试适配器测试位于:https://github.com/Microsoft/vscode-node-debug/blob/master/src/tests/adapter.test.ts 他们测试典型的 VS Code 场景。您可能想研究一下“应该在断点处停止”测试(第 108 行)。

为了节省空间,“停止原因”现在显示在 CALL STACK View 的标题中:

enter image description here

关于node.js - VSC 不会在正确的断点处中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616514/

相关文章:

javascript - Node js excel 导出 :Download file without creating a file

javascript - Cognito - adminRespondToAuthChallenge 返回用户的无效 session

node.js - 使用表单将 3 个相同的内容传递给 ExpressJS

testing - vscode-dotnet-test-explorer 调试测试 "Waiting for Process to Attach"

javascript - 如何在事件上设置断点?

node.js - MongoDB聚合和项目结果总数

visual-studio-code - VS Code 是否应该覆盖导航标题中的现有选项卡?

typescript - 版本不匹配 global tsc 1.0.3.0 != VS Code 的语言服务 (2.2.1)

javascript - 运行调试器时回调未运行;

reactjs - 在 VS Code 中调试错误 : Cannot connect to the target at localhost:3000: could not find any debuggable target