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 void _objc_inform_on_crash(const char *fmt, ...)
38 void _objc_inform(const char *fmt, ...)
47 void _objc_fatal(const char *fmt, ...)
58 void __objc_error(id rcv, const char *fmt, ...)
68 void _objc_error(id rcv, const char *fmt, va_list args)
77 #include <vproc_priv.h>
79 OBJC_EXPORT void (*_error)(id, const char *, va_list);
81 static void _objc_trap(void) __attribute__((noreturn));
83 // Add "message" to any forthcoming crash log.
84 static void _objc_crashlog(const char *message)
90 // for debugging at BOOT time.
91 extern char **_NSGetProgname(void);
92 FILE *crashlog = fopen("/_objc_crash.log", "a");
93 setbuf(crashlog, NULL);
94 fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
100 static mutex_t crashlog_lock = MUTEX_INITIALIZER;
101 mutex_lock(&crashlog_lock);
103 char *oldmsg = (char *)CRGetCrashLogMessage();
106 newmsg = strdup(message);
108 asprintf(&newmsg, "%s\n%s", oldmsg, message);
112 // Strip trailing newline
113 char *c = &newmsg[strlen(newmsg)-1];
114 if (*c == '\n') *c = '\0';
116 if (oldmsg) free(oldmsg);
117 CRSetCrashLogMessage(newmsg);
120 mutex_unlock(&crashlog_lock);
123 // Returns true if logs should be sent to stderr as well as syslog.
124 // Copied from CFUtilities.c
125 static bool also_do_stderr(void)
128 int ret = fstat(STDERR_FILENO, &st);
129 if (ret < 0) return false;
130 mode_t m = st.st_mode & S_IFMT;
131 if (m == S_IFREG || m == S_IFSOCK) return true;
132 if (!(m == S_IFIFO || m == S_IFCHR)) return false;
134 // if it could be a pipe back to launchd, fail
136 vproc_swap_integer(NULL, VPROC_GSK_IS_MANAGED, NULL, &val);
137 if (val) return false;
142 // Print "message" to the console.
143 static void _objc_syslog(const char *message)
145 syslog(LOG_ERR, "%s", message);
147 if (also_do_stderr()) {
148 write(STDERR_FILENO, message, strlen(message));
153 * _objc_error is the default *_error handler.
156 __attribute__((noreturn))
158 // used by ExceptionHandling.framework
160 void _objc_error(id self, const char *fmt, va_list ap)
165 vasprintf(&buf1, fmt, ap);
166 asprintf(&buf2, "objc[%d]: %s: %s\n",
167 getpid(), object_getClassName(self), buf1);
169 _objc_crashlog(buf2);
175 * this routine handles errors that involve an object (or class).
177 void __objc_error(id rcv, const char *fmt, ...)
183 (*_error)(rcv, fmt, vp);
185 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
190 * this routine handles severe runtime errors...like not being able
191 * to read the mach headers, allocate space, etc...very uncommon.
193 void _objc_fatal(const char *fmt, ...)
200 vasprintf(&buf1, fmt, ap);
203 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
205 _objc_crashlog(buf2);
211 * this routine handles soft runtime errors...like not being able
212 * add a category to a class (because it wasn't linked in).
214 void _objc_inform(const char *fmt, ...)
221 vasprintf(&buf1, fmt, ap);
224 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
233 * Like _objc_inform(), but prints the message only in any
234 * forthcoming crash log, not to the console.
236 void _objc_inform_on_crash(const char *fmt, ...)
243 vasprintf(&buf1, fmt, ap);
246 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
247 _objc_crashlog(buf2);
255 * Like calling both _objc_inform and _objc_inform_on_crash.
257 void _objc_inform_now_and_on_crash(const char *fmt, ...)
264 vasprintf(&buf1, fmt, ap);
267 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
268 _objc_crashlog(buf2);
276 /* Kill the process in a way that generates a crash log.
277 * This is better than calling exit(). */
278 static void _objc_trap(void)
283 /* Try to keep _objc_warn_deprecated out of crash logs
284 * caused by _objc_trap(). rdar://4546883 */
285 __attribute__((used))
286 static void _objc_trap2(void)
295 void _objc_warn_deprecated(void)
298 void _objc_inform_deprecated(const char *oldf, const char *newf)
300 if (PrintDeprecation) {
302 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
304 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
307 _objc_warn_deprecated();