#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void InitDump(void);
void Dump_handle(int sig_no);
int main(int argc, char *argv[])
{
printf("pid: %d\n", getpid());
InitDump();
while(1);
return 0;
}
void InitDump(void)
{
signal(SIGSEGV, Dump_handle);
signal(SIGBUS, Dump_handle);
signal(SIGFPE, Dump_handle);
signal(SIGABRT, Dump_handle);
sigset(SIGTERM, Dump_handle);
sigset(SIGINT, Dump_handle);
}
void Dump_handle(int sig_no)
{
const static int BUF_SIZE = 30;
void *buffer[BUF_SIZE] = {0};
int pointer_num = backtrace(buffer, BUF_SIZE);
fprintf(stderr, "sig_no:%d, Obtained %d stack frames.\n", sig_no, pointer_num);
char** string_buffer = backtrace_symbols(buffer, pointer_num);
int fd = open("crash.log", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
backtrace_symbols_fd(buffer, pointer_num, fd);
(void)close(fd);
if(string_buffer == NULL){
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
printf("--------print backtrace begin--------\n");
for(int i = 0; i < pointer_num; i++){
fprintf(stderr, "%s\n", string_buffer[i]);
}
printf("--------print backtrace end----------\n");
free(string_buffer);
exit(0);
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75