This is a library intended to make it easier to debug uncaught exceptions in code that makes use of libdispatch. Any blocks excuted with dispatch_client_callout are enclosed in an Objective-C try/catch block that invokes objc_terminate() if an exception isn't caught. While the end result is roughly the same as a typical unhandled exception program termination, it can make it somewhat harder to debug the uncaught exception. This library wraps the block in a new try/catch that will record and print the stack trace of the dispatch invocation.
The Xcode project is configured to build a shared library target. Include the "dispatch.h" header and use dispatch functions as normal. Every instance of an overriden dispatch function will add a compiler warning message indicating that the function is being overriden. The original functions are also redeclared as *_noexcept (e.g. dispatch_async_noexcept) if you want to use them in a context where you know exceptions won't be thrown.
Call gc_dispatch_except_log_backtrace(true)
to enable logging of backtraces when an uncaught exception blows up. Of course there's some overhead. When traces are enabled, you'll get something like this printed to stderr:
FATAL APPLICATION ERROR -- An uncaught exception was intercepted before it could hit libdispatch and trigger an undefined behavior fault. Exception will be rethrown in C++ context. Block callee backtrace:
0 libdispatch_noexcept.dylib 0x00000001000acb1d _Z26_gc_dispatch_get_backtracev + 125
1 libdispatch_noexcept.dylib 0x00000001000af643 _ZL21_dispatchRethrowBlockIJEEDtfp_EU13block_pointerFvDpT_E + 403
2 libdispatch_noexcept.dylib 0x00000001000afd8d _gc_dispatch_after_with_exception_handler + 29
3 test_with_dispatch_noexcept 0x0000000100000e43 _Z16dispatch_throwerv + 67
4 test_with_dispatch_noexcept 0x0000000100000e97 main + 39
5 libdyld.dylib 0x00007fff9c71f235 start + 1
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: A nasty error has occurred
There are two binary targets, test_with_dispatch_noexcept and test_without_dispatch_noexcept, that can be used to demonstrate the different exception handling behavior.
An Xcode project is provided for building the shared library on macOS. A CMakeList file is provided for building on Win32 or Linux, if that's helpful for you.
- Jeremy Agostino - JeremyAgost
- Marc Aldorasi - m42a
- Mitchell Wong - mitchellwong
This project is licensed under the MIT License - see the LICENSE file for details