debug threads

This commit is contained in:
Fedor Indutny 2012-01-07 00:28:20 +06:00 committed by Ben Noordhuis
parent 99679c6430
commit 6b2091b58a
2 changed files with 53 additions and 16 deletions

View File

@ -25,7 +25,7 @@ var util = require('util'),
vm = require('vm'), vm = require('vm'),
repl = require('repl'), repl = require('repl'),
inherits = util.inherits, inherits = util.inherits,
spawn = require('child_process').spawn; fork = require('child_process').fork;
exports.start = function(argv, stdin, stdout) { exports.start = function(argv, stdin, stdout) {
argv || (argv = process.argv.slice(2)); argv || (argv = process.argv.slice(2));
@ -39,7 +39,7 @@ exports.start = function(argv, stdin, stdout) {
stdin = stdin || process.openStdin(); stdin = stdin || process.openStdin();
stdout = stdout || process.stdout; stdout = stdout || process.stdout;
var args = ['--debug-brk'].concat(argv), var args = argv,
interface = new Interface(stdin, stdout, args); interface = new Interface(stdin, stdout, args);
stdin.resume(); stdin.resume();
@ -169,6 +169,8 @@ function Client() {
this.scripts = {}; this.scripts = {};
this.breakpoints = []; this.breakpoints = [];
this.isolates = process.features.isolates;
// Note that 'Protocol' requires strings instead of Buffers. // Note that 'Protocol' requires strings instead of Buffers.
socket.setEncoding('utf8'); socket.setEncoding('utf8');
socket.on('data', function(d) { socket.on('data', function(d) {
@ -1595,20 +1597,51 @@ Interface.prototype.trySpawn = function(cb) {
} }
} }
if (!this.child) { var client = self.client = new Client(),
this.child = spawn(process.execPath, this.args); connectionAttempts = 0;
this.child.stdout.on('data', this.childPrint.bind(this)); if (!this.child) {
this.child.stderr.on('data', this.childPrint.bind(this)); if (client.isolates) {
this.child = fork(this.args.shift(), this.args, {
thread: true,
debug: function(d) {
d.onmessage = function(event) {
client._onResponse({
headers: {},
body: JSON.parse(event)
});
};
// Monkey patch client to send requests directly to debugger
client.req = function(req, cb) {
req.type = 'request';
cb.request_seq = req.seq = this.protocol.reqSeq++;
this._reqCallbacks.push(cb);
d.write(JSON.stringify(req));
};
client.emit('ready');
client._onResponse({
headers: { Type: 'connect' },
body: {}
});
},
debugBrk: true
});
this.child.kill = function() {
self.error('isolate.kill is not implemented yet!');
};
} else {
this.child = fork('--debug-brk', this.args);
}
} }
this.pause(); this.pause();
var client = self.client = new Client(),
connectionAttempts = 0;
client.once('ready', function() { client.once('ready', function() {
self.stdout.write(' ok\n'); if (!client.isolates) self.stdout.write(' ok\n');
// Restore breakpoints // Restore breakpoints
breakpoints.forEach(function(bp) { breakpoints.forEach(function(bp) {
@ -1656,11 +1689,14 @@ Interface.prototype.trySpawn = function(cb) {
function attemptConnect() { function attemptConnect() {
++connectionAttempts; ++connectionAttempts;
self.stdout.write('.'); self.stdout.write('.');
client.connect(port, host); client.connect(port, host);
} }
setTimeout(function() { if (!client.isolates) {
self.print('connecting..', true); setTimeout(function() {
attemptConnect(); self.print('connecting..', true);
}, 50); attemptConnect();
}, 50);
}
}; };

View File

@ -361,7 +361,8 @@ var spawn = exports.spawn = function(file, args, options) {
windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments), windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments),
envPairs: envPairs, envPairs: envPairs,
customFds: options ? options.customFds : null, customFds: options ? options.customFds : null,
stdinStream: options ? options.stdinStream : null stdinStream: options ? options.stdinStream : null,
options: options
}); });
return child; return child;
@ -538,7 +539,7 @@ Isolate.prototype.spawn = function(options) {
var self = this; var self = this;
if (self._handle) throw new Error('Isolate already running.'); if (self._handle) throw new Error('Isolate already running.');
self._handle = isolates.create(options.args); self._handle = isolates.create(options.args, options.options);
if (!self._handle) throw new Error('Cannot create isolate.'); if (!self._handle) throw new Error('Cannot create isolate.');
self._handle.onmessage = function(msg) { self._handle.onmessage = function(msg) {