forked from snej/MYUtilities
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExceptionUtils.m
196 lines (155 loc) · 5.71 KB
/
ExceptionUtils.m
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
//
// ExceptionUtils.m
// MYUtilities
//
// Created by Jens Alfke on 1/5/08.
// Copyright 2008-2013 Jens Alfke. All rights reserved.
// See BSD license at bottom of file.
//
#import "ExceptionUtils.h"
#import "Logging.h"
#import "Test.h"
#include <sys/sysctl.h>
#include <unistd.h>
#ifndef Warn
#define Warn NSLog
#endif
static void (*sExceptionReporter)(NSException*);
void MYSetExceptionReporter( void (*reporter)(NSException*) )
{
sExceptionReporter = reporter;
}
void MYReportException( NSException *x, NSString *where, ... )
{
va_list args;
va_start(args,where);
where = [[NSString alloc] initWithFormat: where arguments: args];
va_end(args);
Warn(@"Exception caught in %@:\n\t%@\n%@",where,x,x.my_callStack);
if( sExceptionReporter )
sExceptionReporter(x);
[where release];
}
@implementation NSException (MYUtilities)
- (NSArray*) my_callStackReturnAddresses
{
// On 10.5 or later, can get the backtrace:
if( [self respondsToSelector: @selector(callStackReturnAddresses)] )
return [self valueForKey: @"callStackReturnAddresses"];
else
return nil;
}
- (NSArray*) my_callStackReturnAddressesSkipping: (unsigned)skip limit: (unsigned)limit
{
NSArray *addresses = [self my_callStackReturnAddresses];
if( addresses ) {
unsigned n = (unsigned) [addresses count];
skip = MIN(skip,n);
limit = MIN(limit,n-skip);
addresses = [addresses subarrayWithRange: NSMakeRange(skip,limit)];
}
return addresses;
}
- (NSString*) my_callStack
{
NSMutableString* result = [NSMutableString string];
unsigned lines = 0;
for (NSString* line in [self callStackSymbols]) {
NSString* symbol = @"";
if (line.length > 40) {
NSRange space = [line rangeOfString: @" " options: 0 range: NSMakeRange(41, line.length - 41)];
if (space.length > 0)
symbol = [line substringFromIndex: NSMaxRange(space)];
}
// Skip frames that are part of the exception/assertion handling itself:
if( [symbol hasPrefix: @"-[NSAssertionHandler"] || [symbol hasPrefix: @"+[NSException"]
|| [symbol hasPrefix: @"-[NSException"] || [symbol hasPrefix: @"_AssertFailed"]
|| [symbol hasPrefix: @"__exception"] || [symbol hasPrefix: @"objc_"] )
continue;
if( result.length )
[result appendString: @"\n"];
[result appendString: @"\t"];
[result appendString: line];
// Don't show the "__start" frame below "main":
if( [symbol hasPrefix: @"main "] || [symbol hasPrefix: @"start "] )
break;
if (++lines >= 15) {
[result appendString: @"\n\t..."];
break;
}
}
return result;
}
@end
#ifdef NSAppKitVersionNumber10_4 // only enable this in a project that uses AppKit
@implementation MYExceptionReportingApplication
static void report( NSException *x ) {
[NSApp reportException: x];
}
- (id) init
{
self = [super init];
if (self != nil) {
MYSetExceptionReporter(&report);
}
return self;
}
- (void)reportException:(NSException *)x
{
[super reportException: x];
[self performSelector: @selector(_showExceptionAlert:) withObject: x afterDelay: 0.0];
MYSetExceptionReporter(NULL); // ignore further exceptions till alert is dismissed
}
- (void) _showExceptionAlert: (NSException*)x
{
NSString *stack = [x my_callStack] ?:@"";
NSInteger r = NSRunCriticalAlertPanel( @"Internal Error!",
[NSString stringWithFormat: @"Uncaught exception: %@\n%@\n\n%@\n\n"
"Please report this bug (you can copy & paste the text).",
[x name], [x reason], stack],
@"Continue",@"Quit",nil);
if( r == NSAlertAlternateReturn )
exit(1);
MYSetExceptionReporter(&report);
}
@end
#endif
BOOL IsGDBAttached( void )
{
#ifdef GNUSTEP
return NO;
#else
// From: <http://lists.apple.com/archives/Xcode-users/2004/Feb/msg00241.html>
int mib[4];
size_t bufSize = 0;
struct kinfo_proc kp;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
bufSize = sizeof (kp);
if (sysctl(mib, 4, &kp, &bufSize, NULL, 0) < 0) {
Warn(@"Error %i calling sysctl",errno);
return NO;
}
return (kp.kp_proc.p_flag & P_TRACED) != 0;
#endif
}
/*
Copyright (c) 2008-2013, Jens Alfke <[email protected]>. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/