LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ErrorHandling.cpp
Go to the documentation of this file.
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 
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/Signals.h"
21 #include "llvm/Support/Threading.h"
23 #include "llvm-c/Core.h"
24 #include <cassert>
25 #include <cstdlib>
26 
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 
35 using namespace llvm;
36 
38 static void *ErrorHandlerUserData = 0;
39 
41  void *user_data) {
42  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;
46  ErrorHandlerUserData = user_data;
47 }
48 
50  ErrorHandler = 0;
51 }
52 
53 void llvm::report_fatal_error(const char *Reason, bool GenCrashDiag) {
54  report_fatal_error(Twine(Reason), GenCrashDiag);
55 }
56 
57 void llvm::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
58  report_fatal_error(Twine(Reason), GenCrashDiag);
59 }
60 
61 void llvm::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
62  report_fatal_error(Twine(Reason), GenCrashDiag);
63 }
64 
65 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
66  if (ErrorHandler) {
67  ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
68  } else {
69  // 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;
73  raw_svector_ostream OS(Buffer);
74  OS << "LLVM ERROR: " << Reason << "\n";
75  StringRef MessageStr = OS.str();
76  ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
77  (void)written; // If something went wrong, we deliberately just give up.
78  }
79 
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.
84 
85  exit(1);
86 }
87 
88 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
89  unsigned line) {
90  // 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.
93  if (msg)
94  dbgs() << msg << "\n";
95  dbgs() << "UNREACHABLE executed";
96  if (file)
97  dbgs() << " at " << file << ":" << line;
98  dbgs() << "!\n";
99  abort();
100 #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
105 }
106 
107 static void bindingsErrorHandler(void *user_data, const std::string& reason,
108  bool gen_crash_diag) {
109  LLVMFatalErrorHandler handler =
110  LLVM_EXTENSION reinterpret_cast<LLVMFatalErrorHandler>(user_data);
111  handler(reason.c_str());
112 }
113 
116  LLVM_EXTENSION reinterpret_cast<void *>(Handler));
117 }
118 
121 }
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
static void bindingsErrorHandler(void *user_data, const std::string &reason, bool gen_crash_diag)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static void * ErrorHandlerUserData
void LLVMResetFatalErrorHandler()
std::string str() const
str - Return the twine contents as a std::string.
Definition: Twine.cpp:16
LLVM_ATTRIBUTE_NORETURN void llvm_unreachable_internal(const char *msg=0, const char *file=0, unsigned line=0)
void(* fatal_error_handler_t)(void *user_data, const std::string &reason, bool gen_crash_diag)
An error handler callback.
Definition: ErrorHandling.h:26
static fatal_error_handler_t ErrorHandler
void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=0)
const char * data() const
Definition: StringRef.h:107
void remove_fatal_error_handler()
#define LLVM_EXTENSION
Definition: Compiler.h:282
void(* LLVMFatalErrorHandler)(const char *Reason)
Definition: Core.h:422
bool llvm_is_multithreaded()
Definition: Threading.cpp:54
void RunInterruptHandlers()
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
ssize_t write(int fildes, const void *buf, size_t nbyte);
void LLVMInstallFatalErrorHandler(LLVMFatalErrorHandler Handler)