Debugging gf3/sandbox module

I'm doing my baby steps in node.js, and i'm trying to understand sandbox mechanism.

Currently i'm using node v4.0.0 and node-inspector v0.12.3.

I've installed gf3/sandbox module and run it with this simple code:

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

In order to debug easily, i've also commented the timeout function in sandbox.js file:

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

The issue is that debug DOESN'T break on ANY line code of shovel.js , and i'm 100% sure the module is using its code.

Why is that ? And what can I do in order to debug shovel.js ?


sandbox.js is spawning shovel.js as child process without debugging enabled(eg no --debug option). So the child process executes normally and your breakpoints are simply ignored. You need to start child process in debug mode too.

If you want to debug both sandbox.js and shovel.js at the same time, then use different debug ports. I'm not sure about node-inspector, but here is an example of how you can do it with the debugger module. I'm sure you can tweak a bit to make it work with node-inspector.

  • Comment the timeout code like you already did
  • 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'] });    
    
  • start example.js in debug mode. By default, it starts at 5858 port:

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

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

     node debug localhost:5859
    
  • For node-inspector, I think you need to use node-debug command instead of this.options.node for child process. Also there are options to set debug port explicitly.


    From above, These could be the steps for node-inspector . Note: I haven't tested it:

  • Same as above
  • 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'] });    
    
  • start example.js in debug mode. By default, it starts at 5858 port:

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

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

  • 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

  • 链接地址: http://www.djcxy.com/p/29246.html

    上一篇: 如何在Pod副本中运行一个etcd集群?

    下一篇: 调试gf3 /沙箱模块