From db579389977a5720975fec22abcb325d7a4023e2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 3 Jul 2026 18:07:32 +0200 Subject: [PATCH] inspector: add --cond to node inspect probe mode On a hot path, the probe can record every hit and require filtering afterwards. This patch adds a per-probe `--cond ` option that allows limiting the hit to only when the expression is truthy at the probe location. V8 evaluates it as the breakpoint's native condition, so the target is not paused when it does not hold, and a condition that throws is treated as false. Since in CDP, a location can only carry one breakpoint per URL pattern, probes sharing a location must share one condition (or none). Conflicting conditions are rejected. Example: ```js // app.js let total = 0; for (let i = 0; i < 10; i++) { total += i; // line 4 } ``` ``` $ out/Release/node inspect --probe app.js:4 --expr 'total' \ --cond 'i % 3 === 0' app.js ``` ``` Hit 1 at file:///path/to/app.js:3:3 total = 0 Hit 2 at file:///path/to/app.js:3:3 total = 3 Hit 3 at file:///path/to/app.js:3:3 total = 15 Hit 4 at file:///path/to/app.js:3:3 total = 36 Completed ``` Signed-off-by: Joyee Cheung --- doc/api/debugger.md | 31 +++++++--- lib/internal/debugger/inspect.js | 1 + lib/internal/debugger/inspect_helpers.js | 10 ++- lib/internal/debugger/inspect_probe.js | 62 ++++++++++++++++--- .../test-debugger-probe-cond-invalid.js | 40 ++++++++++++ .../test-debugger-probe-cond-max-hit.js | 47 ++++++++++++++ .../test-debugger-probe-cond-shared.js | 61 ++++++++++++++++++ .../test-debugger-probe-cond-throws.js | 37 +++++++++++ test/parallel/test-debugger-probe-cond.js | 44 +++++++++++++ 9 files changed, 316 insertions(+), 17 deletions(-) create mode 100644 test/parallel/test-debugger-probe-cond-invalid.js create mode 100644 test/parallel/test-debugger-probe-cond-max-hit.js create mode 100644 test/parallel/test-debugger-probe-cond-shared.js create mode 100644 test/parallel/test-debugger-probe-cond-throws.js create mode 100644 test/parallel/test-debugger-probe-cond.js diff --git a/doc/api/debugger.md b/doc/api/debugger.md index 6544e01238ce61..912b08f236c741 100644 --- a/doc/api/debugger.md +++ b/doc/api/debugger.md @@ -237,6 +237,10 @@ added: - v26.1.0 - v24.16.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/64328 + description: Add per-probe `--cond ` option to only record a hit when the + condition is truthy at the probe location. - version: v26.4.0 pr-url: https://github.com/nodejs/node/pull/63704 description: Add per-probe `--max-hit ` option to limit evaluated hits and finish @@ -269,8 +273,8 @@ printf-style debugging without having to modify the application code and clean up afterwards. It also supports structured JSON output for tool use. ```console -$ node inspect --probe :[:] --expr [--max-hit ] - [--probe :[:] --expr [--max-hit ] ...] +$ node inspect --probe :[:] --expr [--cond ] [--max-hit ] + [--probe :[:] --expr [--cond ] [--max-hit ] ...] [--json] [--preview] [--timeout=] [--port=] [--] [ ...]