From c2881e9056a0258e69600e6c5048c206a6cccdf0 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 23 Sep 2026 00:24:22 +0200 Subject: [PATCH] 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 ` activates the target's inspector (if it's not already activated) and connects to it. - `--host ` and `--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 --- doc/api/debugger.md | 145 +++++++- lib/internal/debugger/inspect.js | 10 +- lib/internal/debugger/inspect_client.js | 3 + lib/internal/debugger/inspect_helpers.js | 55 ++- lib/internal/debugger/inspect_probe.js | 344 +++++++++++++++--- test/common/child_process.js | 1 + test/common/debugger-probe.js | 140 ++++++- .../fixtures/debugger/probe-attach-blocked.js | 5 + test/fixtures/debugger/probe-attach-busy.js | 10 + .../debugger/probe-attach-finishes.js | 11 + test/fixtures/debugger/probe-attach-loop.js | 10 + .../test-debugger-probe-attach-invalid.js | 17 + .../test-debugger-probe-attach-pause-early.js | 28 ++ .../test-debugger-probe-attach-refused.js | 39 ++ ...t-debugger-probe-attach-startup-timeout.js | 28 ++ ...-debugger-probe-attach-target-completes.js | 38 ++ ...st-debugger-probe-attach-target-crashes.js | 61 ++++ .../test-debugger-probe-attach-timeout.js | 37 ++ test/parallel/test-debugger-probe-attach.js | 56 +++ .../test-debugger-probe-bound-never-hit.js | 3 + test/parallel/test-debugger-probe-json.js | 9 +- .../test-debugger-probe-late-resolution.js | 12 +- test/parallel/test-debugger-probe-miss.js | 4 +- .../test-debugger-probe-startup-disconnect.js | 3 +- .../test-debugger-probe-attach-pid.js | 54 +++ 25 files changed, 1025 insertions(+), 98 deletions(-) create mode 100644 test/fixtures/debugger/probe-attach-blocked.js create mode 100644 test/fixtures/debugger/probe-attach-busy.js create mode 100644 test/fixtures/debugger/probe-attach-finishes.js create mode 100644 test/fixtures/debugger/probe-attach-loop.js create mode 100644 test/parallel/test-debugger-probe-attach-invalid.js create mode 100644 test/parallel/test-debugger-probe-attach-pause-early.js create mode 100644 test/parallel/test-debugger-probe-attach-refused.js create mode 100644 test/parallel/test-debugger-probe-attach-startup-timeout.js create mode 100644 test/parallel/test-debugger-probe-attach-target-completes.js create mode 100644 test/parallel/test-debugger-probe-attach-target-crashes.js create mode 100644 test/parallel/test-debugger-probe-attach-timeout.js create mode 100644 test/parallel/test-debugger-probe-attach.js create mode 100644 test/sequential/test-debugger-probe-attach-pid.js diff --git a/doc/api/debugger.md b/doc/api/debugger.md index c7490af82924..04db8523ed73 100644 --- a/doc/api/debugger.md +++ b/doc/api/debugger.md @@ -237,6 +237,11 @@ added: - v26.1.0 - v24.16.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/67201 + description: Add `--pid ` and `--host `/`--port ` to probe a process that's + already running. The report now includes a top-level `target` field to describe what was + probed, and a `locations` field showing which probe is bound to which location. - version: - v26.6.0 - v24.20.0 @@ -270,8 +275,10 @@ changes: `node inspect` supports a non-interactive probe mode for inspecting runtime values in an application via the flag `--probe`. -Currently, probe mode only supports launching a new process from the entry point -script specified on the command line. +There are two ways to use probe mode: + +* Launch a new process and probe it by specifying the entry point script. +* Attach to and probe a running process using `--pid ` or `--host `/`--port `. The probe mode sets one or more source breakpoints, evaluates specified expressions whenever the execution reaches a breakpoint, and prints one @@ -281,10 +288,18 @@ printf-style debugging without having to modify the application code and clean up afterwards. It also supports structured JSON output for tool use. ```console +# To launch a new process and probe it: $ node inspect --probe :[:] --expr [--cond ] [--max-hit ] [--probe :[:] --expr [--cond ] [--max-hit ] ...] [--json] [--preview] [--timeout=] [--port=] [--] [ ...]