[EH] Print message for uncaught exceptions#18003
Conversation
After emscripten-core#17979, when `ASSERTIONS` is set, uncaught exceptions carry stack traces that are printed to the screen for debugging help. This adds an exception message to the exception objects in addition to that. The message it adds is produced by `__get_exception_message` function: https://github.com/emscripten-core/emscripten/blob/f6c46570e3780e52050bf822a07b342ec4bdddbe/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp#L75-L111 If an exception is a subclass of `std::exception`, it returns `std::exception::what()`. If not, it just prints its type. If an exception is uncaught and its type is a subclass of `std::exception`, now we print the `what()` message along with the stack trace when `ASSERTION` is set. In case our exception is `std::runtime_error` and the message is "my exception", when uncaught, this prints: ``` exiting due to exception: [object WebAssembly.Exception],Error: std::runtime_error,my exception at __cxa_throw (wasm://wasm/009a7c9a:wasm-function[1551]:0x24367) ... ``` Fixes emscripten-core#6330.
| // In debug mode, call a JS library function to use WebAssembly.Exception JS | ||
| // API, which enables us to include stack traces | ||
| __throw_exception_with_stack_trace(&exception_header->unwindHeader, true); | ||
| __throw_exception_with_stack_trace(&exception_header->unwindHeader); |
There was a problem hiding this comment.
This second argument is what I should've deleted in #17979 and forgot. (I first added an option in the JS f unction and then deleted it after feedback, but forgot to delete the caller side, which didn't error out)
|
Will convert this to a draft while fixing CI errors. |
| # Stack trace example for this example code: | ||
| # exiting due to exception: [object WebAssembly.Exception],Error | ||
| # Stack trace and message example for this example code: | ||
| # exiting due to exception: [object WebAssembly.Exception],Error: std::runtime_error, my message |
There was a problem hiding this comment.
Unrelated, but I wonder if we could drop the [object WebAssembly.Exception], here. It seems to be just unnecessary visual noise.
There was a problem hiding this comment.
I don't think It's easy to consistently take out that part, because different engines can have different error formats. For example, d8 prints:
exiting due to exception: [object WebAssembly.Exception],Error: std::runtime_error,my message
at __cxa_throw (wasm://wasm/00462286:wasm-function[44]:0x3b0f)
at bar() (wasm://wasm/00462286:wasm-function[9]:0x98c)
at foo() (wasm://wasm/00462286:wasm-function[10]:0x992)
at __original_main (wasm://wasm/00462286:wasm-function[11]:0x9c4)
at main (wasm://wasm/00462286:wasm-function[12]:0x9e1)
at test.js:934:22
at callMain (test.js:1874:15)
at doRun (test.js:1928:23)
at run (test.js:1943:5)Whereas Node prints:
/usr/local/google/home/aheejin/test/stack/test.js:147
throw ex;
^
WebAssembly.Exception: std::runtime_error,my message
at __cxa_throw (wasm://wasm/00462286:wasm-function[44]:0x3b0f)
at bar() (wasm://wasm/00462286:wasm-function[9]:0x98c)
at foo() (wasm://wasm/00462286:wasm-function[10]:0x992)
at __original_main (wasm://wasm/00462286:wasm-function[11]:0x9c4)
at main (wasm://wasm/00462286:wasm-function[12]:0x9e1)
at /usr/local/google/home/aheejin/test/stack/test.js:934:22
at callMain (/usr/local/google/home/aheejin/test/stack/test.js:1874:15)
at doRun (/usr/local/google/home/aheejin/test/stack/test.js:1928:23)
at run (/usr/local/google/home/aheejin/test/stack/test.js:1943:5)
Node.js v17.5.0So while both of them have WebAssembly.Exception, their printing formats differ, making taking it out in a consistent way difficult. We can just simply replace WebAssembly.Exception with an empty string, but not sure if that's better..?
There was a problem hiding this comment.
Leaving it as is sounds fine for to me then. We can attempt to refine in a followup perhaps.
RReverser
left a comment
There was a problem hiding this comment.
This is great, thanks!
After #17979, when
ASSERTIONSis set, uncaught exceptions carry stack traces that are printed to the screen for debugging help. This adds an exception message to the exception objects in addition to that.The message it adds is produced by
__get_exception_messagefunction:emscripten/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp
Lines 75 to 111 in f6c4657
If an exception is a subclass of
std::exception, it returnsstd::exception::what(). If not, it just prints its type.If an exception is uncaught and its type is a subclass of
std::exception, now we print thewhat()message along with the stack trace whenASSERTIONSis set. In case our exception isstd::runtime_errorand the message is "my exception", when uncaught, this prints:Fixes #6330.