1 //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines an API used to indicate fatal error conditions. Non-fatal 11 // errors (most of them) should be handled through LLVMContext. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm-c/Core.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/Config/config.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Signals.h" 22 #include "llvm/Support/Mutex.h" 23 #include "llvm/Support/MutexGuard.h" 24 #include "llvm/Support/Threading.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include <cassert> 27 #include <cstdlib> 28 29 #if defined(HAVE_UNISTD_H) 30 # include <unistd.h> 31 #endif 32 #if defined(_MSC_VER) 33 # include <io.h> 34 # include <fcntl.h> 35 #endif 36 37 using namespace llvm; 38 39 static fatal_error_handler_t ErrorHandler = nullptr; 40 static void *ErrorHandlerUserData = nullptr; 41 42 static sys::Mutex ErrorHandlerMutex; 43 44 void llvm::install_fatal_error_handler(fatal_error_handler_t handler, 45 void *user_data) { 46 llvm::MutexGuard Lock(ErrorHandlerMutex); 47 assert(!ErrorHandler && "Error handler already registered!\n"); 48 ErrorHandler = handler; 49 ErrorHandlerUserData = user_data; 50 } 51 52 void llvm::remove_fatal_error_handler() { 53 llvm::MutexGuard Lock(ErrorHandlerMutex); 54 ErrorHandler = nullptr; 55 ErrorHandlerUserData = nullptr; 56 } 57 58 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { 59 report_fatal_error(Twine(Reason), GenCrashDiag); 60 } 61 62 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { 63 report_fatal_error(Twine(Reason), GenCrashDiag); 64 } 65 66 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { 67 report_fatal_error(Twine(Reason), GenCrashDiag); 68 } 69 70 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { 71 llvm::fatal_error_handler_t handler = nullptr; 72 void* handlerData = nullptr; 73 { 74 // Only acquire the mutex while reading the handler, so as not to invoke a 75 // user-supplied callback under a lock. 76 llvm::MutexGuard Lock(ErrorHandlerMutex); 77 handler = ErrorHandler; 78 handlerData = ErrorHandlerUserData; 79 } 80 81 if (handler) { 82 handler(handlerData, Reason.str(), GenCrashDiag); 83 } else { 84 // Blast the result out to stderr. We don't try hard to make sure this 85 // succeeds (e.g. handling EINTR) and we can't use errs() here because 86 // raw ostreams can call report_fatal_error. 87 SmallVector<char, 64> Buffer; 88 raw_svector_ostream OS(Buffer); 89 OS << "LLVM ERROR: " << Reason << "\n"; 90 StringRef MessageStr = OS.str(); 91 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); 92 (void)written; // If something went wrong, we deliberately just give up. 93 } 94 95 // If we reached here, we are failing ungracefully. Run the interrupt handlers 96 // to make sure any special cleanups get done, in particular that we remove 97 // files registered with RemoveFileOnSignal. 98 sys::RunInterruptHandlers(); 99 100 exit(1); 101 } 102 103 void llvm::llvm_unreachable_internal(const char *msg, const char *file, 104 unsigned line) { 105 // This code intentionally doesn't call the ErrorHandler callback, because 106 // llvm_unreachable is intended to be used to indicate "impossible" 107 // situations, and not legitimate runtime errors. 108 if (msg) 109 dbgs() << msg << "\n"; 110 dbgs() << "UNREACHABLE executed"; 111 if (file) 112 dbgs() << " at " << file << ":" << line; 113 dbgs() << "!\n"; 114 abort(); 115 #ifdef LLVM_BUILTIN_UNREACHABLE 116 // Windows systems and possibly others don't declare abort() to be noreturn, 117 // so use the unreachable builtin to avoid a Clang self-host warning. 118 LLVM_BUILTIN_UNREACHABLE; 119 #endif 120 } 121 122 static void bindingsErrorHandler(void *user_data, const std::string& reason, 123 bool gen_crash_diag) { 124 LLVMFatalErrorHandler handler = 125 LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data); 126 handler(reason.c_str()); 127 } 128 129 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { 130 install_fatal_error_handler(bindingsErrorHandler, 131 LLVM_EXTENSION reinterpret_cast<void *>(Handler)); 132 } 133 134 void LLVMResetFatalErrorHandler() { 135 remove_fatal_error_handler(); 136 } 137