]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-errors.mm
objc4-709.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 <_simple.h>
78
79 OBJC_EXPORT void (*_error)(id, const char *, va_list);
80
81 // Return true if c is a UTF8 continuation byte
82 static bool isUTF8Continuation(char c)
83 {
84 return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
85 }
86
87 // Add "message" to any forthcoming crash log.
88 mutex_t crashlog_lock;
89 static void _objc_crashlog(const char *message)
90 {
91 char *newmsg;
92
93 #if 0
94 {
95 // for debugging at BOOT time.
96 extern char **_NSGetProgname(void);
97 FILE *crashlog = fopen("/_objc_crash.log", "a");
98 setbuf(crashlog, NULL);
99 fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
100 fclose(crashlog);
101 sync();
102 }
103 #endif
104
105 mutex_locker_t lock(crashlog_lock);
106
107 char *oldmsg = (char *)CRGetCrashLogMessage();
108 size_t oldlen;
109 const size_t limit = 8000;
110
111 if (!oldmsg) {
112 newmsg = strdup(message);
113 } else if ((oldlen = strlen(oldmsg)) > limit) {
114 // limit total length by dropping old contents
115 char *truncmsg = oldmsg + oldlen - limit;
116 // advance past partial UTF-8 bytes
117 while (isUTF8Continuation(*truncmsg)) truncmsg++;
118 asprintf(&newmsg, "... %s\n%s", truncmsg, message);
119 } else {
120 asprintf(&newmsg, "%s\n%s", oldmsg, message);
121 }
122
123 if (newmsg) {
124 // Strip trailing newline
125 char *c = &newmsg[strlen(newmsg)-1];
126 if (*c == '\n') *c = '\0';
127
128 if (oldmsg) free(oldmsg);
129 CRSetCrashLogMessage(newmsg);
130 }
131 }
132
133 // Returns true if logs should be sent to stderr as well as syslog.
134 // Copied from CFUtilities.c
135 static bool also_do_stderr(void)
136 {
137 struct stat st;
138 int ret = fstat(STDERR_FILENO, &st);
139 if (ret < 0) return false;
140 mode_t m = st.st_mode & S_IFMT;
141 if (m == S_IFREG || m == S_IFSOCK || m == S_IFIFO || m == S_IFCHR) {
142 return true;
143 }
144 return false;
145 }
146
147 // Print "message" to the console.
148 static void _objc_syslog(const char *message)
149 {
150 _simple_asl_log(ASL_LEVEL_ERR, nil, message);
151
152 if (also_do_stderr()) {
153 write(STDERR_FILENO, message, strlen(message));
154 }
155 }
156
157 /*
158 * _objc_error is the default *_error handler.
159 */
160 #if !__OBJC2__
161 // used by ExceptionHandling.framework
162 #endif
163 __attribute__((noreturn))
164 void _objc_error(id self, const char *fmt, va_list ap)
165 {
166 char *buf;
167 vasprintf(&buf, fmt, ap);
168 _objc_fatal("%s: %s", object_getClassName(self), buf);
169 }
170
171 /*
172 * this routine handles errors that involve an object (or class).
173 */
174 void __objc_error(id rcv, const char *fmt, ...)
175 {
176 va_list vp;
177
178 va_start(vp,fmt);
179 #if !__OBJC2__
180 (*_error)(rcv, fmt, vp);
181 #endif
182 _objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
183 va_end(vp);
184 }
185
186 static __attribute__((noreturn))
187 void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap)
188 {
189 char *buf1;
190 vasprintf(&buf1, fmt, ap);
191
192 char *buf2;
193 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
194 _objc_syslog(buf2);
195
196 if (DebugDontCrash) {
197 char *buf3;
198 asprintf(&buf3, "objc[%d]: HALTED\n", getpid());
199 _objc_syslog(buf3);
200 _Exit(1);
201 }
202 else {
203 abort_with_reason(OS_REASON_OBJC, reason, buf1, flags);
204 }
205 }
206
207 void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
208 const char *fmt, ...)
209 {
210 va_list ap;
211 va_start(ap, fmt);
212 _objc_fatalv(reason, flags, fmt, ap);
213 }
214
215 void _objc_fatal(const char *fmt, ...)
216 {
217 va_list ap;
218 va_start(ap,fmt);
219 _objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED,
220 OS_REASON_FLAG_ONE_TIME_FAILURE,
221 fmt, ap);
222 }
223
224 /*
225 * this routine handles soft runtime errors...like not being able
226 * add a category to a class (because it wasn't linked in).
227 */
228 void _objc_inform(const char *fmt, ...)
229 {
230 va_list ap;
231 char *buf1;
232 char *buf2;
233
234 va_start (ap,fmt);
235 vasprintf(&buf1, fmt, ap);
236 va_end (ap);
237
238 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
239 _objc_syslog(buf2);
240
241 free(buf2);
242 free(buf1);
243 }
244
245
246 /*
247 * Like _objc_inform(), but prints the message only in any
248 * forthcoming crash log, not to the console.
249 */
250 void _objc_inform_on_crash(const char *fmt, ...)
251 {
252 va_list ap;
253 char *buf1;
254 char *buf2;
255
256 va_start (ap,fmt);
257 vasprintf(&buf1, fmt, ap);
258 va_end (ap);
259
260 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
261 _objc_crashlog(buf2);
262
263 free(buf2);
264 free(buf1);
265 }
266
267
268 /*
269 * Like calling both _objc_inform and _objc_inform_on_crash.
270 */
271 void _objc_inform_now_and_on_crash(const char *fmt, ...)
272 {
273 va_list ap;
274 char *buf1;
275 char *buf2;
276
277 va_start (ap,fmt);
278 vasprintf(&buf1, fmt, ap);
279 va_end (ap);
280
281 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
282 _objc_crashlog(buf2);
283 _objc_syslog(buf2);
284
285 free(buf2);
286 free(buf1);
287 }
288
289 #endif
290
291
292 BREAKPOINT_FUNCTION(
293 void _objc_warn_deprecated(void)
294 );
295
296 void _objc_inform_deprecated(const char *oldf, const char *newf)
297 {
298 if (PrintDeprecation) {
299 if (newf) {
300 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
301 } else {
302 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
303 }
304 }
305 _objc_warn_deprecated();
306 }