在Linux中处理SIGSEGV?

编程入门 行业动态 更新时间:2024-10-18 08:28:24
本文介绍了在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?

本文发布于:2023-10-09 05:52:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Linux   SIGSEGV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!