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)
79 // Return true if c is a UTF8 continuation byte
80 static bool isUTF8Continuation(char c)
82 return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
85 // Add "message" to any forthcoming crash log.
86 mutex_t crashlog_lock;
87 static void _objc_crashlog(const char *message)
93 // for debugging at BOOT time.
94 extern char **_NSGetProgname(void);
95 FILE *crashlog = fopen("/_objc_crash.log", "a");
96 setbuf(crashlog, NULL);
97 fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
103 mutex_locker_t lock(crashlog_lock);
105 char *oldmsg = (char *)CRGetCrashLogMessage();
107 const size_t limit = 8000;
110 newmsg = strdup(message);
111 } else if ((oldlen = strlen(oldmsg)) > limit) {
112 // limit total length by dropping old contents
113 char *truncmsg = oldmsg + oldlen - limit;
114 // advance past partial UTF-8 bytes
115 while (isUTF8Continuation(*truncmsg)) truncmsg++;
116 asprintf(&newmsg, "... %s\n%s", truncmsg, message);
118 asprintf(&newmsg, "%s\n%s", oldmsg, message);
122 // Strip trailing newline
123 char *c = &newmsg[strlen(newmsg)-1];
124 if (*c == '\n') *c = '\0';
126 if (oldmsg) free(oldmsg);
127 CRSetCrashLogMessage(newmsg);
131 // Returns true if logs should be sent to stderr as well as syslog.
132 // Copied from CFUtilities.c
133 static bool also_do_stderr(void)
136 int ret = fstat(STDERR_FILENO, &st);
137 if (ret < 0) return false;
138 mode_t m = st.st_mode & S_IFMT;
139 if (m == S_IFREG || m == S_IFSOCK || m == S_IFIFO || m == S_IFCHR) {
145 // Print "message" to the console.
146 static void _objc_syslog(const char *message)
148 _simple_asl_log(ASL_LEVEL_ERR, nil, message);
150 if (also_do_stderr()) {
151 write(STDERR_FILENO, message, strlen(message));
156 * _objc_error is the default *_error handler.
159 // used by ExceptionHandling.framework
161 __attribute__((noreturn, cold))
162 void _objc_error(id self, const char *fmt, va_list ap)
165 vasprintf(&buf, fmt, ap);
166 _objc_fatal("%s: %s", object_getClassName(self), buf);
170 * this routine handles errors that involve an object (or class).
172 void __objc_error(id rcv, const char *fmt, ...)
178 (*_error)(rcv, fmt, vp);
180 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
184 static __attribute__((noreturn, cold))
185 void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap)
188 vasprintf(&buf1, fmt, ap);
191 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
194 if (DebugDontCrash) {
196 asprintf(&buf3, "objc[%d]: HALTED\n", getpid());
201 _objc_crashlog(buf1);
202 abort_with_reason(OS_REASON_OBJC, reason, buf1, flags);
206 void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
207 const char *fmt, ...)
211 _objc_fatalv(reason, flags, fmt, ap);
214 void _objc_fatal(const char *fmt, ...)
218 _objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED,
219 OS_REASON_FLAG_ONE_TIME_FAILURE,
224 * this routine handles soft runtime errors...like not being able
225 * add a category to a class (because it wasn't linked in).
227 void _objc_inform(const char *fmt, ...)
234 vasprintf(&buf1, fmt, ap);
237 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
246 * Like _objc_inform(), but prints the message only in any
247 * forthcoming crash log, not to the console.
249 void _objc_inform_on_crash(const char *fmt, ...)
256 vasprintf(&buf1, fmt, ap);
259 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
260 _objc_crashlog(buf2);
268 * Like calling both _objc_inform and _objc_inform_on_crash.
270 void _objc_inform_now_and_on_crash(const char *fmt, ...)
277 vasprintf(&buf1, fmt, ap);
280 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
281 _objc_crashlog(buf2);
292 void _objc_warn_deprecated(void)
295 void _objc_inform_deprecated(const char *oldf, const char *newf)
297 if (PrintDeprecation) {
299 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
301 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
304 _objc_warn_deprecated();