这个问题是针对 Linux 提出的。使用 GCC 编译器。
如果 SIGSEGV(我的意思是通常会导致 SIGSEGV 的违规行为)发生在旨在捕获 SIGSEGV 的信号处理程序中,可以预期会有什么行为?有助于讨论的代码示例:
/* In main or whatever */
{
struct sigaction sa = {}; /* initialised to all zero (I vote for GCC style breach of standard here) */
sa.sa_handler = DisasterSignals;
sa.sa_flags = SA_RESETHAND | SA_NODEFER; /* To have or have not */
sigaction(SIGSEGV, &sa, NULL);
}
static void DisasterSignals(int signal)
{
/* We cannot save the situation, the purpose of catching the signal is
only to do something clever to aid debugging before we go. */
/* Q: What if we segfault in here?? */
abort(); /* This should give us the expected core dump (if we survive to this point) */
}
想象一下,在点“Q”中,有一个违规的机器指令。
1) 没有 SA_RESETHAND | SA_NODEFER
:这似乎将系统置于逻辑陷阱中:在“Q”处,应该生成 SIGSEGV。但是 SIGSEGV 在信号处理程序中被阻止(默认 sigaction 行为)。死刑如何继续?会结冰吗?它会跳过有问题的指令吗(我猜不会)?
2) 使用 SA_RESETHAND | SA_NODEFER
:我猜在这种情况下,当重复 SIGSEGV 时,程序将以“正常”方式崩溃。
3) 只有 SA_NODEFER
:我猜在这种情况下,信号处理程序在重复 SIGSEGV 时被递归调用;如果 SIGSEGV 总是重复,我们会卡住直到堆栈溢出,然后呢。
最佳答案
默认情况下,在处理信号时它会被屏蔽,因此无法递归触发。如果屏蔽信号由程序执行触发(无效内存访问、段错误、除以 0 等),则行为未定义:
If SIGBUS, SIGFPE, SIGILL, or SIGSEGV are generated while they are blocked, the result is undefined, unless the signal was generated by kill(2), sigqueue(3), or raise(3).
在我的系统上,它会导致进程崩溃。
SA_NODEFER
没有屏蔽,因此可以递归处理信号,直到堆栈溢出。添加 SA_RESETHAND
将恢复默认操作(SIGSEGV 崩溃)。
我将您的示例改编为简单的测试程序,因此您可以验证此行为:
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
volatile char *ptr;
static void DisasterSignals(int signal)
{
/* We cannot save the situation, the purpose of catching the signal is
only to do something clever to aid debugging before we go. */
write(1, "11\n", 3);
*ptr = 1;
write(1, "13\n", 3);
abort(); /* This should give us the expected core dump (if we survive to this point) */
}
struct sigaction sa = {}; /* initialised to all zero (I vote for GCC style breach of standard here) */
int main()
{
sa.sa_handler = DisasterSignals;
sa.sa_flags = /*SA_RESETHAND | */SA_NODEFER; /* To have or have not */
sigaction(SIGSEGV, &sa, NULL);
write(1, "25\n", 3);
*ptr = 1;
}
关于c - 如果我捕捉到 SIGSEGV 并且信号处理程序导致另一个 SIGSEGV,会发生什么情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265851/