gpt4 book ai didi

javascript - 使用 Node 检查器调试 V8

转载 作者:行者123 更新时间:2023-11-30 05:38:32 25 4
gpt4 key购买 nike

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

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

Node-inspector 连接良好,甚至能够暂停/取消暂停并显示代码。但是,逐步执行不起作用,断点也不起作用,可能还有很多其他的东西。当我尝试做这些事情时,我从 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。在 Node 检查器中,我得到了这个:

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(无域) 下获得了一个额外的条目。当我做出改变时

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

它消失了,但是,根据 doc,这并不是我所期望的那样。 .

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

此外,file:// 下的 Sources 中的所有文件都在第一行添加了 (function (exports, require, module, __filename, __dirname) { 前缀源代码。该行不是来 self 的代码,而是来自 Node 检查器。为什么要在此处添加它?特别奇怪,因为我的代码添加了一个略有不同的前缀 ( function(module, exports) {.

最佳答案

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

我会说 95% 的 Node Inspector 代码仅使用 V8 协议(protocol)。查找 DebuggerClient.prototype.evaluateGlobal 的用法以查找使用 Node 特定功能的位置。

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

更新

首先尝试 Node 的 CLI 调试器:

$ node debug localhost:5858

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

关于javascript - 使用 Node 检查器调试 V8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22153131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com