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/ADT/Twine.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/ErrorHandling.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include "llvm/System/Signals.h" 20 #include "llvm/System/Threading.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/Config/config.h" 23 #include <cassert> 24 #include <cstdlib> 25 26 #if defined(HAVE_UNISTD_H) 27 # include <unistd.h> 28 #endif 29 #if defined(_MSC_VER) 30 # include <io.h> 31 # include <fcntl.h> 32 #endif 33 34 using namespace llvm; 35 using namespace std; 36 37 static fatal_error_handler_t ErrorHandler = 0; 38 static void *ErrorHandlerUserData = 0; 39 40 void llvm::install_fatal_error_handler(fatal_error_handler_t handler, 41 void *user_data) { 42 assert(!llvm_is_multithreaded() && 43 "Cannot register error handlers after starting multithreaded mode!\n"); 44 assert(!ErrorHandler && "Error handler already registered!\n"); 45 ErrorHandler = handler; 46 ErrorHandlerUserData = user_data; 47 } 48 49 void llvm::remove_fatal_error_handler() { 50 ErrorHandler = 0; 51 } 52 53 void llvm::report_fatal_error(const char *Reason) { 54 report_fatal_error(Twine(Reason)); 55 } 56 57 void llvm::report_fatal_error(const std::string &Reason) { 58 report_fatal_error(Twine(Reason)); 59 } 60 61 void llvm::report_fatal_error(const Twine &Reason) { 62 if (ErrorHandler) { 63 ErrorHandler(ErrorHandlerUserData, Reason.str()); 64 } else { 65 // Blast the result out to stderr. We don't try hard to make sure this 66 // succeeds (e.g. handling EINTR) and we can't use errs() here because 67 // raw ostreams can call report_fatal_error. 68 SmallVector<char, 64> Buffer; 69 raw_svector_ostream OS(Buffer); 70 OS << "LLVM ERROR: " << Reason << "\n"; 71 StringRef MessageStr = OS.str(); 72 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); 73 (void)written; // If something went wrong, we deliberately just give up. 74 } 75 76 // If we reached here, we are failing ungracefully. Run the interrupt handlers 77 // to make sure any special cleanups get done, in particular that we remove 78 // files registered with RemoveFileOnSignal. 79 sys::RunInterruptHandlers(); 80 81 exit(1); 82 } 83 84 void llvm::llvm_unreachable_internal(const char *msg, const char *file, 85 unsigned line) { 86 // This code intentionally doesn't call the ErrorHandler callback, because 87 // llvm_unreachable is intended to be used to indicate "impossible" 88 // situations, and not legitimate runtime errors. 89 if (msg) 90 dbgs() << msg << "\n"; 91 dbgs() << "UNREACHABLE executed"; 92 if (file) 93 dbgs() << " at " << file << ":" << line; 94 dbgs() << "!\n"; 95 abort(); 96 } 97