linux 为什么 没有生成core文件? 之二

来源:互联网 发布:天天向上网络 编辑:程序博客网 时间:2024/06/02 00:49
前文转了两篇简单的linux下进程crash后生成core文件的文章,包括信号、core机制的简述、没有生成core文件原因的排查,以及利用core文件调试定位crash point。
但我遇到的问题在前面的文章中还是无解。后来测试后发现,是因为代码中安装了SIGSEGV信号处理函数,在信号处理函数中打印堆栈信息。所以这时进程收到SIGSEGV后,系统不再自动生成core文件。所以问题变成:手动处理SIGSEGV信号后,怎么样让系统还生成core文件?或者:
How to handle SICSEGV, but also generate a core dump?

网上查了会,收到3种相关解决方案。
一:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
 
void sighandler(int signum)
{
    printf("Process %d got signal %d\n", getpid(), signum);
    signal(signum, SIG_DFL);
    kill(getpid(), signum);
}

int main()
{
    signal(SIGSEGV, sighandler);
    printf("Process %d waits for someone to send it SIGSEGV\n",
        getpid());
    sleep(1000);

    return 0;
}

二:
void segfaulthandler(int parameter){    char gcore[50];    sprintf(gcore, "gcore -s -c \"core\" %u", getpid());    system(gcore);    exit(1);}
 
int main(void){ signal(SIGSEGV, segfaulthandler);}
三:

/*

Handler functions that terminate the program are typically used to causeorderly cleanup or recovery from program error signals and interactiveinterrupts.

The cleanest way for a handler to terminate the process is to raise thesame signal that ran the handler in the first place. Here is how to dothis

*/

volatile sig_atomic_t fatal_error_in_progress = 0;voidfatal_error_signal (int sig){  /* Since this handler is established for more than one kind of signal,      it might still get invoked recursively by delivery of some other kind     of signal.  Use a static variable to keep track of that. */  if (fatal_error_in_progress)    raise (sig);  fatal_error_in_progress = 1;  /* Now do the clean up actions:     - reset terminal modes     - kill child processes     - remove lock files */  ...  /* Now reraise the signal.  Since the signal is blocked,     it will receive its default handling, which is     to terminate the process.  We could just call     exit or abort, but reraising the signal     sets the return status from the process correctly. */  raise (sig);}

经过验证,发现前两种方法能生成core,但是core文件的堆栈信息不对,已经不是crash时的堆栈信息。第三种方法似乎没有用...
由于时间紧,究竟什么原因我还没有仔细分析,先留个记录吧
知道原因的朋友请留言,谢谢!!

0 0
原创粉丝点击