-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc-errors.mm
304 lines (254 loc) · 6.94 KB
/
objc-errors.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 1999-2003, 2005-2007 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* objc-errors.m
* Copyright 1988-2001, NeXT Software, Inc., Apple Computer, Inc.
*/
#include "objc-private.h"
#if TARGET_OS_WIN32
#include <conio.h>
void _objc_inform_on_crash(const char *fmt, ...)
{
}
void _objc_inform(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
_vcprintf(fmt, args);
va_end(args);
_cprintf("\n");
}
void _objc_fatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
_vcprintf(fmt, args);
va_end(args);
_cprintf("\n");
abort();
}
void __objc_error(id rcv, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
_vcprintf(fmt, args);
va_end(args);
abort();
}
void _objc_error(id rcv, const char *fmt, va_list args)
{
_vcprintf(fmt, args);
abort();
}
#else
#include <_simple.h>
// Return true if c is a UTF8 continuation byte
static bool isUTF8Continuation(char c)
{
return (c & 0xc0) == 0x80; // continuation byte is 0b10xxxxxx
}
// Add "message" to any forthcoming crash log.
mutex_t crashlog_lock;
static void _objc_crashlog(const char *message)
{
char *newmsg;
#if 0
{
// for debugging at BOOT time.
extern char **_NSGetProgname(void);
FILE *crashlog = fopen("/_objc_crash.log", "a");
setbuf(crashlog, NULL);
fprintf(crashlog, "[%s] %s\n", *_NSGetProgname(), message);
fclose(crashlog);
sync();
}
#endif
mutex_locker_t lock(crashlog_lock);
char *oldmsg = (char *)CRGetCrashLogMessage();
size_t oldlen;
const size_t limit = 8000;
if (!oldmsg) {
newmsg = strdup(message);
} else if ((oldlen = strlen(oldmsg)) > limit) {
// limit total length by dropping old contents
char *truncmsg = oldmsg + oldlen - limit;
// advance past partial UTF-8 bytes
while (isUTF8Continuation(*truncmsg)) truncmsg++;
asprintf(&newmsg, "... %s\n%s", truncmsg, message);
} else {
asprintf(&newmsg, "%s\n%s", oldmsg, message);
}
if (newmsg) {
// Strip trailing newline
char *c = &newmsg[strlen(newmsg)-1];
if (*c == '\n') *c = '\0';
if (oldmsg) free(oldmsg);
CRSetCrashLogMessage(newmsg);
}
}
// Returns true if logs should be sent to stderr as well as syslog.
// Copied from CFUtilities.c
static bool also_do_stderr(void)
{
struct stat st;
int ret = fstat(STDERR_FILENO, &st);
if (ret < 0) return false;
mode_t m = st.st_mode & S_IFMT;
if (m == S_IFREG || m == S_IFSOCK || m == S_IFIFO || m == S_IFCHR) {
return true;
}
return false;
}
// Print "message" to the console.
static void _objc_syslog(const char *message)
{
_simple_asl_log(ASL_LEVEL_ERR, nil, message);
if (also_do_stderr()) {
write(STDERR_FILENO, message, strlen(message));
}
}
/*
* _objc_error is the default *_error handler.
*/
#if !__OBJC2__
// used by ExceptionHandling.framework
#endif
__attribute__((noreturn))
void _objc_error(id self, const char *fmt, va_list ap)
{
char *buf;
vasprintf(&buf, fmt, ap);
_objc_fatal("%s: %s", object_getClassName(self), buf);
}
/*
* this routine handles errors that involve an object (or class).
*/
void __objc_error(id rcv, const char *fmt, ...)
{
va_list vp;
va_start(vp,fmt);
#if !__OBJC2__
(*_error)(rcv, fmt, vp);
#endif
_objc_error (rcv, fmt, vp); /* In case (*_error)() returns. */
va_end(vp);
}
static __attribute__((noreturn))
void _objc_fatalv(uint64_t reason, uint64_t flags, const char *fmt, va_list ap)
{
char *buf1;
vasprintf(&buf1, fmt, ap);
char *buf2;
asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
_objc_syslog(buf2);
if (DebugDontCrash) {
char *buf3;
asprintf(&buf3, "objc[%d]: HALTED\n", getpid());
_objc_syslog(buf3);
_Exit(1);
}
else {
abort_with_reason(OS_REASON_OBJC, reason, buf1, flags);
}
}
void _objc_fatal_with_reason(uint64_t reason, uint64_t flags,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_objc_fatalv(reason, flags, fmt, ap);
}
void _objc_fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
_objc_fatalv(OBJC_EXIT_REASON_UNSPECIFIED,
OS_REASON_FLAG_ONE_TIME_FAILURE,
fmt, ap);
}
/*
* this routine handles soft runtime errors...like not being able
* add a category to a class (because it wasn't linked in).
*/
void _objc_inform(const char *fmt, ...)
{
va_list ap;
char *buf1;
char *buf2;
va_start (ap,fmt);
vasprintf(&buf1, fmt, ap);
va_end (ap);
asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
_objc_syslog(buf2);
free(buf2);
free(buf1);
}
/*
* Like _objc_inform(), but prints the message only in any
* forthcoming crash log, not to the console.
*/
void _objc_inform_on_crash(const char *fmt, ...)
{
va_list ap;
char *buf1;
char *buf2;
va_start (ap,fmt);
vasprintf(&buf1, fmt, ap);
va_end (ap);
asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
_objc_crashlog(buf2);
free(buf2);
free(buf1);
}
/*
* Like calling both _objc_inform and _objc_inform_on_crash.
*/
void _objc_inform_now_and_on_crash(const char *fmt, ...)
{
va_list ap;
char *buf1;
char *buf2;
va_start (ap,fmt);
vasprintf(&buf1, fmt, ap);
va_end (ap);
asprintf(&buf2, "objc[%d]: %s\n", getpid(), buf1);
_objc_crashlog(buf2);
_objc_syslog(buf2);
free(buf2);
free(buf1);
}
#endif
BREAKPOINT_FUNCTION(
void _objc_warn_deprecated(void)
);
void _objc_inform_deprecated(const char *oldf, const char *newf)
{
if (PrintDeprecation) {
if (newf) {
_objc_inform("The function %s is obsolete. Use %s instead. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf, newf);
} else {
_objc_inform("The function %s is obsolete. Do not use it. Set a breakpoint on _objc_warn_deprecated to find the culprit.", oldf);
}
}
_objc_warn_deprecated();
}