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>
80 OBJC_EXPORT void (*_error)(id, const char *, va_list);
82 static void _objc_trap(void) __attribute__((noreturn));
84 // Return true if c is a UTF8 continuation byte
85 static bool isUTF8Continuation(char c)
87 return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
90 // Add "message" to any forthcoming crash log.
91 static void _objc_crashlog(const char *message)
97 // for debugging at BOOT time.
98 extern char **_NSGetProgname(void);
99 FILE *crashlog = fopen("/_objc_crash.log", "a");
100 setbuf(crashlog, NULL);
101 fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
107 static mutex_t crashlog_lock;
108 mutex_locker_t lock(crashlog_lock);
110 char *oldmsg = (char *)CRGetCrashLogMessage();
112 const size_t limit = 8000;
115 newmsg = strdup(message);
116 } else if ((oldlen = strlen(oldmsg)) > limit) {
117 // limit total length by dropping old contents
118 char *truncmsg = oldmsg + oldlen - limit;
119 // advance past partial UTF-8 bytes
120 while (isUTF8Continuation(*truncmsg)) truncmsg++;
121 asprintf(&newmsg, "... %s\n%s", truncmsg, message);
123 asprintf(&newmsg, "%s\n%s", oldmsg, message);
127 // Strip trailing newline
128 char *c = &newmsg[strlen(newmsg)-1];
129 if (*c == '\n') *c = '\0';
131 if (oldmsg) free(oldmsg);
132 CRSetCrashLogMessage(newmsg);
136 // Returns true if logs should be sent to stderr as well as syslog.
137 // Copied from CFUtilities.c
138 static bool also_do_stderr(void)
141 int ret = fstat(STDERR_FILENO, &st);
142 if (ret < 0) return false;
143 mode_t m = st.st_mode & S_IFMT;
144 if (m == S_IFREG || m == S_IFSOCK || m == S_IFIFO || m == S_IFCHR) {
150 // Print "message" to the console.
151 static void _objc_syslog(const char *message)
153 _simple_asl_log(ASL_LEVEL_ERR, nil, message);
155 if (also_do_stderr()) {
156 write(STDERR_FILENO, message, strlen(message));
161 * _objc_error is the default *_error handler.
164 __attribute__((noreturn))
166 // used by ExceptionHandling.framework
168 void _objc_error(id self, const char *fmt, va_list ap)
173 vasprintf(&buf1, fmt, ap);
174 asprintf(&buf2, "objc[%d]: %s: %s\n",
175 getpid(), object_getClassName(self), buf1);
177 _objc_crashlog(buf2);
183 * this routine handles errors that involve an object (or class).
185 void __objc_error(id rcv, const char *fmt, ...)
191 (*_error)(rcv, fmt, vp);
193 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
198 * this routine handles severe runtime errors...like not being able
199 * to read the mach headers, allocate space, etc...very uncommon.
201 void _objc_fatal(const char *fmt, ...)
208 vasprintf(&buf1, fmt, ap);
211 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
213 _objc_crashlog(buf2);
219 * this routine handles soft runtime errors...like not being able
220 * add a category to a class (because it wasn't linked in).
222 void _objc_inform(const char *fmt, ...)
229 vasprintf(&buf1, fmt, ap);
232 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
241 * Like _objc_inform(), but prints the message only in any
242 * forthcoming crash log, not to the console.
244 void _objc_inform_on_crash(const char *fmt, ...)
251 vasprintf(&buf1, fmt, ap);
254 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
255 _objc_crashlog(buf2);
263 * Like calling both _objc_inform and _objc_inform_on_crash.
265 void _objc_inform_now_and_on_crash(const char *fmt, ...)
272 vasprintf(&buf1, fmt, ap);
275 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
276 _objc_crashlog(buf2);
284 /* Kill the process in a way that generates a crash log.
285 * This is better than calling exit(). */
286 static void _objc_trap(void)
291 /* Try to keep _objc_warn_deprecated out of crash logs
292 * caused by _objc_trap(). rdar://4546883 */
293 __attribute__((used))
294 static void _objc_trap2(void)
303 void _objc_warn_deprecated(void)
306 void _objc_inform_deprecated(const char *oldf, const char *newf)
308 if (PrintDeprecation) {
310 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
312 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
315 _objc_warn_deprecated();