/* my_libsignal.c */#include<stdio.h>#include<stdlib.h>#define __USE_GNU
#include<signal.h>#include<ucontext.h>#include<string.h>#include<sys/mman.h>#define STACK_SIZE (4096*8)
staticvoidsignal_handler(intsig,siginfo_t*sig_info,void*sig_data){ucontext_t*ctx=sig_data;size_t*gregs=(size_t*)ctx->uc_mcontext.gregs;if(sig=SIGSEGV){fprintf(stderr,"si_addr %p\n",sig_info->si_addr);fprintf(stderr,"[ucontext] %p\n",sig_data);fprintf(stderr,"[REG_RIP] %p\n",(void*)gregs[REG_RIP]);fflush(stderr);// exit(1);
}}__attribute__((constructor))voidset_signal(){stack_tss;ss.ss_sp=malloc(STACK_SIZE);ss.ss_size=STACK_SIZE;ss.ss_flags=0;if(sigaltstack(&ss,NULL)){fprintf(stderr,"Failed sigaltstack\n");exit(1);}structsigactionact;sigemptyset(&act.sa_mask);sigaddset(&act.sa_mask,SIGSEGV);act.sa_sigaction=signal_handler;act.sa_flags=SA_SIGINFO|SA_RESTART|SA_ONSTACK;if(sigaction(SIGSEGV,&act,NULL)==-1){/* error */fprintf(stderr,"Failed to set my signal handler.\n");exit(1);}printf("set_signal() is called\n");}