Skip to content

Commit ef9c5ce

Browse files
committed
[EH] Print message for uncaught exceptions
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.
1 parent 3e53d5c commit ef9c5ce

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

emcc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,6 +2641,7 @@ def get_full_import_name(name):
26412641
# using the JS API, which needs this C++ tag exported.
26422642
if settings.ASSERTIONS and settings.WASM_EXCEPTIONS:
26432643
settings.EXPORTED_FUNCTIONS += ['___cpp_exception']
2644+
settings.EXPORT_EXCEPTION_HANDLING_HELPERS = True;
26442645

26452646
# Make `getExceptionMessage` and other necessary functions available for use.
26462647
if settings.EXPORT_EXCEPTION_HANDLING_HELPERS:

src/library_exceptions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ var LibraryExceptions = {
431431
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Exception
432432
// In release builds, this function is not needed and the native
433433
// _Unwind_RaiseException in libunwind is used instead.
434-
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag'],
434+
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag', '$getExceptionMessage'],
435435
__throw_exception_with_stack_trace: function(ex) {
436436
var e = new WebAssembly.Exception(getCppExceptionTag(), [ex], {traceStack: true});
437437
// The generated stack trace will be in the form of:
@@ -446,6 +446,7 @@ var LibraryExceptions = {
446446
var arr = e.stack.split('\n');
447447
arr.splice(1,1);
448448
e.stack = arr.join('\n');
449+
e.message = getExceptionMessage(e);
449450
throw e;
450451
},
451452
#endif

system/lib/libcxxabi/src/cxa_exception.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ exception.
256256

257257
#if defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG)
258258
extern "C" {
259-
void __throw_exception_with_stack_trace(_Unwind_Exception*, bool);
259+
void __throw_exception_with_stack_trace(_Unwind_Exception*);
260260
} // extern "C"
261261
#endif
262262

@@ -293,7 +293,7 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
293293
#else
294294
// In debug mode, call a JS library function to use WebAssembly.Exception JS
295295
// API, which enables us to include stack traces
296-
__throw_exception_with_stack_trace(&exception_header->unwindHeader, true);
296+
__throw_exception_with_stack_trace(&exception_header->unwindHeader);
297297
#endif
298298
#else
299299
_Unwind_RaiseException(&exception_header->unwindHeader);

0 commit comments

Comments
 (0)