How to get a thread dump of a running Node.js process?

Viewed 6779

In the Java JVM, kill -3 forces the process to print the current stack traces of all running threads. I found it very effective to quickly locate bottlenecks.

Is there an equivalent in V8? Can I make V8 print the current stack trace?


Clarification: I assume, due to the asynchronous nature of node, it will be less useful than for a typical non-asynchronous program. Still, if there is an easy way to get access to a few stack traces, it does not take much time to look at it.

From my experience, some obvious bottlenecks can be quickly located that way before you need to switch to more advanced tools.

3 Answers

On this thread you have an idea using:

# node --debug-brk buggy.js
Debugger listening on port 5858

In another terminal:

# node debug -p $(pgrep -f 'node.*buggy')
connecting to 127.0.0.1:5858 ... ok
break in buggy.js:16
 14 }
 15 
>16 z();
 17 
 18 });
debug> cont

(wait for the node process to start using 100% CPU)

debug> step
break in buggy.js:3
  1 function x() {
  2     var i = 0;
> 3     while(1) {
  4             i++;
  5     }
debug> bt
#0 buggy.js:3:8
#1 buggy.js:9:2
#2 buggy.js:13:2
#3 buggy.js:16:1

Source: https://github.com/nodejs/node-v0.x-archive/issues/25263

Related