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