• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
abort_message(const char * format,...)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 #endif
45 
46 #if defined(__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
47     // record message in crash report
48     char* buffer;
49     va_list list2;
50     va_start(list2, format);
51     vasprintf(&buffer, format, list2);
52     va_end(list2);
53     CRSetCrashLogMessage(buffer);
54 #elif defined(__BIONIC__)
55     char* buffer;
56     va_list list2;
57     va_start(list2, format);
58     vasprintf(&buffer, format, list2);
59     va_end(list2);
60 
61 #if __ANDROID_API__ >= 21
62     // Show error in tombstone.
63     android_set_abort_message(buffer);
64 
65     // Show error in logcat.
66     openlog("libc++abi", 0, 0);
67     syslog(LOG_CRIT, "%s", buffer);
68     closelog();
69 #else
70     // The good error reporting wasn't available in Android until L. Since we're
71     // about to abort anyway, just call __assert2, which will log _somewhere_
72     // (tombstone and/or logcat) in older releases.
73     __assert2(__FILE__, __LINE__, __func__, buffer);
74 #endif // __ANDROID_API__ >= 21
75 #endif // __BIONIC__
76 
77     abort();
78 }
79