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 <syslog.h>
17extern "C" void android_set_abort_message(const char* msg);
18#endif // __BIONIC__
19
20#if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
21# include <CrashReporterClient.h>
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
26
27[[gnu::weak]] void __libcpp_verbose_abort(char const* format, ...) noexcept {
28 // Write message to stderr. We do this before formatting into a
29 // buffer so that we still get some information out if that fails.
30 {
31 va_list list;
32 va_start(list, format);
33 std::vfprintf(stderr, format: format, arg: list);
34 va_end(list);
35 }
36
37 // Format the arguments into an allocated buffer for CrashReport & friends.
38 // We leak the buffer on purpose, since we're about to abort() anyway.
39 char* buffer;
40 (void)buffer;
41 va_list list;
42 va_start(list, format);
43
44#if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
45 // Note that we should technically synchronize accesses here (by e.g. taking a lock),
46 // however concretely we're only setting a pointer, so the likelihood of a race here
47 // is low.
48 vasprintf(&buffer, format, list);
49 CRSetCrashLogMessage(buffer);
50#elif defined(__BIONIC__)
51 vasprintf(&buffer, format, list);
52
53 // Show error in tombstone.
54 android_set_abort_message(buffer);
55
56 // Show error in logcat.
57 openlog("libc++", 0, 0);
58 syslog(LOG_CRIT, "%s", buffer);
59 closelog();
60#endif
61 va_end(list);
62
63 std::abort();
64}
65
66_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
67_LIBCPP_END_NAMESPACE_STD
68