Thursday, June 01, 2006

How SIGINFO structure makes our life easy...

Well have been fixing code failing due to SIGFPE, The code was written to handle all signals like (SIGBUS,SIGFPE,SIGSEGV....). Well the culprit is SIGFPE which generally happen due to compiler optimization, the tricky thing is that I dont recevie SIGFPE when I run this through a debugger, it happens only when I run it normally.
Writing siginfo handlers will help you get the culprit virtual address which causes the problem. See the following handy code.
=======================================
struct sigaction action;
action.sa_sigaction = my_handler;
action.sa_flags = SA_RESTART | SA_SIGINFO | SA_RESETHAND;
if(sigaction(SIGBUS,&action,NULL)<0) {
printf("Cannot register handler\n");
}

HANDLER:
void my_handler(int sig,siginfo_t *siginfo,void *ucontext){
if(sig == SIGBUS){
printf("SIGBUS Caught\n");
switch(siginfo->si_code){
case BUS_ADRALN:
printf("SIGBUS due to address alignment at %x\n",siginfo->si_addr);
break;
case BUS_ADRERR:
printf("SIGBUS due to BUS_ADRERR%08lx\n",siginfo->si_addr);
break;
case BUS_OBJERR:
printf("SIGBUS due to hardware\n");
} q


Cheers! V.

No comments: