Crash Reporter for iPhone Applications (Part 2)

crashDuck.png

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

  1. int main(int argc, char *argv[]) {
  2.         struct sigaction mySigAction;
  3.         mySigAction.sa_sigaction = mysighandler;
  4.         mySigAction.sa_flags = SA_SIGINFO;
  5.         sigemptyset(&mySigAction.sa_mask);
  6.         sigaction(SIGQUIT, &mySigAction, NULL);
  7.         sigaction(SIGILL, &mySigAction, NULL);
  8.         sigaction(SIGTRAP, &mySigAction, NULL);
  9.         sigaction(SIGABRT, &mySigAction, NULL);
  10.         sigaction(SIGEMT, &mySigAction, NULL);
  11.         sigaction(SIGFPE, &mySigAction, NULL);
  12.         sigaction(SIGBUS, &mySigAction, NULL);
  13.         sigaction(SIGSEGV, &mySigAction, NULL);
  14.         sigaction(SIGSYS, &mySigAction, NULL);
  15.         sigaction(SIGPIPE, &mySigAction, NULL);
  16.         sigaction(SIGALRM, &mySigAction, NULL);
  17.         sigaction(SIGXCPU, &mySigAction, NULL);
  18.         sigaction(SIGXFSZ, &mySigAction, NULL);
  19.        
  20.         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  21.         int retVal = UIApplicationMain(argc, argv, nil, nil);
  22.         [pool release];
  23.         return retVal;
  24. }

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)

  1. void mysighandler(int sig, siginfo_t *info, void *context) {
  2.         void *backtraceFrames[128];
  3.         int frameCount = backtrace(backtraceFrames, 128);
  4.  
  5.         // report the error
  6. }

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)

About these ads
This entry was posted in Development and tagged , . Bookmark the permalink.

6 Responses to Crash Reporter for iPhone Applications (Part 2)

  1. Pingback: raoli.com » Blog Archive » links for 2008-10-21

  2. Arnab says:

    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

  3. Rudyard K. Prab says:

    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.

  4. Clark Dennison says:

    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.

  5. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s