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 <__config>
10 #include <__verbose_abort>
11 #include <cstdarg>
12 #include <cstdio>
13 #include <cstdlib>
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 #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
26 #  include <CrashReporterClient.h>
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 _LIBCPP_WEAK
32 void __libcpp_verbose_abort(char const* format, ...) {
33   // Write message to stderr. We do this before formatting into a
34   // buffer so that we still get some information out if that fails.
35   {
36     va_list list;
37     va_start(list, format);
38     std::vfprintf(stderr, format, list);
39     va_end(list);
40   }
41 
42   // Format the arguments into an allocated buffer for CrashReport & friends.
43   // We leak the buffer on purpose, since we're about to abort() anyway.
44   char* buffer; (void)buffer;
45   va_list list;
46   va_start(list, format);
47 
48 #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
49   // Note that we should technically synchronize accesses here (by e.g. taking a lock),
50   // however concretely we're only setting a pointer, so the likelihood of a race here
51   // is low.
52   vasprintf(&buffer, format, list);
53   CRSetCrashLogMessage(buffer);
54 #elif defined(__BIONIC__)
55   vasprintf(&buffer, format, list);
56 
57 #  if __ANDROID_API__ >= 21
58   // Show error in tombstone.
59   android_set_abort_message(buffer);
60 
61   // Show error in logcat.
62   openlog("libc++", 0, 0);
63   syslog(LOG_CRIT, "%s", buffer);
64   closelog();
65 #  else
66   // The good error reporting wasn't available in Android until L. Since we're
67   // about to abort anyway, just call __assert2, which will log _somewhere_
68   // (tombstone and/or logcat) in older releases.
69   __assert2(__FILE__, __LINE__, __func__, buffer);
70 #  endif // __ANDROID_API__ >= 21
71 #endif
72   va_end(list);
73 
74   std::abort();
75 }
76 
77 _LIBCPP_END_NAMESPACE_STD
78