Home | History | Annotate | Download | only in src
      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 #pragma GCC visibility push(hidden)
     26 
     27 #ifdef __APPLE__
     28 #   if defined(__has_include) && __has_include(<CrashReporterClient.h>)
     29 #       define HAVE_CRASHREPORTERCLIENT_H
     30 #       include <CrashReporterClient.h>
     31 #   endif
     32 #endif
     33 
     34 __attribute__((visibility("hidden"), noreturn))
     35 void abort_message(const char* format, ...)
     36 {
     37     // write message to stderr
     38 #ifdef __APPLE__
     39     fprintf(stderr, "libc++abi.dylib: ");
     40 #endif
     41     va_list list;
     42     va_start(list, format);
     43     vfprintf(stderr, format, list);
     44     va_end(list);
     45     fprintf(stderr, "\n");
     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 }
     80 
     81 #pragma GCC visibility pop
     82