????1. ?????????????????
????????????????????????o???1???????飬????????????????????????????????????λ?????Linux????core dump??????????????core????????????????????core????????????г?????????????core??????????????????????????????????????????????core??????????????64bit????????У????????????????????????????λ??????????????????????????????????????????????????????е??????б????????????core????????
?????????????????????????????????????Linux ?? core ??????????潲????core??????????????ЩLinux???????????
????2. ???????????
????????????????????????????????????????д??????????????????????????????????????????????????????????????????????????????????У???????????????????????????????????????????????????????????????????λ??
??????????????????????д???????????????????????????????????????
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <execinfo.h>
/* ??????????????????????? */
typedef struct sigInfo
{
int     signum;
char    signame[20];
} sigInfo;
/* ???????????????????????????о???6?? */
sigInfo sigCatch[] = {
{1?? "SIGHUP"}?? {2?? "SIGINT"}?? {3?? "SIGQUIT"}??
{6?? "SIGABRT"}?? {8?? "SIGFPE"}?? {11?? "SIGSEGV"}
};
/* ??????????????????? */
void blackbox_handler(int sig)
{
printf("Enter blackbox_handler: ");
printf("SIG name is %s?? SIG num is %d "?? strsignal(sig)?? sig);
// ?????????
printf("Stack information: ");
int j?? nptrs;
#define SIZE 100
void *buffer[100];
char **strings;
nptrs = backtrace(buffer?? SIZE);
printf("backtrace() returned %d addresses "?? nptrs);
strings = backtrace_symbols(buffer?? nptrs);
if (strings == NULL)
{
perror("backtrace_symbol");
exit(EXIT_FAILURE);
}
for(j = 0; j < nptrs; j++)
printf("%s "?? strings[j]);
free(strings);
_exit(EXIT_SUCCESS);
}
/* ??bug?????????ó?????????????Щ????? */
void bug_func()
{
int rand;
struct timeval tpstart;
pid_t  my_pid = getpid();
// ?????????
gettimeofday(&tpstart?? NULL);
srand(tpstart.tv_usec);
while ((rand = random()) > (sizeof(sigCatch)/sizeof(sigInfo)));
printf("rand=%d "?? rand);
//????????????
switch(rand % (sizeof(sigCatch)/sizeof(sigInfo)))
{
case 0:
{
// SIGHUP
kill(my_pid?? SIGHUP);
break;
}
case 1:
{
// SIGINT
kill(my_pid?? SIGINT);
break;
}
case 2:
{
// SIGQUIT
kill(my_pid?? SIGQUIT);
break;
}
case 3:
{
// SIGABRT
abort();
break;
}
case 4:
{
// SIGFPE
int a = 6 / 0;
break;
}
case 5:
{
// SIGSEGV
kill(my_pid?? SIGSEGV);
break;
}
default:
return;
}
}
int main()
{
int i?? j;
struct  sigaction   sa;
// ???????????????????
memset(&sa?? 0?? sizeof(sa));
sa.sa_handler = blackbox_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
for (i = 0; i < sizeof(sigCatch)/sizeof(sigInfo); i++)
{
// ????????????
if(sigaction(sigCatch[i].signum?? &sa?? NULL) < 0)
{
return EXIT_FAILURE;
}
}
bug_func();
while(1);
return EXIT_SUCCESS;
}
????2.1 ?????Щ?????
????????????????????sigInfo?????????????????????????????????????????????????????????????????????????????? kill –l ??????????????????????????????У???????????????????????Linux????API????strsignal???????????????亯????????£?
????#include <string.h>
????char *strsignal(int sig);
????????????????????sigCatch??????????????????????