summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-03-23 14:45:13 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-03-23 14:55:50 -0700
commit10159a897c404ec52d63faeb54a052c48cb1a6c9 (patch)
treef3a843d544eba03bec536f61d65ba7bf8736c265
parent4967e68bcb7f59134c35675a46f6020d2996eb99 (diff)
downloadbinaryen-10159a897c404ec52d63faeb54a052c48cb1a6c9.tar.gz
binaryen-10159a897c404ec52d63faeb54a052c48cb1a6c9.tar.bz2
binaryen-10159a897c404ec52d63faeb54a052c48cb1a6c9.zip
add function stack printing in interpreter
-rw-r--r--src/wasm-interpreter.h16
-rw-r--r--src/wasm-js.cpp12
2 files changed, 28 insertions, 0 deletions
diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h
index e9ced6b6f..f1b9fc8f9 100644
--- a/src/wasm-interpreter.h
+++ b/src/wasm-interpreter.h
@@ -116,6 +116,15 @@ public:
return callFunction(export_->value, arguments);
}
+ std::string printFunctionStack() {
+ std::string ret = "/== (binaryen interpreter stack trace)\n";
+ for (int i = int(functionStack.size()) - 1; i >= 0; i--) {
+ ret += std::string("|: ") + functionStack[i].str + "\n";
+ }
+ ret += std::string("\\==\n");
+ return ret;
+ }
+
private:
size_t callDepth = 0;
@@ -124,6 +133,10 @@ private:
int indent = 0;
#endif
+ // Function stack. We maintain this explicitly to allow printing of
+ // stack traces.
+ std::vector<Name> functionStack;
+
//
// Calls a function. This can be used both internally (calls from
// the interpreter to another method), or when you want to call into
@@ -656,6 +669,7 @@ private:
if (callDepth > maxCallDepth) externalInterface->trap("stack limit");
callDepth++;
+ functionStack.push_back(name);
Function *function = wasm.functionsMap[name];
assert(function);
@@ -671,6 +685,8 @@ private:
if (function->result == none) ret = Literal();
assert(function->result == ret.type);
callDepth--;
+ assert(functionStack.back() == name);
+ functionStack.pop_back();
#ifdef WASM_INTERPRETER_DEBUG
std::cout << "exiting " << function->name << " with " << ret << '\n';
#endif
diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp
index 03eddd031..d978e4a2a 100644
--- a/src/wasm-js.cpp
+++ b/src/wasm-js.cpp
@@ -382,6 +382,18 @@ extern "C" void EMSCRIPTEN_KEEPALIVE instantiate() {
};
instance = new ModuleInstance(*module, new JSExternalInterface());
+
+ // stack trace hooks
+ EM_ASM({
+ Module['outside']['extraStackTrace'] = function() {
+ return Pointer_stringify(Module['_interpreter_stack_trace']());
+ };
+ });
+}
+
+extern "C" int EMSCRIPTEN_KEEPALIVE interpreter_stack_trace() {
+ std::string stack = instance->printFunctionStack();
+ return (int)strdup(stack.c_str()); // XXX leak
}
// Does a call from js into an export of the module.