-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcommon.js
351 lines (289 loc) · 9.36 KB
/
common.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const util = require('util');
const spawn = require('child_process').spawn;
const EventEmitter = require('events').EventEmitter;
exports.fixturesDir = path.join(__dirname, 'fixtures');
exports.projectDir = path.join(__dirname, '..');
exports.core = path.join(os.tmpdir(), 'core');
exports.promptDelay = 200;
function llnodeDebug() {
// Node v4.x does not support rest
const args = Array.prototype.slice.call(arguments);
console.error.apply(console, [`[TEST][${process.pid}]`].concat(args));
}
const debug = exports.debug =
process.env.TEST_LLNODE_DEBUG ? llnodeDebug : () => { };
let pluginName;
if (process.platform === 'darwin')
pluginName = 'llnode.dylib';
else if (process.platform === 'win32')
pluginName = 'llnode.dll';
else
pluginName = 'llnode.so';
exports.llnodePath = path.join(exports.projectDir, pluginName);
exports.saveCoreTimeout = 360 * 1000;
exports.loadCoreTimeout = 60 * 1000;
let versionMark = /^lldb-|^lldb version/;
exports.versionMark = versionMark;
function SessionOutput(session, stream, timeout) {
EventEmitter.call(this);
this.waiting = false;
this.waitQueue = [];
let buf = '';
this.timeout = timeout || 40000;
this.session = session;
this.flush = function flush() {
for (;;) {
// NOTE(mmarchini): don't emit line events while not waiting, otherwise
// we might miss something.
if (!this.waiting)
break
let line = '';
let index = 0;
let inv = buf.indexOf('invalid_expr');
if (inv !== -1) {
line = buf;
index = buf.length;
} else {
index = buf.indexOf('\n');
if (index === -1)
break;
line = buf.slice(0, index);
}
buf = buf.slice(index + 1);
if (/process \d+ exited/i.test(line))
session.kill();
else
this.emit('line', line);
}
}
stream.on('data', (data) => {
buf += data;
this.flush();
});
// Ignore errors
stream.on('error', (err) => {
debug('[stream error]', err);
});
}
util.inherits(SessionOutput, EventEmitter);
SessionOutput.prototype._queueWait = function _queueWait(retry) {
if (this.waiting) {
this.waitQueue.push(retry);
return false;
}
this.waiting = true;
return true;
};
SessionOutput.prototype._unqueueWait = function _unqueueWait() {
this.waiting = false;
if (this.waitQueue.length > 0)
this.waitQueue.shift()();
};
SessionOutput.prototype.timeoutAfter = function timeoutAfter(timeout) {
this.timeout = timeout;
};
SessionOutput.prototype.wait = function wait(regexp, callback, allLines) {
if (!this._queueWait(() => { this.wait(regexp, callback, allLines); }))
return;
const self = this;
const lines = [];
function onLine(line) {
lines.push(line);
if (self.session)
debug(`[LINE][${self.session.lldb.pid}]`, line);
if (!regexp.test(line))
return;
self.removeListener('line', onLine);
self._unqueueWait();
done = true;
callback(null, allLines ? lines : line);
}
let done = false;
const check = setTimeout(() => {
if (done)
return;
self.removeListener('line', onLine);
self._unqueueWait();
console.error(`${'='.repeat(10)} lldb output ${'='.repeat(10)}`);
console.error(lines.join('\n'));
console.error('='.repeat(33));
const message = `Test timeout in ${this.timeout} ` +
`waiting for ${regexp}`;
callback(new Error(message));
}, this.timeout).unref();
this.on('line', onLine);
this.flush();
};
SessionOutput.prototype.waitBreak = function waitBreak(callback) {
this.wait(/Process \d+ stopped/i, (err) => {
if (err)
return callback(err);
// Do not resume immediately since the process would print
// the instructions out and input sent before the stdout finish
// could be lost
setTimeout(callback, exports.promptDelay);
});
};
SessionOutput.prototype.linesUntil = function linesUntil(regexp, callback) {
this.wait(regexp, callback, true);
};
function Session(options) {
EventEmitter.call(this);
const timeout = parseInt(process.env.TEST_TIMEOUT) || 40000;
const lldbBin = process.env.TEST_LLDB_BINARY || 'lldb';
const env = Object.assign({}, process.env);
debug('lldb binary:', lldbBin);
if (options.scenario) {
this.needToKill = true; // need to send 'kill' when quitting
// lldb -- node scenario.js
const args = [
'--',
process.execPath,
'--abort_on_uncaught_exception',
'--expose_externalize_string',
path.join(exports.fixturesDir, options.scenario)
];
debug('lldb args:', args);
this.lldb = spawn(lldbBin, args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: env
});
this.lldb.stdin.write(`plugin load "${exports.llnodePath}"\n`);
this.lldb.stdin.write('run\n');
} else if (options.core) {
this.needToKill = false; // need to send 'target delete 0' when quitting
debug('loading core', options.core);
// lldb node -c core
this.lldb = spawn(lldbBin, [], {
stdio: ['pipe', 'pipe', 'pipe'],
env: env
});
this.lldb.stdin.write(`plugin load "${exports.llnodePath}"\n`);
this.lldb.stdin.write(`target create "${options.executable}"` +
` --core "${options.core}"\n`);
}
this.stdout = new SessionOutput(this, this.lldb.stdout, timeout);
this.stderr = new SessionOutput(this, this.lldb.stderr, timeout);
// Map these methods to stdout for compatibility with legacy tests.
this.wait = SessionOutput.prototype.wait.bind(this.stdout);
this.waitError = SessionOutput.prototype.wait.bind(this.stderr);
this.waitBreak = SessionOutput.prototype.waitBreak.bind(this.stdout);
this.linesUntil = SessionOutput.prototype.linesUntil.bind(this.stdout);
this.timeoutAfter = SessionOutput.prototype.timeoutAfter.bind(this.stdout);
}
util.inherits(Session, EventEmitter);
exports.Session = Session;
Session.create = function create(scenario) {
return new Session({ scenario: scenario });
};
function saveCoreLLDB(scenario, core, cb) {
// Create a core and test
const sess = Session.create(scenario);
sess.timeoutAfter(exports.saveCoreTimeout);
sess.waitBreak(() => {
sess.send(`process save-core ${core}`);
// Just a separator
sess.send('version');
});
sess.wait(/lldb-/, (err) => {
if (err) {
return cb(err);
}
sess.quit();
cb(null);
});
}
function spawnWithTimeout(cmd, cb) {
const proc = spawn(cmd, {
shell: true,
stdio: ['pipe', 'pipe', 'pipe'] });
const stdout = new SessionOutput(null, proc.stdout, exports.saveCoreTimeout);
const stderr = new SessionOutput(null, proc.stderr, exports.saveCoreTimeout);
stdout.on('line', (line) => { debug('[stdout]', line); });
stderr.on('line', (line) => { debug('[stderr]', line); });
const timeout = setTimeout(() => {
console.error(`timeout while saving core dump for scenario "${scenario}"`);
proc.kill();
}, exports.saveCoreTimeout);
proc.on('exit', (status) => {
clearTimeout(timeout);
cb(null);
});
}
function saveCoreLinux(executable, scenario, core, cb) {
// TODO(mmarchini): Let users choose if they want system generated core dumps
// or gdb generated core dumps.
const cmd = `gdb ${executable} --batch -ex 'run ` +
`--abort-on-uncaught-exception --expose-externalize-string ` +
`${path.join(exports.fixturesDir, scenario)}' -ex 'generate-core-file ` +
`${core}'`;
spawnWithTimeout(cmd, () => {
cb()
});
}
exports.saveCore = function saveCore(options, cb) {
const scenario = options.scenario;
const core = options.core || exports.core;
if (process.platform === 'linux') {
saveCoreLinux(process.execPath, scenario, core, cb);
} else {
saveCoreLLDB(scenario, core, cb);
}
}
// Load the core dump with the executable
Session.loadCore = function loadCore(executable, core, cb) {
const sess = new Session({
executable: executable,
core: core
});
sess.timeoutAfter(exports.loadCoreTimeout);
sess.waitCoreLoad(cb);
return sess;
};
Session.prototype.waitCoreLoad = function waitCoreLoad(callback) {
this.wait(/Core file[^\n]+was loaded/, callback);
};
Session.prototype.kill = function kill() {
// if a 'quit' has been sent to lldb, killing it could result in ECONNRESET
if (this.lldb.channel) {
debug('kill lldb');
this.lldb.kill();
this.lldb = null;
}
};
Session.prototype.quit = function quit() {
if (this.needToKill) {
this.send('kill'); // kill the process launched in lldb
} else {
this.send('target delete 0'); // Delete the loaded core dump
}
this.send('quit');
};
Session.prototype.send = function send(line, callback) {
debug(`[SEND][${this.lldb.pid}]`, line);
this.lldb.stdin.write(line + '\n', callback);
};
Session.prototype.hasSymbol = function hasSymbol(symbol, callback) {
this.send('target modules dump symtab');
this.send('version');
let pattern = new RegExp(symbol);
this.linesUntil(versionMark, (err, lines) => {
if(pattern.test(lines.join('\n'))) {
callback(err, true);
} else {
return callback(err, false);
}
});
};
function nodejsVersion() {
const candidateIndex = process.version.indexOf('-');
const endIndex = candidateIndex != -1 ? candidateIndex : process.version.length;
const version = process.version.substring(1, endIndex);
const versionArray = version.split('.').map(s => Number(s));
return versionArray;
}
exports.nodejsVersion = nodejsVersion;