abort_message.cpp (2179B)
1 //===------------------------- abort_message.cpp --------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <stdlib.h> 11 #include <stdio.h> 12 #include <stdarg.h> 13 #include "abort_message.h" 14 15 #ifdef __BIONIC__ 16 #include <android/api-level.h> 17 #if __ANDROID_API__ >= 21 18 #include <syslog.h> 19 extern "C" void android_set_abort_message(const char* msg); 20 #else 21 #include <assert.h> 22 #endif // __ANDROID_API__ >= 21 23 #endif // __BIONIC__ 24 25 #ifdef __APPLE__ 26 # if defined(__has_include) && __has_include(<CrashReporterClient.h>) 27 # define HAVE_CRASHREPORTERCLIENT_H 28 # include <CrashReporterClient.h> 29 # endif 30 #endif 31 32 void abort_message(const char* format, ...) 33 { 34 // write message to stderr 35 #if !defined(NDEBUG) || !defined(LIBCXXABI_BAREMETAL) 36 #ifdef __APPLE__ 37 fprintf(stderr, "libc++abi.dylib: "); 38 #endif 39 va_list list; 40 va_start(list, format); 41 vfprintf(stderr, format, list); 42 va_end(list); 43 fprintf(stderr, "\n"); 44 fflush(stderr); 45 #endif 46 47 #if defined(__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H) 48 // record message in crash report 49 char* buffer; 50 va_list list2; 51 va_start(list2, format); 52 vasprintf(&buffer, format, list2); 53 va_end(list2); 54 CRSetCrashLogMessage(buffer); 55 #elif defined(__BIONIC__) 56 char* buffer; 57 va_list list2; 58 va_start(list2, format); 59 vasprintf(&buffer, format, list2); 60 va_end(list2); 61 62 #if __ANDROID_API__ >= 21 63 // Show error in tombstone. 64 android_set_abort_message(buffer); 65 66 // Show error in logcat. 67 openlog("libc++abi", 0, 0); 68 syslog(LOG_CRIT, "%s", buffer); 69 closelog(); 70 #else 71 // The good error reporting wasn't available in Android until L. Since we're 72 // about to abort anyway, just call __assert2, which will log _somewhere_ 73 // (tombstone and/or logcat) in older releases. 74 __assert2(__FILE__, __LINE__, __func__, buffer); 75 #endif // __ANDROID_API__ >= 21 76 #endif // __BIONIC__ 77 78 abort(); 79 }