Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 585
ohadinho Avatar asked Sep 22 '15 07:09

ohadinho


1 Answers

sandbox.js is spawning shovel.js as child process without debugging enabled(e.g. 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.

  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
    

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:

  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

like image 113
hassansin Avatar answered Sep 27 '22 19:09

hassansin