Skip to content

Commit 9e09fc0

Browse files
indutnyry
authored andcommitted
more cli options
* node debug localhost:5858 - connects to remote debugger * node debug -p `pgrep node` - connects to running process * Fixed double-run of debugger on SIGUSR1
1 parent d91f64f commit 9e09fc0

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

lib/_debugger.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ var util = require('util'),
2727
inherits = util.inherits,
2828
spawn = require('child_process').spawn;
2929

30-
exports.port = 5858;
31-
3230
exports.start = function(argv, stdin, stdout) {
3331
argv || (argv = process.argv.slice(2));
3432

@@ -922,6 +920,8 @@ Interface.prototype.controlEval = function(code, context, filename, callback) {
922920

923921
// Used for debugger's remote evaluation (`repl`) commands
924922
Interface.prototype.debugEval = function(code, context, filename, callback) {
923+
if (!this.requireConnection()) return;
924+
925925
var self = this,
926926
client = this.client;
927927

@@ -1409,14 +1409,43 @@ Interface.prototype.killChild = function() {
14091409
// Spawns child process (and restores breakpoints)
14101410
Interface.prototype.trySpawn = function(cb) {
14111411
var self = this,
1412-
breakpoints = this.breakpoints || [];
1412+
breakpoints = this.breakpoints || [],
1413+
port = 5858,
1414+
host = 'localhost';
14131415

14141416
this.killChild();
14151417

1416-
this.child = spawn(process.execPath, this.args);
1418+
// Connecting to remote debugger
1419+
// `node debug localhost:5858`
1420+
if (this.args.length === 2) {
1421+
var match = this.args[1].match(/^([^:]+):(\d+)$/);
1422+
if (match) {
1423+
host = match[1];
1424+
port = parseInt(match[2], 10);
1425+
this.child = {
1426+
kill: function() {
1427+
// TODO Do we really need to handle it?
1428+
}
1429+
};
1430+
}
1431+
} else if (this.args.length === 3) {
1432+
// `node debug -p pid`
1433+
if (this.args[1] === '-p' && /^\d+$/.test(this.args[2])) {
1434+
this.child = {
1435+
kill: function() {
1436+
// TODO Do we really need to handle it?
1437+
}
1438+
};
1439+
process.kill(parseInt(this.args[2], 10), 'SIGUSR1');
1440+
}
1441+
}
1442+
1443+
if (!this.child) {
1444+
this.child = spawn(process.execPath, this.args);
14171445

1418-
this.child.stdout.on('data', this.childPrint.bind(this));
1419-
this.child.stderr.on('data', this.childPrint.bind(this));
1446+
this.child.stdout.on('data', this.childPrint.bind(this));
1447+
this.child.stderr.on('data', this.childPrint.bind(this));
1448+
}
14201449

14211450
this.pause();
14221451

@@ -1461,16 +1490,16 @@ Interface.prototype.trySpawn = function(cb) {
14611490
client.on('error', connectError);
14621491
function connectError() {
14631492
// If it's failed to connect 4 times then don't catch the next error
1464-
if (connectionAttempts >= 4) {
1493+
if (connectionAttempts >= 10) {
14651494
client.removeListener('error', connectError);
14661495
}
1467-
setTimeout(attemptConnect, 50);
1496+
setTimeout(attemptConnect, 500);
14681497
}
14691498

14701499
function attemptConnect() {
14711500
++connectionAttempts;
14721501
self.stdout.write('.');
1473-
client.connect(exports.port);
1502+
client.connect(port, host);
14741503
}
14751504

14761505
setTimeout(function() {

src/node.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2356,14 +2356,17 @@ static void EnableDebug(bool wait_connect) {
23562356

23572357
// Print out some information.
23582358
fprintf(stderr, "debugger listening on port %d", debug_port);
2359+
2360+
debugger_running = true;
23592361
}
23602362

23612363

23622364
static volatile bool hit_signal;
2365+
static volatile bool debugger_running = false;
23632366

23642367

23652368
static void DebugSignalCB(const Debug::EventDetails& details) {
2366-
if (hit_signal && details.GetEvent() == v8::Break) {
2369+
if (!debugger_running && hit_signal && details.GetEvent() == v8::Break) {
23672370
hit_signal = false;
23682371
fprintf(stderr, "Hit SIGUSR1 - starting debugger agent.\n");
23692372
EnableDebug(false);

0 commit comments

Comments
 (0)