wasm-stepping-byte-offsets.js 4.38 KB
Newer Older
1
// Copyright 2019 the V8 project authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
utils.load('test/inspector/wasm-inspector-test.js');

7
let {session, contextGroup, Protocol} =
8
    InspectorTest.start('Tests stepping through wasm scripts by byte offsets');
9
session.setupScriptMap();
10 11 12 13

var builder = new WasmModuleBuilder();

var func_a_idx =
14
    builder.addFunction('wasm_A', kSig_v_i).addBody([kExprNop, kExprNop]).index;
15 16 17 18 19 20 21 22 23 24 25 26

// wasm_B calls wasm_A <param0> times.
builder.addFunction('wasm_B', kSig_v_i)
    .addBody([
      // clang-format off
      kExprLoop, kWasmStmt,               // while
        kExprLocalGet, 0,                 // -
        kExprIf, kWasmStmt,               // if <param0> != 0
          kExprLocalGet, 0,               // -
          kExprI32Const, 1,               // -
          kExprI32Sub,                    // -
          kExprLocalSet, 0,               // decrease <param0>
27
          ...wasmI32Const(1024),          // some longer i32 const (2 byte imm)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
          kExprCallFunction, func_a_idx,  // -
          kExprBr, 1,                     // continue
          kExprEnd,                       // -
        kExprEnd,                         // break
      // clang-format on
    ])
    .exportAs('main');


var module_bytes = builder.toArray();

(async function test() {
  for (const action of ['stepInto', 'stepOver', 'stepOut', 'resume'])
    InspectorTest.logProtocolCommandCalls('Debugger.' + action);

  await Protocol.Debugger.enable();
44
  InspectorTest.log('Setting up global instance variable.');
45
  WasmInspectorTest.instantiate(module_bytes);
46
  const [, {params: wasmScript}] = await Protocol.Debugger.onceScriptParsed(2);
47 48 49

  InspectorTest.log('Got wasm script: ' + wasmScript.url);

50 51
  // Set the breakpoint on a non-breakable position. This should resolve to the
  // next instruction.
52
  InspectorTest.log(
53 54
      `Setting breakpoint on offset 59 (should be propagated to 60, the ` +
      `offset of the call), url ${wasmScript.url}`);
55
  const bpmsg = await Protocol.Debugger.setBreakpoint({
56
    location: {scriptId: wasmScript.scriptId, lineNumber: 0, columnNumber: 59}
57 58 59 60 61 62 63 64
  });

  const actualLocation = bpmsg.result.actualLocation;
  InspectorTest.logMessage(actualLocation);
  Protocol.Runtime.evaluate({ expression: 'instance.exports.main(4)' });
  await waitForPauseAndStep('stepInto');  // into call to wasm_A
  await waitForPauseAndStep('stepOver');  // over first nop
  await waitForPauseAndStep('stepOut');   // out of wasm_A
65
  await waitForPauseAndStep('stepOut');   // out of wasm_B, stop on breakpoint
66
  await waitForPauseAndStep('stepOver');  // over call
67 68
  await waitForPauseAndStep('stepInto');  // == stepOver br
  await waitForPauseAndStep('resume');    // to next breakpoint (3rd iteration)
69 70
  await waitForPauseAndStep('stepInto');  // into wasm_A
  await waitForPauseAndStep('stepOut');   // out to wasm_B
71 72
  // Now step 9 times, until we are in wasm_A again.
  for (let i = 0; i < 9; ++i) await waitForPauseAndStep('stepInto');
73 74
  // 3 more times, back to wasm_B.
  for (let i = 0; i < 3; ++i) await waitForPauseAndStep('stepInto');
75
  // Then just resume.
76 77 78
  await waitForPauseAndStep('resume');
  InspectorTest.log('exports.main returned!');
  InspectorTest.log('Finished!');
79 80
})().catch(reason => InspectorTest.log(`Failed: ${reason}`))
    .finally(InspectorTest.completeTest);
81 82 83

async function waitForPauseAndStep(stepAction) {
  const {params: {callFrames}} = await Protocol.Debugger.oncePaused();
84
  await session.logSourceLocation(callFrames[0].location);
85 86 87 88 89 90 91
  for (var frame of callFrames) {
    const functionName = frame.functionName || '(anonymous)';
    const lineNumber = frame.location.lineNumber;
    const columnNumber = frame.location.columnNumber;
    InspectorTest.log(`at ${functionName} (${lineNumber}:${columnNumber}):`);
    for (var scope of frame.scopeChain) {
      InspectorTest.logObject(' - scope (' + scope.type + '):');
92
      if (scope.type === 'module' || scope.type === 'global') {
93 94 95 96 97 98 99 100
        InspectorTest.logObject('   -- skipped');
      } else {
        const {result: {result: {value}}} =
          await Protocol.Runtime.callFunctionOn({
            objectId: scope.object.objectId,
            functionDeclaration: 'function() { return this; }',
            returnByValue: true
          });
101
        InspectorTest.log(`   ${JSON.stringify(value)}`);
102 103 104 105 106
      }
    }
  }
  Protocol.Debugger[stepAction]();
}