blob: 66b2c83fe73ee82ddf6163c9c8001054f01d7464 [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"
Chris Lattner7942e3c2010-08-17 23:22:10 +000022#include "llvm/Config/config.h"
Torok Edwin31e24662009-07-07 17:32:34 +000023#include <cassert>
24#include <cstdlib>
Chris Lattner73ebaff2010-08-17 23:03:53 +000025
26#if defined(HAVE_UNISTD_H)
27# include <unistd.h>
28#endif
29#if defined(_MSC_VER)
30# include <io.h>
31# include <fcntl.h>
32#endif
33
Torok Edwin31e24662009-07-07 17:32:34 +000034using namespace llvm;
35using namespace std;
36
Chris Lattnerb0094c22010-04-07 23:12:29 +000037static fatal_error_handler_t ErrorHandler = 0;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000038static void *ErrorHandlerUserData = 0;
39
Chris Lattnerb0094c22010-04-07 23:12:29 +000040void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
41 void *user_data) {
Torok Edwin31e24662009-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 Dunbarca15f3d2009-08-10 03:36:26 +000046 ErrorHandlerUserData = user_data;
Torok Edwin31e24662009-07-07 17:32:34 +000047}
48
Chris Lattnerb0094c22010-04-07 23:12:29 +000049void llvm::remove_fatal_error_handler() {
Torok Edwin31e24662009-07-07 17:32:34 +000050 ErrorHandler = 0;
51}
52
Chris Lattner73ebaff2010-08-17 23:03:53 +000053void llvm::report_fatal_error(const char *Reason) {
54 report_fatal_error(Twine(Reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000055}
56
Chris Lattner73ebaff2010-08-17 23:03:53 +000057void llvm::report_fatal_error(const std::string &Reason) {
58 report_fatal_error(Twine(Reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000059}
60
Daniel Dunbarafd693c2010-11-13 02:48:51 +000061void llvm::report_fatal_error(StringRef Reason) {
62 report_fatal_error(Twine(Reason));
63}
64
Chris Lattner73ebaff2010-08-17 23:03:53 +000065void llvm::report_fatal_error(const Twine &Reason) {
66 if (ErrorHandler) {
67 ErrorHandler(ErrorHandlerUserData, Reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000068 } else {
Chris Lattner73ebaff2010-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 Gohmand77e5a82010-08-18 22:04:43 +000073 raw_svector_ostream OS(Buffer);
74 OS << "LLVM ERROR: " << Reason << "\n";
75 StringRef MessageStr = OS.str();
Duncan Sandsdd2fdd82010-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 Edwin31e24662009-07-07 17:32:34 +000078 }
Daniel Dunbard5477082010-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
Torok Edwin31e24662009-07-07 17:32:34 +000085 exit(1);
86}
87
Chris Lattnerb0094c22010-04-07 23:12:29 +000088void llvm::llvm_unreachable_internal(const char *msg, const char *file,
89 unsigned line) {
Dan Gohman073f5b62009-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 Edwinc25e7582009-07-11 20:10:48 +000093 if (msg)
David Greeneaf6c8cc2010-01-05 01:28:29 +000094 dbgs() << msg << "\n";
95 dbgs() << "UNREACHABLE executed";
Torok Edwin93990d72009-07-14 12:49:22 +000096 if (file)
David Greeneaf6c8cc2010-01-05 01:28:29 +000097 dbgs() << " at " << file << ":" << line;
98 dbgs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000099 abort();
100}