PageRenderTime 99ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 26ms app.codeStats 1ms

/gecko_api/include/nsObjCExceptions.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 224 lines | 131 code | 36 blank | 57 comment | 6 complexity | db41a84f207b02949475bccf767af85c MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License
  6 * Version 1.1 (the "License"); you may not use this file except in
  7 * compliance with the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is mozilla.org code.
 16 *
 17 * The Initial Developer of the Original Code is 
 18 * Mozilla Corporation.
 19 * Portions created by the Initial Developer are Copyright (C) 2008
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *  Josh Aas <josh@mozilla.com>
 24 *  Robert O'Callahan <robert@ocallahan.org>
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the NPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40#ifndef nsObjCExceptions_h_
 41#define nsObjCExceptions_h_
 42
 43#import <Foundation/Foundation.h>
 44
 45#ifdef DEBUG
 46#import <ExceptionHandling/NSExceptionHandler.h>
 47#endif
 48
 49#include <unistd.h>
 50#include <signal.h>
 51#include "nsError.h"
 52
 53// See Mozilla bug 163260.
 54// This file can only be included in an Objective-C context.
 55
 56static void nsObjCExceptionLog(NSException* aException)
 57{
 58  NSLog(@"Mozilla has caught an Obj-C exception [%@: %@]",
 59        [aException name], [aException reason]);
 60
 61#ifdef DEBUG
 62  @try {
 63    // Try to get stack information out of the exception. 10.5 returns the stack
 64    // info with the callStackReturnAddresses selector.
 65    NSArray *stackTrace = nil;
 66    if ([aException respondsToSelector:@selector(callStackReturnAddresses)]) {
 67      NSArray* addresses = (NSArray*)
 68        [aException performSelector:@selector(callStackReturnAddresses)];
 69      if ([addresses count])
 70        stackTrace = addresses;
 71    }
 72
 73    // 10.4 doesn't respond to callStackReturnAddresses so we'll try to pull the
 74    // stack info out of the userInfo. It might not be there, sadly :(
 75    if (!stackTrace)
 76      stackTrace = [[aException userInfo] objectForKey:NSStackTraceKey];
 77
 78    if (stackTrace) {
 79      // The command line should look like this:
 80      //   /usr/bin/atos -p <pid> -printHeader <stack frame addresses>
 81      NSMutableArray *args =
 82        [NSMutableArray arrayWithCapacity:[stackTrace count] + 3];
 83
 84      [args addObject:@"-p"];
 85      int pid = [[NSProcessInfo processInfo] processIdentifier];
 86      [args addObject:[NSString stringWithFormat:@"%d", pid]];
 87
 88      [args addObject:@"-printHeader"];
 89
 90      unsigned int stackCount = [stackTrace count];
 91      unsigned int stackIndex = 0;
 92      for (; stackIndex < stackCount; stackIndex++)
 93        [args addObject:[[stackTrace objectAtIndex:stackIndex] stringValue]];
 94
 95      NSPipe *outPipe = [NSPipe pipe];
 96
 97      NSTask *task = [[NSTask alloc] init];
 98      [task setLaunchPath:@"/usr/bin/atos"];
 99      [task setArguments:args];
100      [task setStandardOutput:outPipe];
101      [task setStandardError:outPipe];
102
103      NSLog(@"Generating stack trace for Obj-C exception...");
104
105      // This will throw an exception if the atos tool cannot be found, and in
106      // that case we'll just hit our @catch block below.
107      [task launch];
108
109      [task waitUntilExit];
110      [task release];
111
112      NSData *outData =
113        [[outPipe fileHandleForReading] readDataToEndOfFile];
114      NSString *outString =
115        [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
116
117      NSLog(@"Stack trace:\n%@", outString);
118
119      [outString release];
120    }
121    else {
122      NSLog(@"<No stack information available for Obj-C exception>");
123    }
124  }
125  @catch (NSException *exn) {
126    NSLog(@"Failed to generate stack trace for Obj-C exception [%@: %@]",
127          [exn name], [exn reason]);
128  }
129#endif
130}
131
132static void nsObjCExceptionAbort()
133{
134  // We need to raise a mach-o signal here, the Mozilla crash reporter on
135  // Mac OS X does not respond to POSIX signals. Raising mach-o signals directly
136  // is tricky so we do it by just derefing a null pointer.
137  int* foo = NULL;
138  *foo = 1;
139}
140
141static void nsObjCExceptionLogAbort(NSException *e)
142{
143  nsObjCExceptionLog(e);
144  nsObjCExceptionAbort();
145}
146
147#define NS_OBJC_TRY(_e, _fail)                     \
148@try { _e; }                                       \
149@catch(NSException *_exn) {                        \
150  nsObjCExceptionLog(_exn);                        \
151  _fail;                                           \
152}
153
154#define NS_OBJC_TRY_EXPR(_e, _fail)                \
155({                                                 \
156   typeof(_e) _tmp;                                \
157   @try { _tmp = (_e); }                           \
158   @catch(NSException *_exn) {                     \
159     nsObjCExceptionLog(_exn);                     \
160     _fail;                                        \
161   }                                               \
162   _tmp;                                           \
163})
164
165#define NS_OBJC_TRY_EXPR_NULL(_e)                  \
166NS_OBJC_TRY_EXPR(_e, 0)
167
168#define NS_OBJC_TRY_IGNORE(_e)                     \
169NS_OBJC_TRY(_e, )
170
171// To reduce code size the abort versions do not reuse above macros. This allows
172// catch blocks to only contain one call.
173
174#define NS_OBJC_TRY_ABORT(_e)                      \
175@try { _e; }                                       \
176@catch(NSException *_exn) {                        \
177  nsObjCExceptionLogAbort(_exn);                   \
178}
179
180#define NS_OBJC_TRY_EXPR_ABORT(_e)                 \
181({                                                 \
182   typeof(_e) _tmp;                                \
183   @try { _tmp = (_e); }                           \
184   @catch(NSException *_exn) {                     \
185     nsObjCExceptionLogAbort(_exn);                \
186   }                                               \
187   _tmp;                                           \
188})
189
190// For wrapping blocks of Obj-C calls. Terminates app after logging.
191#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK @try {
192#define NS_OBJC_END_TRY_ABORT_BLOCK   } @catch(NSException *_exn) {            \
193                                        nsObjCExceptionLogAbort(_exn);         \
194                                      }
195
196// Same as above ABORT_BLOCK but returns a value after the try/catch block to
197// suppress compiler warnings. This allows us to avoid having to refactor code
198// to get scoping right when wrapping an entire method.
199
200#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL @try {
201#define NS_OBJC_END_TRY_ABORT_BLOCK_NIL   } @catch(NSException *_exn) {        \
202                                            nsObjCExceptionLogAbort(_exn);     \
203                                          }                                    \
204                                          return nil;
205
206#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL @try {
207#define NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL   } @catch(NSException *_exn) {     \
208                                               nsObjCExceptionLogAbort(_exn);  \
209                                             }                                 \
210                                             return nsnull;
211
212#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT @try {
213#define NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT   } @catch(NSException *_exn) {   \
214                                                 nsObjCExceptionLogAbort(_exn);\
215                                               }                               \
216                                               return NS_ERROR_FAILURE;
217
218#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN    @try {
219#define NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(_rv) } @catch(NSException *_exn) {   \
220                                                  nsObjCExceptionLogAbort(_exn);\
221                                                }                               \
222                                                return _rv;
223
224#endif // nsObjCExceptions_h_