Re: HowTo: catch a signal and get a CrashLog too (Leopard)
Re: HowTo: catch a signal and get a CrashLog too (Leopard)
- Subject: Re: HowTo: catch a signal and get a CrashLog too (Leopard)
- From: "James Peach" <email@hidden>
- Date: Tue, 18 Dec 2007 21:30:21 -0800
On 18/12/2007, David Hoerl <email@hidden> wrote:
> [This for archive searchers. signal() signals CrashLog CrashReporter
> OSX Darwin]
>
> It took me a while to get this going, thus the post. I have a C-based
> command line tool (CLI) process. It writes logs, and while it could
> report its own failures, crashes resulted in no log entry. So, I
> started catching signals, and write my log (stderr), which worked
> fine (see below).
>
> However, it seems that at least in Leopard, when you catch a signal,
> CrashReporter will not process the crash. Hmmm. So I found that if I
> raised another uncaught signal in the handler, voila, CrashReporter
> creates a crash report, so now I get both.
>
> #include <signal.h>
>
> static void crashCatcher(int sig);
> static char exitStr[] = "Exit due to Signal ";
> static char finalStr[] = ".\n";
>
> // whatever you want to catch - man 3 signal
> (void)signal(SIGILL, crashCatcher);
> (void)signal(SIGFPE, crashCatcher);
> (void)signal(SIGBUS, crashCatcher);
> (void)signal(SIGSEGV, crashCatcher);
> (void)signal(SIGSYS, crashCatcher); // probably overkill
> (void)signal(SIGILL, crashCatcher);
>
> static void
> crashCatcher(int sig)
> {
> char c;
>
> // should not use buffered IO as it may use malloc and friends
> // suggested by Dalrymple and Hillegass, "Advanced Mac OS Prog.",
> // been very helpful to me
> write(STDERR_FILENO, exitStr, strlen(exitStr));
> write(STDERR, exitStr, sizeof(exitStr)-1);
> c = '0' + (sig/10);
> write(STDERR, &c, 1);
> c = '0' + (sig);
> write(STDERR, &c, 1);
> write(STDERR, finalStr, sizeof(finalStr)-1);
>
> // since we caught the signal, CrashReporter will otherwise not run
> // however, this signal is uncaught, so it will crash us and
> CrashReporter will run!
> raise(SIGQUIT);
> }
This works, but re-raising a signal does run the risk of producing a
stack trace that CrashReporter can't walk correctly. I've seen cases
where my own stack logging code and CrashReporter disagree wildly on
what the real stack trace is.
IMHO it's better to just let the fatal signals go straight through to
CrashReporter unless you have a really food reason.
--
James Peach | email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden