xref: /llvm-project/libcxxabi/src/abort_message.cpp (revision 9fd92634749c75b39be829c22240567ccda3ffce)
1 //===----------------------------------------------------------------------===//
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 <syslog.h>
16 extern "C" void android_set_abort_message(const char* msg);
17 #endif // __BIONIC__
18 
19 #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
20 #   include <CrashReporterClient.h>
21 #   define _LIBCXXABI_USE_CRASHREPORTER_CLIENT
22 #endif
23 
24 void __abort_message(const char* format, ...)
25 {
26     // Write message to stderr. We do this before formatting into a
27     // variable-size buffer so that we still get some information if
28     // formatting into the variable-sized buffer fails.
29 #if !defined(NDEBUG) || !defined(LIBCXXABI_BAREMETAL)
30     {
31         fprintf(stderr, "libc++abi: ");
32         va_list list;
33         va_start(list, format);
34         vfprintf(stderr, format, list);
35         va_end(list);
36         fprintf(stderr, "\n");
37     }
38 #endif
39 
40     // Format the arguments into an allocated buffer. We leak the buffer on
41     // purpose, since we're about to abort() anyway.
42 #if defined(_LIBCXXABI_USE_CRASHREPORTER_CLIENT)
43     char* buffer;
44     va_list list;
45     va_start(list, format);
46     vasprintf(&buffer, format, list);
47     va_end(list);
48 
49     CRSetCrashLogMessage(buffer);
50 #elif defined(__BIONIC__)
51     char* buffer;
52     va_list list;
53     va_start(list, format);
54     vasprintf(&buffer, format, list);
55     va_end(list);
56 
57     // Show error in tombstone.
58     android_set_abort_message(buffer);
59 
60     // Show error in logcat.
61     openlog("libc++abi", 0, 0);
62     syslog(LOG_CRIT, "%s", buffer);
63     closelog();
64 #endif // __BIONIC__
65 
66     abort();
67 }
68