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