2

我正在尝试使用node-inspector调试在 V8 中运行的 JavaScript 脚本。在应用程序方面,我刚刚做了

v8::Debug::EnableAgent("MyApp", 5858);

节点检查器连接良好,甚至能够暂停/取消暂停并显示代码。但是,逐步执行不起作用,断点也不起作用,可能还有很多其他事情。当我尝试做这些事情时,我从 Node-inspector 收到这些错误:

Node Inspector v0.7.0
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setSkipAllPauses
Received request for a method not implemented: Debugger.setBreakpoint
Received request for a method not implemented: Debugger.setBreakpoint
Received request for a method not implemented: Debugger.setBreakpoint

所以我想我错过了一些东西。

我不确定我想要做的事情是否得到支持——因为我猜,node-inspector 是针对 Node.js 而不是针对任意 V8,对吧?如果是这样,需要什么才能使其工作?


感谢 Miroslav 的帮助,尤其是。跑DEBUG=node-inspector:protocol:* node-inspector,我走得更远了。逐步执行现在可以工作,并且断点在大多数情况下都可以(除非您选择了错误的源文件 - 见下文)。

我提供了一个像这样的全局process对象:

// process object: http://nodejs.org/api/process.html#process_process
process.stdout = ...
process.stderr = ...
process._baseDir = ...
process.mainModule = {filename: process._baseDir + "/main.js"}
process.argv = ["myapp.exe", process.mainModule.filename]
process.cwd = function() { return process._baseDir; }

现在我Internal error: TypeError: Cannot read property 'line' of null在控制台中得到错误。在节点检查器中,我得到了这个:

Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:v8-debug request: {"seq":170,"type":"request","command":"backtrace","arguments":{"inlineRefs":true,"fromFrame":0,"toFrame":50,"maxStringLength":10000}}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:devtools frontend: {"method":"Debugger.setOverlayMessage","id":48}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:devtools backend: {"id":48}
Wed, 19 Mar 2014 11:58:43 GMT node-inspector:protocol:v8-debug response: {"seq":41,"request_seq":170,"type":"response","success":false,"message":"Internal error: TypeError: Cannot read property 'line' of null"}

另一件事是脚本文件并不总是正确的。在 C++ 方面,我现在像这样加载它们:

ReturnType execJsFile(const std::string& jsSourceDir, const std::string& extfilename) {
    v8::TryCatch try_catch;

    std::string fullfilename = jsSourceDir + "/" + extfilename;
    std::string sourceStr;
    CHECK_RETURN(readFile(fullfilename, sourceStr));

    // The origin is for the debugger, e.g. node-inspector. It expects an URL.
    Local<String> origin = jsStr("file:///" + fullfilename);
    Local<String> source = jsStr(sourceStr);

    Local<v8::Script> script = Script::Compile(source, origin);
    if(script.IsEmpty()) {
        assert(try_catch.HasCaught());
        return "JS compile failed: " + jsReportExceptionToString(Isolate::GetCurrent(), &try_catch);;
    }

    Local<Value> result = script->Run();
    if(result.IsEmpty()) {
        assert(try_catch.HasCaught());
        return "JS script execution failed: " + jsReportExceptionToString(Isolate::GetCurrent(), &try_catch);
    }

    return true;
}

这会将file://域下的所有文件放在源列表中。但是,main.js(no domain). 当我做出改变

process.mainModule = {filename: "file:///" + process._baseDir + "/main.js"}

但是,它消失了,这不是我所期望的“根据文档” 。

当我在 main.js 中暂停/中断执行时,它会显示在另一个 Source 中[VM] main.js并获得黄色背景。

此外,Sources 下file://的所有文件都会在源(function (exports, require, module, __filename, __dirname) {代码的第一行添加前缀。该行不是来自我的代码,而是来自节点检查器。为什么要在这里添加?尤其是。奇怪,因为我的代码添加了一个稍微不同的前缀( function(module, exports) {

4

2 回答 2

1

运行DEBUG=node-inspector:protocol:* node-inspector并检查消息,您可能可以在那里找到更多信息。您也可以尝试使用旧版本,例如 0.1.9,它可能对特定于 Node 的东西的依赖较少。

我想说 95% 的 Node Inspector 代码只使用 V8 协议。查找用法DebuggerClient.prototype.evaluateGlobal以查找使用节点特定功能的位置。

首先要改变的getResourceTreelib/PageAgent.js. 要么实现你自己的方式来列出所有源文件(包括那些尚未加载的),要么返回一个空树。

更新

首先尝试 Node 的 CLI 调试器:

$ node debug localhost:5858

据我所知,CLI 调试器仅使用 V8 调试器协议功能,没有特定于节点。当您能够使用 CLI 调试您的 V8 应用程序时,您将知道任何其他问题都在 Node Inspector 中,而不是在您的 V8 应用程序中。

于 2014-03-04T18:28:52.623 回答
0

这是使用节点检查器调试 V8 的解决方案:

首先,最新版本 (0.12.3) 有许多用于调试 node.js 脚本的扩展,所以我使用了对 node.js 的责任较小的旧版本 (0.5.0)。

我使用以下命令在 Node.js 命令提示符下安装 v0.5.0:

npm install node-inspector@0.5.0

它默认安装在文件夹“%USERPROFILE%\node_modules”中。

我在 %USERPROFILE%\node_modules\node-inspector\lib\PageAgent.js 中添加了一行代码:

getResourceTree: function(params, done) {
    return; //return empty tree
...

这样就完成了安装。

以下是调试使用 ClearScript .NET 库执行的 javascript 文件的指南:

打开命令提示符并执行以下命令:

%USERPROFILE%\node_modules\.bin\node-inspector.cmd

如果 node-inspector 成功运行,您应该看到以下行。

Node Inspector v0.5.0
info - socket.io started 
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

按照以下代码行(VB.NET 代码)初始化您的 V8 引擎:

昏暗引擎作为新的 V8ScriptEngine("global", V8ScriptEngineFlags.EnableDebugging, 9222)

在代码中的这一行之后放置一个断点并运行您的应用程序以到达该断点。

确保你有“调试器”;JavaScript 代码的第一行中的语句。

打开 Chrome 浏览器并导航到以下地址:

http://127.0.0.1:8080/debug?port=9222

按 Vİsual Studio 工具栏中的“继续调试”按钮。

现在您应该看到您的脚本代码在第一个“调试器”处停止;在 Chrome 浏览器中行。

您可以从这里继续调试。

于 2015-10-06T11:45:25.703 回答