The accounts menu now distinguishes accounts which are on a service compatible with...
[adiumx.git] / Source / AIExceptionController.m
blob51d1a89b47cac435ca7812a49c637e8cac9a4a16
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AICrashReporter.h"
18 #import "AIExceptionController.h"
19 #import <AIUtilities/AIFileManagerAdditions.h>
20 #import <AIUtilities/AIStringAdditions.h>
21 #import <ExceptionHandling/NSExceptionHandler.h>
22 #include <unistd.h>
24 /*!
25  * @class AIExceptionController
26  * @brief Catches application exceptions and forwards them to the crash reporter application
27  *
28  * Once configured, sets itself as the NSExceptionHandler delegate to decode the stack traces
29  * generated via NSExceptionHandler, write them to a file, and launch the crash reporter.
30  */
31 @implementation AIExceptionController
33 //Enable exception catching for the crash reporter
34 static BOOL catchExceptions = NO;
36 //These exceptions can be safely ignored.
37 static NSSet *safeExceptionReasons = nil, *safeExceptionNames = nil;
39 + (void)enableExceptionCatching
41     //Log and Handle all exceptions
42         NSExceptionHandler *exceptionHandler = [NSExceptionHandler defaultExceptionHandler];
43     [exceptionHandler setExceptionHandlingMask:(NSHandleUncaughtExceptionMask |
44                                                                                                 NSHandleUncaughtSystemExceptionMask | 
45                                                                                                 NSHandleUncaughtRuntimeErrorMask |
46                                                                                                 NSHandleTopLevelExceptionMask /*|
47                                                                                                 NSHandleOtherExceptionMask*/)];
48         [exceptionHandler setDelegate:self];
50         catchExceptions = YES;
52         //Remove any existing exception logs
53     [[NSFileManager defaultManager] trashFileAtPath:EXCEPTIONS_PATH];
55         //Set up exceptions to except
56         //More of these (matched by substring) can be found in -raise
57         if (!safeExceptionReasons) {
58                 safeExceptionReasons = [[NSSet alloc] initWithObjects:
59                         @"_sharedInstance is invalid.", //Address book framework is weird sometimes
60                         @"No text was found", //ICeCoffEE is an APE haxie which would crash us whenever a user pasted, or something like that
61                         @"No URL is selected", //ICeCoffEE also crashes us when clicking links. How obnoxious. Release software should not use NSAssert like this.
62 #warning Error 1000 is kCGErrorFirst. This special case was added in r5425, so long ago that it's possible that this was really supposed to be 1007, and has been fixed since then.
63                         @"Error (1000) creating CGSWindow", //This looks like an odd NSImage error... it occurs sporadically, seems harmless, and doesn't appear avoidable
64                         @"Error (1007) creating CGSWindow", //kCGErrorRangeCheck: Raised by NSImage when we create one that's bigger than a window can hold. See <http://www.cocoabuilder.com/archive/message/cocoa/2004/2/5/96193>.
65                         @"Access invalid attribute location 0 (length 0)", //The undo manager can throw this one when restoring a large amount of attributed text... doesn't appear avoidable
66                         @"Invalid parameter not satisfying: (index >= 0) && (index < (_itemArray ? CFArrayGetCount(_itemArray) : 0))", //A couple AppKit methods, particularly NSSpellChecker, seem to expect this exception to be happily thrown in the normal course of operation. Lovely. Also needed for FontSight compatibility.
67                         @"Invalid parameter not satisfying: (index >= 0) && (index <= (_itemArray ? CFArrayGetCount(_itemArray) : 0))", //Like the above, but <= instead of <
68                         @"Invalid parameter not satisfying: entry", //NSOutlineView throws this, particularly if it gets clicked while reloading or the computer sleeps while reloading
69                         @"Invalid parameter not satisfying: aString != nil", //The Find command can throw this, as can other AppKit methods
70                         nil];
71         }
72         if (!safeExceptionNames) {
73                 safeExceptionNames = [[NSSet alloc] initWithObjects:
74                         @"GIFReadingException", //GIF reader sucks
75                         @"NSPortTimeoutException", //Harmless - it timed out for a reason
76                         @"NSInvalidReceivePortException", //Same story as NSPortTimeoutException
77                         @"NSAccessibilityException", //Harmless - one day we should figure out how we aren't accessible, but not today
78                         @"NSImageCacheException", //NSImage is silly
79                         @"NSArchiverArchiveInconsistency", //Odd system hacks can lead to this one
80                         @"NSUnknownKeyException", //No reason to crash on invalid Applescript syntax
81                         @"NSObjectInaccessibleException", //We don't use DO, but spell checking does; AppleScript execution requires multiple run loops, and the HIToolbox can get confused and try to spellcheck in the applescript thread. Silly Apple.
82                         @"NSCharacterConversionException", //We can't help it if a character can't be converted...
83                         @"NSRTFException", //Better to ignore than to crash
84                         nil];
85         }
88 // mask is NSHandle<exception type>Mask, exception's userInfo has stack trace for key NSStackTraceKey
89 + (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldHandleException:(NSException *)exception mask:(unsigned int)aMask
91         BOOL            shouldLaunchCrashReporter = YES;
92         if (catchExceptions) {
93                 NSString        *theReason = [exception reason];
94                 NSString        *theName   = [exception name];
95                 NSString        *backtrace = nil;
97                 //Ignore various known harmless or unavoidable exceptions (From the system or system hacks)
98                 if ((!theReason) || //Harmless
99                         [safeExceptionReasons containsObject:theReason] || 
100                         [theReason rangeOfString:@"NSRunStorage, _NSBlockNumberForIndex()"].location != NSNotFound || //NSLayoutManager throws this for fun in a purely-AppKit stack trace
101                         [theReason rangeOfString:@"Broken pipe"].location != NSNotFound || //libezv throws broken pipes as NSFileHandleOperationException with this in the reason; I'd rather we watched for "broken pipe" than ignore all file handle errors
102                         [theReason rangeOfString:@"incomprehensible archive"].location != NSNotFound || //NSKeyedUnarchiver can get confused and throw this; it's out of our control
103                         [theReason rangeOfString:@"-whiteComponent not defined"].location != NSNotFound || //Random NSColor exception for certain coded color values
104                         [theReason rangeOfString:@"Failed to get fache"].location != NSNotFound || //Thrown by NSFontManager when availableFontFamilies is called if it runs into a corrupt font
105                         [theReason rangeOfString:@"NSWindow: -_newFirstResponderAfterResigining"].location != NSNotFound || //NSAssert within system code, harmless
106                         [theReason rangeOfString:@"-patternImage not defined"].location != NSNotFound || //Painters Color Picker throws an exception during the normal course of operation.  Don't you hate that?
107                         [theReason rangeOfString:@"Failed to set font"].location != NSNotFound || //Corrupt fonts
108                         [theReason rangeOfString:@"Delete invalid attribute range"].location != NSNotFound || //NSAttributedString's initWithCoder can throw this
109                         [theReason rangeOfString:@"NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds"].location != NSNotFound || //-[NSLayoutManager textContainerForGlyphAtIndex:effectiveRange:] as of 10.4 can throw this
110                         [theReason rangeOfString:@"TSMProcessRawKeyCode failed"].location != NSNotFound || //May be raised by -[NSEvent charactersIgnoringModifiers]
111                         [theReason rangeOfString:@"Invalid PMPrintSettings in print info"].location != NSNotFound || //Invalid saved print settings can make the print dialogue throw this
112                         [theReason rangeOfString:@"-[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds"].location != NSNotFound || //Can't find the source of this, but it seems to happen randomly and not provide a stack trace.
113                         [theReason rangeOfString:@"SketchUpColor"].location != NSNotFound || //NSColorSwatch addition which can yield an exception
114                         [theReason rangeOfString:@"-[NSConcreteFileHandle dealloc]: Bad file descriptor"].location != NSNotFound || // NSFileHandle on an invalid file descriptor should log but not crash
115                         (!theName) || //Harmless
116                         [theName rangeOfString:@"RSS"].location != NSNotFound || //Sparkle's RSS handling whines sometimes, but we don't care.
117                    [safeExceptionNames containsObject:theName])
118                 {
119                         shouldLaunchCrashReporter = NO;
120                 }
121                 
122                 //Check the stack trace for a third set of known offenders
123                 if (shouldLaunchCrashReporter) {
124                         backtrace = [exception decodedExceptionStackTrace];
125                 }
126                 if (!backtrace ||
127                         [backtrace rangeOfString:@"-[NSFontPanel setPanelFont:isMultiple:] (in AppKit)"].location != NSNotFound || //NSFontPanel likes to create exceptions
128                         [backtrace rangeOfString:@"-[NSScrollView(NSScrollViewAccessibility) accessibilityChildrenAttribute]"].location != NSNotFound || //Perhaps we aren't implementing an accessibility method properly? No idea what though :(
129                         [backtrace rangeOfString:@"-[WebBridge objectLoadedFromCacheWithURL:response:data:]"].location != NSNotFound || //WebBridge throws this randomly it seems
130                         [backtrace rangeOfString:@"-[NSTextView(NSSharing) _preflightSpellChecker:]"].location != NSNotFound || //Systemwide spell checker gets corrupted on some systems; other apps just end up logging to console, and we should do the same.
131                         [backtrace rangeOfString:@"-[NSFontManager(NSFontManagerCollectionAdditions) _collectionsChanged:]"].location != NSNotFound || //Deleting an empty collection in 10.4.3 (and possibly other versions) throws an NSRangeException with this in the backtrace.
132                         [backtrace rangeOfString:@"[NSSpellChecker sharedSpellChecker]"].location != NSNotFound //The spell checker screws up and starts throwing an exception on every word on many people's systems.
133                    )
134                 {
135                            shouldLaunchCrashReporter = NO;
136                 }
138                 if (shouldLaunchCrashReporter) {
139                         NSString        *bundlePath = [[NSBundle mainBundle] bundlePath];
140                         NSString        *crashReporterPath = [bundlePath stringByAppendingPathComponent:RELATIVE_PATH_TO_CRASH_REPORTER];
141                         NSString        *versionString = [[NSProcessInfo processInfo] operatingSystemVersionString];
142                         NSString        *preferredLocalization = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
143         
144                         NSLog(@"Launching the Adium Crash Reporter because an exception of type %@ occurred:\n%@", theName,theReason);
146                         //Pass the exception to the crash reporter and close this application
147                         [[NSString stringWithFormat:@"OS Version:\t%@\nLanguage:\t%@\nException:\t%@\nReason:\t%@\nStack trace:\n%@",
148                                 versionString,preferredLocalization,theName,theReason,backtrace] writeToFile:EXCEPTIONS_PATH atomically:YES];
150                         [[NSWorkspace sharedWorkspace] openFile:bundlePath withApplication:crashReporterPath];
152                         exit(-1);
153                 } else {
154                         /*
155                         NSLog(@"The following unhandled exception was ignored: %@ (%@)\nStack trace:\n%@",
156                                   theName,
157                                   theReason,
158                                   (backtrace ? backtrace : @"(Unavailable)"));
159                         AILog(@"The following unhandled exception was ignored: %@ (%@)\nStack trace:\n%@",
160                                   theName,
161                                   theReason,
162                                   (backtrace ? backtrace : @"(Unavailable)"));
163                          */
164                 }
165         }
167         return shouldLaunchCrashReporter;
170 @end
172 @implementation NSException (AIExceptionControllerAdditions)
173 //Decode the stack trace within [self userInfo] and return it
174 - (NSString *)decodedExceptionStackTrace
176         NSDictionary    *dict = [self userInfo];
177         NSString        *stackTrace = nil;
179         //Turn the nonsense of memory addresses into a human-readable backtrace complete with line numbers
180         if (dict && (stackTrace = [dict objectForKey:NSStackTraceKey])) {
181                 NSMutableString         *processedStackTrace;
182                 NSString                        *str;
183                 
184                 /*We use several command line apps to decode our exception:
185                  *      * atos -p PID addresses...: converts addresses (hex numbers) to symbol names that we can read.
186                  *      * tail -n +3: strip the first three lines.
187                  *      * head -n +NUM: reduces to the first NUM lines. we pass NUM as the number of addresses minus 4.
188                  *      * c++filt: de-mangles C++ names.
189                  *              example, before:
190                  *                      __ZNK12CApplication23CreateClipboardTextViewERsR12CViewManager (in TextWrangler)
191                  *              example, after:
192                  *                      CApplication::CreateClipboardTextView(short&, CViewManager&) const (in TextWrangler)
193                  *      * cat -n: adds line numbers. fairly meaningless, but fun.
194                  */
195                 str = [NSString stringWithFormat:@"%s -p %d %@ | tail -n +3 | head -n +%d | %s | cat -n",
196                         [[[[NSBundle mainBundle] pathForResource:@"atos" ofType:nil] stringByEscapingForShell] fileSystemRepresentation], //atos arg 0
197                         [[NSProcessInfo processInfo] processIdentifier], //atos arg 2 (argument to -p)
198                         stackTrace, //atos arg 3..inf
199                         ([[stackTrace componentsSeparatedByString:@"  "] count] - 4), //head arg 3
200                         [[[[NSBundle mainBundle] pathForResource:@"c++filt" ofType:nil] stringByEscapingForShell] fileSystemRepresentation]]; //c++filt arg 0   
202                 FILE *file = popen( [str UTF8String], "r" );
203                 NSMutableData *data = [[NSMutableData alloc] init];
205                 if (file) {
206                         NSZone  *zone = [self zone];
208                         size_t   bufferSize = getpagesize();
209                         char    *buffer = NSZoneMalloc(zone, bufferSize);
210                         if (!buffer) {
211                                 buffer = alloca(bufferSize = 512);
212                                 zone = NULL;
213                         }
215                         size_t   amountRead;
217                         while ((amountRead = fread(buffer, sizeof(char), bufferSize, file))) {
218                                 [data appendBytes:buffer length:amountRead];
219                         }
221                         if (zone) NSZoneFree(zone, buffer);
223                         pclose(file);
224                 }
226                 //we use ISO 8859-1 because it preserves all bytes. UTF-8 doesn't (beacuse
227                 //      certain sequences of bytes may get added together or cause the string to be rejected).
228                 //and it shouldn't matter; we shouldn't be getting high-ASCII in the backtrace anyway. :)
229                 processedStackTrace = [[[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] autorelease];
230                 [data release];
231                 
232                 //Clear out a useless string inserted into some stack traces as of 10.4 to improve crashlog readability
233                 [processedStackTrace replaceOccurrencesOfString:@"task_start_peeking: can't suspend failed  (ipc/send) invalid destination port"
234                                                                                          withString:@""
235                                                                                                 options:NSLiteralSearch
236                                                                                                   range:NSMakeRange(0, [processedStackTrace length])];
237                 
238                 return processedStackTrace;
239         }
240         
241         //If we are unable to decode the stack trace, return the best we have
242         return stackTrace;
245 @end