]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-errors.mm
objc4-750.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))
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))
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 abort_with_reason(OS_REASON_OBJC, reason, buf1, flags);
202 }
203 }
204
205 void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
206 const char *fmt, ...)
207 {
208 va_list ap;
209 va_start(ap, fmt);
210 _objc_fatalv(reason, flags, fmt, ap);
211 }
212
213 void _objc_fatal(const char *fmt, ...)
214 {
215 va_list ap;
216 va_start(ap,fmt);
217 _objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED,
218 OS_REASON_FLAG_ONE_TIME_FAILURE,
219 fmt, ap);
220 }
221
222 /*
223 * this routine handles soft runtime errors...like not being able
224 * add a category to a class (because it wasn't linked in).
225 */
226 void _objc_inform(const char *fmt, ...)
227 {
228 va_list ap;
229 char *buf1;
230 char *buf2;
231
232 va_start (ap,fmt);
233 vasprintf(&buf1, fmt, ap);
234 va_end (ap);
235
236 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
237 _objc_syslog(buf2);
238
239 free(buf2);
240 free(buf1);
241 }
242
243
244 /*
245 * Like _objc_inform(), but prints the message only in any
246 * forthcoming crash log, not to the console.
247 */
248 void _objc_inform_on_crash(const char *fmt, ...)
249 {
250 va_list ap;
251 char *buf1;
252 char *buf2;
253
254 va_start (ap,fmt);
255 vasprintf(&buf1, fmt, ap);
256 va_end (ap);
257
258 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
259 _objc_crashlog(buf2);
260
261 free(buf2);
262 free(buf1);
263 }
264
265
266 /*
267 * Like calling both _objc_inform and _objc_inform_on_crash.
268 */
269 void _objc_inform_now_and_on_crash(const char *fmt, ...)
270 {
271 va_list ap;
272 char *buf1;
273 char *buf2;
274
275 va_start (ap,fmt);
276 vasprintf(&buf1, fmt, ap);
277 va_end (ap);
278
279 asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
280 _objc_crashlog(buf2);
281 _objc_syslog(buf2);
282
283 free(buf2);
284 free(buf1);
285 }
286
287 #endif
288
289
290 BREAKPOINT_FUNCTION(
291 void _objc_warn_deprecated(void)
292 );
293
294 void _objc_inform_deprecated(const char *oldf, const char *newf)
295 {
296 if (PrintDeprecation) {
297 if (newf) {
298 _objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
299 } else {
300 _objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
301 }
302 }
303 _objc_warn_deprecated();
304 }