|
| 1 | +/* |
| 2 | + * Copyright 2019 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Prints the a map of function indexes to function names. This can be |
| 19 | +// useful for interpreting a stack trace from a production environment |
| 20 | +// where names did not exist on the client. The map looks like this: |
| 21 | +// |
| 22 | +// 0:foo |
| 23 | +// 1:bar |
| 24 | +// 2:baz |
| 25 | +// |
| 26 | + |
| 27 | +#include "ir/module-utils.h" |
| 28 | +#include "pass.h" |
| 29 | +#include "support/file.h" |
| 30 | +#include "wasm.h" |
| 31 | + |
| 32 | +namespace wasm { |
| 33 | + |
| 34 | +struct PrintFunctionMap : public Pass { |
| 35 | + bool modifiesBinaryenIR() override { return false; } |
| 36 | + |
| 37 | + void run(Module* module) override { |
| 38 | + // If an argument is provided, write to that file; otherwise write to |
| 39 | + // stdout. |
| 40 | + auto outFile = getArgumentOrDefault("symbolmap", ""); |
| 41 | + Output output(outFile, Flags::Text); |
| 42 | + auto& o = output.getStream(); |
| 43 | + Index i = 0; |
| 44 | + auto write = [&](Function* func) { |
| 45 | + o << i++ << ':' << func->name.str << '\n'; |
| 46 | + }; |
| 47 | + ModuleUtils::iterImportedFunctions(*module, write); |
| 48 | + ModuleUtils::iterDefinedFunctions(*module, write); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +Pass* createPrintFunctionMapPass() { return new PrintFunctionMap(); } |
| 53 | + |
| 54 | +} // namespace wasm |
0 commit comments