1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
'use strict';
const common = require('../common');
var assert = require('assert');
var debug = require('_debugger');
process.env.NODE_DEBUGGER_TIMEOUT = 2000;
var debugPort = common.PORT;
debug.port = debugPort;
var spawn = require('child_process').spawn;
setTimeout(function() {
if (nodeProcess) nodeProcess.kill('SIGTERM');
throw new Error('timeout');
}, 10000).unref();
var resCount = 0;
var p = new debug.Protocol();
p.onResponse = function() {
resCount++;
};
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');
assert.strictEqual(resCount, 1);
// Make sure split messages go in.
var parts = [];
parts.push('Content-Length: 336\r\n');
assert.strictEqual(parts[0].length, 21);
parts.push('\r\n');
assert.strictEqual(parts[1].length, 2);
var bodyLength = 0;
parts.push('{"seq":12,"type":"event","event":"break","body":' +
'{"invocationText":"#<a Server>');
assert.strictEqual(parts[2].length, 78);
bodyLength += parts[2].length;
parts.push('.[anonymous](req=#<an IncomingMessage>, ' +
'res=#<a ServerResponse>)","sourceLine"');
assert.strictEqual(parts[3].length, 78);
bodyLength += parts[3].length;
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}}}');
assert.strictEqual(parts[4].length, 180);
bodyLength += parts[4].length;
assert.strictEqual(bodyLength, 336);
for (var i = 0; i < parts.length; i++) {
p.execute(parts[i]);
}
assert.strictEqual(resCount, 2);
// 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",' +
'"id":34,"lineOffset":0,"columnOffset":0,"lineCount":241,' +
'"sourceStart":"(function(module, exports, require) {' +
'var dns = process.binding(\'cares\')' +
';\\nvar ne","sourceLength":6137,"scriptType":2,"compilationType":0,' +
'"context":{"ref":0},"text":"dns.js (lines: 241)"}},"refs":' +
'[{"handle":0' +
',"type":"context","text":"#<a ContextMirror>"}],"running":true}' +
'\r\n\r\nContent-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.strictEqual(resCount, 4);
var expectedConnections = 0;
var tests = [];
function addTest(cb) {
expectedConnections++;
tests.push(cb);
}
addTest(function(client, done) {
console.error('requesting version');
client.reqVersion(function(err, v) {
assert.ifError(err);
console.log('version: %s', v);
assert.strictEqual(process.versions.v8, v);
done();
});
});
addTest(function(client, done) {
console.error('requesting scripts');
client.reqScripts(function(err) {
assert.ifError(err);
console.error('got %d scripts', Object.keys(client.scripts).length);
var foundMainScript = false;
for (var k in client.scripts) {
var script = client.scripts[k];
if (script && script.name === 'bootstrap_node.js') {
foundMainScript = true;
break;
}
}
assert.ok(foundMainScript);
done();
});
});
addTest(function(client, done) {
console.error('eval 2+2');
client.reqEval('2+2', function(err, res) {
console.error(res);
assert.ifError(err);
assert.strictEqual(res.text, '4');
assert.strictEqual(res.value, 4);
done();
});
});
var connectCount = 0;
var script = 'setTimeout(function() { console.log("blah"); });' +
'setInterval(function() {}, 1000000);';
var nodeProcess;
function doTest(cb, done) {
var args = ['--debug=' + debugPort, '-e', script];
nodeProcess = spawn(process.execPath, args);
nodeProcess.stdout.once('data', function() {
console.log('>>> new node process: %d', nodeProcess.pid);
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');
}
console.log('>>> starting debugger session');
});
var didTryConnect = false;
nodeProcess.stderr.setEncoding('utf8');
var b = '';
nodeProcess.stderr.on('data', function(data) {
console.error('got stderr data %j', data);
nodeProcess.stderr.resume();
b += data;
if (didTryConnect === false && b.match(/Debugger listening on /)) {
didTryConnect = true;
// 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() {
// Wait for some data before trying to connect
const c = new debug.Client();
console.error('>>> connecting...');
c.connect(debug.port);
c.on('break', function() {
c.reqContinue(function() {});
});
c.on('ready', function() {
connectCount++;
console.log('ready!');
cb(c, function() {
c.end();
c.on('end', function() {
console.error(
'>>> killing node process %d\n\n',
nodeProcess.pid);
nodeProcess.kill();
done();
});
});
});
c.on('error', function(err) {
if (err.code !== 'ECONNREFUSED') throw err;
setTimeout(tryConnect, 10);
});
}
}
});
}
function run() {
const t = tests[0];
if (!t) return;
doTest(t, function() {
tests.shift();
run();
});
}
run();
process.on('exit', function(code) {
if (!code)
assert.strictEqual(connectCount, expectedConnections);
});
|