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