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/ADT/Twine.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/System/Signals.h"
20 #include "llvm/System/Threading.h"
21 #include "llvm/ADT/SmallVector.h"
25 #if defined(HAVE_UNISTD_H)
36 static fatal_error_handler_t ErrorHandler = 0;
37 static void *ErrorHandlerUserData = 0;
39 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
41 assert(!llvm_is_multithreaded() &&
42 "Cannot register error handlers after starting multithreaded mode!\n");
43 assert(!ErrorHandler && "Error handler already registered!\n");
44 ErrorHandler = handler;
45 ErrorHandlerUserData = user_data;
48 void llvm::remove_fatal_error_handler() {
52 void llvm::report_fatal_error(const char *Reason) {
53 report_fatal_error(Twine(Reason));
56 void llvm::report_fatal_error(const std::string &Reason) {
57 report_fatal_error(Twine(Reason));
60 void llvm::report_fatal_error(const Twine &Reason) {
62 ErrorHandler(ErrorHandlerUserData, Reason.str());
64 // Blast the result out to stderr. We don't try hard to make sure this
65 // succeeds (e.g. handling EINTR) and we can't use errs() here because
66 // raw ostreams can call report_fatal_error.
67 SmallVector<char, 64> Buffer;
68 StringRef ReasonStr = Reason.toStringRef(Buffer);
70 ::write(2, "LLVM ERROR: ", 12);
71 ::write(2, ReasonStr.data(), ReasonStr.size());
75 // If we reached here, we are failing ungracefully. Run the interrupt handlers
76 // to make sure any special cleanups get done, in particular that we remove
77 // files registered with RemoveFileOnSignal.
78 sys::RunInterruptHandlers();
83 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
85 // This code intentionally doesn't call the ErrorHandler callback, because
86 // llvm_unreachable is intended to be used to indicate "impossible"
87 // situations, and not legitimate runtime errors.
89 dbgs() << msg << "\n";
90 dbgs() << "UNREACHABLE executed";
92 dbgs() << " at " << file << ":" << line;