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 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 <cassert> 22 #include <cstdlib> 23 using namespace llvm; 24 using namespace std; 25 26 static fatal_error_handler_t ErrorHandler = 0; 27 static void *ErrorHandlerUserData = 0; 28 29 void llvm::install_fatal_error_handler(fatal_error_handler_t handler, 30 void *user_data) { 31 assert(!llvm_is_multithreaded() && 32 "Cannot register error handlers after starting multithreaded mode!\n"); 33 assert(!ErrorHandler && "Error handler already registered!\n"); 34 ErrorHandler = handler; 35 ErrorHandlerUserData = user_data; 36 } 37 38 void llvm::remove_fatal_error_handler() { 39 ErrorHandler = 0; 40 } 41 42 void llvm::report_fatal_error(const char *reason) { 43 report_fatal_error(Twine(reason)); 44 } 45 46 void llvm::report_fatal_error(const std::string &reason) { 47 report_fatal_error(Twine(reason)); 48 } 49 50 void llvm::report_fatal_error(const Twine &reason) { 51 if (!ErrorHandler) { 52 errs() << "LLVM ERROR: " << reason << "\n"; 53 } else { 54 ErrorHandler(ErrorHandlerUserData, reason.str()); 55 } 56 57 // If we reached here, we are failing ungracefully. Run the interrupt handlers 58 // to make sure any special cleanups get done, in particular that we remove 59 // files registered with RemoveFileOnSignal. 60 sys::RunInterruptHandlers(); 61 62 exit(1); 63 } 64 65 void llvm::llvm_unreachable_internal(const char *msg, const char *file, 66 unsigned line) { 67 // This code intentionally doesn't call the ErrorHandler callback, because 68 // llvm_unreachable is intended to be used to indicate "impossible" 69 // situations, and not legitimate runtime errors. 70 if (msg) 71 dbgs() << msg << "\n"; 72 dbgs() << "UNREACHABLE executed"; 73 if (file) 74 dbgs() << " at " << file << ":" << line; 75 dbgs() << "!\n"; 76 abort(); 77 } 78