我正在尝试使用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) {
。