Files
node/test/sequential/test-debugger-probe-attach-pid.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

55 lines
2.1 KiB
JavaScript

// Tests that when a probe session starts by attaching to a process with --pid,
// which activates inspector in the target process, should stop the probe session after
// reaching --max-hit.
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const fixtures = require('../common/fixtures');
const { runProbe, startTarget } = require('../common/debugger-probe');
async function main() {
const target = await startTarget('probe-attach-loop.js', { args: [`--inspect-port=${common.PORT}`] });
const { pid } = target.child;
const attachArgs = ['--pid', `${pid}`, '--port', `${common.PORT}`];
await runProbe(['--probe', 'probe-attach-loop.js:6', '--expr', 'typeof count', '--max-hit', '1', ...attachArgs], {
target,
status: 0,
report: {
v: 2,
target: { pid, host: '127.0.0.1', port: common.PORT },
probes: [{ expr: 'typeof count', target: { suffix: 'probe-attach-loop.js', line: 6 }, maxHit: 1 }],
results: [{
probe: 0, event: 'hit', hit: 1, result: { type: 'string', value: 'number' },
location: { url: fixtures.fileURL('debugger', 'probe-attach-loop.js').href, line: 6, column: 3 },
}, { event: 'completed' }],
},
});
// The target should be left running after --max-hit is reached and it is detached.
// We should be able to attach a second session to it. Killing the target at this point
// should result in a probe_target_exit with exit code 0
// 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: 0,
report: {
v: 2,
probes: [{ expr: '1', target: { suffix: 'probe-attach-loop.js', line: 10 } }],
results: [{
event: 'error',
pending: [0],
error: {
code: 'probe_target_exit',
message: 'Target exited before probes: probe-attach-loop.js:10',
},
}],
},
});
await target.exited;
}
main().then(common.mustCall());