Files
node/test/parallel/test-debugger-probe-cond-shared.js
Joyee Cheung 8c6cbb8e0a 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: https://github.com/nodejs/node/pull/64328
Refs: https://github.com/nodejs/node/issues/63646
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-07-13 17:00:51 +00:00

62 lines
1.6 KiB
JavaScript

// This tests that two probes at the same location with the same --cond share a
// single breakpoint and both evaluate only when the condition holds.
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const fixtures = require('../common/fixtures');
const { spawnSyncAndAssert } = require('../common/child_process');
const { assertProbeJson } = require('../common/debugger-probe');
const cwd = fixtures.path('debugger');
const probeUrl = fixtures.fileURL('debugger', 'probe-max-hit.js').href;
spawnSyncAndAssert(process.execPath, [
'inspect',
'--json',
'--probe', 'probe-max-hit.js:5',
'--expr', 'index',
'--cond', 'index === 1',
'--probe', 'probe-max-hit.js:5',
'--expr', 'total',
'--cond', 'index === 1',
'probe-max-hit.js',
], { cwd }, {
stdout(output) {
assertProbeJson(output, {
v: 2,
probes: [
{
expr: 'index',
condition: 'index === 1',
target: { suffix: 'probe-max-hit.js', line: 5 },
},
{
expr: 'total',
condition: 'index === 1',
target: { suffix: 'probe-max-hit.js', line: 5 },
},
],
results: [
{
probe: 0,
event: 'hit',
hit: 1,
location: { url: probeUrl, line: 5, column: 3 },
result: { type: 'number', value: 1, description: '1' },
},
{
probe: 1,
event: 'hit',
hit: 1,
location: { url: probeUrl, line: 5, column: 3 },
result: { type: 'number', value: 1, description: '1' },
},
{ event: 'completed' },
],
});
},
trim: true,
});