Skip to content

Instantly share code, notes, and snippets.

@jiangzhuo
Forked from qzaidi/jsstack.js
Created September 5, 2016 17:32
Show Gist options
  • Save jiangzhuo/bf2c3441c20b33e176153ef78cb2f54a to your computer and use it in GitHub Desktop.
Save jiangzhuo/bf2c3441c20b33e176153ef78cb2f54a to your computer and use it in GitHub Desktop.
jsstack - pstack like tool for node.js apps
#!/usr/bin/env node
"use strict";
var debug = require('_debugger');
var c = new debug.Client();
function main() {
console.log('requesting trace');
c.reqBacktrace(function(err,trace) {
if (!err) {
trace.frames.forEach(function(frame) {
process.stderr.write(frame.text + '\n');
});
process.exit(0);
} else {
console.error(err);
process.exit(1);
}
});
}
function usage() {
var procname = process.argv[1].split('/').pop()
console.log('Usage: ' + procname + ' <pid of node process>');
process.exit(1);
}
c.on('error', function() {
console.log('[Failed]');
});
c.once('ready', function() {
console.log('[ok]');
c.reqVersion(function(err,version,state) {
console.log('v8: ' + version);
main();
});
});
if (process.argv.length < 3)
usage();
var pid = process.argv[2];
process.kill(pid,'SIGUSR1');
setTimeout(function() {
process.stdout.write('connecting ... ');
c.connect(5858);
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment