Crash Reporter for iPhone Applications (Part 2)

In part one I describe how to set up an Exception Handler, Uli discovered as first one that this handles not all cases. The missing part is a signal handler to get information of SIGSEGV, SIGBUS, … signals.
I thought the hard part of this is getting the backtrace inside a signal handler. I already found code for this, but I couldn’t use it because it was GPL. I tried the easy way, offering the author money to release it under public domain, oh boy this was a waste of time. But now that I found my own solution for this I’m happy that I didn’t spend money on this. (1 line versus 20 lines of code)
Let’s start with setting up a signal handler, the good old man page helps.
-
int main(int argc, char *argv[]) {
-
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);
-
-
int retVal = UIApplicationMain(argc, argv, nil, nil);
-
[pool release];
-
return retVal;
-
}
And here the line of code I was searching for over two days: backtrace(3). You don’t find this little bastard if you search in Xcode Help with iPhone OS Library selected (there goes my two days)
-
void mysighandler(int sig, siginfo_t *info, void *context) {
-
void *backtraceFrames[128];
-
int frameCount = backtrace(backtraceFrames, 128);
-
-
// report the error
-
}
Like in the Exception Handler you now just use backtrace_symbols(3). (Example)
Good luck with working down the crash reports you will get now
Crash Reporter for iPhone Applications (Part 1)
Crash Reporter for iPhone Applications (Part 2)

