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 #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
25 # include <CrashReporterClient.h>
26 # define _LIBCXXABI_USE_CRASHREPORTER_CLIENT
27 #endif
28
abort_message(const char * format,...)29 void abort_message(const char* format, ...)
30 {
31 // Write message to stderr. We do this before formatting into a
32 // variable-size buffer so that we still get some information if
33 // formatting into the variable-sized buffer fails.
34 #if !defined(NDEBUG) || !defined(LIBCXXABI_BAREMETAL)
35 {
36 fprintf(stderr, "libc++abi: ");
37 va_list list;
38 va_start(list, format);
39 vfprintf(stderr, format, list);
40 va_end(list);
41 fprintf(stderr, "\n");
42 }
43 #endif
44
45 // Format the arguments into an allocated buffer. We leak the buffer on
46 // purpose, since we're about to abort() anyway.
47 #if defined(_LIBCXXABI_USE_CRASHREPORTER_CLIENT)
48 char* buffer;
49 va_list list;
50 va_start(list, format);
51 vasprintf(&buffer, format, list);
52 va_end(list);
53
54 CRSetCrashLogMessage(buffer);
55 #elif defined(__BIONIC__)
56 char* buffer;
57 va_list list;
58 va_start(list, format);
59 vasprintf(&buffer, format, list);
60 va_end(list);
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