mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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>
40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
// Tests attach failures result in an error instead of a report. Without --pid the target
|
|
// should already be listening, so a failed connection should be an immediate error.
|
|
// An inspector that belongs to another process should be rejected.
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
common.skipIfInspectorDisabled();
|
|
|
|
const net = require('net');
|
|
const { once } = require('events');
|
|
const { runProbe, startTarget } = require('../common/debugger-probe');
|
|
|
|
async function main() {
|
|
// Create a port that will refuse connections.
|
|
const server = net.createServer((socket) => socket.resetAndDestroy());
|
|
await once(server.listen(0, '127.0.0.1'), 'listening');
|
|
const { port } = server.address();
|
|
|
|
await runProbe(['--probe', 'app.js:1', '--expr', '1', '--port', `${port}`], {
|
|
status: 1,
|
|
report: null,
|
|
stderr: new RegExp(`Could not attach to 127\\.0\\.0\\.1:${port}: `),
|
|
});
|
|
server.close();
|
|
|
|
const other = await startTarget('probe-attach-loop.js');
|
|
const target = await startTarget('probe-attach-loop.js', { args: ['--inspect-port=0'] });
|
|
await runProbe(['--probe', 'app.js:1', '--expr', '1', '--pid', `${target.child.pid}`, '--port', `${other.port}`], {
|
|
target,
|
|
status: 1,
|
|
report: null,
|
|
stderr: new RegExp(`The inspector at 127\\.0\\.0\\.1:${other.port} belongs to process ${other.child.pid}, ` +
|
|
`not to --pid ${target.child.pid}\\.`),
|
|
});
|
|
other.child.kill();
|
|
target.child.kill();
|
|
}
|
|
|
|
main().then(common.mustCall());
|