blob: 5e25c0f347254af3bb9d8df2f41e83dbd9e3211a [file] [log] [blame]
Howard Hinnant0d517a42012-01-24 19:58:25 +00001//===------------------------- abort_message.cpp --------------------------===//
Howard Hinnantbfbf7002012-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 Hinnantbfbf7002012-01-23 23:58:26 +000013#include "abort_message.h"
14
Dan Albert07a78742014-08-19 16:30:07 +000015#ifdef __BIONIC__
Dan Albertb32408e2015-01-16 20:00:49 +000016#include <android/api-level.h>
17#if __ANDROID_API__ >= 21
Dan Albert07a78742014-08-19 16:30:07 +000018#include <syslog.h>
Dan Albertb32408e2015-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 Albert07a78742014-08-19 16:30:07 +000024
Howard Hinnanteaa65af2012-02-02 20:47:28 +000025#pragma GCC visibility push(hidden)
26
Dan Albert3bd13ca2015-02-05 01:33:15 +000027#ifdef __APPLE__
Howard Hinnant0d517a42012-01-24 19:58:25 +000028# if defined(__has_include) && __has_include(<CrashReporterClient.h>)
Matthias Braun8a3e3392015-02-06 01:25:08 +000029# define HAVE_CRASHREPORTERCLIENT_H
Howard Hinnant0d517a42012-01-24 19:58:25 +000030# include <CrashReporterClient.h>
Howard Hinnant0d517a42012-01-24 19:58:25 +000031# endif
32#endif
33
34__attribute__((visibility("hidden"), noreturn))
Howard Hinnantbfbf7002012-01-23 23:58:26 +000035void abort_message(const char* format, ...)
36{
Howard Hinnant82a39902012-01-24 00:52:33 +000037 // write message to stderr
Dan Albert3bd13ca2015-02-05 01:33:15 +000038#ifdef __APPLE__
Howard Hinnant82a39902012-01-24 00:52:33 +000039 fprintf(stderr, "libc++abi.dylib: ");
Howard Hinnantbfbf7002012-01-23 23:58:26 +000040#endif
Howard Hinnant82a39902012-01-24 00:52:33 +000041 va_list list;
42 va_start(list, format);
43 vfprintf(stderr, format, list);
44 va_end(list);
45 fprintf(stderr, "\n");
Dan Albert07a78742014-08-19 16:30:07 +000046
Matthias Braun8a3e3392015-02-06 01:25:08 +000047#if defined(__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
Howard Hinnant0d517a42012-01-24 19:58:25 +000048 // record message in crash report
49 char* buffer;
50 va_list list2;
51 va_start(list2, format);
52 vasprintf(&buffer, format, list2);
53 va_end(list2);
54 CRSetCrashLogMessage(buffer);
Dan Albert3bd13ca2015-02-05 01:33:15 +000055#elif defined(__BIONIC__)
Dan Albert07a78742014-08-19 16:30:07 +000056 char* buffer;
57 va_list list2;
58 va_start(list2, format);
59 vasprintf(&buffer, format, list2);
60 va_end(list2);
61
Dan Albertb32408e2015-01-16 20:00:49 +000062#if __ANDROID_API__ >= 21
Dan Albert07a78742014-08-19 16:30:07 +000063 // Show error in tombstone.
64 android_set_abort_message(buffer);
65
66 // Show error in logcat.
67 openlog("libc++abi", 0, 0);
68 syslog(LOG_CRIT, "%s", buffer);
69 closelog();
Dan Albertb32408e2015-01-16 20:00:49 +000070#else
71 // The good error reporting wasn't available in Android until L. Since we're
72 // about to abort anyway, just call __assert2, which will log _somewhere_
73 // (tombstone and/or logcat) in older releases.
74 __assert2(__FILE__, __LINE__, __func__, buffer);
75#endif // __ANDROID_API__ >= 21
76#endif // __BIONIC__
Howard Hinnant0d517a42012-01-24 19:58:25 +000077
Howard Hinnant82a39902012-01-24 00:52:33 +000078 abort();
Howard Hinnantbfbf7002012-01-23 23:58:26 +000079}
Howard Hinnanteaa65af2012-02-02 20:47:28 +000080
81#pragma GCC visibility pop