v8/test/inspector/debugger/set-breakpoint.js
Kim-Anh Tran a7c8a3ea9b [debugger] Consider close-by functions when setting a breakpoint
This changes the behavior of SetBreakpointForScript to find more
accurate break positions.

Previously, setting a breakpoint would only consider the shared
function info that contained the requested position for setting a
breakpoint. More intuitively, a breakpoint should not necessarily
be set in a function that contains the position, but in the closest
breakable location that comes after the position we requested.

To achieve this we:
1. find the shared function info of the inner most function
that contains the requested_position.
This function's end position is used to find other shared function
infos in step 2.

2. search for all shared function infos that intersect with the
range [requested_position, inner_most_function.break_position[.

3. From the shared function infos extracted in 2, find the one
that has the closest breakable location to requested_position.

Also-By: bmeurer@chromium.org
Fixed: chromium:1137141
Change-Id: I4f4c6c3aac1ebea50cbcad9543b539ab1ded2b05
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2742198
Commit-Queue: Kim-Anh Tran <kimanh@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73392}
2021-03-15 07:00:49 +00:00

113 lines
3.9 KiB
JavaScript

// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let {session, contextGroup, Protocol} =
InspectorTest.start('Check different set breakpoint cases.');
contextGroup.addScript(
`function f() {
a=1;
};
function g() {
// Comment.
f();
}
eval('function h(){}');
eval('function sourceUrlFunc() { a = 2; }\\n//# sourceURL=sourceUrlScript');`);
(async function test() {
session.setupScriptMap();
Protocol.Debugger.enable();
{
InspectorTest.log('Set breakpoint at function g..');
const location = await functionLocation('g');
const {result} = await Protocol.Debugger.setBreakpoint({location});
InspectorTest.log('Breakpoint set at:');
await session.logSourceLocation(result.actualLocation);
InspectorTest.log('Call g..');
Protocol.Runtime.evaluate({expression: 'g()'});
const {params:{
callFrames:[topFrame],
hitBreakpoints
}} = await Protocol.Debugger.oncePaused();
InspectorTest.log('Breakpoint hit at:');
await session.logSourceLocation(topFrame.location);
const hitBreakpoint = hitBreakpoints[0] === result.breakpointId;
InspectorTest.log(`hitBreakpoints contains breakpoint: ${hitBreakpoint}\n`);
}
{
InspectorTest.log('Set breakpoint at function f when we on pause..');
const {result} = await Protocol.Debugger.setBreakpoint({
location: await functionLocation('f')
});
InspectorTest.log('Breakpoint set at:');
await session.logSourceLocation(result.actualLocation);
InspectorTest.log('Resume..');
Protocol.Debugger.resume();
const {params:{
callFrames:[topFrame],
hitBreakpoints
}} = await Protocol.Debugger.oncePaused();
InspectorTest.log('Breakpoint hit at:');
await session.logSourceLocation(topFrame.location);
const hitBreakpoint = hitBreakpoints[0] === result.breakpointId;
InspectorTest.log(`hitBreakpoints contains breakpoint: ${hitBreakpoint}\n`);
await Protocol.Debugger.resume();
}
{
InspectorTest.log('Set breakpoint by url at sourceUrlFunc..');
const {lineNumber, columnNumber} = await functionLocation('sourceUrlFunc');
const {result} = await Protocol.Debugger.setBreakpointByUrl({
url: 'sourceUrlScript',
lineNumber,
columnNumber
});
InspectorTest.log('Breakpoint set at:');
await session.logSourceLocation(result.locations[0]);
InspectorTest.log('Call sourceUrlFunc..');
Protocol.Runtime.evaluate({expression: 'sourceUrlFunc()'});
const {params:{
callFrames:[topFrame],
hitBreakpoints
}} = await Protocol.Debugger.oncePaused();
InspectorTest.log('Breakpoint hit at:');
await session.logSourceLocation(topFrame.location);
const hitBreakpoint = hitBreakpoints[0] === result.breakpointId;
InspectorTest.log(`hitBreakpoints contains breakpoint: ${hitBreakpoint}\n`);
await Protocol.Debugger.resume();
}
{
InspectorTest.log(
'Set breakpoint at empty line by url in top level function..');
const {result:{breakpointId}} = await Protocol.Debugger.setBreakpointByUrl({
url: 'test-script',
lineNumber: 4,
columnNumber: 0
});
Protocol.Runtime.evaluate({
expression: `//# sourceURL=test-script\nfunction i1(){};\n\n\n\n\nfunction i2(){}\n// last line`
});
const [{params: {location}}] =
await Promise.all([Protocol.Debugger.onceBreakpointResolved()]);
InspectorTest.log('Breakpoint resolved at:');
await session.logSourceLocation(location);
}
await Protocol.Debugger.disable();
InspectorTest.completeTest();
})();
async function functionLocation(name) {
const {result:{result}} = await Protocol.Runtime.evaluate({expression: name});
const {result:{internalProperties}} = await Protocol.Runtime.getProperties({
objectId: result.objectId
});
const {value:{value}} = internalProperties.find(
prop => prop.name === '[[FunctionLocation]]');
return value;
}