LLVM API Documentation

ErrorHandling.cpp
Go to the documentation of this file.
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-c/Core.h"
00017 #include "llvm/ADT/SmallVector.h"
00018 #include "llvm/ADT/Twine.h"
00019 #include "llvm/Config/config.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/Signals.h"
00022 #include "llvm/Support/Threading.h"
00023 #include "llvm/Support/raw_ostream.h"
00024 #include <cassert>
00025 #include <cstdlib>
00026 
00027 #if defined(HAVE_UNISTD_H)
00028 # include <unistd.h>
00029 #endif
00030 #if defined(_MSC_VER)
00031 # include <io.h>
00032 # include <fcntl.h>
00033 #endif
00034 
00035 using namespace llvm;
00036 
00037 static fatal_error_handler_t ErrorHandler = nullptr;
00038 static void *ErrorHandlerUserData = nullptr;
00039 
00040 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
00041                                        void *user_data) {
00042   assert(!llvm_is_multithreaded() &&
00043          "Cannot register error handlers after starting multithreaded mode!\n");
00044   assert(!ErrorHandler && "Error handler already registered!\n");
00045   ErrorHandler = handler;
00046   ErrorHandlerUserData = user_data;
00047 }
00048 
00049 void llvm::remove_fatal_error_handler() {
00050   ErrorHandler = nullptr;
00051 }
00052 
00053 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
00054   report_fatal_error(Twine(Reason), GenCrashDiag);
00055 }
00056 
00057 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
00058   report_fatal_error(Twine(Reason), GenCrashDiag);
00059 }
00060 
00061 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
00062   report_fatal_error(Twine(Reason), GenCrashDiag);
00063 }
00064 
00065 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
00066   if (ErrorHandler) {
00067     ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
00068   } else {
00069     // Blast the result out to stderr.  We don't try hard to make sure this
00070     // succeeds (e.g. handling EINTR) and we can't use errs() here because
00071     // raw ostreams can call report_fatal_error.
00072     SmallVector<char, 64> Buffer;
00073     raw_svector_ostream OS(Buffer);
00074     OS << "LLVM ERROR: " << Reason << "\n";
00075     StringRef MessageStr = OS.str();
00076     ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
00077     (void)written; // If something went wrong, we deliberately just give up.
00078   }
00079 
00080   // If we reached here, we are failing ungracefully. Run the interrupt handlers
00081   // to make sure any special cleanups get done, in particular that we remove
00082   // files registered with RemoveFileOnSignal.
00083   sys::RunInterruptHandlers();
00084 
00085   exit(1);
00086 }
00087 
00088 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
00089                                      unsigned line) {
00090   // This code intentionally doesn't call the ErrorHandler callback, because
00091   // llvm_unreachable is intended to be used to indicate "impossible"
00092   // situations, and not legitimate runtime errors.
00093   if (msg)
00094     dbgs() << msg << "\n";
00095   dbgs() << "UNREACHABLE executed";
00096   if (file)
00097     dbgs() << " at " << file << ":" << line;
00098   dbgs() << "!\n";
00099   abort();
00100 #ifdef LLVM_BUILTIN_UNREACHABLE
00101   // Windows systems and possibly others don't declare abort() to be noreturn,
00102   // so use the unreachable builtin to avoid a Clang self-host warning.
00103   LLVM_BUILTIN_UNREACHABLE;
00104 #endif
00105 }
00106 
00107 static void bindingsErrorHandler(void *user_data, const std::string& reason,
00108                                  bool gen_crash_diag) {
00109   LLVMFatalErrorHandler handler =
00110       LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
00111   handler(reason.c_str());
00112 }
00113 
00114 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
00115   install_fatal_error_handler(bindingsErrorHandler,
00116                               LLVM_EXTENSION reinterpret_cast<void *>(Handler));
00117 }
00118 
00119 void LLVMResetFatalErrorHandler() {
00120   remove_fatal_error_handler();
00121 }