Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
debug threads
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny authored and bnoordhuis committed Jan 10, 2012
1 parent 99679c6 commit 6b2091b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
64 changes: 50 additions & 14 deletions lib/_debugger.js
Expand Up @@ -25,7 +25,7 @@ var util = require('util'),
vm = require('vm'),
repl = require('repl'),
inherits = util.inherits,
spawn = require('child_process').spawn;
fork = require('child_process').fork;

exports.start = function(argv, stdin, stdout) {
argv || (argv = process.argv.slice(2));
Expand All @@ -39,7 +39,7 @@ exports.start = function(argv, stdin, stdout) {
stdin = stdin || process.openStdin();
stdout = stdout || process.stdout;

var args = ['--debug-brk'].concat(argv),
var args = argv,
interface = new Interface(stdin, stdout, args);

stdin.resume();
Expand Down Expand Up @@ -169,6 +169,8 @@ function Client() {
this.scripts = {};
this.breakpoints = [];

this.isolates = process.features.isolates;

// Note that 'Protocol' requires strings instead of Buffers.
socket.setEncoding('utf8');
socket.on('data', function(d) {
Expand Down Expand Up @@ -1595,20 +1597,51 @@ Interface.prototype.trySpawn = function(cb) {
}
}

if (!this.child) {
this.child = spawn(process.execPath, this.args);
var client = self.client = new Client(),
connectionAttempts = 0;

this.child.stdout.on('data', this.childPrint.bind(this));
this.child.stderr.on('data', this.childPrint.bind(this));
if (!this.child) {
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();

var client = self.client = new Client(),
connectionAttempts = 0;

client.once('ready', function() {
self.stdout.write(' ok\n');
if (!client.isolates) self.stdout.write(' ok\n');

// Restore breakpoints
breakpoints.forEach(function(bp) {
Expand Down Expand Up @@ -1656,11 +1689,14 @@ Interface.prototype.trySpawn = function(cb) {
function attemptConnect() {
++connectionAttempts;
self.stdout.write('.');

client.connect(port, host);
}

setTimeout(function() {
self.print('connecting..', true);
attemptConnect();
}, 50);
if (!client.isolates) {
setTimeout(function() {
self.print('connecting..', true);
attemptConnect();
}, 50);
}
};
5 changes: 3 additions & 2 deletions lib/child_process.js
Expand Up @@ -361,7 +361,8 @@ var spawn = exports.spawn = function(file, args, options) {
windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments),
envPairs: envPairs,
customFds: options ? options.customFds : null,
stdinStream: options ? options.stdinStream : null
stdinStream: options ? options.stdinStream : null,
options: options
});

return child;
Expand Down Expand Up @@ -538,7 +539,7 @@ Isolate.prototype.spawn = function(options) {
var self = this;

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.');

self._handle.onmessage = function(msg) {
Expand Down

0 comments on commit 6b2091b

Please sign in to comment.