catch sigsegv

0 Reference 

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

http://blog.csdn.net/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");
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章