Skip to content

inspector: add --cond to node inspect probe mode#64328

Open
joyeecheung wants to merge 1 commit into
nodejs:mainfrom
joyeecheung:probe-cond
Open

inspector: add --cond to node inspect probe mode#64328
joyeecheung wants to merge 1 commit into
nodejs:mainfrom
joyeecheung:probe-cond

Conversation

@joyeecheung

Copy link
Copy Markdown
Member

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:

// 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

Refs: #63646

@nodejs-github-bot nodejs-github-bot added debugger Issues and PRs related to the debugger subsystem. needs-ci PRs that need a full CI run. labels Jul 6, 2026
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>
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment on lines +443 to +444
if (conditionByLocation.has(key)) {
if (conditionByLocation.get(key) !== condition) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is structured into separate has/get checks because undefined is a legitimate value and differentiating between no-value and undefined is necessary? If so, a comment here would be helpful.

Comment thread doc/api/debugger.md
@@ -321,6 +331,8 @@ $ node inspect --probe app.js:10 --expr "user"
--json --preview -- --no-warnings app.js --arg-for-app=foo
```

<!-- TODO(joyeecheung): add more examples for different options -->

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example that shows --cond being used would be good :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debugger Issues and PRs related to the debugger subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants