xref: /freebsd/contrib/llvm-project/libcxx/src/verbose_abort.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
161cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
261cfbce3SDimitry Andric //
361cfbce3SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
461cfbce3SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
561cfbce3SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
661cfbce3SDimitry Andric //
761cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
861cfbce3SDimitry Andric 
961cfbce3SDimitry Andric #include <__config>
1061cfbce3SDimitry Andric #include <__verbose_abort>
1161cfbce3SDimitry Andric #include <cstdarg>
1261cfbce3SDimitry Andric #include <cstdio>
1361cfbce3SDimitry Andric #include <cstdlib>
1461cfbce3SDimitry Andric 
1561cfbce3SDimitry Andric #ifdef __BIONIC__
1661cfbce3SDimitry Andric #  include <android/api-level.h>
1761cfbce3SDimitry Andric #  if __ANDROID_API__ >= 21
1861cfbce3SDimitry Andric #    include <syslog.h>
1961cfbce3SDimitry Andric extern "C" void android_set_abort_message(const char* msg);
2061cfbce3SDimitry Andric #  else
2161cfbce3SDimitry Andric #    include <assert.h>
2261cfbce3SDimitry Andric #  endif // __ANDROID_API__ >= 21
2361cfbce3SDimitry Andric #endif   // __BIONIC__
2461cfbce3SDimitry Andric 
2561cfbce3SDimitry Andric #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
2661cfbce3SDimitry Andric #  include <CrashReporterClient.h>
2761cfbce3SDimitry Andric #endif
2861cfbce3SDimitry Andric 
2961cfbce3SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3061cfbce3SDimitry Andric 
31*cb14a3feSDimitry Andric _LIBCPP_WEAK void __libcpp_verbose_abort(char const* format, ...) {
3261cfbce3SDimitry Andric   // Write message to stderr. We do this before formatting into a
3361cfbce3SDimitry Andric   // buffer so that we still get some information out if that fails.
3461cfbce3SDimitry Andric   {
3561cfbce3SDimitry Andric     va_list list;
3661cfbce3SDimitry Andric     va_start(list, format);
3761cfbce3SDimitry Andric     std::vfprintf(stderr, format, list);
3861cfbce3SDimitry Andric     va_end(list);
3961cfbce3SDimitry Andric   }
4061cfbce3SDimitry Andric 
4161cfbce3SDimitry Andric   // Format the arguments into an allocated buffer for CrashReport & friends.
4261cfbce3SDimitry Andric   // We leak the buffer on purpose, since we're about to abort() anyway.
43*cb14a3feSDimitry Andric   char* buffer;
44*cb14a3feSDimitry Andric   (void)buffer;
4561cfbce3SDimitry Andric   va_list list;
4661cfbce3SDimitry Andric   va_start(list, format);
4761cfbce3SDimitry Andric 
4861cfbce3SDimitry Andric #if defined(__APPLE__) && __has_include(<CrashReporterClient.h>)
4961cfbce3SDimitry Andric   // Note that we should technically synchronize accesses here (by e.g. taking a lock),
5061cfbce3SDimitry Andric   // however concretely we're only setting a pointer, so the likelihood of a race here
5161cfbce3SDimitry Andric   // is low.
5261cfbce3SDimitry Andric   vasprintf(&buffer, format, list);
5361cfbce3SDimitry Andric   CRSetCrashLogMessage(buffer);
5461cfbce3SDimitry Andric #elif defined(__BIONIC__)
5561cfbce3SDimitry Andric   vasprintf(&buffer, format, list);
5661cfbce3SDimitry Andric 
5761cfbce3SDimitry Andric #  if __ANDROID_API__ >= 21
5861cfbce3SDimitry Andric   // Show error in tombstone.
5961cfbce3SDimitry Andric   android_set_abort_message(buffer);
6061cfbce3SDimitry Andric 
6161cfbce3SDimitry Andric   // Show error in logcat.
6261cfbce3SDimitry Andric   openlog("libc++", 0, 0);
6361cfbce3SDimitry Andric   syslog(LOG_CRIT, "%s", buffer);
6461cfbce3SDimitry Andric   closelog();
6561cfbce3SDimitry Andric #  else
6661cfbce3SDimitry Andric   // The good error reporting wasn't available in Android until L. Since we're
6761cfbce3SDimitry Andric   // about to abort anyway, just call __assert2, which will log _somewhere_
6861cfbce3SDimitry Andric   // (tombstone and/or logcat) in older releases.
6961cfbce3SDimitry Andric   __assert2(__FILE__, __LINE__, __func__, buffer);
7061cfbce3SDimitry Andric #  endif // __ANDROID_API__ >= 21
7161cfbce3SDimitry Andric #endif
7261cfbce3SDimitry Andric   va_end(list);
7361cfbce3SDimitry Andric 
7461cfbce3SDimitry Andric   std::abort();
7561cfbce3SDimitry Andric }
7661cfbce3SDimitry Andric 
7761cfbce3SDimitry Andric _LIBCPP_END_NAMESPACE_STD
78