Nodeclipse / nodeclipse

Nodeclipse-1 : Eclipse plugin for Node.js, PhantomJS development (Nodeclipse core plugin); Maven and Gradle (with Android) plugins
https://nodeclipse.github.io/
158 stars 78 forks source link

[research] debug Node.JS child forked process #146

Open paulvi opened 10 years ago

paulvi commented 10 years ago

see http://stackoverflow.com/questions/22908734/debug-angularjs-protractor-e2e-testfile-with-eclipse-and-chrome-developer-tools

http://stackoverflow.com/questions/16840623/how-to-debug-node-js-child-forked-process

sroe commented 10 years ago

If you use node --debug-brk for starting your node-app then the debugger is stopping at the first line like you explained in your https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.help/contents/debug.md Helpfile. So if you fork a childprocess with in node v0.10 this childprocess gets the arguments of process.execArgv also by default (like explained here http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options). So the childprocess is also stopping at the first line of code. But it gets the same port as the masterprocess, so you are not able to connect a debugger and let the childprocess resume its code. Thats why you need to pop the process.execArgv or change it to e.g. "--debug-brk=5555" (if 5555 is a port which no current process listens on) before forking the childprocess. I hope this explaination is clear enough.

As I posted on SO it is not protractor related as I thought at the beginning of my research :-).

paulvi commented 10 years ago

Hi Sebastian

That is clear that every Node process should have its own debug port passed.

Thats why you need to pop the process.execArgv

You mean any Node.js developer?
How to do that for me is not clear. I have not yet tried what is at http://stackoverflow.com/questions/16840623/how-to-debug-node-js-child-forked-process that why I created this issue (to return later when have time).

sroe commented 10 years ago

No not every developer need to pop or change the process.execArgv Parameters when debugging child-process. When you do not need to connect to the process there is no need to change anything in the code. It is only required if someone use --debug or --debug-brk and wants to connect with a remote debugger (you dont have this problem if you use the node.js console debugger oder chrome debugger I think). Because --debug-brk is stopping the v8 debugger at the first line of code (sets a breakpoint there I believe) inside the childprocess also and is not able to resume this process without a remote debugger connection. Because node.js uses process.execArgv as default parameter for childprocesses which are generated by fork function, the developer needs to care about this behavior at the current version of node.js as they described in the stackoverflow post. I dont know how the guys of node.js changed this behavior in v0.12.

How to: before child.fork you need to change process.execArgv e.g. process.execArgv[0] = '--debug-brk=5555'; so you would change the first argument (if it is present inside the array, you should check that by if-statement or something) inside the array. If the first statement is required by your app code you should change the array item with the --debug-brk from the mainprocess, because this was only used for starting the mainprocess (which you are running in while chil.fork is getting called).

paulvi commented 10 years ago

I would recommend for port numbers to use numbers larger than 5858 with +2 for every child process.