内存出错信号处理例子 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);
}
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);
}
相关文章推荐
- 队列小记三之链式存储
- JS前端下载文件
- Android内存泄漏的各种原因详解
- JSP乱码问题
- 123
- JavaBeans和关系数据库的镜像
- hdu 5285 wyh2000 and pupil(判断是否是二分图+贪心)
- IIS出现server application error解决方案
- 剑指off-复制复杂链表
- 123
- 学java 遇到的那些事儿
- Android应用内存泄漏与分析
- Python学习笔记——Django配置问题1:syncdb
- Appium 环境搭建
- Jsp页面获取项目名称
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
- 无废话网页重构系列——(8)重构难点:语义和命名
- 关于spring mvc 循环加载mapper.xml 文件
- layoutSubviews
- 怎样给ESXI5.5 系统安装补丁