LLVM: lib/Support/ErrorHandling.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
18#include "llvm/Config/config.h"
19#include "llvm/Config/llvm-config.h"
29#include
30#include
31#include
32#include
33
34#if defined(HAVE_UNISTD_H)
35# include <unistd.h>
36#endif
37#if defined(_WIN32)
38# include <io.h>
39# include <fcntl.h>
40#endif
41
42using namespace llvm;
43
46
49
50#if LLVM_ENABLE_THREADS == 1
51
52
53
54
55
56
57
58
59
60
61
62static std::mutex ErrorHandlerMutex;
63static std::mutex BadAllocErrorHandlerMutex;
64#endif
65
67 while (count > 0) {
69 if (written <= 0)
70 return false;
71 buf += written;
73 }
74 return true;
75}
76
78 void *user_data) {
79#if LLVM_ENABLE_THREADS == 1
80 std::lock_guardstd::mutex Lock(ErrorHandlerMutex);
81#endif
85}
86
88#if LLVM_ENABLE_THREADS == 1
89 std::lock_guardstd::mutex Lock(ErrorHandlerMutex);
90#endif
93}
94
98
102
105 void* handlerData = nullptr;
106 {
107
108
109#if LLVM_ENABLE_THREADS == 1
110 std::lock_guardstd::mutex Lock(ErrorHandlerMutex);
111#endif
114 }
115
116 if (handler) {
117 handler(handlerData, Reason.str().c_str(), GenCrashDiag);
118 } else {
119
120
121
124 OS << "LLVM ERROR: " << Reason << "\n";
127 }
128
129
130
131
133
134 if (GenCrashDiag)
135 abort();
136 else
137 exit(1);
138}
139
158
160 void *user_data) {
161#if LLVM_ENABLE_THREADS == 1
162 std::lock_guardstd::mutex Lock(BadAllocErrorHandlerMutex);
163#endif
165 "Bad alloc error handler already registered!\n");
168}
169
171#if LLVM_ENABLE_THREADS == 1
172 std::lock_guardstd::mutex Lock(BadAllocErrorHandlerMutex);
173#endif
176}
177
180 void *HandlerData = nullptr;
181 {
182
183
184#if LLVM_ENABLE_THREADS == 1
185 std::lock_guardstd::mutex Lock(BadAllocErrorHandlerMutex);
186#endif
189 }
190
191 if (Handler) {
192 Handler(HandlerData, Reason, GenCrashDiag);
194 }
195
196#ifdef LLVM_ENABLE_EXCEPTIONS
197
198 throw std::bad_alloc();
199#else
200
201
202 const char *OOMMessage = "LLVM ERROR: out of memory\n";
203 const char *Newline = "\n";
204 write_retry(2, OOMMessage, strlen(OOMMessage));
206 write_retry(2, Newline, strlen(Newline));
207 abort();
208#endif
209}
210
211#ifdef LLVM_ENABLE_EXCEPTIONS
212
213
215}
216#else
217
218
222
223
224
227 (void)old;
229 "new-handler already installed");
230}
231#endif
232
234 unsigned line) {
235
236
237
238 if (msg)
239 dbgs() << msg << "\n";
240 dbgs() << "UNREACHABLE executed";
242 dbgs() << " at " << file << ":" << line;
243 dbgs() << "!\n";
244 abort();
245#ifdef LLVM_BUILTIN_UNREACHABLE
246
247
248 LLVM_BUILTIN_UNREACHABLE;
249#endif
250}
251
253 bool gen_crash_diag) {
256 handler(reason);
257}
258
263
267
268#ifdef _WIN32
269
270#define WIN32_NO_STATUS
272#undef WIN32_NO_STATUS
273#include <ntstatus.h>
274#include <winerror.h>
275
276
277
278
279
280
281
282
283
284
285
286
287
288extern "C" NTSYSAPI NTSTATUS NTAPI RtlGetLastNtStatus();
289
290
291
292
294 unsigned EV = ::GetLastError();
295
296
297
298 if (EV == ERROR_ACCESS_DENIED) {
299 llvm::errc code = RtlGetLastNtStatus() == STATUS_DELETE_PENDING
303 }
305}
306
307
308#define MAP_ERR_TO_COND(x, y) \
309 case x: \
310 return make_error_code(errc::y)
311
313 switch (EV) {
315 MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
319 MAP_ERR_TO_COND(ERROR_BROKEN_PIPE, broken_pipe);
324 MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
325 MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
326 MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
328 MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
333 MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
337 MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
348 MAP_ERR_TO_COND(ERROR_NOT_SUPPORTED, not_supported);
349 MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
353 MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
354 MAP_ERR_TO_COND(ERROR_REPARSE_TAG_INVALID, invalid_argument);
356 MAP_ERR_TO_COND(ERROR_SEEK, io_error);
359 MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
368 default:
369 return std::error_code(EV, std::system_category());
370 }
371}
372
373#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_EXTENSION
LLVM_EXTENSION - Support compilers where we have a keyword to suppress pedantic diagnostics.
static fatal_error_handler_t ErrorHandler
Definition ErrorHandling.cpp:44
static void out_of_memory_new_handler()
Definition ErrorHandling.cpp:219
static bool write_retry(int fd, const char *buf, size_t count)
Definition ErrorHandling.cpp:66
static void bindingsErrorHandler(void *user_data, const char *reason, bool gen_crash_diag)
Definition ErrorHandling.cpp:252
static fatal_error_handler_t BadAllocErrorHandler
Definition ErrorHandling.cpp:47
static void * BadAllocErrorHandlerUserData
Definition ErrorHandling.cpp:48
static void * ErrorHandlerUserData
Definition ErrorHandling.cpp:45
Provides a library for accessing information about this process and other processes on the operating ...
dot regions Print regions of function to dot file(with no function bodies)"
This file defines the SmallVector class.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
A raw_ostream that writes to an SmallVector or SmallString.
StringRef str() const
Return a StringRef for the vector contents.
void LLVMResetFatalErrorHandler()
Reset the fatal error handler.
Definition ErrorHandling.cpp:264
void(* LLVMFatalErrorHandler)(const char *Reason)
void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler)
Install a fatal error handler.
Definition ErrorHandling.cpp:259
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
decltype(auto) RetryAfterSignal(const FailT &Fail, const Fun &F, const Args &... As)
LLVM_ABI void RunInterruptHandlers()
This function runs all the registered interrupt handlers, including the removal of files registered b...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code mapLastWindowsError()
std::error_code make_error_code(BitcodeError E)
LLVM_ABI void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
install_fatal_error_handler - Installs a new error handler to be used whenever a serious (non-recover...
Definition ErrorHandling.cpp:77
LLVM_ABI void install_bad_alloc_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
Installs a new bad alloc error handler that should be used whenever a bad alloc error,...
Definition ErrorHandling.cpp:159
LLVM_ABI void remove_bad_alloc_error_handler()
Restores default bad alloc error handling behavior.
Definition ErrorHandling.cpp:170
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
@ no_such_file_or_directory
@ device_or_resource_busy
@ resource_unavailable_try_again
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ABI void install_out_of_memory_new_handler()
Definition ErrorHandling.cpp:225
LLVM_ABI void llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, unsigned line=0)
This function calls abort(), and prints the optional message to stderr.
Definition ErrorHandling.cpp:233
void(*)(void *user_data, const char *reason, bool gen_crash_diag) fatal_error_handler_t
An error handler callback.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
LLVM_ABI void remove_fatal_error_handler()
Restores default error handling behaviour.
Definition ErrorHandling.cpp:87
LLVM_ABI Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue)
LLVM_ABI std::error_code mapWindowsError(unsigned EV)
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
Definition ErrorHandling.cpp:178
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.