Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

higher

macrumors newbie
Original poster
Dec 1, 2008
11
0
Dear All : I use c language in mac os ,and i want to catch the exception SIGFPE.
the question is that i use unix signal function to catch the SIGBUS. of couse it was catched and the application is going on. But the system also get the exception and give me the log
PID: 1112
Thread: 0
Exception: EXC_ARITHMETIC (0x0003)
Codes: EXC_I386_DIV (divide by zero) Thread 0 Crashed:
0 test 0x00002ac8 test(int, int) + 148 (main.cpp:40)
1 test 0x00002b46 main + 82 (main.cpp:52)
2 test 0x00002346 _start + 228 (crt.c:272)
3 test 0x00002261 start + 41
it's a sample test function. can anyone help me? test code is blow:

#include <iostream>
#include <sys/signal.h>
#include <setjmp.h>
#include <sys/signal.h>
typedef void (*sighandler_t)(int);
static sigjmp_buf jmpbuf;
void sig_action(int i)
{
printf("signal: %d\n", i);
siglongjmp(jmpbuf, 1);
return ;
};
int test (int m ,int j)
{

int tt2 = 0;
sighandler_t old ;
old = signal(SIGFPE, sig_action);
if(sigsetjmp(jmpbuf, 1))
{
goto E1;
}
//raise(SIGFPE); // if I use raise to send the exception, everything is ok.
tt2 += m/j;
E1:
old = signal(SIGFPE, old);
return tt2;
}
int main (int argc, char * const argv[])
{
int i = 0, value = 0 ;
for (i = 0; i< 5; i++)
{
printf("i=%d\n",i);
value = test(9, 0);
printf("i2=%d\n",i);
}

std::cout << "Hello, World!\n"<<value<<"i:"<<i;
return 0;
}
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Under Xcode, it works fine for me both ways. (I removed the cout and replaced it with a printf, to make it straight c, but I don't think that would make a difference.)

Output without raise():
Code:
[Session started at 2008-12-02 07:23:26 -0600.]
i=0
signal: 8
i2=0
i=1
signal: 8
i2=1
i=2
signal: 8
i2=2
i=3
signal: 8
i2=3
i=4
signal: 8
i2=4
Goodbye.

The Debugger has exited with status 0.

Output with raise():
Code:
[Session started at 2008-12-02 07:23:26 -0600.]
i=0
signal: 8
i2=0
i=1
signal: 8
i2=1
i=2
signal: 8
i2=2
i=3
signal: 8
i2=3
i=4
signal: 8
i2=4
Goodbye.

The Debugger has exited with status 0.

Edit: Here's the cleaned up code
Code:
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>



typedef void (*sighandler_t)(int);
static sigjmp_buf jmpbuf;

void sig_action(int i)
{
	printf("signal: %d\n", i);
	siglongjmp(jmpbuf, 1);
	return ;
};

int test (int m ,int j) {
	int tt2 = 0;	
	sighandler_t old ;
	old = signal(SIGFPE, sig_action);
	if(sigsetjmp(jmpbuf, 1)) {
		goto E1;
	}
	printf("before raise...\n" ) ; 
	raise(SIGFPE); // if I use raise to send the exception, everything is ok.
	printf("after raise...\n") ; 
	tt2 += m/j;
E1:
	old = signal(SIGFPE, old);
	return tt2;
}

int main (int argc, char * const argv[]) {
	int i = 0, value = 0 ;
	for (i = 0; i< 5; i++) {	
		printf("i=%d\n",i);
		value = test(9, 0);
		printf("i2=%d\n",i);
	}
	printf("Goodbye.\n") ; 
	return 0;
}
 

higher

macrumors newbie
Original poster
Dec 1, 2008
11
0
if you use raise funciton to send exception signal, everything is ok.

but if you use m/j and use double click the app , you will find the consol.log give the exception code (SIGFPE) and also the app will go fine.

can you help me?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
In the system.log, with the raise() condition, I get these logged records:
Code:
Dec  3 10:07:45 todd-burchs-computer kernel[0]: unknown SIGFPE code 0, subcode 0
Dec  3 10:07:45: --- last message repeated 1 time ---
Dec  3 10:07:45 todd-burchs-computer login[69281]: DEAD_PROCESS: 69281 ttys001
Dec  3 10:07:45 todd-burchs-computer kernel[0]: unknown SIGFPE code 0, subcode 0

With the m/j SIGFPE condition, I get these logged messages:
Code:
Dec  3 10:09:36 todd-burchs-computer kernel[0]: unknown SIGFPE code 1, subcode 0
Dec  3 10:09:36: --- last message repeated 1 time ---
Dec  3 10:09:36 todd-burchs-computer login[69335]: DEAD_PROCESS: 69335 ttys001
Dec  3 10:09:36 todd-burchs-computer kernel[0]: unknown SIGFPE code 1, subcode 0
 

higher

macrumors newbie
Original poster
Dec 1, 2008
11
0
sorry system.log is consol.log

sorry system.log is consol.log
your system give the system.log unknow SIGFPE, that because you are using the mac 10.5 system (it's true, i try it on Leopard<10.5> and get the same words "Unknow SIGFPE").

I use mac x 10.4 and the system log give the carsh log for SIGFPE.

and now I use another carbon exception handler funciton : InstallExceptionHandler , it works fine in on thread.
But the new question is that : if i have two thread and they all use the InstallExceptionHandler function, one works fine and the other crash without catching the SIGFPE.

can you help me?
 

higher

macrumors newbie
Original poster
Dec 1, 2008
11
0
installExceptionHandler has problem whit thread that if two thread use this funciton, the second thread must be create before the first thread call InstallExceptionHandler function, otherwise the second thread can't use installExceptionHandler to catch exception.

blow is the words for Apple:
Calls to InstallExceptionHandler only affecting cooperative threads that had already been created, however had no effect for any threads created after it was called (as it should have). InstallExceptionHandler has been modified so that it now applies to currently executing cooperative threads and all threads created after it has been called

Thank you toddburch, i use signal function well in leopard os and it will give a crash log in tiger os.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.