Is it possible to catch Unix signals globally?

I'm inquiring mostly about signals like EXCBADACCESS, etc?

MacBook Pro 17", 2.66 GHz i7, 4GB 1067 DDR3 RAM, OS X 10.6.4 /// iPhone 4, 32GB, iOS 4

Posted on Feb 4, 2011 2:01 AM

Reply
9 replies

Feb 4, 2011 5:40 AM in response to Den B.

By global do you mean all of signals?

if so just loop from from SIGHUP to SIGUSR2 and register each.

for (int i = SIGHUP; i <= SIGUSR2; i++){
signal(i,signalHandler);
}


Keep in mind it's a very (I can't stress this enough) very, bad idea to try and catch seg faults, and by the sound of your post it looks like you're trying to to that. When you get EXC BADACCESS it means there is something wrong with your program that needs to be corrected. Chances are that when you get EXC BADACCESS and you catch the seg fault signal that is sent, you will not be able to correct the damage that has already been done(memory wise).

Feb 4, 2011 9:14 AM in response to EDLundquist

Thanks. And, no, I'm not trying to recover from it. I'm trying to log it for debugging purposes.

And I forgot to mention - this is for iOS and not for Mac app.

Anyway, I'm still struggling to catch it. Here's where I am so far. Correct me if I'm wrong:

1. I add the code you gave me into the main() function, right?

#include <execinfo.h>
void signal_handler (int signum);
int main(int argc, char *argv[]) {
for (int i = SIGHUP; i <= SIGUSR2; i++){
signal(i, signal_handler);
}

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
void signal_handler (int signum)
{
NSLog(@"### Signal caught ####");
}


2. And then to trigger a memory fault:

int v = strlen((const char*)1);
static int e = 0;
e += v;


In the code above the signal_handler() is never invoked. Instead I get an app crash in the console window only. Any idea why?

Feb 5, 2011 12:29 PM in response to Den B.

I just popped


//
// main.m
//
// Created by Eric Lundquist on 2/5/11.
// Copyright N/A 2011. All rights reserved.
//
#import <UIKit/UIKit.h>
#import<mach/mach.h>
void signal_handler (int signum)
{
printf("### Signal caught #### ");
exit(0);
}
int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
for (int i = SIGHUP; i < SIGUSR2; i++){
signal(i, signal_handler);
}
signal(EXCBADACCESS, signal_handler);
int v = strlen((const char*)1);
static int e = 0;
e += v;

int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}


into a test bed and ran it in a simulator and the signal was caught fine. Would you mind posting how and where you are setting up your signal handling mechanism?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Is it possible to catch Unix signals globally?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.