4

My app includes crash reporting library that using NSSetUncaughtExceptionHandler in order to catch the crashes. I need to implement custom action (log crash and display alert view) before\after the crash reporting implementation. To achieve this behavior, first I'm keep a reference to previous UncaughtExceptionHandler using NSGetUncaughtExceptionHandler(), and than registering my custom exception handler and signals handler. In my custom handler I tried to execute previous handler before\after my custom actions, but this throws a signal SIGABRT for previousHandler(exception) (in both cases). Here is the code example :

static NSUncaughtExceptionHandler *previousHandler;

void InstallUncaughtExceptionHandler()
{
    // keep reference to previous handler
    previousHandler = NSGetUncaughtExceptionHandler();

    // register my custom exception handler
    NSSetUncaughtExceptionHandler(&HandleException);
    signal(SIGABRT, SignalHandler);
    signal(SIGILL, SignalHandler);
    signal(SIGSEGV, SignalHandler);
    signal(SIGFPE, SignalHandler);
    signal(SIGBUS, SignalHandler);
    signal(SIGPIPE, SignalHandler);
}

void HandleException(NSException *exception)
{
    // execute previous handler 
    previousHandler(exception);
    // my custom actions
}

void SignalHandler(int signal)
{
    NSLog(@"SignalHandler");
}
  1. How can I execute previous handler without throws a signal ?
  2. Any ideas why SignalHandler doesn't called when system throws a signal ?
Tsahi Deri
  • 571
  • 1
  • 3
  • 14

2 Answers2

3

Don't register the signal handlers. I have to obfuscate a bit the code presented below, but it's from a production app that's on the App Store:

AppDelegate application:didFinishLaunchingWithOptions:

fabricHandler = NSGetUncaughtExceptionHandler();
NSSetUncaughtExceptionHandler(&customUncaughtExceptionHandler);

Handler:

void customUncaughtExceptionHandler(NSException *exception) {
    // Custom handling

    if (fabricHandler) {
        fabricHandler(exception);
    }
}
Avi
  • 7,469
  • 2
  • 21
  • 22
  • 1
    I have also added 'if' statement but it still crashed. you think the signal handlers cause the problem ? – Tsahi Deri Nov 02 '15 at 08:53
  • Try without them. They aren't necessary, as the default handlers call whatever `NSGetUncaughtExceptionHandler()` returns. You might be creating some kind of loop, but I am not sure. – Avi Nov 02 '15 at 09:04
0

The PreviousSignalHandler might 1) Reset all set signal handlers 2) Call abort

One of the reasons it will abort. So you can do all the stuff you want to do and call the previous handler.

HTH