summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/source-map-loader/wasm-dwarf/wasmXScopes.js
blob: 82faa51dbe70606a76d8c15d17b9fff32b322db0 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

/* eslint camelcase: 0*/

"use strict";

const {
  decodeExpr,
} = require("resource://devtools/client/shared/source-map-loader/wasm-dwarf/wasmDwarfExpressions");

const xScopes = new Map();

function indexLinkingNames(items) {
  const result = new Map();
  let queue = [...items];
  while (queue.length) {
    const item = queue.shift();
    if ("uid" in item) {
      result.set(item.uid, item);
    } else if ("linkage_name" in item) {
      // TODO the linkage_name string value is used for compatibility
      // with old format. Remove in favour of the uid referencing.
      result.set(item.linkage_name, item);
    }
    if ("children" in item) {
      queue = [...queue, ...item.children];
    }
  }
  return result;
}

function getIndexedItem(index, key) {
  if (typeof key === "object" && key != null) {
    return index.get(key.uid);
  }
  if (typeof key === "string") {
    return index.get(key);
  }
  return null;
}

async function getXScopes(sourceId, getSourceMap) {
  if (xScopes.has(sourceId)) {
    return xScopes.get(sourceId);
  }
  const map = await getSourceMap(sourceId);
  if (!map || !map.xScopes) {
    xScopes.set(sourceId, null);
    return null;
  }
  const { code_section_offset, debug_info } = map.xScopes;
  const xScope = {
    code_section_offset,
    debug_info,
    idIndex: indexLinkingNames(debug_info),
    sources: map.sources,
  };
  xScopes.set(sourceId, xScope);
  return xScope;
}

function isInRange(item, pc) {
  if ("ranges" in item) {
    return item.ranges.some(r => r[0] <= pc && pc < r[1]);
  }
  if ("high_pc" in item) {
    return item.low_pc <= pc && pc < item.high_pc;
  }
  return false;
}

function decodeExprAt(expr, pc) {
  if (typeof expr === "string") {
    return decodeExpr(expr);
  }
  const foundAt = expr.find(i => i.range[0] <= pc && pc < i.range[1]);
  return foundAt ? decodeExpr(foundAt.expr) : null;
}

function getVariables(scope, pc) {
  const vars = scope.children
    ? scope.children.reduce((result, item) => {
        switch (item.tag) {
          case "variable":
          case "formal_parameter":
            result.push({
              name: item.name || "",
              expr: item.location ? decodeExprAt(item.location, pc) : null,
            });
            break;
          case "lexical_block":
            // FIXME build scope blocks (instead of combining)
            const tmp = getVariables(item, pc);
            result = [...tmp.vars, ...result];
            break;
        }
        return result;
      }, [])
    : [];
  const frameBase = scope.frame_base ? decodeExpr(scope.frame_base) : null;
  return {
    vars,
    frameBase,
  };
}

function filterScopes(items, pc, lastItem, index) {
  if (!items) {
    return [];
  }
  return items.reduce((result, item) => {
    switch (item.tag) {
      case "compile_unit":
        if (isInRange(item, pc)) {
          result = [
            ...result,
            ...filterScopes(item.children, pc, lastItem, index),
          ];
        }
        break;
      case "namespace":
      case "structure_type":
      case "union_type":
        result = [
          ...result,
          ...filterScopes(item.children, pc, lastItem, index),
        ];
        break;
      case "subprogram":
        if (isInRange(item, pc)) {
          const s = {
            id: item.linkage_name,
            name: item.name,
            variables: getVariables(item, pc),
          };
          result = [...result, s, ...filterScopes(item.children, pc, s, index)];
        }
        break;
      case "inlined_subroutine":
        if (isInRange(item, pc)) {
          const linkedItem = getIndexedItem(index, item.abstract_origin);
          const s = {
            id: item.abstract_origin,
            name: linkedItem ? linkedItem.name : void 0,
            variables: getVariables(item, pc),
          };
          if (lastItem) {
            lastItem.file = item.call_file;
            lastItem.line = item.call_line;
          }
          result = [...result, s, ...filterScopes(item.children, pc, s, index)];
        }
        break;
    }
    return result;
  }, []);
}

class XScope {
  xScope;
  sourceMapContext;

  constructor(xScopeData, sourceMapContext) {
    this.xScope = xScopeData;
    this.sourceMapContext = sourceMapContext;
  }

  search(generatedLocation) {
    const { code_section_offset, debug_info, sources, idIndex } = this.xScope;
    const pc = generatedLocation.line - (code_section_offset || 0);
    const scopes = filterScopes(debug_info, pc, null, idIndex);
    scopes.reverse();

    return scopes.map(i => {
      if (!("file" in i)) {
        return {
          displayName: i.name || "",
          variables: i.variables,
        };
      }
      const sourceId = this.sourceMapContext.generatedToOriginalId(
        generatedLocation.sourceId,
        sources[i.file || 0]
      );
      return {
        displayName: i.name || "",
        variables: i.variables,
        location: {
          line: i.line || 0,
          sourceId,
        },
      };
    });
  }
}

async function getWasmXScopes(sourceId, sourceMapContext) {
  const { getSourceMap } = sourceMapContext;
  const xScopeData = await getXScopes(sourceId, getSourceMap);
  if (!xScopeData) {
    return null;
  }
  return new XScope(xScopeData, sourceMapContext);
}

function clearWasmXScopes() {
  xScopes.clear();
}

module.exports = {
  getWasmXScopes,
  clearWasmXScopes,
};