Linux:处理分段错误并获得核心转储

时间:2020-12-02 22:27:25

When my application crashes with a segmentation fault I'd like to get a core dump from the system. I do that by configuring before hand

当我的应用程序崩溃时出现分段错误,我想从系统中获取核心转储。我通过手工配置来做到这一点

ulimit -c unlimited

I would also like to have an indication in my application logs that a segmentation fault has occured. I do that by using sigaction(). If I do that however, the signal does not reach its default handling and a core dump is not saved.

我还想在我的应用程序日志中指出已发生分段错误。我通过使用sigaction()来做到这一点。但是,如果我这样做,则信号不会达到其默认处理,并且不会保存核心转储。

How can I have both the system core dump an a log line from my own signal handler at the same time?

如何让系统核心同时从我自己的信号处理程序转储日志行?

2 个解决方案

#1


5  

  1. Overwrite the default signal handler for SIGSEGV to call your custom logging function.
  2. 覆盖SIGSEGV的默认信号处理程序以调用自定义日志记录功能。

  3. After it is logged, restore and trigger the default handler that will create the core dump.
  4. 记录后,恢复并触发将创建核心转储的默认处理程序。

Here is a sample program using signal:

这是一个使用信号的示例程序:

void sighandler(int signum)
{
  myLoggingFunction();

  // this is the trick: it will trigger the core dump
  signal(signum, SIG_DFL);
  kill(getpid(), signum);
}

int main()
{
   signal(SIGSEGV, sighandler);

   // ...
}

The same idea should also work with sigaction.

同样的想法也应该与sigaction一起使用。

Source: How to handle SIGSEGV, but also generate a core dump

来源:如何处理SIGSEGV,还生成核心转储

#2


1  

The answer: set the sigaction with flag SA_RESETHAND and just return from the handler. The same instruction occurs again, causing a segmentation fault again and invoking the default handler.

答案:使用标志SA_RESETHAND设置sigaction并从处理程序返回。再次出现相同的指令,再次导致分段错误并调用默认处理程序。

#1


5  

  1. Overwrite the default signal handler for SIGSEGV to call your custom logging function.
  2. 覆盖SIGSEGV的默认信号处理程序以调用自定义日志记录功能。

  3. After it is logged, restore and trigger the default handler that will create the core dump.
  4. 记录后,恢复并触发将创建核心转储的默认处理程序。

Here is a sample program using signal:

这是一个使用信号的示例程序:

void sighandler(int signum)
{
  myLoggingFunction();

  // this is the trick: it will trigger the core dump
  signal(signum, SIG_DFL);
  kill(getpid(), signum);
}

int main()
{
   signal(SIGSEGV, sighandler);

   // ...
}

The same idea should also work with sigaction.

同样的想法也应该与sigaction一起使用。

Source: How to handle SIGSEGV, but also generate a core dump

来源:如何处理SIGSEGV,还生成核心转储

#2


1  

The answer: set the sigaction with flag SA_RESETHAND and just return from the handler. The same instruction occurs again, causing a segmentation fault again and invoking the default handler.

答案:使用标志SA_RESETHAND设置sigaction并从处理程序返回。再次出现相同的指令,再次导致分段错误并调用默认处理程序。