set-breakpoint-on-instrumentation.js 3.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
// Copyright 2022 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.

const {session, contextGroup, Protocol} = InspectorTest.start(
    'Test if breakpoints are hit that are set on instrumentation pause in js');

session.setupScriptMap();
function setBreakpoint(msg, condition) {
  const reason = msg.params.reason;
  if (reason === 'instrumentation') {
    const top_frame = msg.params.callFrames[0];
    const scriptId = top_frame.location.scriptId;
    const columnNumber = top_frame.location.columnNumber;

    InspectorTest.log('Setting breakpoint at instrumentation break location');
    const breakpoint_info = {
      'location': {scriptId, 'lineNumber': 0, columnNumber}
    };
    if (condition) {
      breakpoint_info.condition = condition;
    }
    return Protocol.Debugger.setBreakpoint(breakpoint_info);
  }
  return Promise.resolve();
}

function handlePause(msg) {
  const top_frame = msg.params.callFrames[0];
  const reason = msg.params.reason;
  const url = session.getCallFrameUrl(top_frame);
  InspectorTest.log(`Paused at ${url} with reason "${reason}".`);
  InspectorTest.log(
      `Hit breakpoints: ${JSON.stringify(msg.params.hitBreakpoints)}`)
  return Protocol.Debugger.resume();
};

// Helper function to check if we can successfully set and evaluate breakpoints
// on an instrumentation pause.
async function runSetBreakpointOnInstrumentationTest(condition) {
  await Protocol.Runtime.enable();
  await Protocol.Debugger.enable();

  InspectorTest.log('set breakpoint and evaluate script..');
  await Protocol.Debugger.setInstrumentationBreakpoint(
      {instrumentation: 'beforeScriptExecution'});
  const runPromise =
      Protocol.Runtime.evaluate({expression: '//# sourceURL=foo.js'});

  // First pause: instrumentation
  const msg = await Protocol.Debugger.oncePaused();
  await setBreakpoint(msg, condition);
  await handlePause(msg);

  // Second pause: if condition evaluates to true
  if (!condition || eval(condition)) {
    await handlePause(await Protocol.Debugger.oncePaused());
  }

  InspectorTest.log('Done.');
  await runPromise;
  await Protocol.Runtime.disable();
  await Protocol.Debugger.disable();
}

InspectorTest.runAsyncTestSuite([
  // Test if we can set a breakpoint on the first breakable location (which is
  // the same location as where the instrumentation breakpoint hits) and
  // successfully hit the breakpoint.
  async function testSetBreakpointOnInstrumentationPause() {
    await runSetBreakpointOnInstrumentationTest();
  },

  // Test if we can set a conditional breakpoint on the first breakable location
  // and successfully hit the breakpoint.
  async function
      testSetConditionalBreakpointTrueConditionOnInstrumentationPause() {
        await runSetBreakpointOnInstrumentationTest('4 > 3');
      },

  // Test if we can set a conditional breakpoint on the first breakable location
  // which evaluates to false, and therefore does not trigger a pause.
  async function
      testSetConditionalBreakpointFalseConditionOnInstrumentationPause() {
        await runSetBreakpointOnInstrumentationTest('3 > 4');
      }
]);