LLVM API Documentation
00001 //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines an API used to indicate fatal error conditions. Non-fatal 00011 // errors (most of them) should be handled through LLVMContext. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Support/ErrorHandling.h" 00016 #include "llvm/ADT/SmallVector.h" 00017 #include "llvm/ADT/Twine.h" 00018 #include "llvm/Config/config.h" 00019 #include "llvm/Support/Debug.h" 00020 #include "llvm/Support/Signals.h" 00021 #include "llvm/Support/Threading.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 #include <cassert> 00024 #include <cstdlib> 00025 00026 #if defined(HAVE_UNISTD_H) 00027 # include <unistd.h> 00028 #endif 00029 #if defined(_MSC_VER) 00030 # include <io.h> 00031 # include <fcntl.h> 00032 #endif 00033 00034 using namespace llvm; 00035 00036 static fatal_error_handler_t ErrorHandler = 0; 00037 static void *ErrorHandlerUserData = 0; 00038 00039 void llvm::install_fatal_error_handler(fatal_error_handler_t handler, 00040 void *user_data) { 00041 assert(!llvm_is_multithreaded() && 00042 "Cannot register error handlers after starting multithreaded mode!\n"); 00043 assert(!ErrorHandler && "Error handler already registered!\n"); 00044 ErrorHandler = handler; 00045 ErrorHandlerUserData = user_data; 00046 } 00047 00048 void llvm::remove_fatal_error_handler() { 00049 ErrorHandler = 0; 00050 } 00051 00052 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { 00053 report_fatal_error(Twine(Reason), GenCrashDiag); 00054 } 00055 00056 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { 00057 report_fatal_error(Twine(Reason), GenCrashDiag); 00058 } 00059 00060 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { 00061 report_fatal_error(Twine(Reason), GenCrashDiag); 00062 } 00063 00064 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { 00065 if (ErrorHandler) { 00066 ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag); 00067 } else { 00068 // Blast the result out to stderr. We don't try hard to make sure this 00069 // succeeds (e.g. handling EINTR) and we can't use errs() here because 00070 // raw ostreams can call report_fatal_error. 00071 SmallVector<char, 64> Buffer; 00072 raw_svector_ostream OS(Buffer); 00073 OS << "LLVM ERROR: " << Reason << "\n"; 00074 StringRef MessageStr = OS.str(); 00075 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); 00076 (void)written; // If something went wrong, we deliberately just give up. 00077 } 00078 00079 // If we reached here, we are failing ungracefully. Run the interrupt handlers 00080 // to make sure any special cleanups get done, in particular that we remove 00081 // files registered with RemoveFileOnSignal. 00082 sys::RunInterruptHandlers(); 00083 00084 exit(1); 00085 } 00086 00087 void llvm::llvm_unreachable_internal(const char *msg, const char *file, 00088 unsigned line) { 00089 // This code intentionally doesn't call the ErrorHandler callback, because 00090 // llvm_unreachable is intended to be used to indicate "impossible" 00091 // situations, and not legitimate runtime errors. 00092 if (msg) 00093 dbgs() << msg << "\n"; 00094 dbgs() << "UNREACHABLE executed"; 00095 if (file) 00096 dbgs() << " at " << file << ":" << line; 00097 dbgs() << "!\n"; 00098 abort(); 00099 }