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

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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值