Skip to content

Commit 015b161

Browse files
authored
wavm: emit stack trace when an exception happens (#84)
Signed-off-by: mathetake <[email protected]>
1 parent 40fd3d0 commit 015b161

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/wavm/wavm.cc

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "include/proxy-wasm/wasm_vm.h"
1818

1919
#include <cstdlib>
20+
#include <iomanip>
2021
#include <iostream>
2122
#include <map>
2223
#include <memory>
2324
#include <optional>
25+
#include <sstream>
2426
#include <unordered_map>
2527
#include <utility>
2628
#include <vector>
@@ -42,6 +44,7 @@
4244
#include "WAVM/Runtime/Intrinsics.h"
4345
#include "WAVM/Runtime/Linker.h"
4446
#include "WAVM/Runtime/Runtime.h"
47+
#include "WAVM/RuntimeABI/RuntimeABI.h"
4548
#include "WAVM/WASM/WASM.h"
4649
#include "WAVM/WASTParse/WASTParse.h"
4750

@@ -90,16 +93,37 @@ namespace {
9093
WAVM::Runtime::catchRuntimeExceptions( \
9194
[&] { _x; }, \
9295
[&](WAVM::Runtime::Exception *exception) { \
93-
auto description = describeException(exception); \
94-
_wavm->fail(FailState::RuntimeError, \
95-
"Function: " + std::string(function_name) + " failed: " + description); \
96-
destroyException(exception); \
96+
_wavm->fail(FailState::RuntimeError, getFailMessage(function_name, exception)); \
9797
throw std::exception(); \
9898
}); \
9999
} catch (...) { \
100100
} \
101101
} while (0)
102102

103+
std::string getFailMessage(std::string_view function_name, WAVM::Runtime::Exception *exception) {
104+
std::string message = "Function " + std::string(function_name) +
105+
" failed: " + WAVM::Runtime::describeExceptionType(exception->type) +
106+
"\nProxy-Wasm plugin in-VM backtrace:\n";
107+
std::vector<std::string> callstack_descriptions =
108+
WAVM::Runtime::describeCallStack(exception->callStack);
109+
110+
// Since the first frame is on host and useless for developers, e.g.: `host!envoy+112901013`
111+
// we start with index 1 here
112+
for (size_t i = 1; i < callstack_descriptions.size(); i++) {
113+
std::ostringstream oss;
114+
std::string description = callstack_descriptions[i];
115+
if (description.find("wasm!") == std::string::npos) {
116+
// end of WASM's call stack
117+
break;
118+
}
119+
oss << std::setw(3) << std::setfill(' ') << std::to_string(i);
120+
message += oss.str() + ": " + description + "\n";
121+
}
122+
123+
WAVM::Runtime::destroyException(exception);
124+
return message;
125+
}
126+
103127
struct WasmUntaggedValue : public WAVM::IR::UntaggedValue {
104128
WasmUntaggedValue() = default;
105129
WasmUntaggedValue(I32 inI32) { i32 = inI32; }

0 commit comments

Comments
 (0)