Learning Uncaught Exception Handling on Mac

#import <Foundation/Foundation.h>

#import <ExceptionHandling/NSExceptionHandler.h>

 

void printExceptionStackTrace(NSException *e)

{

          NSString *stack = [[e userInfo] objectForKey:NSStackTraceKey];

          if (stack)

          {

                   NSTask *task = [[NSTask alloc] init];

                   NSString *pid = [[NSNumber numberWithInt:getpid()] stringValue];

                   NSMutableArray *args = [NSMutableArray array];

                   [args addObject:@"-p"];

                   [args addObject:pid];

                   [args addObjectsFromArray:[stack componentsSeparatedByString:@" "]];

                   [task setLaunchPath:@"/usr/bin/atos"];

                   [task setArguments:args];

                   [task launch];

                   [task waitUntilExit];

                   [task release];

          }

}

 

void _uncaughtExceptionHandler(NSException *e)

{

          NSLog(@"In uncaught exception handler");//: %@ (%@)", e, [e userInfo]);

          printExceptionStackTrace(e);

 

          NSArray *stacks = [e callStackReturnAddresses];

          id frame;

          for (frame in stacks)

                   NSLog(@"%x", frame);

 

          exit (0);

}

 

void mysighandler(int sig, siginfo_t *info, void *context)

{

          NSLog(@"Signal handler");

          exit (0);

}

 

int main (int argc, const char *argv[])

{

          // setting up signal handler.

          struct sigaction mySigAction;

          mySigAction.sa_sigaction = mysighandler;

          mySigAction.sa_flags = SA_SIGINFO;

          sigemptyset(&mySigAction.sa_mask);

          sigaction(SIGQUIT, &mySigAction, NULL);

          sigaction(SIGILL, &mySigAction, NULL);

          sigaction(SIGTRAP, &mySigAction, NULL);

          sigaction(SIGABRT, &mySigAction, NULL);

          sigaction(SIGEMT, &mySigAction, NULL);

          sigaction(SIGFPE, &mySigAction, NULL);

          sigaction(SIGBUS, &mySigAction, NULL);

          sigaction(SIGSEGV, &mySigAction, NULL);

          sigaction(SIGSYS, &mySigAction, NULL);

          sigaction(SIGPIPE, &mySigAction, NULL);

          sigaction(SIGALRM, &mySigAction, NULL);

          sigaction(SIGXCPU, &mySigAction, NULL);

          sigaction(SIGXFSZ, &mySigAction, NULL);

 

          NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 

          //NSSetUncaughtExceptionHandler((NSUncaughtExceptionHandler *)_uncaughtExceptionHandler);

 

          [[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask];

 

          @try {

                   //int i = 0;

                   //int j = 1 / i;

                   [NSException raise:NSGenericException format:@"Test Exception"];

          }

          @catch (NSException * e) {

                   NSLog(@"In catch block: %@: %@", e, [e userInfo]);

                   //printExceptionStackTrace(e);

 

                   NSArray *stacks = [e callStackReturnAddresses];

                   id frame;

                   for (frame in stacks)

                             NSLog(@"%i", frame);

          }

          @finally {

                   //…

          }

          // Uncaught exception…

          //[NSException raise:NSGenericException format:@"Test UncaughtException"];

 

          [pool release];

          return 0;

}

Visual Studio 2010 has a good Mixed mode managed dump debugging

I just got a copy of Visual Studio 2010 beta 1 and installed on my Windows 7 machine, playing with new functions available for dump debugging. As the following shows, the mixed callstacks in managed dump can be parsed correctly in Post-Mortem mode. (But it’s only available for managed dump target for CLR v4. Who care? That’s enough!! It’s trend that all our applications will move to v4. It’s only a matter of time.)

There are other great functions such as Fun-Eval, known as “Virtual Code Execution”. Check Mike Stall’s MSDN blog:

http://blogs.msdn.com/jmstall/archive/2009/05/21/virtual-code-execution-via-il-interpretation.aspx

VS2010_1

Looking forward to beta 2, RC build, and RTM.

Austin