2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2013-02-16 03:31:43 +01:00
|
|
|
process.env.NODE_DEBUGGER_TIMEOUT = 2000;
|
2010-12-22 04:14:29 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-12-22 07:31:30 +01:00
|
|
|
var debug = require('_debugger');
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2013-02-27 02:33:30 +01:00
|
|
|
var debugPort = common.PORT + 1337;
|
|
|
|
debug.port = debugPort;
|
2010-12-22 04:14:29 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
2013-02-26 02:46:48 +01:00
|
|
|
setTimeout(function() {
|
|
|
|
if (nodeProcess) nodeProcess.kill('SIGTERM');
|
|
|
|
throw new Error('timeout');
|
|
|
|
}, 10000).unref();
|
|
|
|
|
2010-12-22 04:14:29 +01:00
|
|
|
|
|
|
|
var resCount = 0;
|
2010-12-22 07:31:30 +01:00
|
|
|
var p = new debug.Protocol();
|
2011-10-05 00:08:18 +02:00
|
|
|
p.onResponse = function(res) {
|
2010-12-22 04:14:29 +01:00
|
|
|
resCount++;
|
|
|
|
};
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
p.execute('Type: connect\r\n' +
|
|
|
|
'V8-Version: 3.0.4.1\r\n' +
|
|
|
|
'Protocol-Version: 1\r\n' +
|
|
|
|
'Embedding-Host: node v0.3.3-pre\r\n' +
|
|
|
|
'Content-Length: 0\r\n\r\n');
|
2010-12-22 04:14:29 +01:00
|
|
|
assert.equal(1, resCount);
|
|
|
|
|
2010-12-31 02:33:06 +01:00
|
|
|
// Make sure split messages go in.
|
|
|
|
|
|
|
|
var parts = [];
|
|
|
|
parts.push('Content-Length: 336\r\n');
|
|
|
|
assert.equal(21, parts[0].length);
|
|
|
|
parts.push('\r\n');
|
|
|
|
assert.equal(2, parts[1].length);
|
|
|
|
var bodyLength = 0;
|
|
|
|
|
|
|
|
parts.push('{"seq":12,"type":"event","event":"break","body":' +
|
|
|
|
'{"invocationText":"#<a Server>');
|
|
|
|
assert.equal(78, parts[2].length);
|
|
|
|
bodyLength += parts[2].length;
|
|
|
|
|
2012-02-19 00:01:35 +01:00
|
|
|
parts.push('.[anonymous](req=#<an IncomingMessage>, ' +
|
|
|
|
'res=#<a ServerResponse>)","sourceLine"');
|
2010-12-31 02:33:06 +01:00
|
|
|
assert.equal(78, parts[3].length);
|
|
|
|
bodyLength += parts[3].length;
|
|
|
|
|
2012-02-19 00:01:35 +01:00
|
|
|
parts.push(':45,"sourceColumn":4,"sourceLineText":" debugger;",' +
|
|
|
|
'"script":{"id":24,"name":"/home/ryan/projects/node/' +
|
|
|
|
'benchmark/http_simple.js","lineOffset":0,"columnOffset":0,' +
|
|
|
|
'"lineCount":98}}}');
|
2010-12-31 02:33:06 +01:00
|
|
|
assert.equal(180, parts[4].length);
|
|
|
|
bodyLength += parts[4].length;
|
|
|
|
|
|
|
|
assert.equal(336, bodyLength);
|
|
|
|
|
|
|
|
for (var i = 0; i < parts.length; i++) {
|
|
|
|
p.execute(parts[i]);
|
|
|
|
}
|
|
|
|
assert.equal(2, resCount);
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure that if we get backed up, we still manage to get all the
|
|
|
|
// messages
|
|
|
|
var d = 'Content-Length: 466\r\n\r\n' +
|
|
|
|
'{"seq":10,"type":"event","event":"afterCompile","success":true,' +
|
|
|
|
'"body":{"script":{"handle":1,"type":"script","name":"dns.js",' +
|
2012-02-19 00:01:35 +01:00
|
|
|
'"id":34,"lineOffset":0,"columnOffset":0,"lineCount":241,' +
|
2015-05-19 13:00:06 +02:00
|
|
|
'"sourceStart":"(function(module, exports, require) {' +
|
2012-02-19 00:01:35 +01:00
|
|
|
'var dns = process.binding(\'cares\')' +
|
2010-12-31 02:33:06 +01:00
|
|
|
';\\nvar ne","sourceLength":6137,"scriptType":2,"compilationType":0,' +
|
2012-02-19 00:01:35 +01:00
|
|
|
'"context":{"ref":0},"text":"dns.js (lines: 241)"}},"refs":' +
|
|
|
|
'[{"handle":0' +
|
2010-12-31 02:33:06 +01:00
|
|
|
',"type":"context","text":"#<a ContextMirror>"}],"running":true}' +
|
|
|
|
'Content-Length: 119\r\n\r\n' +
|
|
|
|
'{"seq":11,"type":"event","event":"scriptCollected","success":true,' +
|
|
|
|
'"body":{"script":{"id":26}},"refs":[],"running":true}';
|
|
|
|
p.execute(d);
|
|
|
|
assert.equal(4, resCount);
|
|
|
|
|
2010-12-22 23:31:57 +01:00
|
|
|
var expectedConnections = 0;
|
|
|
|
var tests = [];
|
2011-10-05 00:08:18 +02:00
|
|
|
function addTest(cb) {
|
2010-12-22 23:31:57 +01:00
|
|
|
expectedConnections++;
|
|
|
|
tests.push(cb);
|
|
|
|
}
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
addTest(function(client, done) {
|
|
|
|
console.error('requesting version');
|
|
|
|
client.reqVersion(function(err, v) {
|
2011-09-27 19:16:29 +02:00
|
|
|
assert.ok(!err);
|
2011-10-05 00:08:18 +02:00
|
|
|
console.log('version: %s', v);
|
2010-12-22 23:31:57 +01:00
|
|
|
assert.equal(process.versions.v8, v);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
addTest(function(client, done) {
|
|
|
|
console.error('requesting scripts');
|
|
|
|
client.reqScripts(function(err) {
|
2011-09-27 19:16:29 +02:00
|
|
|
assert.ok(!err);
|
2011-10-05 00:08:18 +02:00
|
|
|
console.error('got %d scripts', Object.keys(client.scripts).length);
|
2010-12-31 02:52:14 +01:00
|
|
|
|
2010-12-22 23:31:57 +01:00
|
|
|
var foundMainScript = false;
|
2010-12-31 02:52:14 +01:00
|
|
|
for (var k in client.scripts) {
|
|
|
|
var script = client.scripts[k];
|
|
|
|
if (script && script.name === 'node.js') {
|
2010-12-22 23:31:57 +01:00
|
|
|
foundMainScript = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.ok(foundMainScript);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
addTest(function(client, done) {
|
|
|
|
console.error('eval 2+2');
|
|
|
|
client.reqEval('2+2', function(err, res) {
|
2010-12-22 23:31:57 +01:00
|
|
|
console.error(res);
|
2011-09-27 19:16:29 +02:00
|
|
|
assert.ok(!err);
|
|
|
|
assert.equal('4', res.text);
|
|
|
|
assert.equal(4, res.value);
|
2010-12-22 23:31:57 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2010-12-22 07:31:30 +01:00
|
|
|
var connectCount = 0;
|
2015-05-19 13:00:06 +02:00
|
|
|
var script = 'setTimeout(function() { console.log("blah"); });' +
|
|
|
|
'setInterval(function() {}, 1000000);';
|
2013-02-26 02:46:48 +01:00
|
|
|
|
|
|
|
var nodeProcess;
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2010-12-22 23:31:57 +01:00
|
|
|
function doTest(cb, done) {
|
2013-02-27 02:33:30 +01:00
|
|
|
var args = ['--debug=' + debugPort, '-e', script];
|
|
|
|
nodeProcess = spawn(process.execPath, args);
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2013-02-26 02:46:48 +01:00
|
|
|
nodeProcess.stdout.once('data', function(c) {
|
2011-10-05 00:08:18 +02:00
|
|
|
console.log('>>> new node process: %d', nodeProcess.pid);
|
2013-03-08 18:29:13 +01:00
|
|
|
var failed = true;
|
|
|
|
try {
|
|
|
|
process._debugProcess(nodeProcess.pid);
|
|
|
|
failed = false;
|
|
|
|
} finally {
|
|
|
|
// At least TRY not to leave zombie procs if this fails.
|
|
|
|
if (failed)
|
|
|
|
nodeProcess.kill('SIGTERM');
|
|
|
|
}
|
2011-11-04 16:23:02 +01:00
|
|
|
console.log('>>> starting debugger session');
|
2010-12-22 07:31:30 +01:00
|
|
|
});
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2010-12-22 07:31:30 +01:00
|
|
|
var didTryConnect = false;
|
|
|
|
nodeProcess.stderr.setEncoding('utf8');
|
2011-10-05 00:08:18 +02:00
|
|
|
var b = '';
|
|
|
|
nodeProcess.stderr.on('data', function(data) {
|
2013-02-26 02:46:48 +01:00
|
|
|
console.error('got stderr data %j', data);
|
|
|
|
nodeProcess.stderr.resume();
|
2011-01-12 01:30:01 +01:00
|
|
|
b += data;
|
2013-10-16 02:54:24 +02:00
|
|
|
if (didTryConnect === false && b.match(/Debugger listening on port/)) {
|
2010-12-22 07:31:30 +01:00
|
|
|
didTryConnect = true;
|
|
|
|
|
2013-10-16 02:54:24 +02:00
|
|
|
// The timeout is here to expose a race in the bootstrap process.
|
|
|
|
// Without the early SIGUSR1 debug handler, it effectively results
|
|
|
|
// in an infinite ECONNREFUSED loop.
|
|
|
|
setTimeout(tryConnect, 100);
|
|
|
|
|
|
|
|
function tryConnect() {
|
2011-01-12 01:30:01 +01:00
|
|
|
// Wait for some data before trying to connect
|
|
|
|
var c = new debug.Client();
|
2013-02-26 02:46:48 +01:00
|
|
|
console.error('>>> connecting...');
|
2011-10-05 00:08:18 +02:00
|
|
|
c.connect(debug.port);
|
2013-02-26 02:46:48 +01:00
|
|
|
c.on('break', function(brk) {
|
|
|
|
c.reqContinue(function() {});
|
|
|
|
});
|
2011-10-05 00:08:18 +02:00
|
|
|
c.on('ready', function() {
|
2011-01-12 01:30:01 +01:00
|
|
|
connectCount++;
|
2011-10-05 00:08:18 +02:00
|
|
|
console.log('ready!');
|
|
|
|
cb(c, function() {
|
2014-01-13 22:16:25 +01:00
|
|
|
c.end();
|
|
|
|
c.on('end', function() {
|
|
|
|
console.error(
|
|
|
|
'>>> killing node process %d\n\n',
|
|
|
|
nodeProcess.pid);
|
|
|
|
nodeProcess.kill();
|
|
|
|
done();
|
|
|
|
});
|
2011-01-12 01:30:01 +01:00
|
|
|
});
|
2010-12-22 23:31:57 +01:00
|
|
|
});
|
2013-10-16 02:54:24 +02:00
|
|
|
c.on('error', function(err) {
|
|
|
|
if (err.code !== 'ECONNREFUSED') throw err;
|
|
|
|
setTimeout(tryConnect, 10);
|
|
|
|
});
|
|
|
|
}
|
2010-12-22 07:31:30 +01:00
|
|
|
}
|
2010-12-22 04:14:29 +01:00
|
|
|
});
|
2010-12-22 07:31:30 +01:00
|
|
|
}
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2010-12-22 07:31:30 +01:00
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
function run() {
|
2010-12-22 23:31:57 +01:00
|
|
|
var t = tests[0];
|
|
|
|
if (!t) return;
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
doTest(t, function() {
|
2010-12-22 23:31:57 +01:00
|
|
|
tests.shift();
|
|
|
|
run();
|
2010-12-22 04:14:29 +01:00
|
|
|
});
|
2010-12-22 23:31:57 +01:00
|
|
|
}
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2010-12-22 23:31:57 +01:00
|
|
|
run();
|
2010-12-22 04:14:29 +01:00
|
|
|
|
2013-02-26 02:46:48 +01:00
|
|
|
process.on('exit', function(code) {
|
|
|
|
if (!code)
|
|
|
|
assert.equal(expectedConnections, connectCount);
|
2010-12-22 04:14:29 +01:00
|
|
|
});
|