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 <kohei.ueno119@gmail.com>
This commit is contained in:
Eungyu Lee 2023-03-12 13:47:18 +09:00 committed by GitHub
parent 03fd5ae04d
commit 7f2ab4e629
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 3 deletions

View File

@ -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);
}

20
test/fixtures/debugger/twenty-lines.js vendored Normal file
View File

@ -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;

View File

@ -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);
}