Added MYWindowUtils, being used by Murky.
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 (MYUtilities)
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: 1 limit: 15];
79 // We pipe the hex return addresses through the 'atos' tool to get symbolic names:
80 // Adapted from <http://paste.lisp.org/display/47196>:
81 NSMutableString *cmd = [NSMutableString stringWithFormat: @"/usr/bin/atos -p %d", getpid()];
83 foreach(addr,addresses) {
84 [cmd appendFormat: @" %p", [addr pointerValue]];
86 file = popen( [cmd UTF8String], "r" );
89 NSMutableString *result = [NSMutableString string];
91 NSMutableData *output = [NSMutableData data];
94 while ((length = fread( buffer, 1, sizeof( buffer ), file ) ))
95 [output appendBytes: buffer length: length];
97 NSString *outStr = [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease];
100 foreach( line, [outStr componentsSeparatedByString: @"\n"] ) {
101 // Skip frames that are part of the exception/assertion handling itself:
102 if( [line hasPrefix: @"-[NSAssertionHandler"] || [line hasPrefix: @"+[NSException"]
103 || [line hasPrefix: @"-[NSException"] || [line hasPrefix: @"_AssertFailed"]
104 || [line hasPrefix: @"objc_"] )
107 [result appendString: @"\n"];
108 [result appendString: @"\t"];
109 [result appendString: line];
110 // Don't show the "__start" frame below "main":
111 if( [line hasPrefix: @"main "] )
116 foreach(addr,addresses) {
118 [result appendString: @" <- "];
119 [result appendFormat: @"%p", [addr pointerValue]];
130 #ifdef NSAppKitVersionNumber10_4 // only enable this in a project that uses AppKit
132 @implementation MYExceptionReportingApplication
135 static void report( NSException *x ) {
136 [NSApp reportException: x];
143 MYSetExceptionReporter(&report);
149 - (void)reportException:(NSException *)x
151 [super reportException: x];
152 [self performSelector: @selector(_showExceptionAlert:) withObject: x afterDelay: 0.0];
153 MYSetExceptionReporter(NULL); // ignore further exceptions till alert is dismissed
156 - (void) _showExceptionAlert: (NSException*)x
158 NSString *stack = [x my_callStack] ?:@"";
159 int r = NSRunCriticalAlertPanel( @"Internal Error!",
160 [NSString stringWithFormat: @"Uncaught exception: %@\n%@\n\n%@\n\n"
161 "Please report this bug (you can copy & paste the text).",
162 [x name], [x reason], stack],
163 @"Continue",@"Quit",nil);
164 if( r == NSAlertAlternateReturn )
166 MYSetExceptionReporter(&report);
175 BOOL IsGDBAttached( void )
177 // From: <http://lists.apple.com/archives/Xcode-users/2004/Feb/msg00241.html>
180 struct kinfo_proc kp;
184 mib[2] = KERN_PROC_PID;
187 bufSize = sizeof (kp);
188 if (sysctl(mib, 4, &kp, &bufSize, NULL, 0) < 0) {
189 Warn(@"Error %i calling sysctl",errno);
192 return (kp.kp_proc.p_flag & P_TRACED) != 0;
198 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
200 Redistribution and use in source and binary forms, with or without modification, are permitted
201 provided that the following conditions are met:
203 * Redistributions of source code must retain the above copyright notice, this list of conditions
204 and the following disclaimer.
205 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
206 and the following disclaimer in the documentation and/or other materials provided with the
209 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
210 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
211 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
212 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
213 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
214 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
215 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
216 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.