diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index d39128db1a6..f9717cc5f55 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -416,6 +416,22 @@ void LLVMShutdown(); char *LLVMCreateMessage(const char *Message); void LLVMDisposeMessage(char *Message); +typedef void (*LLVMFatalErrorHandler)(const char *Reason); + +/** + * Install a fatal error handler. By default, if LLVM detects a fatal error, it + * will call exit(1). This may not be appropriate in many contexts. For example, + * doing exit(1) will bypass many crash reporting/tracing system tools. This + * function allows you to install a callback that will be invoked prior to the + * call to exit(1). + */ +void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler); + +/** + * Reset the fatal error handler. This resets LLVM's fatal error handling + * behavior to the default. + */ +void LLVMResetFatalErrorHandler(void); /** * @defgroup LLVMCCoreContext Contexts diff --git a/lib/Support/ErrorHandling.cpp b/lib/Support/ErrorHandling.cpp index 9425445a853..a0b7619cd2b 100644 --- a/lib/Support/ErrorHandling.cpp +++ b/lib/Support/ErrorHandling.cpp @@ -20,6 +20,7 @@ #include "llvm/Support/Signals.h" #include "llvm/Support/Threading.h" #include "llvm/Support/raw_ostream.h" +#include "llvm-c/Core.h" #include #include @@ -102,3 +103,19 @@ void llvm::llvm_unreachable_internal(const char *msg, const char *file, LLVM_BUILTIN_UNREACHABLE; #endif } + +static void bindingsErrorHandler(void *user_data, const std::string& reason, + bool gen_crash_diag) { + LLVMFatalErrorHandler handler = + reinterpret_cast(user_data); + handler(reason.c_str()); +} + +void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { + install_fatal_error_handler( + bindingsErrorHandler, reinterpret_cast(Handler)); +} + +void LLVMResetFatalErrorHandler() { + remove_fatal_error_handler(); +}