]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-errors.m
objc4-371.2.tar.gz
[apple/objc4.git] / runtime / objc-errors.m
1 /*
2 * Copyright (c) 1999-2003, 2005-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * objc-errors.m
25 * Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
26 */
27
28
29 #include <stdarg.h>
30 #include <unistd.h>
31 #include <syslog.h>
32 #include <sys/fcntl.h>
33
34 #import "objc-private.h"
35
36 __private_extern__ char *__crashreporter_info__ = NULL;
37
38 OBJC_EXPORT void (*_error)(id, const char *, va_list);
39
40 static void _objc_trap(void) __attribute__((noreturn));
41
42 // Add "message" to any forthcoming crash log.
43 static void _objc_crashlog(const char *message)
44 {
45 char *newmsg;
46
47 if (!__crashreporter_info__) {
48 newmsg = strdup(message);
49 } else {
50 asprintf(&newmsg, "%s\n%s", __crashreporter_info__, message);
51 }
52
53 if (newmsg) {
54 // Strip trailing newline
55 char *c = &newmsg[strlen(newmsg)-1];
56 if (*c == '\n') *c = '\0';
57
58 if (__crashreporter_info__) free(__crashreporter_info__);
59 __crashreporter_info__ = newmsg;
60 }
61 }
62
63 // Print "message" to the console.
64 static void _objc_syslog(const char *message)
65 {
66 if (fcntl(STDERR_FILENO, F_GETFL, 0) != -1) {
67 // stderr is open - use it
68 write(STDERR_FILENO, message, strlen(message));
69 if (message[strlen(message)-1] != '\n') {
70 write(STDERR_FILENO, "\n", 1);
71 }
72 } else {
73 syslog(LOG_ERR, "%s", message);
74 }
75 }
76 /*
77 * this routine handles errors that involve an object (or class).
78 */
79 __private_extern__ void __objc_error(id rcv, const char *fmt, ...)
80 {
81 va_list vp;
82
83 va_start(vp,fmt);
84 #if !__OBJC2__
85 (*_error)(rcv, fmt, vp);
86 #endif
87 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
88 va_end(vp);
89 }
90
91 /*
92 * _objc_error is the default *_error handler.
93 */
94 #if __OBJC2__
95 __private_extern__
96 #endif
97 void _objc_error(id self, const char *fmt, va_list ap)
98 {
99 char *buf1;
100 char *buf2;
101
102 vasprintf(&buf1, fmt, ap);
103 asprintf(&buf2, "objc[%d]: %s: %s\n",
104 getpid(), object_getClassName(self), buf1);
105 _objc_syslog(buf2);
106 _objc_crashlog(buf2);
107
108 _objc_trap();
109 }
110
111 /*
112 * this routine handles severe runtime errors...like not being able
113 * to read the mach headers, allocate space, etc...very uncommon.
114 */
115 __private_extern__ void _objc_fatal(const char *fmt, ...)
116 {
117 va_list ap;
118 char *buf1;
119 char *buf2;
120
121 va_start(ap,fmt);
122 vasprintf(&buf1, fmt, ap);
123 va_end (ap);
124
125 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
126 _objc_syslog(buf2);
127 _objc_crashlog(buf2);
128
129 _objc_trap();
130 }
131
132 /*
133 * this routine handles soft runtime errors...like not being able
134 * add a category to a class (because it wasn't linked in).
135 */
136 __private_extern__ void _objc_inform(const char *fmt, ...)
137 {
138 va_list ap;
139 char *buf1;
140 char *buf2;
141
142 va_start (ap,fmt);
143 vasprintf(&buf1, fmt, ap);
144 va_end (ap);
145
146 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
147 _objc_syslog(buf2);
148
149 free(buf2);
150 free(buf1);
151 }
152
153
154 /*
155 * Like _objc_inform(), but prints the message only in any
156 * forthcoming crash log, not to the console.
157 */
158 __private_extern__ void _objc_inform_on_crash(const char *fmt, ...)
159 {
160 va_list ap;
161 char *buf1;
162 char *buf2;
163
164 va_start (ap,fmt);
165 vasprintf(&buf1, fmt, ap);
166 va_end (ap);
167
168 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
169 _objc_crashlog(buf2);
170
171 free(buf2);
172 free(buf1);
173 }
174
175
176 /*
177 * Like calling both _objc_inform and _objc_inform_on_crash.
178 */
179 __private_extern__ void _objc_inform_now_and_on_crash(const char *fmt, ...)
180 {
181 va_list ap;
182 char *buf1;
183 char *buf2;
184
185 va_start (ap,fmt);
186 vasprintf(&buf1, fmt, ap);
187 va_end (ap);
188
189 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
190 _objc_crashlog(buf2);
191 _objc_syslog(buf2);
192
193 free(buf2);
194 free(buf1);
195 }
196
197
198 /* Kill the process in a way that generates a crash log.
199 * This is better than calling exit(). */
200 static void _objc_trap(void)
201 {
202 __builtin_trap();
203 }
204
205 /* Try to keep _objc_warn_deprecated out of crash logs
206 * caused by _objc_trap(). rdar://4546883 */
207 __attribute__((used))
208 static void _objc_trap2(void)
209 {
210 __builtin_trap();
211 }
212
213 __private_extern__ void _objc_warn_deprecated(const char *old, const char *new)
214 {
215 if (PrintDeprecation) {
216 if (new) {
217 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", old, new);
218 } else {
219 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", old);
220 }
221 }
222 }
223
224
225 /* Entry points for breakable errors. For some reason, can't inhibit the compiler's inlining aggression.
226 */
227
228 __private_extern__ void objc_assign_ivar_error(id base, ptrdiff_t offset) {
229 }
230
231 __private_extern__ void objc_assign_global_error(id value, id *slot) {
232 }
233
234 __private_extern__ void objc_exception_during_finalize_error(void) {
235 }
236