Skip to content

Commit 7306f60

Browse files
authored
Add --print-function-map to print out a map of function index to name (#2155)
* work * fix * fix * format
1 parent c0fba4a commit 7306f60

7 files changed

Lines changed: 70 additions & 0 deletions

File tree

build-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ mkdir -p ${OUT}
129129
$BINARYEN_SRC/passes/Precompute.cpp \
130130
$BINARYEN_SRC/passes/Print.cpp \
131131
$BINARYEN_SRC/passes/PrintFeatures.cpp \
132+
$BINARYEN_SRC/passes/PrintFunctionMap.cpp \
132133
$BINARYEN_SRC/passes/PrintCallGraph.cpp \
133134
$BINARYEN_SRC/passes/RedundantSetElimination.cpp \
134135
$BINARYEN_SRC/passes/RelooperJumpThreading.cpp \

src/passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SET(passes_SOURCES
4343
Print.cpp
4444
PrintCallGraph.cpp
4545
PrintFeatures.cpp
46+
PrintFunctionMap.cpp
4647
StackIR.cpp
4748
Strip.cpp
4849
StripTargetFeatures.cpp

src/passes/PrintFunctionMap.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 "pass.h"
28+
#include "wasm.h"
29+
30+
namespace wasm {
31+
32+
struct PrintFunctionMap : public Pass {
33+
bool modifiesBinaryenIR() override { return false; }
34+
35+
void run(PassRunner* runner, Module* module) override {
36+
Index i = 0;
37+
for (auto& func : module->functions) {
38+
std::cout << i++ << ':' << func->name.str << '\n';
39+
}
40+
}
41+
};
42+
43+
Pass* createPrintFunctionMapPass() { return new PrintFunctionMap(); }
44+
45+
} // namespace wasm

src/passes/pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ void PassRegistry::registerPasses() {
211211
"print-full", "print in full s-expression format", createFullPrinterPass);
212212
registerPass(
213213
"print-call-graph", "print call graph", createPrintCallGraphPass);
214+
registerPass("print-function-map",
215+
"print a map of function indexes to names",
216+
createPrintFunctionMapPass);
214217
registerPass("print-stack-ir",
215218
"print out Stack IR (useful for internal debugging)",
216219
createPrintStackIRPass);

src/passes/passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Pass* createPrecomputePropagatePass();
7272
Pass* createPrinterPass();
7373
Pass* createPrintCallGraphPass();
7474
Pass* createPrintFeaturesPass();
75+
Pass* createPrintFunctionMapPass();
7576
Pass* createPrintStackIRPass();
7677
Pass* createRelooperJumpThreadingPass();
7778
Pass* createRemoveNonJSOpsPass();

test/passes/print-function-map.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
0:Foo
2+
1:bar
3+
2:baz
4+
(module
5+
(type $FUNCSIG$v (func))
6+
(import "env" "foo" (func $Foo))
7+
(func $bar (; 1 ;) (type $FUNCSIG$v)
8+
(nop)
9+
)
10+
(func $baz (; 2 ;) (type $FUNCSIG$v)
11+
(nop)
12+
)
13+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(module
2+
(import "env" "foo" (func $Foo))
3+
(func $bar)
4+
(func $baz)
5+
)
6+

0 commit comments

Comments
 (0)