lib: use debugger statement to break scripts in -e --inspect-brk by joyeecheung · Pull Request #24946 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/internal/bootstrap/node.js
35 changes: 34 additions & 1 deletion test/common/inspector-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,39 @@ function fires(promise, error, timeoutMs) {
]);
}

const firstBreakIndex = {
// -e will wrap the source with `debugger;\n${source}`
eval: 0,
// CJS module will be wrapped with `require('module').wrapper`
cjs: -1,
// The following are not wrapped
esm: -1,
vm: -1
};

const firstBreakOffset = {
eval: firstBreakIndex.eval + 1,
cjs: firstBreakIndex.cjs + 1,
esm: firstBreakIndex.esm + 1,
vm: firstBreakIndex.vm + 1,
none: 0
};

function getDebuggerStatementIndices(script, mode = 'none') {
const lines = script.split('\n');
const result = [];
for (let i = 0; i < lines.length; ++i) {
const line = lines[i];
if (/\bdebugger\b/.test(line)) {
result.push(firstBreakOffset[mode] + i);
}
}
return result;
}

module.exports = {
NodeInstance
NodeInstance,
getDebuggerStatementIndices,
firstBreakIndex,
firstBreakOffset
};
14 changes: 11 additions & 3 deletions test/sequential/test-inspector-async-hook-setup-at-inspect-brk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIf32Bits();
const { NodeInstance } = require('../common/inspector-helper.js');
const {
NodeInstance,
getDebuggerStatementIndices,
firstBreakIndex: {
eval: evalDebugBreakIndex
}
} = require('../common/inspector-helper.js');
const assert = require('assert');

const script = `
Expand All @@ -13,14 +19,16 @@ setTimeout(() => {
}, 50);
`;

const [ breakIndex ] = getDebuggerStatementIndices(script, 'eval');

async function skipBreakpointAtStart(session) {
await session.waitForBreakOnLine(3, '[eval]');
await session.waitForBreakOnLine(evalDebugBreakIndex, '[eval]');
await session.send({ 'method': 'Debugger.resume' });
}

async function checkAsyncStackTrace(session) {
console.error('[test]', 'Verify basic properties of asyncStackTrace');
const paused = await session.waitForBreakOnLine(4, '[eval]');
const paused = await session.waitForBreakOnLine(breakIndex, '[eval]');
assert(paused.params.asyncStackTrace,
`${Object.keys(paused.params)} contains "asyncStackTrace" property`);
assert(paused.params.asyncStackTrace.description, 'Timeout');
Expand Down
29 changes: 21 additions & 8 deletions test/sequential/test-inspector-async-stack-traces-promise-then.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIf32Bits();
const { NodeInstance } = require('../common/inspector-helper');
const {
NodeInstance,
getDebuggerStatementIndices,
firstBreakOffset: {
eval: evalDebugBreakOffset
},
firstBreakIndex: {
eval: evalDebugBreakIndex
}
} = require('../common/inspector-helper.js');
const assert = require('assert');

const script = `runTest();
function runTest() {
const fn = `function runTest() {
const p = Promise.resolve();
p.then(function break1() { // lineNumber 3
debugger;
Expand All @@ -17,6 +25,11 @@ function runTest() {
});
}
`;
const script = `${fn}\nrunTest();`;

const fnBreaks = getDebuggerStatementIndices(fn);
const scriptBreak1 = fnBreaks[0] + evalDebugBreakOffset;
const scriptBreak2 = fnBreaks[1] + evalDebugBreakOffset;

async function runTests() {
const instance = new NodeInstance(undefined, script);
Expand All @@ -31,18 +44,18 @@ async function runTests() {
{ 'method': 'Runtime.runIfWaitingForDebugger' }
]);

await session.waitForBreakOnLine(2, '[eval]');
await session.waitForBreakOnLine(evalDebugBreakIndex, '[eval]');
await session.send({ 'method': 'Debugger.resume' });

console.error('[test] Waiting for break1');
debuggerPausedAt(await session.waitForBreakOnLine(6, '[eval]'),
'break1', 'runTest:5');
debuggerPausedAt(await session.waitForBreakOnLine(scriptBreak1, '[eval]'),
'break1', `runTest:${fnBreaks[0]}`);

await session.send({ 'method': 'Debugger.resume' });

console.error('[test] Waiting for break2');
debuggerPausedAt(await session.waitForBreakOnLine(9, '[eval]'),
'break2', 'runTest:8');
debuggerPausedAt(await session.waitForBreakOnLine(scriptBreak2, '[eval]'),
'break2', `runTest:${fnBreaks[1]}`);

await session.runToCompletion();
assert.strictEqual((await instance.expectShutdown()).exitCode, 0);
Expand Down
14 changes: 11 additions & 3 deletions test/sequential/test-inspector-async-stack-traces-set-interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIf32Bits();
const { NodeInstance } = require('../common/inspector-helper');
const {
NodeInstance,
firstBreakIndex: {
eval: evalDebugBreakIndex
},
getDebuggerStatementIndices
} = require('../common/inspector-helper');
const assert = require('assert');

const script = 'setInterval(() => { debugger; }, 50);';

const [ scriptBreak ] = getDebuggerStatementIndices(script, 'eval');

async function skipFirstBreakpoint(session) {
console.log('[test]', 'Skipping the first breakpoint in the eval script');
await session.waitForBreakOnLine(2, '[eval]');
await session.waitForBreakOnLine(evalDebugBreakIndex, '[eval]');
await session.send({ 'method': 'Debugger.resume' });
}

async function checkAsyncStackTrace(session) {
console.error('[test]', 'Verify basic properties of asyncStackTrace');
const paused = await session.waitForBreakOnLine(2, '[eval]');
const paused = await session.waitForBreakOnLine(scriptBreak, '[eval]');
assert(paused.params.asyncStackTrace,
`${Object.keys(paused.params)} contains "asyncStackTrace" property`);
assert(paused.params.asyncStackTrace.description, 'Timeout');
Expand Down
9 changes: 7 additions & 2 deletions test/sequential/test-inspector-break-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ const common = require('../common');
common.skipIfInspectorDisabled();

const assert = require('assert');
const { NodeInstance } = require('../common/inspector-helper.js');
const {
NodeInstance,
firstBreakIndex: {
eval: evalDebugBreakIndex
}
} = require('../common/inspector-helper.js');

async function runTests() {
const instance = new NodeInstance(undefined, 'console.log(10)');
Expand All @@ -14,7 +19,7 @@ async function runTests() {
{ 'method': 'Debugger.enable' },
{ 'method': 'Runtime.runIfWaitingForDebugger' }
]);
await session.waitForBreakOnLine(2, '[eval]');
await session.waitForBreakOnLine(evalDebugBreakIndex, '[eval]');
await session.runToCompletion();
assert.strictEqual((await instance.expectShutdown()).exitCode, 0);
}
Expand Down
26 changes: 18 additions & 8 deletions test/sequential/test-inspector-scriptparsed-context.js