分段故障处理 [英] Segmentation fault handling

查看:374
本文介绍了分段故障处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我用它来捕捉任何段错误或CTRL-C的应用程序。
使用低于code,我能够赶上分段错误,但处理程序被一再呼吁。我怎样才能阻止他们。
为了您的信息,我不想离开我的应用程序。我可以照顾释放所有损坏的缓冲区。

I have an application which I use to catch any segmentation fault or ctrl-c. Using the below code, I am able to catch the segmentation fault but the handler is being called again and again. How can I stop them. For your information, I don't want to exit my application. I just can take care to free all the corrupted buffers.

这可能吗?

void SignalInit(void )
{

struct sigaction sigIntHandler;

sigIntHandler.sa_handler = mysighandler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
sigaction(SIGSEGV, &sigIntHandler, NULL);

}

和处理程序是这样的。

void mysighandler()
{
MyfreeBuffers(); /*related to my applciation*/
}

下面的分段故障信号处理程序被调用多次和明显MyfreeBuffers()给了我错误的释放已经释放的内存。我只是想只有一次免费的,但还是不想退出应用程序。

Here for Segmentation fault signal, handler is being called multiple times and as obvious MyfreeBuffers() gives me errors for freeing already freed memory. I just want to free only once but still dont want to exit application.

请帮忙。

推荐答案

对于喜欢的东西 SIGSEGV 默认行为是终止您的过程但你已经安装的处理程序它,它会调用处理覆盖缺省行为。但指令段错误的问题,可能您的处理程序完成后重试,如果你有没有采取措施来解决第一​​个赛格故障,重试的指令将再次故障,它的推移和。

The default action for things like SIGSEGV is to terminate your process but as you've installed a handler for it, it'll call your handler overriding the default behavior. But the problem is segfaulting instruction may be retried after your handler finishes and if you haven't taken measures to fix the first seg fault, the retried instruction will again fault and it goes on and on.

因此​​,首先点,导致 SIGSEGV 并尝试修复它(你可以调用像回溯()在处理程序,看看自己哪里出了问题)

So first spot the instruction that resulted in SIGSEGV and try to fix it (you can call something like backtrace() in the handler and see for yourself what went wrong)

此外,POSIX标准说,

Also, the POSIX standard says that,

一个过程的行为从正常返回后是未定义
  一个信号捕获函数[XSI] SIGBUS,SIGFPE,SIGILL或
  不是由杀()产生SIGSEGV信号,[RTS] sigqueue()
  或提高()。

The behavior of a process is undefined after it returns normally from a signal-catching function for a [XSI] SIGBUS, SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(), [RTS] sigqueue(), or raise().

那么,做理想的事情是修复您的段错误摆在首位。 的处理程序段错误并不意味着绕过底层的错误状态

So, the ideal thing to do is to fix your segfault in the first place. Handler for segfault is not meant to bypass the underlying error condition

因此​​,最好的建议将是─的不要赶 SIGSEGV 的。让它转储核心。分析的核心。修复无效的内存引用,你去那里!

So the best suggestion would be- Don't catch the SIGSEGV. Let it dump core. Analyze the core. Fix the invalid memory reference and there you go!

这篇关于分段故障处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆