blob: c4d5ea4c3db7daefe8fa570e1466d09e6abd975f [file] [log] [blame]
Chris Lattnerb0094c22010-04-07 23:12:29 +00001//===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
Torok Edwin31e24662009-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 Lattnerb0094c22010-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 Edwin31e24662009-07-07 17:32:34 +000013//===----------------------------------------------------------------------===//
14
Daniel Dunbar82a29b62009-07-24 07:58:10 +000015#include "llvm/ADT/Twine.h"
David Greeneaf6c8cc2010-01-05 01:28:29 +000016#include "llvm/Support/Debug.h"
Torok Edwin31e24662009-07-07 17:32:34 +000017#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/raw_ostream.h"
Daniel Dunbard5477082010-05-08 02:10:36 +000019#include "llvm/System/Signals.h"
Torok Edwin31e24662009-07-07 17:32:34 +000020#include "llvm/System/Threading.h"
Chris Lattner73ebaff2010-08-17 23:03:53 +000021#include "llvm/ADT/SmallVector.h"
Torok Edwin31e24662009-07-07 17:32:34 +000022#include <cassert>
23#include <cstdlib>
Chris Lattner73ebaff2010-08-17 23:03:53 +000024
25#if defined(HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28#if defined(_MSC_VER)
29# include <io.h>
30# include <fcntl.h>
31#endif
32
Torok Edwin31e24662009-07-07 17:32:34 +000033using namespace llvm;
34using namespace std;
35
Chris Lattnerb0094c22010-04-07 23:12:29 +000036static fatal_error_handler_t ErrorHandler = 0;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000037static void *ErrorHandlerUserData = 0;
38
Chris Lattnerb0094c22010-04-07 23:12:29 +000039void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
40 void *user_data) {
Torok Edwin31e24662009-07-07 17:32:34 +000041 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;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000045 ErrorHandlerUserData = user_data;
Torok Edwin31e24662009-07-07 17:32:34 +000046}
47
Chris Lattnerb0094c22010-04-07 23:12:29 +000048void llvm::remove_fatal_error_handler() {
Torok Edwin31e24662009-07-07 17:32:34 +000049 ErrorHandler = 0;
50}
51
Chris Lattner73ebaff2010-08-17 23:03:53 +000052void llvm::report_fatal_error(const char *Reason) {
53 report_fatal_error(Twine(Reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000054}
55
Chris Lattner73ebaff2010-08-17 23:03:53 +000056void llvm::report_fatal_error(const std::string &Reason) {
57 report_fatal_error(Twine(Reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000058}
59
Chris Lattner73ebaff2010-08-17 23:03:53 +000060void llvm::report_fatal_error(const Twine &Reason) {
61 if (ErrorHandler) {
62 ErrorHandler(ErrorHandlerUserData, Reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000063 } else {
Chris Lattner73ebaff2010-08-17 23:03:53 +000064 // 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);
69
70 ::write(2, "LLVM ERROR: ", 12);
71 ::write(2, ReasonStr.data(), ReasonStr.size());
72 ::write(2, "\n", 1);
Torok Edwin31e24662009-07-07 17:32:34 +000073 }
Daniel Dunbard5477082010-05-08 02:10:36 +000074
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();
79
Torok Edwin31e24662009-07-07 17:32:34 +000080 exit(1);
81}
82
Chris Lattnerb0094c22010-04-07 23:12:29 +000083void llvm::llvm_unreachable_internal(const char *msg, const char *file,
84 unsigned line) {
Dan Gohman073f5b62009-08-20 17:15:19 +000085 // 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.
Torok Edwinc25e7582009-07-11 20:10:48 +000088 if (msg)
David Greeneaf6c8cc2010-01-05 01:28:29 +000089 dbgs() << msg << "\n";
90 dbgs() << "UNREACHABLE executed";
Torok Edwin93990d72009-07-14 12:49:22 +000091 if (file)
David Greeneaf6c8cc2010-01-05 01:28:29 +000092 dbgs() << " at " << file << ":" << line;
93 dbgs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000094 abort();
95}