This is a sample project for the reusable class ATMPPR_Signal, which provides interfaces for installing (and removing) UNIX signal handlers

See ViewController.m for example usage, but basically to set a signal handler for one (or more) signal(s) just use ...


+(void)catchSignals:(NSArray<NSNumber*>*)signals signalHandler:(void (^ _Nullable)(int signalRaised))handlerBlock;


as in ...

    [ATMPPR_Signal catchSignals:ATMPPR_Signal.crashSignals signalHandler:^(int signalRaised) {
        NSLog(@"crashing with signal %d (%s)",signalRaised,strForSignal( signalRaised));
    }];


If you just want to handle a single signal you may use the simplified version...

+(void)catchSignal:(int)signalNumber signalHandler:(void (^ _Nullable)(int signalRaised))handlerBlock;

... as in ...

    [ATMPPR_Signal catchSignal:SIGHUP signalHandler:^(int signalRaised) {
        NSLog(@"just caught SIGHUP ... doing something in response");
    }];


As a special case, if you have code that *may* call abort you can "wrap" that code in something similar to a "try catch" block with the following class method:

+(void)codeThatMayCallAbort:(void (^)())tryBlock abortResponse:(void (^ _Nullable)())catchBlock;

    for example:

    [ATMPPR_Signal codeThatMayCallAbort:^{
        NSLog(@"about to call abort");
        abort();
        NSLog(@"after call to abort");  // this line is here simply to show that it never gets called
    } abortResponse:^{
        NSLog(@"inside abort catch block");
    }];


**** IMPORTANT NOTE: running this sample program in xcode can interfere with signal catching (xcode wants to catch the signals first). For better results run the program directly from the terminal to see the console output ...


    $ signals.app/Contents/MacOS/signals
    2017-03-03 00:53:10.484 signals[90417:49983762] crashing with signal 4 (ILLegal instruction)
    2017-03-03 00:53:11.030 signals[90417:49983762] crashing with signal 6 (ABoRT)
    2017-03-03 00:53:12.056 signals[90417:49983762] crashing with signal 10 (BUS error)
    2017-03-03 00:53:12.606 signals[90417:49984120] caught signal 1 (HangUP)
    2017-03-03 00:53:14.110 signals[90417:49984120] caught signal 29 (INFOrmation request)


**** alternately you can enter this command in lldb console after starting the program (but before sending SIGABRT)

process handle SIGABRT SIGILL SIGBUS --notify false --pass true --stop false
