* Minor fixes to glitches detected by the clang static analyzer.
* Added MYAnimatingSplitView class.
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"
15 #include <sys/sysctl.h>
24 static void (*sExceptionReporter)(NSException*);
26 void MYSetExceptionReporter( void (*reporter)(NSException*) )
28 sExceptionReporter = reporter;
31 void MYReportException( NSException *x, NSString *where, ... )
35 where = [[NSString alloc] initWithFormat: where arguments: args];
37 if( sExceptionReporter ) {
38 Warn(@"Exception caught in %@:\n\t%@",where,x);
39 sExceptionReporter(x);
41 Warn(@"Exception caught in %@:\n\t%@\n%@",where,x,x.my_callStack);
46 @implementation NSException (MooseyardUtil)
49 - (NSArray*) my_callStackReturnAddresses
51 // On 10.5 or later, can get the backtrace:
52 if( [self respondsToSelector: @selector(callStackReturnAddresses)] )
53 return [self valueForKey: @"callStackReturnAddresses"];
58 - (NSArray*) my_callStackReturnAddressesSkipping: (unsigned)skip limit: (unsigned)limit
60 NSArray *addresses = [self my_callStackReturnAddresses];
62 unsigned n = [addresses count];
64 limit = MIN(limit,n-skip);
65 addresses = [addresses subarrayWithRange: NSMakeRange(skip,limit)];
71 - (NSString*) my_callStack
73 NSArray *addresses = [self my_callStackReturnAddressesSkipping: 2 limit: 15];
77 // We pipe the hex return addresses through the 'atos' tool to get symbolic names:
78 // Adapted from <http://paste.lisp.org/display/47196>:
79 NSMutableString *cmd = [NSMutableString stringWithFormat: @"/usr/bin/atos -p %d", getpid()];
81 foreach(addr,addresses) {
82 [cmd appendFormat: @" %p", [addr pointerValue]];
84 FILE *file = popen( [cmd UTF8String], "r" );
88 NSMutableData *output = [NSMutableData data];
91 while ((length = fread( buffer, 1, sizeof( buffer ), file ) ))
92 [output appendBytes: buffer length: length];
94 NSString *outStr = [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease];
96 NSMutableString *result = [NSMutableString string];
98 foreach( line, [outStr componentsSeparatedByString: @"\n"] ) {
99 // Skip frames that are part of the exception/assertion handling itself:
100 if( [line hasPrefix: @"-[NSAssertionHandler"] || [line hasPrefix: @"+[NSException"]
101 || [line hasPrefix: @"-[NSException"] || [line hasPrefix: @"_AssertFailed"] )
104 [result appendString: @"\n"];
105 [result appendString: @"\t"];
106 [result appendString: line];
107 // Don't show the "__start" frame below "main":
108 if( [line hasPrefix: @"main "] )
119 #ifdef NSAppKitVersionNumber10_4 // only enable this in a project that uses AppKit
121 @implementation MYExceptionReportingApplication
124 static void report( NSException *x ) {
125 [NSApp reportException: x];
132 MYSetExceptionReporter(&report);
138 - (void)reportException:(NSException *)x
140 [super reportException: x];
141 [self performSelector: @selector(_showExceptionAlert:) withObject: x afterDelay: 0.0];
142 MYSetExceptionReporter(NULL); // ignore further exceptions till alert is dismissed
145 - (void) _showExceptionAlert: (NSException*)x
147 NSString *stack = [x my_callStack] ?:@"";
148 int r = NSRunCriticalAlertPanel( @"Internal Error!",
149 [NSString stringWithFormat: @"Uncaught exception: %@\n%@\n\n%@\n\n"
150 "Please report this bug (you can copy & paste the text).",
151 [x name], [x reason], stack],
152 @"Continue",@"Quit",nil);
153 if( r == NSAlertAlternateReturn )
155 MYSetExceptionReporter(&report);
164 BOOL IsGDBAttached( void )
166 // From: <http://lists.apple.com/archives/Xcode-users/2004/Feb/msg00241.html>
169 struct kinfo_proc kp;
173 mib[2] = KERN_PROC_PID;
176 bufSize = sizeof (kp);
177 if (sysctl(mib, 4, &kp, &bufSize, NULL, 0) < 0) {
178 Warn(@"Error %i calling sysctl",errno);
181 return (kp.kp_proc.p_flag & P_TRACED) != 0;
187 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
189 Redistribution and use in source and binary forms, with or without modification, are permitted
190 provided that the following conditions are met:
192 * Redistributions of source code must retain the above copyright notice, this list of conditions
193 and the following disclaimer.
194 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
195 and the following disclaimer in the documentation and/or other materials provided with the
198 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
199 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
200 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
201 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
202 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
203 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
204 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
205 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.