您的位置:首页 > 其它

内存出错信号处理例子 void signal_handler()

2015-07-20 17:01 295 查看
signal(SIGSEGV,set_on_segment_fault); 

void set_on_segment_fault(int sig)

{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = handler;
sigaction(SIGSEGV, &act, NULL);
}

static void handler(int signum, siginfo_t *info, void  *arg)

{

#ifdef __HI_CODEC__

// ucontext_t* context= (ucontext_t *)arg;
void* buffer[8] = {0, };
size_t nptr;
int i;
char **strings= NULL;
/*
  unsigned long sp,top,bottom;
  int i=1;
  */
fprintf(stderr, "==============================================================\n");
fprintf(stderr, "(1)SIGSEGV : %d\n", info->si_signo);
fprintf(stderr, "ADDR : %p\n", info->si_addr);
fprintf(stderr, "VALUE : %x\n", *(int*)(info->si_addr));
//fprintf(stderr, "%x\n", (int)'H');

// fprintf(stderr,"context : %lx\n",context->uc_mcontext.regs->nip);
// fprintf(stderr,"sp      : %lx\n",context->uc_mcontext.regs->gpr[1]);

#else //__HI_CODEC__
ucontext_t* context= (ucontext_t *)arg;
void* buffer[8] = {0, };
size_t nptr;
int i;
char **strings= NULL;
/*
unsigned long sp,top,bottom;
int i=1;
*/
fprintf(stderr, "==============================================================\n");
fprintf(stderr, "(1)SIGSEGV : %d\n", info->si_signo);
//fprintf(stderr, "ADDR : %p\n", info->si_addr);
//fprintf(stderr, "VALUE : %x\n", *(int*)(info->si_addr));
//fprintf(stderr, "%x\n", (int)'H');

fprintf(stderr,"context : %lx\n",context->uc_mcontext.regs->nip);
fprintf(stderr,"sp      : %lx\n",context->uc_mcontext.regs->gpr[1]);

#endif //__HI_CODEC__
/*
  sp = context->uc_mcontext.regs->gpr[1];
  bottom = sp & ~(THREAD_SIZE-1UL);
  top = bottom + THREAD_SIZE;
  while (sp < top && sp >= bottom) {
  printf("%.16lx ", *(unsigned long *)(sp + 16));
  sp = *(unsigned long *)sp;
  if (!(i % 4))
  printf("\n");
  if (i++ == 32)
  break;
  }
  */
fprintf(stderr, "==============================================================\n");
nptr = backtrace(buffer, 8);
fprintf(stderr, "(2)BACKTRACE num : %d\n" , nptr);
strings = backtrace_symbols(buffer, nptr);
if(strings == NULL)
{
fprintf(stderr, "backtrace symbols err\n");
fprintf(stderr, "==============================================================\n");
// for console.log
fflush(stderr);
sleep(3);
exit(0);
}
for (i=0 ; i<nptr; i++)
{
fprintf(stderr, "addr : %s\n", strings[i]);
}
free(strings);
fprintf(stderr, "==============================================================\n");
// for console.log
fflush(stderr);
sleep(3);
exit(0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: