简体   繁体   English

崩溃期间未捕获到SIGSEGV信号

[英]SIGSEGV signal not caught during crash

I am trying to run a code that will generate a segmentation fault. 我正在尝试运行将生成细分错误的代码。 But the segmentation fault is not generated. 但是不会产生分段错误。

int main()
{

char *variable;

strcpy(variable,"hello");

}

The program crashes but the message saying that a segmentation violation has occurred is not getting displayed . 程序崩溃,但是不会显示表示已经发生分段违反的消息。

Even when I write a signal handler to handle the segmentation fault , the signal is not getting caught. 即使我编写信号处理程序来处理分段错误,信号也不会被捕获。

Do you know why this is occurring ? 你知道为什么会这样吗? Is there any chance that the handling of the SIGSEGV signal by the kernel would have been disabled or something... 是否有可能内核已禁用了SIGSEGV信号的处理或发生了什么...

Thanks, 谢谢,

In my case, the segmentation fault has come using gcc compiler(Output : Segmentation fault). 在我的情况下,使用gcc编译器来产生分段错误(输出:分段错误)。 But if in your case its not coming , try running your binary using strace like strace ./a.out(say). 但是,如果您的情况还没有到,请尝试使用像strace ./a.out(say)这样的strace运行二进制文件。 It will show the the system level command executed on the console. 它将显示在控制台上执行的系统级别命令。 May be u will get an idea from this 也许你会从中得到一个主意

First, you really should get the habit of compiling with gcc -Wall which warns against such mistakes. 首先,您确实应该养成使用gcc -Wall进行编译的习惯,它会警告此类错误。

And to understand what really is happening, use a debugger like gdb to run your code step by step. 要了解实际情况,请使用gdb类的调试器逐步运行代码。 You might also use strace or better ltrace (which would show the argument to strcpy ). 您可能还会使用strace或更好的ltrace (这将显示strcpy的参数)。

Then, a possible explanation (just a guess) might be the following: 然后,可能的解释(仅是猜测)如下:

  • your variable is uninitialized 您的variable未初始化

  • so it keeps the "value" that the register (or stack slot) storing that variable had previously 因此它保留了存储该variable的寄存器(或堆栈插槽)先前具有的“值”

  • main is called by crt0.o which happens to initialize that location with something meaningful (a valid pointer), perhaps argv[0] of main or some shell environment variable main由被称为crt0.o这恰好初始化一些有意义的(有效的指针),该位置,或许argv[0]main或一些shell环境变量

(You could read more about x86-64 ABI to understand how crt0.o sets the stack) (您可以阅读有关x86-64 ABI的更多信息,以了解crt0.o如何设置堆栈)

If you initialize variable=NULL; 如果初始化variable=NULL; you'll get a SIGSEGV 你会得到一个SIGSEGV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM