调试gf3/sandbox模块

7

我正在学习Node.js,尝试理解沙盒机制。

目前我正在使用Node v4.0.0和node-inspector v0.12.3。

我已经安装了gf3/sandbox模块,并使用以下简单代码运行它:

var s = new Sandbox();
s.run('1 + 1 + " apples"',function(output) {
                console.log(output.result);
        });

为了方便调试,我还在文件中注释了超时函数:

// timer = setTimeout(function() {
    // self.child.stdout.removeListener('output', output);
    // stdout = JSON.stringify({ result: 'TimeoutError', console: [] });
    // self.child.kill('SIGKILL');
  // }, self.options.timeout);

问题在于调试器不能在shovel.js的任何代码行上中断,我百分之百确定该模块正在使用自己的代码。这是为什么?我应该怎么做才能调试shovel.js吗?
1个回答

4

sandbox.js是将shovel.js作为子进程启动(通过此链接),但未开启调试模式(即没有使用--debug选项)。因此子进程会正常执行,你的断点会被忽略。你需要在子进程中也开启调试模式。

如果你想同时调试sandbox.jsshovel.js,请使用不同的调试端口。我不确定node-inspector能否实现,但下面是使用debugger模块的示例代码。你可以稍微调整一下以便在node-inspector中运行。

  1. Comment the timeout code like you already did
  2. Pass debug option while spawning child process in sandbox.js. Note the port is 5859:

    self.child = spawn(this.options.node, ['--debug-brk=5859',this.options.shovel], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });    
    
  3. start example.js in debug mode. By default, it starts at 5858 port:

    node --debug-brk example.js
    
  4. Now debug sandbox.js by connecting to 5858:

    node debug localhost:5858
    
  5. Once the child process starts, you can fire up separate terminal and start debugging shovel.js on port 5859:

     node debug localhost:5859
    

对于node-inspector,我认为您需要使用node-debug命令代替子进程中的this.options.node。此外,有选项可以明确设置调试端口。


根据上述内容,以下可能是node-inspector的步骤。 注意:我还没有测试过

  1. Same as above
  2. Open sandbox.js file and change this line like following to pass debug option while spawning child process. Note the port is 5859:

    self.child = spawn('node-debug', ['--debug-port=5859',this.options.shovel], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });    
    
  3. start example.js in debug mode. By default, it starts at 5858 port:

    node-debug example.js
    
  4. Now head to the browser to debug parent process:

    http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858

  5. Once the child process starts, open up another browser window to debug shovel.js:

    http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5859


你好, 感谢您详细的回答。
  1. 我应该把这行代码放在哪里:
self.child = spawn(this.options.node, ['--debug-brk=5859',this.options.shovel], { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });
  1. "debug-brk"是什么?为什么不直接使用"debug"?
  2. 在第4和第5段中,您是不是指的是"node-debug"而不是"node debug"?
谢谢。
- ohadinho
  1. 检查我的回答中的第一个链接。2,3: 我的回答是针对内置 debugger 模块的。这就是为什么要使用 --debug-brk。对于 node-inspector,您需要使用它自己的命令和参数,但步骤将相同。我在Windows上,无法测试,因为 node-inspector 在Windows上表现不佳。
- hassansin
添加了未经测试的步骤以使用node-inspector。如果有效,请告诉我。 - hassansin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接