blob: 1aa8303b9e240d779b69df15aa95d224f6a6665c [file] [log] [blame]
Chris Lattner59c57532010-04-07 23:12:29 +00001//===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
Torok Edwin6c2d2332009-07-07 17:32:34 +00002//
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//
Chris Lattner59c57532010-04-07 23:12:29 +000010// This file defines an API used to indicate fatal error conditions. Non-fatal
11// errors (most of them) should be handled through LLVMContext.
12//
Torok Edwin6c2d2332009-07-07 17:32:34 +000013//===----------------------------------------------------------------------===//
14
Torok Edwin6c2d2332009-07-07 17:32:34 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm-c/Core.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Config/config.h"
20#include "llvm/Support/Debug.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000021#include "llvm/Support/Signals.h"
22#include "llvm/Support/Threading.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000024#include <cassert>
25#include <cstdlib>
Chris Lattner62170822010-08-17 23:03:53 +000026
27#if defined(HAVE_UNISTD_H)
28# include <unistd.h>
29#endif
30#if defined(_MSC_VER)
31# include <io.h>
32# include <fcntl.h>
33#endif
34
Torok Edwin6c2d2332009-07-07 17:32:34 +000035using namespace llvm;
Torok Edwin6c2d2332009-07-07 17:32:34 +000036
Chris Lattner59c57532010-04-07 23:12:29 +000037static fatal_error_handler_t ErrorHandler = 0;
Daniel Dunbar1bf60c22009-08-10 03:36:26 +000038static void *ErrorHandlerUserData = 0;
39
Chris Lattner59c57532010-04-07 23:12:29 +000040void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
41 void *user_data) {
Torok Edwin6c2d2332009-07-07 17:32:34 +000042 assert(!llvm_is_multithreaded() &&
43 "Cannot register error handlers after starting multithreaded mode!\n");
44 assert(!ErrorHandler && "Error handler already registered!\n");
45 ErrorHandler = handler;
Daniel Dunbar1bf60c22009-08-10 03:36:26 +000046 ErrorHandlerUserData = user_data;
Torok Edwin6c2d2332009-07-07 17:32:34 +000047}
48
Chris Lattner59c57532010-04-07 23:12:29 +000049void llvm::remove_fatal_error_handler() {
Torok Edwin6c2d2332009-07-07 17:32:34 +000050 ErrorHandler = 0;
51}
52
Chad Rosier583b4d32013-03-27 18:27:54 +000053void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
54 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000055}
56
Chad Rosier583b4d32013-03-27 18:27:54 +000057void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
58 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbarcd51ea52009-07-24 07:58:10 +000059}
60
Chad Rosier583b4d32013-03-27 18:27:54 +000061void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
62 report_fatal_error(Twine(Reason), GenCrashDiag);
Daniel Dunbar2ed3fe02010-11-13 02:48:51 +000063}
64
Chad Rosier583b4d32013-03-27 18:27:54 +000065void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
Chris Lattner62170822010-08-17 23:03:53 +000066 if (ErrorHandler) {
Chad Rosier583b4d32013-03-27 18:27:54 +000067 ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
Torok Edwin6c2d2332009-07-07 17:32:34 +000068 } else {
Chris Lattner62170822010-08-17 23:03:53 +000069 // Blast the result out to stderr. We don't try hard to make sure this
70 // succeeds (e.g. handling EINTR) and we can't use errs() here because
71 // raw ostreams can call report_fatal_error.
72 SmallVector<char, 64> Buffer;
Dan Gohman3490ff42010-08-18 22:04:43 +000073 raw_svector_ostream OS(Buffer);
74 OS << "LLVM ERROR: " << Reason << "\n";
75 StringRef MessageStr = OS.str();
Duncan Sandsc8ba8d22010-09-16 08:20:49 +000076 ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
77 (void)written; // If something went wrong, we deliberately just give up.
Torok Edwin6c2d2332009-07-07 17:32:34 +000078 }
Daniel Dunbar401d4c92010-05-08 02:10:36 +000079
80 // If we reached here, we are failing ungracefully. Run the interrupt handlers
81 // to make sure any special cleanups get done, in particular that we remove
82 // files registered with RemoveFileOnSignal.
83 sys::RunInterruptHandlers();
84
Chad Rosier379574f2012-11-13 16:42:19 +000085 exit(1);
Torok Edwin6c2d2332009-07-07 17:32:34 +000086}
87
Chris Lattner59c57532010-04-07 23:12:29 +000088void llvm::llvm_unreachable_internal(const char *msg, const char *file,
89 unsigned line) {
Dan Gohmane392e622009-08-20 17:15:19 +000090 // This code intentionally doesn't call the ErrorHandler callback, because
91 // llvm_unreachable is intended to be used to indicate "impossible"
92 // situations, and not legitimate runtime errors.
Torok Edwin56d06592009-07-11 20:10:48 +000093 if (msg)
David Greene80604d52010-01-05 01:28:29 +000094 dbgs() << msg << "\n";
95 dbgs() << "UNREACHABLE executed";
Torok Edwin1ab40be2009-07-14 12:49:22 +000096 if (file)
David Greene80604d52010-01-05 01:28:29 +000097 dbgs() << " at " << file << ":" << line;
98 dbgs() << "!\n";
Torok Edwin6c2d2332009-07-07 17:32:34 +000099 abort();
Reid Kleckner5f4535b2013-07-16 14:04:08 +0000100#ifdef LLVM_BUILTIN_UNREACHABLE
101 // Windows systems and possibly others don't declare abort() to be noreturn,
102 // so use the unreachable builtin to avoid a Clang self-host warning.
103 LLVM_BUILTIN_UNREACHABLE;
104#endif
Torok Edwin6c2d2332009-07-07 17:32:34 +0000105}
Filip Pizloa535b142013-10-17 01:38:28 +0000106
107static void bindingsErrorHandler(void *user_data, const std::string& reason,
108 bool gen_crash_diag) {
109 LLVMFatalErrorHandler handler =
Alp Tokeracf49f32013-10-22 12:30:55 +0000110 LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
Filip Pizloa535b142013-10-17 01:38:28 +0000111 handler(reason.c_str());
112}
113
114void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler) {
Alp Tokeracf49f32013-10-22 12:30:55 +0000115 install_fatal_error_handler(bindingsErrorHandler,
116 LLVM_EXTENSION reinterpret_cast<void *>(Handler));
Filip Pizloa535b142013-10-17 01:38:28 +0000117}
118
119void LLVMResetFatalErrorHandler() {
120 remove_fatal_error_handler();
121}