2 * Copyright (c) 1999-2003, 2005-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
28 #include "objc-private.h"
34 __private_extern__ void _objc_inform_on_crash(const char *fmt, ...)
38 __private_extern__ void _objc_inform(const char *fmt, ...)
47 __private_extern__ void _objc_fatal(const char *fmt, ...)
58 __private_extern__ void __objc_error(id rcv, const char *fmt, ...)
66 __private_extern__ void _objc_error(id rcv, const char *fmt, va_list args)
73 __private_extern__ char *__crashreporter_info__ = NULL;
75 OBJC_EXPORT void (*_error)(id, const char *, va_list);
77 static void _objc_trap(void) __attribute__((noreturn));
79 // Add "message" to any forthcoming crash log.
80 static void _objc_crashlog(const char *message)
86 // for debugging at BOOT time.
87 extern char **_NSGetProgname(void);
88 FILE *crashlog = fopen("/_objc_crash.log", "a");
89 setbuf(crashlog, NULL);
90 fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
96 if (!__crashreporter_info__) {
97 newmsg = strdup(message);
99 asprintf(&newmsg, "%s\n%s", __crashreporter_info__, message);
103 // Strip trailing newline
104 char *c = &newmsg[strlen(newmsg)-1];
105 if (*c == '\n') *c = '\0';
107 if (__crashreporter_info__) free(__crashreporter_info__);
108 __crashreporter_info__ = newmsg;
112 // Print "message" to the console.
113 static void _objc_syslog(const char *message)
115 if (fcntl(STDERR_FILENO, F_GETFL, 0) != -1) {
116 // stderr is open - use it
117 write(STDERR_FILENO, message, strlen(message));
118 if (message[strlen(message)-1] != '\n') {
119 write(STDERR_FILENO, "\n", 1);
122 syslog(LOG_ERR, "%s", message);
126 * this routine handles errors that involve an object (or class).
128 __private_extern__ void __objc_error(id rcv, const char *fmt, ...)
134 (*_error)(rcv, fmt, vp);
136 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
141 * _objc_error is the default *_error handler.
146 void _objc_error(id self, const char *fmt, va_list ap)
151 vasprintf(&buf1, fmt, ap);
152 asprintf(&buf2, "objc[%d]: %s: %s\n",
153 getpid(), object_getClassName(self), buf1);
155 _objc_crashlog(buf2);
161 * this routine handles severe runtime errors...like not being able
162 * to read the mach headers, allocate space, etc...very uncommon.
164 __private_extern__ void _objc_fatal(const char *fmt, ...)
171 vasprintf(&buf1, fmt, ap);
174 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
176 _objc_crashlog(buf2);
182 * this routine handles soft runtime errors...like not being able
183 * add a category to a class (because it wasn't linked in).
185 __private_extern__ void _objc_inform(const char *fmt, ...)
192 vasprintf(&buf1, fmt, ap);
195 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
204 * Like _objc_inform(), but prints the message only in any
205 * forthcoming crash log, not to the console.
207 __private_extern__ void _objc_inform_on_crash(const char *fmt, ...)
214 vasprintf(&buf1, fmt, ap);
217 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
218 _objc_crashlog(buf2);
226 * Like calling both _objc_inform and _objc_inform_on_crash.
228 __private_extern__ void _objc_inform_now_and_on_crash(const char *fmt, ...)
235 vasprintf(&buf1, fmt, ap);
238 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
239 _objc_crashlog(buf2);
247 /* Kill the process in a way that generates a crash log.
248 * This is better than calling exit(). */
249 static void _objc_trap(void)
254 /* Try to keep _objc_warn_deprecated out of crash logs
255 * caused by _objc_trap(). rdar://4546883 */
256 __attribute__((used))
257 static void _objc_trap2(void)
265 __private_extern__ void _objc_warn_deprecated(const char *oldf, const char *newf)
267 if (PrintDeprecation) {
269 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
271 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
277 /* Entry points for breakable errors. For some reason, can't inhibit the compiler's inlining aggression.
280 __private_extern__ void objc_assign_ivar_error(id base, ptrdiff_t offset) {
283 __private_extern__ void objc_assign_global_error(id value, id *slot) {
286 __private_extern__ void objc_exception_during_finalize_error(void) {