summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/debugger/wasm-inspect-many-registers.js
blob: b3e3c38c5822d8f8b61d0389d30ebe72021ec892 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 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 inspecting register values in Liftoff.');

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

const num_locals = 10;
const configs = {
  i32: {type: kWasmI32, add: kExprI32Add, from_i32: kExprNop},
  i64: {type: kWasmI64, add: kExprI64Add, from_i32: kExprI64SConvertI32},
  f32: {type: kWasmF32, add: kExprF32Add, from_i32: kExprF32SConvertI32},
  f64: {type: kWasmF64, add: kExprF64Add, from_i32: kExprF64SConvertI32}
};

Protocol.Debugger.onPaused(async msg => {
  let loc = msg.params.callFrames[0].location;
  let line = [`Paused at offset ${loc.columnNumber}`];
  // Inspect only the top wasm frame.
  var frame = msg.params.callFrames[0];
  for (var scope of frame.scopeChain) {
    if (scope.type == 'module') continue;
    var scope_properties =
        await Protocol.Runtime.getProperties({objectId: scope.object.objectId});
    let str = (await Promise.all(scope_properties.result.result.map(
                   elem => WasmInspectorTest.getWasmValue(elem.value))))
                  .join(', ');
    line.push(`${scope.type}: [${str}]`);
  }
  InspectorTest.log(line.join('; '));
  Protocol.Debugger.resume();
});

// Build a function which receives a lot of arguments. It loads them all and
// adds them together.
// In Liftoff, this will hold many values in registers at the break sites.
function buildModuleBytes(config) {
  const sig = makeSig(
      new Array(num_locals).fill(configs[config].type), [configs[config].type]);
  const body = [];
  for (let i = 0; i < num_locals; ++i) body.push(kExprLocalGet, i);
  for (let i = 0; i < num_locals - 1; ++i) body.push(configs[config].add);
  body.push(kExprReturn);
  const builder = new WasmModuleBuilder();
  const test_func = builder.addFunction('test_' + config, sig).addBody(body);
  const main_body = [];
  for (let i = 0; i < num_locals; ++i)
    main_body.push(kExprI32Const, i, configs[config].from_i32);
  main_body.push(kExprCallFunction, test_func.index, kExprDrop);
  const main =
      builder.addFunction('main', kSig_v_v).addBody(main_body).exportAs('main');

  const module_bytes = builder.toArray();

  // Break at every {kExprLocalGet} and at every addition.
  const interesting_opcodes = [kExprLocalGet, kExprReturn, configs[config].add];
  const breakpoints = [];
  for (let idx = 0; idx < body.length; ++idx) {
    if (interesting_opcodes.find(elem => elem == body[idx])) {
      breakpoints.push(test_func.body_offset + idx);
    }
  }

  return [module_bytes, breakpoints];
}

async function testConfig(config) {
  InspectorTest.log(`Testing ${config}.`);
  const [module_bytes, breakpoints] = buildModuleBytes(config);
  const instance_name = `instance_${config}`;
  // Spawn asynchronously:
  WasmInspectorTest.instantiate(module_bytes, instance_name);
  InspectorTest.log('Waiting for wasm script.');
  const [, {params: wasm_script}] = await Protocol.Debugger.onceScriptParsed(2);
  InspectorTest.log(`Setting ${breakpoints.length} breakpoints.`);
  for (let offset of breakpoints) {
    await Protocol.Debugger.setBreakpoint({
      'location': {
        'scriptId': wasm_script.scriptId,
        'lineNumber': 0,
        'columnNumber': offset
      }
    });
  }
  InspectorTest.log('Calling main.');
  await WasmInspectorTest.evalWithUrl(`${instance_name}.exports.main()`, `run_${config}`);
  InspectorTest.log('main returned.');
}

InspectorTest.runAsyncTestSuite([
  async function test() {
    await Protocol.Debugger.enable();
    for (let config in configs) {
      await testConfig(config);
    }
  }
]);