b9e4eabe926a63fd83dd9504e6ab8e624ae90024
[oota-llvm.git] / lib / Support / ErrorHandling.cpp
1 //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
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 //
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 //
13 //===----------------------------------------------------------------------===//
14
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/Signals.h"
22 #include "llvm/Support/Threading.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <cassert>
25 #include <cstdlib>
26 #include <mutex>
27
28 #if defined(HAVE_UNISTD_H)
29 # include <unistd.h>
30 #endif
31 #if defined(_MSC_VER)
32 # include <io.h>
33 # include <fcntl.h>
34 #endif
35
36 using namespace llvm;
37
38 static fatal_error_handler_t ErrorHandler = nullptr;
39 static void *ErrorHandlerUserData = nullptr;
40
41 static llvm::recursive_mutex ErrorHandlerMutex;
42
43 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
44                                        void *user_data) {
45   std::lock_guard<llvm::recursive_mutex> Lock(ErrorHandlerMutex);
46   assert(!ErrorHandler && "Error handler already registered!\n");
47   ErrorHandler = handler;
48   ErrorHandlerUserData = user_data;
49 }
50
51 void llvm::remove_fatal_error_handler() {
52   std::lock_guard<llvm::recursive_mutex> Lock(ErrorHandlerMutex);
53   ErrorHandler = nullptr;
54 }
55
56 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
57   report_fatal_error(Twine(Reason), GenCrashDiag);
58 }
59
60 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
61   report_fatal_error(Twine(Reason), GenCrashDiag);
62 }
63
64 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
65   report_fatal_error(Twine(Reason), GenCrashDiag);
66 }
67
68 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
69   std::lock_guard<llvm::recursive_mutex> Lock(ErrorHandlerMutex);
70   if (ErrorHandler) {
71     ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
72   } else {
73     // Blast the result out to stderr.  We don't try hard to make sure this
74     // succeeds (e.g. handling EINTR) and we can't use errs() here because
75     // raw ostreams can call report_fatal_error.
76     SmallVector<char, 64> Buffer;
77     raw_svector_ostream OS(Buffer);
78     OS << "LLVM ERROR: " << Reason << "\n";
79     StringRef MessageStr = OS.str();
80     ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
81     (void)written; // If something went wrong, we deliberately just give up.
82   }
83
84   // If we reached here, we are failing ungracefully. Run the interrupt handlers
85   // to make sure any special cleanups get done, in particular that we remove
86   // files registered with RemoveFileOnSignal.
87   sys::RunInterruptHandlers();
88
89   exit(1);
90 }
91
92 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
93                                      unsigned line) {
94   // This code intentionally doesn't call the ErrorHandler callback, because
95   // llvm_unreachable is intended to be used to indicate "impossible"
96   // situations, and not legitimate runtime errors.
97   if (msg)
98     dbgs() << msg << "\n";
99   dbgs() << "UNREACHABLE executed";
100   if (file)
101     dbgs() << " at " << file << ":" << line;
102   dbgs() << "!\n";
103   abort();
104 #ifdef LLVM_BUILTIN_UNREACHABLE
105   // Windows systems and possibly others don't declare abort() to be noreturn,
106   // so use the unreachable builtin to avoid a Clang self-host warning.
107   LLVM_BUILTIN_UNREACHABLE;
108 #endif
109 }
110
111 static void bindingsErrorHandler(void *user_data, const std::string& reason,
112                                  bool gen_crash_diag) {
113   LLVMFatalErrorHandler handler =
114       LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
115   handler(reason.c_str());
116 }
117
118 void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
119   install_fatal_error_handler(bindingsErrorHandler,
120                               LLVM_EXTENSION reinterpret_cast<void *>(Handler));
121 }
122
123 void LLVMResetFatalErrorHandler() {
124   remove_fatal_error_handler();
125 }