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