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