
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.
/* GeSHi (C) 2004 – 2007 Nigel McNie (http://qbnz.com/highlighter) */
.objc .de1, .objc .de2 {font-family: ‘Courier New’, Courier, monospace; font-weight: normal; font-size: 11px; }
.objc {font-family: monospace;}
.objc .imp {font-weight: bold; color: red;}
.objc li {background: #f8f8f8;}
.objc li.li2 {background: #f8f8f8;}
.objc .kw1 {color: #0000ff;}
.objc .kw2 {color: #0000ff;}
.objc .kw3 {color: #0000dd;}
.objc .kw4 {color: #0000ff;}
.objc .kw5 {color: #0000ff;}
.objc .kw6 {color: #0000ff;}
.objc .co1 {color: #ff0000;}
.objc .co2 {color: #339900;}
.objc .coMULTI {color: #ff0000; font-style: italic;}
.objc .es0 {color: #666666; font-weight: bold;}
.objc .br0 {color: #002200;}
.objc .st0 {color: #666666;}
.objc .nu0 {color: #0000dd;}
html>body .entry .objc ol li { margin: 0 0 0 0; }
.syntax { padding-left: 15px; background-color: #E8E8E8; text-align:left; }
-
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)
Pingback: raoli.com » Blog Archive » links for 2008-10-21
Hi Atlan,
Awesome information you have published on this block.Is it possible to give a sample application which can be run on the Iphone Simulator to generate the crash and see how the stack trace/report sending works?
Thanks in advance
-A
It’s not safe to try to report the crash log from within the signal handler. The process is an indeterminate state, and all code executed from within a signal handler must be async-safe. Due to calls to the pthread API (which attempts to acquire globals locks), backtrace(3) implementation is not async-safe, and can thus cause a deadlock in your signal handler.
Rudyard, that’s true, I recommend to use http://code.google.com/p/plcrashreporter/
Chris,
I was trying to use plcrashreport. Would you still recommend it. I have not had much luck using it as I can’t get the simulator build to compile and the iphone release crashes on the first call.
Clark, it’s funny that you ask that, I was just setting up two apps with a Crash Reporter today
At the moment I’m using http://macdevcrashreports.com/ && https://github.com/TheRealKerni/CrashReporterDemo It’s based on plcrashreport and super easy to setup