catch sigsegv

0 Reference 

http://blog.chinaunix.net/uid-24098129-id-312659.html

http://blog.youkuaiyun.com/chenjin_zhong/article/details/6129628

http://blog.sina.com.cn/s/blog_566f698201017dty.html


1 Edit 

sudo vim ./usr/include/i386-linux-gnu/sys/ucontext.h

remove  #ifdef __USE_GNU in line 40

remove #endif in line 83

2 Test program

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <ucontext.h>

void my_sigsegv(int signo, siginfo_t *info, void *data)
{
    printf("Produce sigsegv %d\n",signo);
    ucontext_t *u = (ucontext_t *)data;
    int type = (int )u->uc_mcontext.gregs[REG_ERR] & 2;
    if(type == 0) {
        printf("addr:%p read fault\n",info->si_addr);
    }else if(type == 2) {
        printf("addr:%p write fault\n",info->si_addr);
    }
    fflush(stdout);
   exit(-1);

    return ;
}


int main()
{

    struct sigaction act;
    act.sa_handler = NULL;
    act.sa_sigaction = my_sigsegv;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGSEGV , &act , NULL);

    char *pstr = 0x200000;
    char tmp =  *pstr;
    strcpy(pstr , "hello");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值