????2.2 sigaction????
??????main???????棬????????Щ????????????????????????????????????????????????????????????????????????????????????????????????????????????blackbox_handler???????????????Щ???????????????????????????????????????????????????????sigaction???????亯????????£?
????#include <signal.h>
????int sigaction(int signum?? const struct sigaction *act?? struct sigaction *oldact);
???????ú???????????????????????????
?????????????signum????????????????????????????????????????????????SIGKILL??SIGSTOP?????????????????????????????д?????????????????????????????????? SIGKILL and SIGSTOP cannot be caught?? blocked?? or ignored????
?????????????????????????struct sigaction????壬?y?????<signal.h>?ж??壬??????????????????????act???????????????????????????oldact??????????????????????????????????С????act???????????????????????????????????act????oldact????????????????????????
?????????????????????????壺
????struct sigaction {
????void     (*sa_handler)(int);
????void     (*sa_sigaction)(int?? siginfo_t *?? void *);
????sigset_t   sa_mask;
????int        sa_flags;
????void     (*sa_restorer)(void);   // ?ó???????????
????};
??????????????y??干??5???????
????sa_handler????????????????????????????????????????????SIG_IGN??????????????SIG_DEL??????????????????????
????sa_mask??????????????????????????????????????????????????????????С???????????????????????????????????????λ??????????????????????????????????Щ?????????????????????????????????????????????????????????????????????????????????????????????????????η??????????????????????????????????????
????sa_flags????????????????Щ????????????京????????£??? <signal.h>?ж??壩??
???????
????????
????SA_INTERRUPT ???????ж???????ò??????????
????SA_NOCLDSTOP ??signo??SIGCHLD?????????????????????????????????????????????????????????????μ?SA_NOCLDWAIT??????????????????????????????????????????XSI???????????SIGCHLD????
????SA_NOCLDWAIT ??signo??SIGCHLD???????y?????????????????????????????????y???????????wait??????y????????????????????????????????????-1??????errno?????ECHILD??
????SA_NODEFER ???????????????????????????????????????????????????????sa_mask?????????????
????SA_ONSTACK ????sigaltstack?????????滻???????????????滻????????
????SA_RESETHAND ???????????????????????????????????λ?SIG_DEF???????SA_SIGINFO???????????????????λSIGILL??SIGTRAP?????????????á??????????sigaction????????SA_NODEFER???????????????
????SA_RESTART ???????ж???????????????????
????SA_SIGINFO ?????????????????????????????????siginfo??????????????????????????????????
????sa_sigaction???????????????????????sa_flags????????SA_SIGINFO????????????????????????????????sa_sigaction??sa_handler??Σ???????????????洢??????????ó??????????????????????е????????????????·?????????????????
????void handler(int signo);
??????????????????SA_SIGINFO????????????·?????????????????
????void handler(int signo?? siginfo_t *info?? void *context);
??????????????????????????????????????????е????????????siginfo_t????????y???????????????????y?????????£?
struct siginfo_t
{
int     si_signo;       // signal number
int     si_errno;       // if nonzero?? errno value from <errno.h>
int     si_code;        // additional info (depends on signal)
pid_t   si_pid;         // sending process ID
uid_t   si_uid;         // sending process real user ID
void    *si_addr;       // address that cased the fault
int     si_status;      // exit value or signal number
long    si_band;        // band number for SIGPOLL
/* possibly other fileds also */
}
???????siginfo_t?????????si_signo??si_code???????????????context?????????????????????????????ucntext_t????????????????????????????????
????2.3 ????
??????????????????????????????????????λ????????????????????????????е??λ???????????????????????????????????????????????????????????POSIX.1???????????sigset_t???????????????????????????5?????????????????
????#include <signal.h>
????/* ???????????????0????????-1 */
????int sigemptyset(sigset_t *set);
????int sigfillset(sigset_t *set);
????int sigaddset(sigset_t *set?? int signum);
????int sigdelset(sigset_t *set?? int signum);
????/* ?淵??1???????0?????????-1 */
????int sigismember(const sigset_t *set?? int signum);
???????????????????????????????漲???????????????y??????????????????????????????????ж????λ????????????????????????????????????????????????????????????sigprocmask??????????????????????
????????sigemptyset???????set????????????????????е?????????sigfillset???????set??????????????????????????????ó???????????????????????????sigemptyset??sigfillset??Ρ????????C????????δ?????????????????????????0. ???????????????????????????????????????????????????????sigaddset???????????????м??У?sigdelset???????????????????
????2.4 kill&&raise&&abort????
????bug_func????????????????Щ???????????????????????????????????1???????????????????????????????????????α??????????????????????????Щ????2???????????kill??????abort???????????Щ?????亯????????£?
????#include <signal.h>
????int kill(pid_t pid?? int sig);
????int raise(int sig);
????#include <stdlib.h>
????void abort(void);
????kill??????????????????????顣kill??pid??????4???????????
????pid>0. ???????????????ID?pid??????
????pid==0. ??????????????????????????????????н??????Щ??????????ID????????????????ID?????????????????????Щ???????????????????????“???н???”??????????????????????????????UNIX????????????????????????init??pid????1???????
????pid<0. ??????????????????ID????pid??????????????????????????????????????????“???н????”???????Щ???????
????pid==-1. ???????????????????????????????????????????е????????????Щ???????
????raise?????????kill(getpid()?? signo).
????abort?????????????SIGABRT???????????????????????????????raise????????y????????????????abort??????y???????????????????o??????д???????
????2.5 backtrace&&backtrace_symbols????
??????????????????????????????backtrace??backtrace_symbols???????????????????????????????????????????????£?
????#include <execinfo.h>
????int backtrace(void **buffer?? int size);
????char **backtrace_symbols(void *const *buffer?? int size);
????void backtrace_symbols_fd(void *const *buffer?? int size?? int fd);
????backtrace?????????????????????????????buffer???????????У?size???buffer?п????????????????????????????????size??????????????????????????????????????????
???????backtrace???????????????????????????backtrace_symbols????????????????????????÷???????????????????????????檔????????????????????巵?????????????????backtrace_symbols???????maolloc????????????????????????????????backtrace_symbols_fd??з??????????backtrace_symbols???????????????????????????????????????檔
???????
???????backtrace???????????????????????? –rdynamic ??????磺 gcc –rdynamic blackbox.c –o blackbox ??
????backtrace_symbols????????????????16??????????????????????addr2line???????????????????????????????????? addr2line –e execute_file  addr ??????  addr2line –e ./a.out 0x400d62 ??
??????ú????????У??漰??????Linux????????????Щ???????????API????????????á???????ú???????????Щ???????????????????????????????????????????????
????3. Bug????
??????????У?????????????????????——???????????????????????????????????????β???????????bug????bug?????????????????????
#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>
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);
}
long count = 0;
void bad_iter()
{
int a?? b?? c?? d;
a = b = c = d = 1;
a = b + 3;
c = count + 4;
d = count + 5 * c;
count++;
printf("count:%ld "?? count);
bad_iter();
}
int main()
{
struct  sigaction   sa;
memset(&sa?? 0?? sizeof(sa));
sa.sa_handler = blackbox_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGSEGV?? &sa?? NULL) < 0)
{
return EXIT_FAILURE;
}
bad_iter();
while(1);
return EXIT_SUCCESS;
}
?????ó??????н?????£?
????... ...
????count:261856
????count:261857
????count:261858
????count:261859
????count:261860
????count:261861
????Segmentation fault (core dumped)
????allan@ubuntu:temp$
?????ó????????????????????????????????????ε??麯?????????????t???????????SIGSEGV?????????????????????????????????????????檔?????????????????????????????????????????????????????????????????£????????????????????????????
????????????????y?????????????????????????????????????????????“???滻????”??