#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;
}