node/test/parallel/test-debugger-probe-activation.js
Joyee Cheung a79e224eb2
debugger: add edit-free runtime expression probes to node inspect
Add a non-interactive probe mode to `node inspect` for inspecting
runtime values in a run-to-completion application. This allows
users to perform printf-style debugging without having to modify
the application code and clean up afterwards, it also supports
structured output to be consumed by tools.

Probe mode launches the application, sets one or more source
breakpoints, evaluates one expression at each hit, and prints a
single text or JSON report when execution ends.

Interface:

node inspect [--json] [--preview] [--timeout=<ms>] [--port=<port>]
  --probe <file>:<line>[:<col>] --expr <expr> ...
  [--] <script> [args...]

Example:

```js
// cli.js
let maxRSS = 0;
for (let i = 0; i < 2; i++) {
  const { rss } = process.memoryUsage();
  maxRSS = Math.max(maxRSS, rss);
}
```

```
$ node inspect --probe cli.js:5 --expr 'rss' cli.js
Hit 1 at cli.js:5
  rss = 54935552
Hit 2 at cli.js:5
  rss = 55083008
Completed
```

(Prettified JSON to fit in commit message restrictions).

```js
$ node inspect --json --probe cli.js:5 --expr 'rss' cli.js
{"v":1,"probes":[{"expr":"rss","target":["cli.js",5]}],
"results":[
  {"probe":0,"event":"hit","hit":1,
   "result":{"type":"number","value":55443456,
             "description":"55443456"}},
  {"probe":0,"event":"hit","hit":2,
   "result":{"type":"number","value":55574528,
             "description":"55574528"}},
  {"event":"completed"}]}
```

Signed-off-by: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/62713
Reviewed-By: Jan Martin <jan.krems@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-04-19 15:59:10 +00:00

38 lines
989 B
JavaScript

// This tests that probe mode only activates when --probe is present,
// so that other options can be used as user script arguments without
// accidentally activating probe mode.
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const fixtures = require('../common/fixtures');
const startCLI = require('../common/debugger');
const assert = require('assert');
const script = fixtures.path('debugger', 'three-lines.js');
const cli = startCLI([
script,
'--json',
'--preview',
'--timeout=1',
'--expr',
'value',
]);
(async () => {
try {
await cli.waitForInitialBreak();
await cli.waitForPrompt();
await cli.command('exec JSON.stringify(process.argv.slice(2))');
// Check that it's parsable as usual.
assert.match(
cli.output,
/\["--json","--preview","--timeout=1","--expr","value"\]/,
);
} finally {
const code = await cli.quit();
assert.strictEqual(code, 0);
}
})().then(common.mustCall());