Files
node/test/parallel/test-debugger-probe-attach.js
Joyee Cheung 1c40cface6 inspector: support attaching to a running process in probe mode
Probe mode currently only launches a new process from the entry point
script. This change lets it attach to an already-running process, so a
long-lived application can be inspected without restarting it.

Running `node inspect --probe` without specifying an entry point script
instructs Node.js to attach to a running Node.js process and probe.
The way attaching is done is specified through command line arguments:

- `--pid <pid>` activates the target's inspector (if it's not already
  activated) and connects to it.
- `--host <host>` and `--port <port>` specify an address where an
  inspector is listening. The host defaults to 127.0.0.1 and the
  port defaults to 9229.

When the session ends, probe mode resumes and detaches instead of
killing a process it does not own. The report now includes a top-level
`target` field describing what was probed, and each probe contains a
`locations` field about where it bound.

An attach session ends when a probe reaches `--max-hit`
(reported as `completed`), when the target finishes on its
own (`completed`, or `miss` with the pending probes, and the target is
then allowed to exit), or when `--timeout` fires (reported as
`timeout`, the probing process exit with 0 and reports the hits recorded
so far).

`--timeout` now is also applied to the startup phase in both modes, so
a target that never responds before the breakpoints are bound is
reported as an error instead of hanging.

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67201
Reviewed-By: Jan Martin <jan.krems@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-11-17 09:51:42 +00:00

57 lines
2.2 KiB
JavaScript

// Tests a --host/--port attach session that hits a probe in a script loaded before it attached,
// and ends at --max-hit. The target should continue running, so a second session can still attach
// to it. Killing the target during that session is reported as a probe failure, because without
// --pid an exit cannot be distinguished from a network failure.
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const fixtures = require('../common/fixtures');
const { runProbe, startTarget } = require('../common/debugger-probe');
const url = fixtures.fileURL('debugger', 'probe-attach-loop.js').href;
const location = { url, line: 6, column: 3 };
const hit = (n) => ({ probe: 0, event: 'hit', hit: n, location, result: { type: 'string', value: 'number' } });
async function main() {
const target = await startTarget('probe-attach-loop.js');
const attachArgs = ['--host', target.host, '--port', `${target.port}`];
await runProbe(['--probe', 'probe-attach-loop.js:6', '--expr', 'typeof count', '--max-hit', '2', ...attachArgs], {
target,
status: 0,
report: {
v: 2,
target: { host: target.host, port: target.port },
probes: [{
expr: 'typeof count', target: { suffix: 'probe-attach-loop.js', line: 6 }, maxHit: 2, locations: [location],
}],
results: [hit(1), hit(2), { event: 'completed' }],
},
});
// Line 10 only runs at startup, so this session should wait until the target is killed.
await runProbe(['--probe', 'probe-attach-loop.js:10', '--expr', '1', ...attachArgs], {
target,
onStarted: () => target.child.kill('SIGKILL'),
status: 1,
report: {
v: 2,
probes: [{
expr: '1', target: { suffix: 'probe-attach-loop.js', line: 10 }, locations: [{ url, line: 10, column: 1 }],
}],
results: [{
event: 'error',
pending: [0],
error: {
code: 'probe_failure',
message: 'Inspector connection lost before probes: probe-attach-loop.js:10. The target was likely ' +
"terminated externally. If the failure persists, check the target's process environment.",
},
}],
},
});
await target.exited;
}
main().then(common.mustCall());