]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-errors.mm
objc4-680.tar.gz
[apple/objc4.git] / runtime / objc-errors.mm
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 #include "objc-private.h"
29
30 #if TARGET_OS_WIN32
31
32 #include <conio.h>
33
34 void _objc_inform_on_crash(const char *fmt, ...)
35 {
36 }
37
38 void _objc_inform(const char *fmt, ...)
39 {
40 va_list args;
41 va_start(args, fmt);
42 _vcprintf(fmt, args);
43 va_end(args);
44 _cprintf("\n");
45 }
46
47 void _objc_fatal(const char *fmt, ...)
48 {
49 va_list args;
50 va_start(args, fmt);
51 _vcprintf(fmt, args);
52 va_end(args);
53 _cprintf("\n");
54
55 abort();
56 }
57
58 void __objc_error(id rcv, const char *fmt, ...)
59 {
60 va_list args;
61 va_start(args, fmt);
62 _vcprintf(fmt, args);
63 va_end(args);
64
65 abort();
66 }
67
68 void _objc_error(id rcv, const char *fmt, va_list args)
69 {
70 _vcprintf(fmt, args);
71
72 abort();
73 }
74
75 #else
76
77 #include <vproc_priv.h>
78 #include <_simple.h>
79
80 OBJC_EXPORT void (*_error)(id, const char *, va_list);
81
82 static void _objc_trap(void) __attribute__((noreturn));
83
84 // Return true if c is a UTF8 continuation byte
85 static bool isUTF8Continuation(char c)
86 {
87 return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
88 }
89
90 // Add "message" to any forthcoming crash log.
91 static void _objc_crashlog(const char *message)
92 {
93 char *newmsg;
94
95 #if 0
96 {
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);
102 fclose(crashlog);
103 sync();
104 }
105 #endif
106
107 static mutex_t crashlog_lock;
108 mutex_locker_t lock(crashlog_lock);
109
110 char *oldmsg = (char *)CRGetCrashLogMessage();
111 size_t oldlen;
112 const size_t limit = 8000;
113
114 if (!oldmsg) {
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);
122 } else {
123 asprintf(&newmsg, "%s\n%s", oldmsg, message);
124 }
125
126 if (newmsg) {
127 // Strip trailing newline
128 char *c = &newmsg[strlen(newmsg)-1];
129 if (*c == '\n') *c = '\0';
130
131 if (oldmsg) free(oldmsg);
132 CRSetCrashLogMessage(newmsg);
133 }
134 }
135
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)
139 {
140 struct stat st;
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) {
145 return true;
146 }
147 return false;
148 }
149
150 // Print "message" to the console.
151 static void _objc_syslog(const char *message)
152 {
153 _simple_asl_log(ASL_LEVEL_ERR, nil, message);
154
155 if (also_do_stderr()) {
156 write(STDERR_FILENO, message, strlen(message));
157 }
158 }
159
160 /*
161 * _objc_error is the default *_error handler.
162 */
163 #if __OBJC2__
164 __attribute__((noreturn))
165 #else
166 // used by ExceptionHandling.framework
167 #endif
168 void _objc_error(id self, const char *fmt, va_list ap)
169 {
170 char *buf1;
171 char *buf2;
172
173 vasprintf(&buf1, fmt, ap);
174 asprintf(&buf2, "objc[%d]: %s: %s\n",
175 getpid(), object_getClassName(self), buf1);
176 _objc_syslog(buf2);
177 _objc_crashlog(buf2);
178
179 _objc_trap();
180 }
181
182 /*
183 * this routine handles errors that involve an object (or class).
184 */
185 void __objc_error(id rcv, const char *fmt, ...)
186 {
187 va_list vp;
188
189 va_start(vp,fmt);
190 #if !__OBJC2__
191 (*_error)(rcv, fmt, vp);
192 #endif
193 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
194 va_end(vp);
195 }
196
197 /*
198 * this routine handles severe runtime errors...like not being able
199 * to read the mach headers, allocate space, etc...very uncommon.
200 */
201 void _objc_fatal(const char *fmt, ...)
202 {
203 va_list ap;
204 char *buf1;
205 char *buf2;
206
207 va_start(ap,fmt);
208 vasprintf(&buf1, fmt, ap);
209 va_end (ap);
210
211 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
212 _objc_syslog(buf2);
213 _objc_crashlog(buf2);
214
215 _objc_trap();
216 }
217
218 /*
219 * this routine handles soft runtime errors...like not being able
220 * add a category to a class (because it wasn't linked in).
221 */
222 void _objc_inform(const char *fmt, ...)
223 {
224 va_list ap;
225 char *buf1;
226 char *buf2;
227
228 va_start (ap,fmt);
229 vasprintf(&buf1, fmt, ap);
230 va_end (ap);
231
232 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
233 _objc_syslog(buf2);
234
235 free(buf2);
236 free(buf1);
237 }
238
239
240 /*
241 * Like _objc_inform(), but prints the message only in any
242 * forthcoming crash log, not to the console.
243 */
244 void _objc_inform_on_crash(const char *fmt, ...)
245 {
246 va_list ap;
247 char *buf1;
248 char *buf2;
249
250 va_start (ap,fmt);
251 vasprintf(&buf1, fmt, ap);
252 va_end (ap);
253
254 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
255 _objc_crashlog(buf2);
256
257 free(buf2);
258 free(buf1);
259 }
260
261
262 /*
263 * Like calling both _objc_inform and _objc_inform_on_crash.
264 */
265 void _objc_inform_now_and_on_crash(const char *fmt, ...)
266 {
267 va_list ap;
268 char *buf1;
269 char *buf2;
270
271 va_start (ap,fmt);
272 vasprintf(&buf1, fmt, ap);
273 va_end (ap);
274
275 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
276 _objc_crashlog(buf2);
277 _objc_syslog(buf2);
278
279 free(buf2);
280 free(buf1);
281 }
282
283
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)
287 {
288 __builtin_trap();
289 }
290
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)
295 {
296 __builtin_trap();
297 }
298
299 #endif
300
301
302 BREAKPOINT_FUNCTION(
303 void _objc_warn_deprecated(void)
304 );
305
306 void _objc_inform_deprecated(const char *oldf, const char *newf)
307 {
308 if (PrintDeprecation) {
309 if (newf) {
310 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
311 } else {
312 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
313 }
314 }
315 _objc_warn_deprecated();
316 }