Skip to content

Commit

Permalink
debugger: add a command to set which lines to check for context
Browse files Browse the repository at this point in the history
PR-URL: #46812
Reviewed-By: Kohei Ueno <[email protected]>
  • Loading branch information
Dailyscat authored and danielleadams committed Apr 11, 2023
1 parent ba340a0 commit 51253ba
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -381,6 +381,7 @@ function createRepl(inspector) {
let currentBacktrace;
let selectedFrame;
let exitDebugRepl;
let contextLineNumber = 2;

function resetOnStart() {
knownScripts = {};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)}` :
Expand Down Expand Up @@ -1159,6 +1169,7 @@ function createRepl(inspector) {
},

list,
setContextLineNumber,
});
aliasProperties(context, SHORTCUTS);
}
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/debugger/twenty-lines.js
Original file line number Diff line number Diff line change
@@ -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;
50 changes: 50 additions & 0 deletions test/sequential/test-debugger-set-context-line-number.mjs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 51253ba

Please sign in to comment.