Split ExceptionUtils out of Test.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
7 // See BSD license at bottom of file.
10 #import "ExceptionUtils.h"
19 static void (*sExceptionReporter)(NSException*);
21 void MYSetExceptionReporter( void (*reporter)(NSException*) )
23 sExceptionReporter = reporter;
26 void MYReportException( NSException *x, NSString *where, ... )
30 where = [[NSString alloc] initWithFormat: where arguments: args];
32 if( sExceptionReporter ) {
33 Warn(@"Exception caught in %@:\n\t%@",where,x);
34 sExceptionReporter(x);
36 Warn(@"Exception caught in %@:\n\t%@\n%@",where,x,x.my_callStack);
41 @implementation NSException (MooseyardUtil)
44 - (NSArray*) my_callStackReturnAddresses
46 // On 10.5 or later, can get the backtrace:
47 if( [self respondsToSelector: @selector(callStackReturnAddresses)] )
48 return [self valueForKey: @"callStackReturnAddresses"];
53 - (NSArray*) my_callStackReturnAddressesSkipping: (unsigned)skip limit: (unsigned)limit
55 NSArray *addresses = [self my_callStackReturnAddresses];
57 unsigned n = [addresses count];
59 limit = MIN(limit,n-skip);
60 addresses = [addresses subarrayWithRange: NSMakeRange(skip,limit)];
66 - (NSString*) my_callStack
68 NSArray *addresses = [self my_callStackReturnAddressesSkipping: 2 limit: 15];
72 // We pipe the hex return addresses through the 'atos' tool to get symbolic names:
73 // Adapted from <http://paste.lisp.org/display/47196>:
74 NSMutableString *cmd = [NSMutableString stringWithFormat: @"/usr/bin/atos -p %d", getpid()];
76 foreach(addr,addresses) {
77 [cmd appendFormat: @" %p", [addr pointerValue]];
79 FILE *file = popen( [cmd UTF8String], "r" );
83 NSMutableData *output = [NSMutableData data];
86 while ((length = fread( buffer, 1, sizeof( buffer ), file ) ))
87 [output appendBytes: buffer length: length];
89 NSString *outStr = [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease];
91 NSMutableString *result = [NSMutableString string];
93 foreach( line, [outStr componentsSeparatedByString: @"\n"] ) {
94 // Skip frames that are part of the exception/assertion handling itself:
95 if( [line hasPrefix: @"-[NSAssertionHandler"] || [line hasPrefix: @"+[NSException"]
96 || [line hasPrefix: @"-[NSException"] || [line hasPrefix: @"_AssertFailed"] )
99 [result appendString: @"\n"];
100 [result appendString: @"\t"];
101 [result appendString: line];
102 // Don't show the "__start" frame below "main":
103 if( [line hasPrefix: @"main "] )
114 @implementation MYExceptionReportingApplication
117 static void report( NSException *x ) {
118 [NSApp reportException: x];
125 MYSetExceptionReporter(&report);
131 - (void)reportException:(NSException *)x
133 [super reportException: x];
134 [self performSelector: @selector(_showExceptionAlert:) withObject: x afterDelay: 0.0];
135 MYSetExceptionReporter(NULL); // ignore further exceptions till alert is dismissed
138 - (void) _showExceptionAlert: (NSException*)x
140 NSString *stack = [x my_callStack] ?:@"";
141 int r = NSRunCriticalAlertPanel( @"Cloudy Internal Error!",
142 [NSString stringWithFormat: @"Uncaught exception: %@\n%@\n\n%@\n\n"
143 "Please report this bug to jens@mooseyard.com (you can copy & paste the text).",
144 [x name], [x reason], stack],
145 @"Sorry",@"Quit",nil);
146 if( r == NSAlertAlternateReturn )
148 MYSetExceptionReporter(&report);
155 Copyright (c) 2008, Jens Alfke. All rights reserved.
157 Redistribution and use in source and binary forms, with or without modification, are permitted
158 provided that the following conditions are met:
160 * Redistributions of source code must retain the above copyright notice, this list of conditions
161 and the following disclaimer.
162 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
163 and the following disclaimer in the documentation and/or other materials provided with the
166 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
167 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
168 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
169 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
170 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
171 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
172 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
173 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.