如何在没有核心转储的情况下捕获段错误

时间:2018-04-28 15:10:03

标签: c segmentation-fault

我正在做一个程序,必须知道他的孩子在执行时是否有段错误。

用于具有核心转储且没有核心转储的段错误。对于第一个我做的没有问题,但我无法找到什么可能使我无法捕捉到这个信号。按照手册页我应该收到10,7,10,但我不确定它的意思,我只能尝试其中一个,但10和7都没有做任何事情

if (WTERMSIG(status) == 10)
    my_putstr("Segmentation fault \n");
else if (WTERMSIG(status) == 11)
    my_putstr("Segmentation fault (core dumped)\n");

2 个答案:

答案 0 :(得分:2)

您的问题有点不清楚,但您可以使用(?, ?, t3)检查子进程是否收到任何信号,如果WIFSIGNALED()为真意味着儿童被任何信号异常终止。

WIFSIGNALED(status)

观察孩子是否收到if(fork() == 0 ) { /*... */ /* send the exit status of child by calling exit()*/ } else { int status; wait(&status);/* here parent collect exit status , now parent wants to know--> normal termination or abnormal */ if(WIFEXITED(status)) { /* true if terminates normally */ printf("normally terminates\n",WEXITSTATUS(status)); } if(WIFSIGNALED(status)) { /* if child terminated bcz of any other signal */ printf("terminated by signal\n",WTERMSIG(status));// bcz of what signal } } 没有核心转储,在上面的代码中,一旦孩子收到SIGSEGV,您可以通过拨打SIGSEGV&做一些事情。

答案 1 :(得分:1)

  

if (WTERMSIG(status) == 10) my_putstr("Segmentation fault \n"); else if (WTERMSIG(status) == 11) my_putstr("Segmentation fault (core dumped)\n");

信号10通常为SIGBUS,信号11通常为SIGSEGV。他们的意思不同。打印"分段错误" 这两个令人困惑和错误。

此外,是否转储了core 正交收到了上述哪些信号:如果ulimit -c不是0,那么{{1目标是可写的,然后两个信号将生成core。此外,其他信号(例如core)默认也会生成SIGILL

这是你可能想要的:

core