Saturn
Saturn

Reputation: 18149

What can I do in a signal handler?

struct sigaction newSignalAction;
memset(&newSignalAction, 0, sizeof(newSignalAction));
newSignalAction.sa_handler = &SignalHandler;
sigaction(SIGSEGV, &newSignalAction, NULL);

[TestFlight takeOff:@"etc etc etc"];

Then

void SignalHandler(int sig) {
    NSLog(@"Boom");
}

I tried to perform a SIGSEGV signal:

int *p = NULL;
*p = 1;

But my handler is not called. Instead, Xcode points out the line *p = 1.

I have read that you can't print stuff with a signal handler. Maybe that's the problem. What can I do then? I heard that you're supposed to save data - so I guess I can create files in the Documents directory. I tried to do that, too, but the files are not created either.

Upvotes: 5

Views: 2263

Answers (1)

Greg Parker
Greg Parker

Reputation: 8012

To a close approximation you can't do anything in a signal handler. The problem is that the code interrupted by the signal might be holding locks. If your handler tries to take the same locks then it deadlocks.

The sigaction man page lists the functions that are officially approved for use in a signal handler. The list is short. Neither NSLog() nor Objective-C method dispatch is in that list.

However, if NSLog() doesn't deadlock then it should print output as usual. Perhaps your crash is SIGBUS instead of SIGSEGV. Perhaps the compiler optimizer is transforming your *NULL=1 so that it crashes in a different way. Perhaps TestFlight installs its own signal handler that replaces your signal handler.

Upvotes: 6

Related Questions