wasm-set-breakpoint.js 2.66 KB
Newer Older
1 2 3 4
// 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.

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

7 8
const {session, contextGroup, Protocol} =
    InspectorTest.start('Tests stepping through wasm scripts.');
9

10
const builder = new WasmModuleBuilder();
11

12
const func_a_idx =
13 14 15
    builder.addFunction('wasm_A', kSig_v_v).addBody([kExprNop, kExprNop]).index;

// wasm_B calls wasm_A <param0> times.
16
const func_b = builder.addFunction('wasm_B', kSig_v_i)
17 18 19
    .addBody([
      // clang-format off
      kExprLoop, kWasmStmt,               // while
20
        kExprLocalGet, 0,                 // -
21
        kExprIf, kWasmStmt,               // if <param0> != 0
22
          kExprLocalGet, 0,               // -
23 24
          kExprI32Const, 1,               // -
          kExprI32Sub,                    // -
25
          kExprLocalSet, 0,               // decrease <param0>
26 27 28 29 30 31 32 33
          kExprCallFunction, func_a_idx,  // -
          kExprBr, 1,                     // continue
          kExprEnd,                       // -
        kExprEnd,                         // break
      // clang-format on
    ])
    .exportAs('main');

34
const module_bytes = builder.toArray();
35

36
const getResult = msg => msg.result || InspectorTest.logMessage(msg);
37

38
function setBreakpoint(offset, script) {
39
  InspectorTest.log(
40
      'Setting breakpoint at offset ' + offset + ' on script ' + script.url);
41 42
  return Protocol.Debugger
      .setBreakpoint(
43
          {'location': {'scriptId': script.scriptId, 'lineNumber': 0, 'columnNumber': offset}})
44 45 46 47 48
      .then(getResult);
}

Protocol.Debugger.onPaused(pause_msg => {
  let loc = pause_msg.params.callFrames[0].location;
49 50 51 52
  if (loc.lineNumber != 0) {
    InspectorTest.log('Unexpected line number: ' + loc.lineNumber);
  }
  InspectorTest.log('Breaking on byte offset ' + loc.columnNumber);
53 54 55
  Protocol.Debugger.resume();
});

56 57 58 59
(async function test() {
  await Protocol.Debugger.enable();
  InspectorTest.log('Instantiating.');
  // Spawn asynchronously:
60
  WasmInspectorTest.instantiate(module_bytes);
61
  InspectorTest.log(
62
      'Waiting for wasm script (ignoring first non-wasm script).');
63
  // Ignore javascript and full module wasm script, get scripts for functions.
64 65 66
  const [, {params: wasm_script}] = await Protocol.Debugger.onceScriptParsed(2);
  for (offset of [11, 10, 8, 6, 2, 4]) {
    await setBreakpoint(func_b.body_offset + offset, wasm_script);
67
  }
68
  InspectorTest.log('Calling main(4)');
69
  await WasmInspectorTest.evalWithUrl('instance.exports.main(4)', 'runWasm');
70 71 72 73
  InspectorTest.log('exports.main returned!');
  InspectorTest.log('Finished!');
  InspectorTest.completeTest();
})();