michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Undo the damage that exception_defines.h does. michael@0: #undef try michael@0: #undef catch michael@0: michael@0: #ifndef nsObjCExceptions_h_ michael@0: #define nsObjCExceptions_h_ michael@0: michael@0: #import michael@0: michael@0: #ifdef DEBUG michael@0: #import michael@0: #endif michael@0: michael@0: #if defined(MOZ_CRASHREPORTER) && defined(__cplusplus) michael@0: #include "nsICrashReporter.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include "nsError.h" michael@0: michael@0: // Undo the damage that exception_defines.h does. michael@0: #undef try michael@0: #undef catch michael@0: michael@0: /* NOTE: Macros that claim to abort no longer abort, see bug 486574. michael@0: * If you actually want to log and abort, call "nsObjCExceptionLogAbort" michael@0: * from an exception handler. At some point we will fix this by replacing michael@0: * all macros in the tree with appropriately-named macros. michael@0: */ michael@0: michael@0: // See Mozilla bug 163260. michael@0: // This file can only be included in an Objective-C context. michael@0: michael@0: __attribute__((unused)) michael@0: static void nsObjCExceptionLog(NSException* aException) michael@0: { michael@0: NSLog(@"Mozilla has caught an Obj-C exception [%@: %@]", michael@0: [aException name], [aException reason]); michael@0: michael@0: #if defined(MOZ_CRASHREPORTER) && defined(__cplusplus) michael@0: // Attach exception info to the crash report. michael@0: nsCOMPtr crashReporter = michael@0: do_GetService("@mozilla.org/toolkit/crash-reporter;1"); michael@0: if (crashReporter) michael@0: crashReporter->AppendObjCExceptionInfoToAppNotes(static_cast(aException)); michael@0: #endif michael@0: michael@0: #ifdef DEBUG michael@0: @try { michael@0: // Try to get stack information out of the exception. 10.5 returns the stack michael@0: // info with the callStackReturnAddresses selector. michael@0: NSArray *stackTrace = nil; michael@0: if ([aException respondsToSelector:@selector(callStackReturnAddresses)]) { michael@0: NSArray* addresses = (NSArray*) michael@0: [aException performSelector:@selector(callStackReturnAddresses)]; michael@0: if ([addresses count]) michael@0: stackTrace = addresses; michael@0: } michael@0: michael@0: // 10.4 doesn't respond to callStackReturnAddresses so we'll try to pull the michael@0: // stack info out of the userInfo. It might not be there, sadly :( michael@0: if (!stackTrace) michael@0: stackTrace = [[aException userInfo] objectForKey:NSStackTraceKey]; michael@0: michael@0: if (stackTrace) { michael@0: // The command line should look like this: michael@0: // /usr/bin/atos -p -printHeader michael@0: NSMutableArray *args = michael@0: [NSMutableArray arrayWithCapacity:[stackTrace count] + 3]; michael@0: michael@0: [args addObject:@"-p"]; michael@0: int pid = [[NSProcessInfo processInfo] processIdentifier]; michael@0: [args addObject:[NSString stringWithFormat:@"%d", pid]]; michael@0: michael@0: [args addObject:@"-printHeader"]; michael@0: michael@0: unsigned int stackCount = [stackTrace count]; michael@0: unsigned int stackIndex = 0; michael@0: for (; stackIndex < stackCount; stackIndex++) { michael@0: unsigned long address = michael@0: [[stackTrace objectAtIndex:stackIndex] unsignedLongValue]; michael@0: [args addObject:[NSString stringWithFormat:@"0x%lx", address]]; michael@0: } michael@0: michael@0: NSPipe *outPipe = [NSPipe pipe]; michael@0: michael@0: NSTask *task = [[NSTask alloc] init]; michael@0: [task setLaunchPath:@"/usr/bin/atos"]; michael@0: [task setArguments:args]; michael@0: [task setStandardOutput:outPipe]; michael@0: [task setStandardError:outPipe]; michael@0: michael@0: NSLog(@"Generating stack trace for Obj-C exception..."); michael@0: michael@0: // This will throw an exception if the atos tool cannot be found, and in michael@0: // that case we'll just hit our @catch block below. michael@0: [task launch]; michael@0: michael@0: [task waitUntilExit]; michael@0: [task release]; michael@0: michael@0: NSData *outData = michael@0: [[outPipe fileHandleForReading] readDataToEndOfFile]; michael@0: NSString *outString = michael@0: [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding]; michael@0: michael@0: NSLog(@"Stack trace:\n%@", outString); michael@0: michael@0: [outString release]; michael@0: } michael@0: else { michael@0: NSLog(@""); michael@0: } michael@0: } michael@0: @catch (NSException *exn) { michael@0: NSLog(@"Failed to generate stack trace for Obj-C exception [%@: %@]", michael@0: [exn name], [exn reason]); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: __attribute__((unused)) michael@0: static void nsObjCExceptionAbort() michael@0: { michael@0: // We need to raise a mach-o signal here, the Mozilla crash reporter on michael@0: // Mac OS X does not respond to POSIX signals. Raising mach-o signals directly michael@0: // is tricky so we do it by just derefing a null pointer. michael@0: int* foo = nullptr; michael@0: *foo = 1; michael@0: } michael@0: michael@0: __attribute__((unused)) michael@0: static void nsObjCExceptionLogAbort(NSException *e) michael@0: { michael@0: nsObjCExceptionLog(e); michael@0: nsObjCExceptionAbort(); michael@0: } michael@0: michael@0: #define NS_OBJC_TRY(_e, _fail) \ michael@0: @try { _e; } \ michael@0: @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: _fail; \ michael@0: } michael@0: michael@0: #define NS_OBJC_TRY_EXPR(_e, _fail) \ michael@0: ({ \ michael@0: typeof(_e) _tmp; \ michael@0: @try { _tmp = (_e); } \ michael@0: @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: _fail; \ michael@0: } \ michael@0: _tmp; \ michael@0: }) michael@0: michael@0: #define NS_OBJC_TRY_EXPR_NULL(_e) \ michael@0: NS_OBJC_TRY_EXPR(_e, 0) michael@0: michael@0: #define NS_OBJC_TRY_IGNORE(_e) \ michael@0: NS_OBJC_TRY(_e, ) michael@0: michael@0: // To reduce code size the abort versions do not reuse above macros. This allows michael@0: // catch blocks to only contain one call. michael@0: michael@0: #define NS_OBJC_TRY_ABORT(_e) \ michael@0: @try { _e; } \ michael@0: @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } michael@0: michael@0: #define NS_OBJC_TRY_EXPR_ABORT(_e) \ michael@0: ({ \ michael@0: typeof(_e) _tmp; \ michael@0: @try { _tmp = (_e); } \ michael@0: @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } \ michael@0: _tmp; \ michael@0: }) michael@0: michael@0: // For wrapping blocks of Obj-C calls. Does not actually terminate. michael@0: #define NS_OBJC_BEGIN_TRY_ABORT_BLOCK @try { michael@0: #define NS_OBJC_END_TRY_ABORT_BLOCK } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } michael@0: michael@0: // Same as above ABORT_BLOCK but returns a value after the try/catch block to michael@0: // suppress compiler warnings. This allows us to avoid having to refactor code michael@0: // to get scoping right when wrapping an entire method. michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL @try { michael@0: #define NS_OBJC_END_TRY_ABORT_BLOCK_NIL } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } \ michael@0: return nil; michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL @try { michael@0: #define NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } \ michael@0: return nullptr; michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT @try { michael@0: #define NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } \ michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN @try { michael@0: #define NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(_rv) } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn);\ michael@0: } \ michael@0: return _rv; michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_LOGONLY_BLOCK @try { michael@0: #define NS_OBJC_END_TRY_LOGONLY_BLOCK } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } michael@0: michael@0: #define NS_OBJC_BEGIN_TRY_LOGONLY_BLOCK_RETURN @try { michael@0: #define NS_OBJC_END_TRY_LOGONLY_BLOCK_RETURN(_rv) } @catch(NSException *_exn) { \ michael@0: nsObjCExceptionLog(_exn); \ michael@0: } \ michael@0: return _rv; michael@0: michael@0: #endif // nsObjCExceptions_h_