在 Linux 中处理 SIGSEGV?

编程入门 行业动态 更新时间:2024-10-18 06:14:02
本文介绍了在 Linux 中处理 SIGSEGV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在我的 Linux 应用程序中处理 SIGSEGV.原因是在生成核心转储之前必须进行一些清理(3 方库).更重要的是,清理必须在调用线程的上下文中执行,不能在信号处理程序中执行.所以我计划在信号处理程序中将控制权传递给调用线程,在清理完成后,然后使用 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.

真正的问题似乎是 signal_handler 无法将控制权传递给调用线程,无论我使用 post_sem 还是其他一些.有处理这个案子的想法吗?是否可以劫持 SIGSEGV,然后在 SIGSEGV 处理程序中返回另一个线程执行一些清理工作?

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?

信号(SIGSEGV,信号处理程序);

signal(SIGSEGV, 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,但您尝试进行清理的线程除外;这应该使操作系统将信号传递给该特定线程.我能想到的其他解决方案类似于 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:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Linux   SIGSEGV

发布评论

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

>www.elefans.com

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