Skip to content

Commit 445bcce

Browse files
joyeecheungaduh95
authored andcommitted
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 <expr>` 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 <joyeec9h3@gmail.com> PR-URL: #64328 Refs: #63646 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5d90e48 commit 445bcce

9 files changed

Lines changed: 348 additions & 17 deletions

doc/api/debugger.md

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ debug>
236236
added:
237237
- v24.16.0
238238
changes:
239+
- version: REPLACEME
240+
pr-url: https://github.com/nodejs/node/pull/64328
241+
description: Add per-probe `--cond <expr>` option to only record a hit when the
242+
condition is truthy at the probe location.
239243
- version: v24.19.0
240244
pr-url: https://github.com/nodejs/node/pull/63704
241245
description: Add per-probe `--max-hit <n>` option to limit evaluated hits and finish
@@ -268,8 +272,8 @@ printf-style debugging without having to modify the application code and
268272
clean up afterwards. It also supports structured JSON output for tool use.
269273

270274
```console
271-
$ node inspect --probe <file>:<line>[:<col>] --expr <expr> [--max-hit <n>]
272-
[--probe <file>:<line>[:<col>] --expr <expr> [--max-hit <n>] ...]
275+
$ node inspect --probe <file>:<line>[:<col>] --expr <expr> [--cond <expr>] [--max-hit <n>]
276+
[--probe <file>:<line>[:<col>] --expr <expr> [--cond <expr>] [--max-hit <n>] ...]
273277
[--json] [--preview] [--timeout=<ms>] [--port=<port>]
274278
[--] [<node-option> ...] <script> [<script-args> ...]