Repository navigation
Expand file tree
/
Copy pathdebugger-probe.js
More file actions
247 lines (227 loc) Β· 9.58 KB
/
Copy pathdebugger-probe.js
File metadata and controls
247 lines (227 loc) Β· 9.58 KB
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
'use strict';
const assert = require('assert');
const { spawn } = require('child_process');
const { once } = require('events');
const { checkOutput, spawnSyncAndExit } = require('./child_process');
const fixtures = require('./fixtures');
const kListeningRegex = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\//;
const kReadyRegex = /^PROBE_READY$/m;
// Logged by the probe session when NODE_DEBUG=inspect_probe once the probes are bound.
const kStartedLine = 'CDP <- Runtime.runIfWaitingForDebugger';
function logProcessOutput(tag, { child, stdout, stderr }) {
console.error(`${tag} --- stderr ---`);
console.error(stderr);
console.error(`${tag} --- stdout ---`);
console.error(stdout);
console.error(`${tag} status = ${child.exitCode}, signal = ${child.signalCode}`);
}
// Starts test/fixtures/debugger/<name> with the Node.js with args and env.
// Resolves once it prints `PROBE_READY`, the returned target will continue collecting
// the output of the process, so that it can be printed when a probe session fails.
function startTarget(name, { args = ['--inspect=127.0.0.1:0'], env } = {}) {
const child = spawn(process.execPath, [...args, fixtures.path('debugger', name)],
{ env: { ...process.env, ...env } });
// Only targets with the inspector activated would print the listening address at startup.
const expectListening = args.some((arg) => arg === '--inspect' || arg.startsWith('--inspect='));
// Prevent a failed assertion from leaving a long-running target alive.
process.on('exit', () => child.kill());
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
const target = {
child, stdout: '', stderr: '',
exited: once(child, 'exit').then(([code, signal]) => ({ code, signal })),
};
return new Promise((resolve, reject) => {
let ready = false;
function check() {
if (ready || !kReadyRegex.test(target.stdout)) { return; }
// PROBE_READY message may appear before the listening line on stderr.
// Keep waiting until the listening line is found on stderr.
const match = kListeningRegex.exec(target.stderr);
if (expectListening && match === null) { return; }
ready = true;
target.host = match?.[1];
target.port = match && Number(match[2]);
child.removeListener('exit', onExit);
resolve(target);
}
function onExit() {
logProcessOutput(`[target ${child.pid}]:`, target);
reject(new Error(`Target ${name} exited before it was ready`));
}
child.stdout.on('data', (chunk) => { target.stdout += chunk; check(); });
child.stderr.on('data', (chunk) => { target.stderr += chunk; check(); });
child.once('exit', onExit);
});
}
// Runs `node inspect --json <args>` and checks the passed expectations. On failure,
// print the output of the probe process and of `target` (if given).
// `onStarted` is called once the probes are bound.
async function runProbe(args, { target, onStarted, status, report: expectedReport, stderr: stderrCheck }) {
const argv = ['inspect', '--json', ...args];
const child = spawn(process.execPath, argv, { env: { ...process.env, NODE_DEBUG: 'inspect_probe' } });
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
const probe = { child, stdout: '', stderr: '' };
child.stdout.on('data', (chunk) => { probe.stdout += chunk; });
child.stderr.on('data', (chunk) => {
probe.stderr += chunk;
if (onStarted !== undefined && probe.stderr.includes(kStartedLine)) {
onStarted(target);
onStarted = undefined;
}
});
// Unlike 'exit', 'close' is emitted after stdout and stderr are fully read.
const [code] = await once(child, 'close');
const failures = [];
if (code !== status) {
failures.push(`- probe process exited with status ${code}, expected ${status}`);
}
let report = null;
if (probe.stdout !== '') {
try {
report = JSON.parse(probe.stdout);
} catch {
failures.push('- stdout is not a JSON report');
}
}
if (expectedReport === null) {
if (report !== null) { failures.push('- expected no report'); }
} else if (report === null) {
failures.push('- no report was printed');
} else {
try {
assertProbeJson(report, expectedReport);
} catch (error) {
failures.push(`- report did not match:\n${error.message}`);
}
}
if (stderrCheck !== undefined) {
const { passed, reason } = checkOutput(probe.stderr, stderrCheck);
if (!passed) { failures.push(`- stderr ${reason}`); }
}
if (failures.length === 0) { return { report, stderr: probe.stderr }; }
logProcessOutput(`[probe ${child.pid}]:`, probe);
if (target !== undefined) { logProcessOutput(`[target ${target.child.pid}]:`, target); }
const error = new Error(failures.join('\n'));
error.command = `${process.execPath} ${argv.join(' ')}`;
throw error;
}
// Starts test/fixtures/debugger/<name>, runs `node inspect` against its --host and --port,
// check the expectations, and kills the process afterwards.
async function runAttachedProbe(name, probeArgs, { target: targetOptions, ...options } = {}) {
const target = await startTarget(name, targetOptions);
const result = await runProbe([...probeArgs, '--host', target.host, '--port', `${target.port}`],
{ ...options, target });
target.child.kill();
await target.exited;
return result;
}
// Work around a pre-existing inspector issue: if the debuggee exits too quickly
// the inspector can crash while tearing down. For now normalize the crash
// back to the expected terminal event (e.g. "completed" or "miss")
// until the upstream bug is fixed.
// See https://github.com/nodejs/node/issues/62765
// https://github.com/nodejs/node/issues/58245
// The crash shows up as with exit code 3221225477 on Windows, or signal
// SIGSEGV on other platforms.
const probeTargetExitSignal = 'SIGSEGV';
// 0xC0000005 STATUS_ACCESS_VIOLATION on Windows
const probeTargetExitCode = 3221225477;
function isProbeSegvTeardown(result) {
if (result?.event !== 'error') { return false; }
const error = result.error;
if (error?.signal !== probeTargetExitSignal && error?.exitCode !== probeTargetExitCode) { return false; }
return error.code === 'probe_target_exit' || error.code === 'probe_failure';
}
function findProbeSegvTeardownLine(output) {
const signalPrefix = `Target exited with signal ${probeTargetExitSignal}`;
const codePrefix = `Target exited with code ${probeTargetExitCode}`;
for (const prefix of [signalPrefix, codePrefix]) {
if (output.startsWith(prefix)) { return 0; }
const idx = output.indexOf(`\n${prefix}`);
if (idx !== -1) { return idx + 1; }
}
return -1;
}
// Replace volatile fields in a probe report (stack frames, Node.js version,
// scriptId, callFrames) with stable placeholders for deepStrictEqual.
function normalizeProbeReport(value) {
if (typeof value === 'string') {
return value
.replace(/(?:\n[ \t]+at\s[^\n]*)+/g, '\n<stack>')
.replace(/\nNode\.js v[^\n]+/g, '\nNode.js <version>');
}
if (Array.isArray(value)) {
return value.map(normalizeProbeReport);
}
if (value !== null && typeof value === 'object') {
const result = {};
for (const key of Object.keys(value)) {
if (key === 'scriptId') {
result[key] = '<scriptId>';
} else if (key === 'callFrames') {
result[key] = '<callFrames>';
} else {
result[key] = normalizeProbeReport(value[key]);
}
}
return result;
}
return value;
}
// If expected omits `target` or probe `locations`, assertProbeJson only checks for their presence
// but not the exact values.
function assertProbeJson(output, expected) {
const normalized = typeof output === 'string' ? JSON.parse(output) : output;
const lastResult = normalized.results?.[normalized.results.length - 1];
if (isProbeSegvTeardown(lastResult)) {
// Log to facilitate debugging if this normalization is occurring.
console.log('Normalizing trailing SIGSEGV in JSON probe output');
normalized.results[normalized.results.length - 1] = expected.results.at(-1);
}
if (!Object.hasOwn(expected, 'target')) {
assert.notStrictEqual(normalized.target, undefined);
delete normalized.target;
}
if (Array.isArray(normalized.probes) && Array.isArray(expected.probes)) {
for (let i = 0; i < normalized.probes.length; i++) {
if (expected.probes[i] !== undefined && !Object.hasOwn(expected.probes[i], 'locations')) {
assert.strictEqual(Array.isArray(normalized.probes[i].locations), true,
`probes[${i}].locations must be an array`);
delete normalized.probes[i].locations;
}
}
}
assert.deepStrictEqual(normalizeProbeReport(normalized), normalizeProbeReport(expected));
}
function assertProbeText(output, expected) {
const lineStart = findProbeSegvTeardownLine(output);
let normalized;
if (lineStart !== -1) {
// Log to facilitate debugging if this normalization is occurring.
console.log('Normalizing trailing SIGSEGV in text probe output');
const expectedLineStart = expected.lastIndexOf('\n');
const terminalLine = expectedLineStart === -1 ? expected : expected.slice(expectedLineStart + 1);
normalized = lineStart === 0 ? terminalLine : `${output.slice(0, lineStart - 1)}\n${terminalLine}`;
} else {
normalized = output;
}
assert.strictEqual(normalized, expected);
}
function assertProbeCliError(inspectArgs, expectedStderr, { cwd } = {}) {
spawnSyncAndExit(process.execPath, ['inspect', ...inspectArgs], { cwd }, {
signal: null,
status: 9,
stderr: expectedStderr,
trim: true,
});
}
module.exports = {
assertProbeJson,
assertProbeCliError,
assertProbeText,
runAttachedProbe,
runProbe,
startTarget,
};