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/Errc.h"
00022 #include "llvm/Support/Signals.h"
00023 #include "llvm/Support/ManagedStatic.h"
00024 #include "llvm/Support/Mutex.h"
00025 #include "llvm/Support/MutexGuard.h"
00026 #include "llvm/Support/Threading.h"
00027 #include "llvm/Support/WindowsError.h"
00028 #include "llvm/Support/raw_ostream.h"
00029 #include <cassert>
00030 #include <cstdlib>
00031 
00032 #if defined(HAVE_UNISTD_H)
00033 # include <unistd.h>
00034 #endif
00035 #if defined(_MSC_VER)
00036 # include <io.h>
00037 # include <fcntl.h>
00038 #endif
00039 
00040 using namespace llvm;
00041 
00042 static fatal_error_handler_t ErrorHandler = nullptr;
00043 static void *ErrorHandlerUserData = nullptr;
00044 
00045 static ManagedStatic<sys::Mutex> ErrorHandlerMutex;
00046 
00047 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
00048                                        void *user_data) {
00049   llvm::MutexGuard Lock(*ErrorHandlerMutex);
00050   assert(!ErrorHandler && "Error handler already registered!\n");
00051   ErrorHandler = handler;
00052   ErrorHandlerUserData = user_data;
00053 }
00054 
00055 void llvm::remove_fatal_error_handler() {
00056   llvm::MutexGuard Lock(*ErrorHandlerMutex);
00057   ErrorHandler = nullptr;
00058   ErrorHandlerUserData = nullptr;
00059 }
00060 
00061 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
00062   report_fatal_error(Twine(Reason), GenCrashDiag);
00063 }
00064 
00065 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
00066   report_fatal_error(Twine(Reason), GenCrashDiag);
00067 }
00068 
00069 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
00070   report_fatal_error(Twine(Reason), GenCrashDiag);
00071 }
00072 
00073 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
00074   llvm::fatal_error_handler_t handler = nullptr;
00075   void* handlerData = nullptr;
00076   {
00077     // Only acquire the mutex while reading the handler, so as not to invoke a
00078     // user-supplied callback under a lock.
00079     llvm::MutexGuard Lock(*ErrorHandlerMutex);
00080     handler = ErrorHandler;
00081     handlerData = ErrorHandlerUserData;
00082   }
00083 
00084   if (handler) {
00085     handler(handlerData, Reason.str(), GenCrashDiag);
00086   } else {
00087     // Blast the result out to stderr.  We don't try hard to make sure this
00088     // succeeds (e.g. handling EINTR) and we can't use errs() here because
00089     // raw ostreams can call report_fatal_error.
00090     SmallVector<char, 64> Buffer;
00091     raw_svector_ostream OS(Buffer);
00092     OS << "LLVM ERROR: " << Reason << "\n";
00093     StringRef MessageStr = OS.str();
00094     ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
00095     (void)written; // If something went wrong, we deliberately just give up.
00096   }
00097 
00098   // If we reached here, we are failing ungracefully. Run the interrupt handlers
00099   // to make sure any special cleanups get done, in particular that we remove
00100   // files registered with RemoveFileOnSignal.
00101   sys::RunInterruptHandlers();
00102 
00103   exit(1);
00104 }
00105 
00106 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
00107                                      unsigned line) {
00108   // This code intentionally doesn't call the ErrorHandler callback, because
00109   // llvm_unreachable is intended to be used to indicate "impossible"
00110   // situations, and not legitimate runtime errors.
00111   if (msg)
00112     dbgs() << msg << "\n";
00113   dbgs() << "UNREACHABLE executed";
00114   if (file)
00115     dbgs() << " at " << file << ":" << line;
00116   dbgs() << "!\n";
00117   abort();
00118 #ifdef LLVM_BUILTIN_UNREACHABLE
00119   // Windows systems and possibly others don't declare abort() to be noreturn,
00120   // so use the unreachable builtin to avoid a Clang self-host warning.
00121   LLVM_BUILTIN_UNREACHABLE;
00122 #endif
00123 }
00124 
00125 static void bindingsErrorHandler(void *user_data, const std::string& reason,
00126                                  bool gen_crash_diag) {
00127   LLVMFatalErrorHandler handler =
00128       LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
00129   handler(reason.c_str());
00130 }
00131 
00132 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
00133   install_fatal_error_handler(bindingsErrorHandler,
00134                               LLVM_EXTENSION reinterpret_cast<void *>(Handler));
00135 }
00136 
00137 void LLVMResetFatalErrorHandler() {
00138   remove_fatal_error_handler();
00139 }
00140 
00141 #ifdef LLVM_ON_WIN32
00142 
00143 #include <winerror.h>
00144 
00145 // I'd rather not double the line count of the following.
00146 #define MAP_ERR_TO_COND(x, y)                                                  \
00147   case x:                                                                      \
00148     return make_error_code(errc::y)
00149 
00150 std::error_code llvm::mapWindowsError(unsigned EV) {
00151   switch (EV) {
00152     MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
00153     MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
00154     MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
00155     MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
00156     MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
00157     MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
00158     MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
00159     MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
00160     MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
00161     MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
00162     MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
00163     MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
00164     MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
00165     MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
00166     MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
00167     MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
00168     MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
00169     MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
00170     MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
00171     MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
00172     MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
00173     MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
00174     MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
00175     MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
00176     MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
00177     MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
00178     MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
00179     MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
00180     MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
00181     MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
00182     MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
00183     MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
00184     MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
00185     MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
00186     MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
00187     MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
00188     MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
00189     MAP_ERR_TO_COND(ERROR_SEEK, io_error);
00190     MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
00191     MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
00192     MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
00193     MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
00194     MAP_ERR_TO_COND(WSAEACCES, permission_denied);
00195     MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
00196     MAP_ERR_TO_COND(WSAEFAULT, bad_address);
00197     MAP_ERR_TO_COND(WSAEINTR, interrupted);
00198     MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
00199     MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
00200     MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
00201   default:
00202     return std::error_code(EV, std::system_category());
00203   }
00204 }
00205 
00206 #endif