1 //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an API used to indicate fatal error conditions. Non-fatal
11 // errors (most of them) should be handled through LLVMContext.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm-c/Core.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/Signals.h"
23 #include "llvm/Support/Mutex.h"
24 #include "llvm/Support/MutexGuard.h"
25 #include "llvm/Support/Threading.h"
26 #include "llvm/Support/WindowsError.h"
27 #include "llvm/Support/raw_ostream.h"
31 #if defined(HAVE_UNISTD_H)
41 static fatal_error_handler_t ErrorHandler
= nullptr;
42 static void *ErrorHandlerUserData
= nullptr;
44 static sys::Mutex ErrorHandlerMutex
;
46 void llvm::install_fatal_error_handler(fatal_error_handler_t handler
,
48 llvm::MutexGuard
Lock(ErrorHandlerMutex
);
49 assert(!ErrorHandler
&& "Error handler already registered!\n");
50 ErrorHandler
= handler
;
51 ErrorHandlerUserData
= user_data
;
54 void llvm::remove_fatal_error_handler() {
55 llvm::MutexGuard
Lock(ErrorHandlerMutex
);
56 ErrorHandler
= nullptr;
57 ErrorHandlerUserData
= nullptr;
60 void llvm::report_fatal_error(const char *Reason
, bool GenCrashDiag
) {
61 report_fatal_error(Twine(Reason
), GenCrashDiag
);
64 void llvm::report_fatal_error(const std::string
&Reason
, bool GenCrashDiag
) {
65 report_fatal_error(Twine(Reason
), GenCrashDiag
);
68 void llvm::report_fatal_error(StringRef Reason
, bool GenCrashDiag
) {
69 report_fatal_error(Twine(Reason
), GenCrashDiag
);
72 void llvm::report_fatal_error(const Twine
&Reason
, bool GenCrashDiag
) {
73 llvm::fatal_error_handler_t handler
= nullptr;
74 void* handlerData
= nullptr;
76 // Only acquire the mutex while reading the handler, so as not to invoke a
77 // user-supplied callback under a lock.
78 llvm::MutexGuard
Lock(ErrorHandlerMutex
);
79 handler
= ErrorHandler
;
80 handlerData
= ErrorHandlerUserData
;
84 handler(handlerData
, Reason
.str(), GenCrashDiag
);
86 // Blast the result out to stderr. We don't try hard to make sure this
87 // succeeds (e.g. handling EINTR) and we can't use errs() here because
88 // raw ostreams can call report_fatal_error.
89 SmallVector
<char, 64> Buffer
;
90 raw_svector_ostream
OS(Buffer
);
91 OS
<< "LLVM ERROR: " << Reason
<< "\n";
92 StringRef MessageStr
= OS
.str();
93 ssize_t written
= ::write(2, MessageStr
.data(), MessageStr
.size());
94 (void)written
; // If something went wrong, we deliberately just give up.
97 // If we reached here, we are failing ungracefully. Run the interrupt handlers
98 // to make sure any special cleanups get done, in particular that we remove
99 // files registered with RemoveFileOnSignal.
100 sys::RunInterruptHandlers();
105 void llvm::llvm_unreachable_internal(const char *msg
, const char *file
,
107 // This code intentionally doesn't call the ErrorHandler callback, because
108 // llvm_unreachable is intended to be used to indicate "impossible"
109 // situations, and not legitimate runtime errors.
111 dbgs() << msg
<< "\n";
112 dbgs() << "UNREACHABLE executed";
114 dbgs() << " at " << file
<< ":" << line
;
117 #ifdef LLVM_BUILTIN_UNREACHABLE
118 // Windows systems and possibly others don't declare abort() to be noreturn,
119 // so use the unreachable builtin to avoid a Clang self-host warning.
120 LLVM_BUILTIN_UNREACHABLE
;
124 static void bindingsErrorHandler(void *user_data
, const std::string
& reason
,
125 bool gen_crash_diag
) {
126 LLVMFatalErrorHandler handler
=
127 LLVM_EXTENSION
reinterpret_cast<LLVMFatalErrorHandler
>(user_data
);
128 handler(reason
.c_str());
131 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler
) {
132 install_fatal_error_handler(bindingsErrorHandler
,
133 LLVM_EXTENSION
reinterpret_cast<void *>(Handler
));
136 void LLVMResetFatalErrorHandler() {
137 remove_fatal_error_handler();
142 #include <winerror.h>
144 // I'd rather not double the line count of the following.
145 #define MAP_ERR_TO_COND(x, y) \
147 return make_error_code(errc::y)
149 std::error_code
llvm::mapWindowsError(unsigned EV
) {
151 MAP_ERR_TO_COND(ERROR_ACCESS_DENIED
, permission_denied
);
152 MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS
, file_exists
);
153 MAP_ERR_TO_COND(ERROR_BAD_UNIT
, no_such_device
);
154 MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW
, filename_too_long
);
155 MAP_ERR_TO_COND(ERROR_BUSY
, device_or_resource_busy
);
156 MAP_ERR_TO_COND(ERROR_BUSY_DRIVE
, device_or_resource_busy
);
157 MAP_ERR_TO_COND(ERROR_CANNOT_MAKE
, permission_denied
);
158 MAP_ERR_TO_COND(ERROR_CANTOPEN
, io_error
);
159 MAP_ERR_TO_COND(ERROR_CANTREAD
, io_error
);
160 MAP_ERR_TO_COND(ERROR_CANTWRITE
, io_error
);
161 MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY
, permission_denied
);
162 MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST
, no_such_device
);
163 MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE
, device_or_resource_busy
);
164 MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY
, directory_not_empty
);
165 MAP_ERR_TO_COND(ERROR_DIRECTORY
, invalid_argument
);
166 MAP_ERR_TO_COND(ERROR_DISK_FULL
, no_space_on_device
);
167 MAP_ERR_TO_COND(ERROR_FILE_EXISTS
, file_exists
);
168 MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND
, no_such_file_or_directory
);
169 MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL
, no_space_on_device
);
170 MAP_ERR_TO_COND(ERROR_INVALID_ACCESS
, permission_denied
);
171 MAP_ERR_TO_COND(ERROR_INVALID_DRIVE
, no_such_device
);
172 MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION
, function_not_supported
);
173 MAP_ERR_TO_COND(ERROR_INVALID_HANDLE
, invalid_argument
);
174 MAP_ERR_TO_COND(ERROR_INVALID_NAME
, invalid_argument
);
175 MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION
, no_lock_available
);
176 MAP_ERR_TO_COND(ERROR_LOCKED
, no_lock_available
);
177 MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK
, invalid_argument
);
178 MAP_ERR_TO_COND(ERROR_NOACCESS
, permission_denied
);
179 MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY
, not_enough_memory
);
180 MAP_ERR_TO_COND(ERROR_NOT_READY
, resource_unavailable_try_again
);
181 MAP_ERR_TO_COND(ERROR_OPEN_FAILED
, io_error
);
182 MAP_ERR_TO_COND(ERROR_OPEN_FILES
, device_or_resource_busy
);
183 MAP_ERR_TO_COND(ERROR_OUTOFMEMORY
, not_enough_memory
);
184 MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND
, no_such_file_or_directory
);
185 MAP_ERR_TO_COND(ERROR_BAD_NETPATH
, no_such_file_or_directory
);
186 MAP_ERR_TO_COND(ERROR_READ_FAULT
, io_error
);
187 MAP_ERR_TO_COND(ERROR_RETRY
, resource_unavailable_try_again
);
188 MAP_ERR_TO_COND(ERROR_SEEK
, io_error
);
189 MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION
, permission_denied
);
190 MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES
, too_many_files_open
);
191 MAP_ERR_TO_COND(ERROR_WRITE_FAULT
, io_error
);
192 MAP_ERR_TO_COND(ERROR_WRITE_PROTECT
, permission_denied
);
193 MAP_ERR_TO_COND(WSAEACCES
, permission_denied
);
194 MAP_ERR_TO_COND(WSAEBADF
, bad_file_descriptor
);
195 MAP_ERR_TO_COND(WSAEFAULT
, bad_address
);
196 MAP_ERR_TO_COND(WSAEINTR
, interrupted
);
197 MAP_ERR_TO_COND(WSAEINVAL
, invalid_argument
);
198 MAP_ERR_TO_COND(WSAEMFILE
, too_many_files_open
);
199 MAP_ERR_TO_COND(WSAENAMETOOLONG
, filename_too_long
);
201 return std::error_code(EV
, std::system_category());