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