Linux: handling a segmentation fault and getting a core dump

一世执手 提交于 2019-12-01 03:44:06
  1. Overwrite the default signal handler for SIGSEGV to call your custom logging function.
  2. After it is logged, restore and trigger the default handler that will create the core dump.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!