在Linux中处理SIGSEGV? [英] handle SIGSEGV in Linux?

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

问题描述

我需要在我的Linux应用程序中处理SIGSEGV.原因是在生成core-dump之前必须进行一些清理(3-partry lib).而且,清理必须在调用线程的上下文中执行,而不能在信号处理程序中执行. 因此,我计划在信号处理程序中将控制权传递给调用线程,在清理完成后,然后使用raise(SIGSEGV)生成核心转储.

I need handle the SIGSEGV in my Linux app. The reason is some clean up(3-partry lib) must be done before generate core-dump. What is more, the clean up must be performed in the context of calling thread, cannot do in signal handler. So I plan in signal handler to pass the control to the calling thread, after the clean up finished, then use raise(SIGSEGV) to generate the core-dump.

真正的问题似乎是无论我使用post_sem还是其他方法,signal_handler都无法将控件传递给调用线程. 有解决这个问题的主意吗?可能会劫持SIGSEGV,然后在SIGSEGV中使用hander返回另一个线程来执行一些清理工作?

The real problem seems the signal_handler cannot pass the control to calling thread, no matter I use post_sem or some others. Any idea to handle this case? Possbile to hijack the SIGSEGV, then in SIGSEGV hander return to another thread to perform some clean up?

signal(SIGSEGV,signal_handler);

signal(SIGSEGV, signal_handler);

signal_handler() { ... post_sem(); ... }

signal_handler() { ... post_sem(); ... }

调用线程() { wait_sem(); 清理(​​); ... }

calling thread() { wait_sem(); clean_up(); ... }

推荐答案

您想在SIGSEGV之后进行清理(即严重错误)...我觉得这有点奇怪,因为1)如果您正在调试应用程序,则应该保留所有内容完整无损地存储在核心文件中,以便您可以准确地确定发生了什么; 2)如果您有一个针对客户的发布应用程序(比如说)……那么它应该不是SIGSEGV :)(无论如何都不是我的问题,只是说..)

You want to cleanup after a SIGSEGV (i.e. serious error)... I find this a little weird because, 1) if you were debugging the application you should leave everything intact to be stored in the core file so you can accurately identify what happened and 2) if you have a release application for a customer (let's say) well...it shouldn't SIGSEGV :) (not my problem anyway, just saying..)

关于主题

我认为您可以尝试在所有线程中阻止SIGSEGV,但您尝试进行清理的线程除外;这应该使os将信号传递到该特定线程.我能想到的其他解决方案是类似于setjmp()/longjmp()的东西(尽管没有测试过这些东西).

I think you could try to block SIGSEGV in all threads except the one in which you are trying to do the cleanup; this should make the os deliver the signal to that specific thread. Other solution I could think of is something along the lines of setjmp() / longjmp() (haven't tested any of these though).

请注意,一旦程序获得SEGV,您就处于不稳定状态(即清理也可能失败并生成另一个SEGV等),因此您应该考虑使内核崩溃.

Be careful that once your program got a SEGV, you're on shaky ground (i.e. your cleanup might fail as well and generate another SEGV etc etc) so you should consider just crashing with a core.

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

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