13

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.

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

4

2 回答 2

11
  1. 覆盖默认信号处理程序SIGSEGV以调用您的自定义日志记录函数。
  2. 记录后,恢复并触发将创建核心转储的默认处理程序。

这是一个示例程序,使用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);

   // ...
}

同样的想法也应该适用于sigaction.

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

于 2013-10-11T00:48:52.453 回答
2

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

于 2013-05-23T15:19:09.630 回答