Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debugger: don't spawn child process in remote mode #1282

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ exports.start = function(argv, stdin, stdout) {

if (argv.length < 1) {
console.error('Usage: iojs debug script.js');
console.error(' iojs debug <host>:<port>');
console.error(' iojs debug -p <pid>');
process.exit(1);
}

Expand Down Expand Up @@ -1626,6 +1628,7 @@ Interface.prototype.trySpawn = function(cb) {
this.killChild();
assert(!this.child);

var isRemote = false;
if (this.args.length === 2) {
var match = this.args[1].match(/^([^:]+):(\d+)$/);

Expand All @@ -1634,21 +1637,13 @@ Interface.prototype.trySpawn = function(cb) {
// `node debug localhost:5858`
host = match[1];
port = parseInt(match[2], 10);
this.child = {
kill: function() {
// TODO Do we really need to handle it?
}
};
isRemote = true;
}
} else if (this.args.length === 3) {
// `node debug -p pid`
if (this.args[1] === '-p' && /^\d+$/.test(this.args[2])) {
this.child = {
kill: function() {
// TODO Do we really need to handle it?
}
};
process._debugProcess(parseInt(this.args[2], 10));
isRemote = true;
} else {
var match = this.args[1].match(/^--port=(\d+)$/);
if (match) {
Expand All @@ -1660,10 +1655,13 @@ Interface.prototype.trySpawn = function(cb) {
}
}

this.child = spawn(process.execPath, childArgs);
if (!isRemote) {
// pipe stream into debugger
this.child = spawn(process.execPath, childArgs);

this.child.stdout.on('data', this.childPrint.bind(this));
this.child.stderr.on('data', this.childPrint.bind(this));
this.child.stdout.on('data', this.childPrint.bind(this));
this.child.stderr.on('data', this.childPrint.bind(this));
}

this.pause();

Expand Down Expand Up @@ -1707,9 +1705,10 @@ Interface.prototype.trySpawn = function(cb) {

client.on('error', connectError);
function connectError() {
// If it's failed to connect 4 times then don't catch the next error
// If it's failed to connect 10 times then print failed message
if (connectionAttempts >= 10) {
client.removeListener('error', connectError);
self.stdout.write(' failed, please retry\n');
return;
}
setTimeout(attemptConnect, 500);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo? attemptConnect is called connect now, I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not typo. attemptConnect is called, but failed.

}
Expand All @@ -1720,10 +1719,12 @@ Interface.prototype.trySpawn = function(cb) {
client.connect(port, host);
}

this.child.stderr.once('data', function() {
setImmediate(function() {
self.print('connecting to port ' + port + '..', true);
attemptConnect();
self.print('connecting to ' + host + ':' + port + ' ..', true);
if (isRemote) {
attemptConnect();
} else {
this.child.stderr.once('data', function() {
setImmediate(attemptConnect);
});
});
}
};
52 changes: 52 additions & 0 deletions test/debugger/test-debugger-remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var common = require('../common');
var assert = require('assert');
var spawn = require('child_process').spawn;

var port = common.PORT + 1337;
var buffer = '';
var expected = [];
var scriptToDebug = common.fixturesDir + '/empty.js';

function fail() {
assert(0); // `node --debug-brk script.js` should not quit
}

// running with debug agent
var child = spawn(process.execPath, ['--debug-brk=5959', scriptToDebug]);

console.error('./node', '--debug-brk=5959', scriptToDebug);

// connect to debug agent
var interfacer = spawn(process.execPath, ['debug', 'localhost:5959']);

console.error('./node', 'debug', 'localhost:5959');
interfacer.stdout.setEncoding('utf-8');
interfacer.stdout.on('data', function(data) {
data = (buffer + data).split('\n');
buffer = data.pop();
data.forEach(function(line) {
interfacer.emit('line', line);
});
});

interfacer.on('line', function(line) {
line = line.replace(/^(debug> *)+/, '');
console.log(line);
var expected = '\bconnecting to localhost:5959 ... ok';
assert.ok(expected == line, 'Got unexpected line: ' + line);
});

// give node time to start up the debugger
setTimeout(function() {
child.removeListener('exit', fail);
child.kill();
interfacer.removeListener('exit', fail);
interfacer.kill();
}, 2000);

process.on('exit', function() {
assert(child.killed);
assert(interfacer.killed);
});

interfacer.stderr.pipe(process.stderr);