Chris Lattner | 59c5753 | 2010-04-07 23:12:29 +0000 | [diff] [blame] | 1 | //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===// |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 59c5753 | 2010-04-07 23:12:29 +0000 | [diff] [blame] | 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 | // |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | a6b9600 | 2015-12-18 01:46:52 +0000 | [diff] [blame] | 16 | #include "llvm-c/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/Config/config.h" |
| 20 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 58cb745 | 2014-06-17 18:06:45 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Errc.h" |
Lang Hames | f7f6d3e | 2016-03-16 01:02:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Error.h" |
Chris Bieneman | 489d1dc | 2014-10-03 22:03:12 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Zachary Turner | 586fd74 | 2014-06-13 21:20:44 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Mutex.h" |
| 25 | #include "llvm/Support/MutexGuard.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Threading.h" |
Rafael Espindola | 58cb745 | 2014-06-17 18:06:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/WindowsError.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 30 | #include <cassert> |
| 31 | #include <cstdlib> |
Chris Lattner | 6217082 | 2010-08-17 23:03:53 +0000 | [diff] [blame] | 32 | |
| 33 | #if defined(HAVE_UNISTD_H) |
| 34 | # include <unistd.h> |
| 35 | #endif |
| 36 | #if defined(_MSC_VER) |
| 37 | # include <io.h> |
| 38 | # include <fcntl.h> |
| 39 | #endif |
| 40 | |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 42 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 43 | static fatal_error_handler_t ErrorHandler = nullptr; |
| 44 | static void *ErrorHandlerUserData = nullptr; |
Daniel Dunbar | 1bf60c2 | 2009-08-10 03:36:26 +0000 | [diff] [blame] | 45 | |
Chris Bieneman | 489d1dc | 2014-10-03 22:03:12 +0000 | [diff] [blame] | 46 | static ManagedStatic<sys::Mutex> ErrorHandlerMutex; |
Zachary Turner | 586fd74 | 2014-06-13 21:20:44 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 59c5753 | 2010-04-07 23:12:29 +0000 | [diff] [blame] | 48 | void llvm::install_fatal_error_handler(fatal_error_handler_t handler, |
| 49 | void *user_data) { |
Chris Bieneman | 489d1dc | 2014-10-03 22:03:12 +0000 | [diff] [blame] | 50 | llvm::MutexGuard Lock(*ErrorHandlerMutex); |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 51 | assert(!ErrorHandler && "Error handler already registered!\n"); |
| 52 | ErrorHandler = handler; |
Daniel Dunbar | 1bf60c2 | 2009-08-10 03:36:26 +0000 | [diff] [blame] | 53 | ErrorHandlerUserData = user_data; |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 59c5753 | 2010-04-07 23:12:29 +0000 | [diff] [blame] | 56 | void llvm::remove_fatal_error_handler() { |
Chris Bieneman | 489d1dc | 2014-10-03 22:03:12 +0000 | [diff] [blame] | 57 | llvm::MutexGuard Lock(*ErrorHandlerMutex); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 58 | ErrorHandler = nullptr; |
Zachary Turner | 586fd74 | 2014-06-13 21:20:44 +0000 | [diff] [blame] | 59 | ErrorHandlerUserData = nullptr; |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chad Rosier | 583b4d3 | 2013-03-27 18:27:54 +0000 | [diff] [blame] | 62 | void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) { |
| 63 | report_fatal_error(Twine(Reason), GenCrashDiag); |
Daniel Dunbar | cd51ea5 | 2009-07-24 07:58:10 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chad Rosier | 583b4d3 | 2013-03-27 18:27:54 +0000 | [diff] [blame] | 66 | void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) { |
| 67 | report_fatal_error(Twine(Reason), GenCrashDiag); |
Daniel Dunbar | cd51ea5 | 2009-07-24 07:58:10 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alp Toker | 17d4e98 | 2014-01-27 05:24:39 +0000 | [diff] [blame] | 70 | void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) { |
Chad Rosier | 583b4d3 | 2013-03-27 18:27:54 +0000 | [diff] [blame] | 71 | report_fatal_error(Twine(Reason), GenCrashDiag); |
Daniel Dunbar | 2ed3fe0 | 2010-11-13 02:48:51 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chad Rosier | 583b4d3 | 2013-03-27 18:27:54 +0000 | [diff] [blame] | 74 | void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { |
Zachary Turner | 586fd74 | 2014-06-13 21:20:44 +0000 | [diff] [blame] | 75 | llvm::fatal_error_handler_t handler = nullptr; |
| 76 | void* handlerData = nullptr; |
| 77 | { |
| 78 | // Only acquire the mutex while reading the handler, so as not to invoke a |
| 79 | // user-supplied callback under a lock. |
Chris Bieneman | 489d1dc | 2014-10-03 22:03:12 +0000 | [diff] [blame] | 80 | llvm::MutexGuard Lock(*ErrorHandlerMutex); |
Zachary Turner | 586fd74 | 2014-06-13 21:20:44 +0000 | [diff] [blame] | 81 | handler = ErrorHandler; |
| 82 | handlerData = ErrorHandlerUserData; |
| 83 | } |
| 84 | |
| 85 | if (handler) { |
| 86 | handler(handlerData, Reason.str(), GenCrashDiag); |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 87 | } else { |
Chris Lattner | 6217082 | 2010-08-17 23:03:53 +0000 | [diff] [blame] | 88 | // Blast the result out to stderr. We don't try hard to make sure this |
| 89 | // succeeds (e.g. handling EINTR) and we can't use errs() here because |
| 90 | // raw ostreams can call report_fatal_error. |
| 91 | SmallVector<char, 64> Buffer; |
Dan Gohman | 3490ff4 | 2010-08-18 22:04:43 +0000 | [diff] [blame] | 92 | raw_svector_ostream OS(Buffer); |
| 93 | OS << "LLVM ERROR: " << Reason << "\n"; |
| 94 | StringRef MessageStr = OS.str(); |
Duncan Sands | c8ba8d2 | 2010-09-16 08:20:49 +0000 | [diff] [blame] | 95 | ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); |
| 96 | (void)written; // If something went wrong, we deliberately just give up. |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 97 | } |
Daniel Dunbar | 401d4c9 | 2010-05-08 02:10:36 +0000 | [diff] [blame] | 98 | |
| 99 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 100 | // to make sure any special cleanups get done, in particular that we remove |
| 101 | // files registered with RemoveFileOnSignal. |
| 102 | sys::RunInterruptHandlers(); |
| 103 | |
Chad Rosier | 379574f | 2012-11-13 16:42:19 +0000 | [diff] [blame] | 104 | exit(1); |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 59c5753 | 2010-04-07 23:12:29 +0000 | [diff] [blame] | 107 | void llvm::llvm_unreachable_internal(const char *msg, const char *file, |
| 108 | unsigned line) { |
Dan Gohman | e392e62 | 2009-08-20 17:15:19 +0000 | [diff] [blame] | 109 | // This code intentionally doesn't call the ErrorHandler callback, because |
| 110 | // llvm_unreachable is intended to be used to indicate "impossible" |
| 111 | // situations, and not legitimate runtime errors. |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 112 | if (msg) |
David Greene | 80604d5 | 2010-01-05 01:28:29 +0000 | [diff] [blame] | 113 | dbgs() << msg << "\n"; |
| 114 | dbgs() << "UNREACHABLE executed"; |
Torok Edwin | 1ab40be | 2009-07-14 12:49:22 +0000 | [diff] [blame] | 115 | if (file) |
David Greene | 80604d5 | 2010-01-05 01:28:29 +0000 | [diff] [blame] | 116 | dbgs() << " at " << file << ":" << line; |
| 117 | dbgs() << "!\n"; |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 118 | abort(); |
Reid Kleckner | 5f4535b | 2013-07-16 14:04:08 +0000 | [diff] [blame] | 119 | #ifdef LLVM_BUILTIN_UNREACHABLE |
| 120 | // Windows systems and possibly others don't declare abort() to be noreturn, |
| 121 | // so use the unreachable builtin to avoid a Clang self-host warning. |
| 122 | LLVM_BUILTIN_UNREACHABLE; |
| 123 | #endif |
Torok Edwin | 6c2d233 | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 124 | } |
Filip Pizlo | a535b14 | 2013-10-17 01:38:28 +0000 | [diff] [blame] | 125 | |
| 126 | static void bindingsErrorHandler(void *user_data, const std::string& reason, |
| 127 | bool gen_crash_diag) { |
| 128 | LLVMFatalErrorHandler handler = |
Alp Toker | acf49f3 | 2013-10-22 12:30:55 +0000 | [diff] [blame] | 129 | LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data); |
Filip Pizlo | a535b14 | 2013-10-17 01:38:28 +0000 | [diff] [blame] | 130 | handler(reason.c_str()); |
| 131 | } |
| 132 | |
| 133 | void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) { |
Alp Toker | acf49f3 | 2013-10-22 12:30:55 +0000 | [diff] [blame] | 134 | install_fatal_error_handler(bindingsErrorHandler, |
| 135 | LLVM_EXTENSION reinterpret_cast<void *>(Handler)); |
Filip Pizlo | a535b14 | 2013-10-17 01:38:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void LLVMResetFatalErrorHandler() { |
| 139 | remove_fatal_error_handler(); |
| 140 | } |
Rafael Espindola | 58cb745 | 2014-06-17 18:06:45 +0000 | [diff] [blame] | 141 | |
| 142 | #ifdef LLVM_ON_WIN32 |
| 143 | |
| 144 | #include <winerror.h> |
| 145 | |
| 146 | // I'd rather not double the line count of the following. |
| 147 | #define MAP_ERR_TO_COND(x, y) \ |
| 148 | case x: \ |
| 149 | return make_error_code(errc::y) |
| 150 | |
| 151 | std::error_code llvm::mapWindowsError(unsigned EV) { |
| 152 | switch (EV) { |
| 153 | MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied); |
| 154 | MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists); |
| 155 | MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device); |
| 156 | MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long); |
| 157 | MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy); |
| 158 | MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy); |
| 159 | MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied); |
| 160 | MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error); |
| 161 | MAP_ERR_TO_COND(ERROR_CANTREAD, io_error); |
| 162 | MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error); |
| 163 | MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied); |
| 164 | MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device); |
| 165 | MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy); |
| 166 | MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty); |
| 167 | MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument); |
| 168 | MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device); |
| 169 | MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists); |
| 170 | MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory); |
| 171 | MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device); |
| 172 | MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied); |
| 173 | MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device); |
| 174 | MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported); |
| 175 | MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument); |
| 176 | MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument); |
| 177 | MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available); |
| 178 | MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available); |
| 179 | MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument); |
| 180 | MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied); |
| 181 | MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory); |
| 182 | MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again); |
| 183 | MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error); |
| 184 | MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy); |
| 185 | MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory); |
| 186 | MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory); |
| 187 | MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory); |
| 188 | MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error); |
| 189 | MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again); |
| 190 | MAP_ERR_TO_COND(ERROR_SEEK, io_error); |
| 191 | MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied); |
| 192 | MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open); |
| 193 | MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error); |
| 194 | MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied); |
| 195 | MAP_ERR_TO_COND(WSAEACCES, permission_denied); |
| 196 | MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor); |
| 197 | MAP_ERR_TO_COND(WSAEFAULT, bad_address); |
| 198 | MAP_ERR_TO_COND(WSAEINTR, interrupted); |
| 199 | MAP_ERR_TO_COND(WSAEINVAL, invalid_argument); |
| 200 | MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open); |
| 201 | MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long); |
| 202 | default: |
| 203 | return std::error_code(EV, std::system_category()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #endif |