From afbd81866967f2556f2dc5dbf413685bc4004f96 Mon Sep 17 00:00:00 2001 From: Eungyu Lee <35999704+Dailyscat@users.noreply.github.com> Date: Sun, 12 Mar 2023 13:47:18 +0900 Subject: [PATCH] debugger: add a command to set which lines to check for context PR-URL: https://github.com/nodejs/node/pull/46812 Reviewed-By: Kohei Ueno --- lib/internal/debugger/inspect_repl.js | 17 +++++-- test/fixtures/debugger/twenty-lines.js | 20 ++++++++ .../test-debugger-set-context-line-number.mjs | 50 +++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/debugger/twenty-lines.js create mode 100644 test/sequential/test-debugger-set-context-line-number.mjs diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index a1b3f0d705ec4c..91c0da67d7fd99 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -46,7 +46,7 @@ const { const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes; -const { validateString } = require('internal/validators'); +const { validateString, validateNumber } = require('internal/validators'); const FS = require('fs'); const Path = require('path'); @@ -81,7 +81,7 @@ out, o Step out, leaving the current function backtrace, bt Print the current backtrace list Print the source around the current line where execution is currently paused - +setContextLineNumber Set which lines to check for context setBreakpoint, sb Set a breakpoint clearBreakpoint, cb Clear a breakpoint breakpoints List all known breakpoints @@ -381,6 +381,7 @@ function createRepl(inspector) { let currentBacktrace; let selectedFrame; let exitDebugRepl; + let contextLineNumber = 2; function resetOnStart() { knownScripts = {}; @@ -685,6 +686,15 @@ function createRepl(inspector) { }); } + function setContextLineNumber(delta = 2) { + if (!selectedFrame) { + throw new ERR_DEBUGGER_ERROR('Requires execution to be paused'); + } + validateNumber(delta, 'delta', 1); + contextLineNumber = delta; + print(`The contextLine has been changed to ${delta}.`); + } + function handleBreakpointResolved({ breakpointId, location }) { const script = knownScripts[location.scriptId]; const scriptUrl = script && script.url; @@ -897,7 +907,7 @@ function createRepl(inspector) { inspector.suspendReplWhile(() => PromisePrototypeThen( - SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(2)]), + SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(contextLineNumber)]), ({ 0: watcherList, 1: context }) => { const breakContext = watcherList ? `${watcherList}\n${inspect(context)}` : @@ -1159,6 +1169,7 @@ function createRepl(inspector) { }, list, + setContextLineNumber, }); aliasProperties(context, SHORTCUTS); } diff --git a/test/fixtures/debugger/twenty-lines.js b/test/fixtures/debugger/twenty-lines.js new file mode 100644 index 00000000000000..e29e4801baf8be --- /dev/null +++ b/test/fixtures/debugger/twenty-lines.js @@ -0,0 +1,20 @@ +let x = 0; +x = 1; +x = 2; +x = 3; +x = 4; +x = 5; +x = 6; +x = 7; +x = 8; +x = 9; +x = 10; +x = 11; +x = 12; +x = 13; +x = 14; +x = 15; +x = 16; +x = 17; +x = 18; +module.exports = x; diff --git a/test/sequential/test-debugger-set-context-line-number.mjs b/test/sequential/test-debugger-set-context-line-number.mjs new file mode 100644 index 00000000000000..adb6d9ab9e52b0 --- /dev/null +++ b/test/sequential/test-debugger-set-context-line-number.mjs @@ -0,0 +1,50 @@ +import { skipIfInspectorDisabled } from '../common/index.mjs'; +skipIfInspectorDisabled(); + +import { path } from '../common/fixtures.mjs'; +import startCLI from '../common/debugger.js'; + +import assert from 'assert'; + +const script = path('debugger', 'twenty-lines.js'); +const cli = startCLI([script]); + +function onFatal(error) { + cli.quit(); + throw error; +} + +function getLastLine(output) { + const splittedByLine = output.split(';'); + return splittedByLine[splittedByLine.length - 2]; +} + +// Stepping through breakpoints. +try { + await cli.waitForInitialBreak(); + await cli.waitForPrompt(); + + await cli.command('setContextLineNumber("1")'); + assert.ok(cli.output.includes('argument must be of type number. Received type string')); + + await cli.command('setContextLineNumber(0)'); + assert.ok(cli.output.includes('It must be >= 1. Received 0')); + + // Make sure the initial value is 2. + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('4 x = 3')); + + await cli.command('setContextLineNumber(5)'); + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('8 x = 7')); + + await cli.command('setContextLineNumber(3)'); + await cli.stepCommand('n'); + assert.ok(getLastLine(cli.output).includes('7 x = 6')); + await cli.command('list(3)'); + assert.ok(getLastLine(cli.output).includes('7 x = 6')); + + await cli.quit(); +} catch (error) { + onFatal(error); +}