1#include <sys/types.h> 2#include <sys/signal.h> 3#include <signal.h> 4#include <stdio.h> 5#include <unistd.h> 6#include <string.h> 7 8#define FAULTADDR 0x123123 9 10static void 11handler(int sig, siginfo_t *sip, void *scp) 12{ 13 char buf[1024]; 14 15 if (sip == NULL) 16 _exit(1); 17 if (sip->si_addr == 0) /* wrong address */ 18 _exit(1); 19 20 // snprintf(buf, sizeof buf, "addr %p\n", sip->si_addr); 21 // write(STDOUT_FILENO, buf, strlen(buf)); 22 _exit(0); 23} 24 25 26int 27main(int argc, char *argv[]) 28{ 29 struct sigaction sa; 30 31 memset(&sa, 0, sizeof sa); 32 sigfillset(&sa.sa_mask); 33 sa.sa_sigaction = handler; 34 sa.sa_flags = SA_SIGINFO; 35 36 sigaction(SIGSEGV, &sa, NULL); 37 sigaction(SIGBUS, &sa, NULL); 38 39 *(char *)FAULTADDR = 0; 40} 41