blob: 7a2a9f835bd4fab601564c580f9efb13b795fcde [file] [log] [blame]
Howard Hinnantc632c502012-01-24 19:58:25 +00001//===------------------------- abort_message.cpp --------------------------===//
Howard Hinnant8f696ff2012-01-23 23:58:26 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <stdarg.h>
Howard Hinnant8f696ff2012-01-23 23:58:26 +000013#include "abort_message.h"
14
Dan Albert3ac101d2014-08-19 16:30:07 +000015#ifdef __BIONIC__
Dan Albertd2a72c42015-01-16 20:00:49 +000016#include <android/api-level.h>
17#if __ANDROID_API__ >= 21
Dan Albert3ac101d2014-08-19 16:30:07 +000018#include <syslog.h>
Dan Albertd2a72c42015-01-16 20:00:49 +000019extern "C" void android_set_abort_message(const char* msg);
20#else
21#include <assert.h>
22#endif // __ANDROID_API__ >= 21
23#endif // __BIONIC__
Dan Albert3ac101d2014-08-19 16:30:07 +000024
Dan Alberta1fce462015-02-05 01:33:15 +000025#ifdef __APPLE__
Howard Hinnantc632c502012-01-24 19:58:25 +000026# if defined(__has_include) && __has_include(<CrashReporterClient.h>)
Matthias Braun7940f192015-02-06 01:25:08 +000027# define HAVE_CRASHREPORTERCLIENT_H
Howard Hinnantc632c502012-01-24 19:58:25 +000028# include <CrashReporterClient.h>
Howard Hinnantc632c502012-01-24 19:58:25 +000029# endif
30#endif
31
Howard Hinnant8f696ff2012-01-23 23:58:26 +000032void abort_message(const char* format, ...)
33{
Howard Hinnantae154282012-01-24 00:52:33 +000034 // write message to stderr
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +000035#if !defined(NDEBUG) || !defined(LIBCXXABI_BAREMETAL)
Dan Alberta1fce462015-02-05 01:33:15 +000036#ifdef __APPLE__
Howard Hinnantae154282012-01-24 00:52:33 +000037 fprintf(stderr, "libc++abi.dylib: ");
Howard Hinnant8f696ff2012-01-23 23:58:26 +000038#endif
Howard Hinnantae154282012-01-24 00:52:33 +000039 va_list list;
40 va_start(list, format);
41 vfprintf(stderr, format, list);
42 va_end(list);
43 fprintf(stderr, "\n");
Ranjeet Singhfd7644a2017-02-24 16:43:36 +000044#endif
Dan Albert3ac101d2014-08-19 16:30:07 +000045
Matthias Braun7940f192015-02-06 01:25:08 +000046#if defined(__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
Howard Hinnantc632c502012-01-24 19:58:25 +000047 // record message in crash report
48 char* buffer;
49 va_list list2;
50 va_start(list2, format);
51 vasprintf(&buffer, format, list2);
52 va_end(list2);
53 CRSetCrashLogMessage(buffer);
Dan Alberta1fce462015-02-05 01:33:15 +000054#elif defined(__BIONIC__)
Dan Albert3ac101d2014-08-19 16:30:07 +000055 char* buffer;
56 va_list list2;
57 va_start(list2, format);
58 vasprintf(&buffer, format, list2);
59 va_end(list2);
60
Dan Albertd2a72c42015-01-16 20:00:49 +000061#if __ANDROID_API__ >= 21
Dan Albert3ac101d2014-08-19 16:30:07 +000062 // Show error in tombstone.
63 android_set_abort_message(buffer);
64
65 // Show error in logcat.
66 openlog("libc++abi", 0, 0);
67 syslog(LOG_CRIT, "%s", buffer);
68 closelog();
Dan Albertd2a72c42015-01-16 20:00:49 +000069#else
70 // The good error reporting wasn't available in Android until L. Since we're
71 // about to abort anyway, just call __assert2, which will log _somewhere_
72 // (tombstone and/or logcat) in older releases.
73 __assert2(__FILE__, __LINE__, __func__, buffer);
74#endif // __ANDROID_API__ >= 21
75#endif // __BIONIC__
Howard Hinnantc632c502012-01-24 19:58:25 +000076
Howard Hinnantae154282012-01-24 00:52:33 +000077 abort();
Howard Hinnant8f696ff2012-01-23 23:58:26 +000078}