捕获 C++ 中的 Segfault 或任何其他错误/异常/信号;就像Java中捕获异常一样

发布于 2024-11-07 08:05:13 字数 352 浏览 0 评论 0原文

我基于一个有缺陷的开源库编写了一个 Linux 程序。这个库有时会触发我无法控制的段错误。当然,一旦库出现段错误,整个程序就会崩溃。但是,即使库存在段错误,我也必须确保我的程序继续运行。这是因为我的程序有点充当“服务器”,它至少需要告诉客户端发生了一些不好的事情并从错误中恢复,而不是畏缩......有什么办法可以做到这一点吗?

据我了解,在 Java 中只需要捕获异常即可。但是 C++ 如何处理这个问题呢?

[更新]我知道 C++ 中也有异常处理,但 Segfault 不是异常,不是吗?我不认为发生段错误时会抛出任何东西。据我所知,你必须显式地“抛出”一些东西才能使用 try...catch...。

非常感谢,我对 C++ 很陌生。

I wrote a Linux program based on a buggy open source library. This library sometimes triggers segfaults that I cannot control. And of course once the library has segfaults, the entire program dies. However, I have to make sure my program keeps running even if the library has segfaults. This is because my program sort of serves as a "server" and it needs to at least tell the clients something bad happened and recover from the errors, rather than chicken out... Is there any way to do that?

I understand in Java one just needs to catch an exception. But how does C++ handle this?

[UPDATE]I understand there is also exception handling in C++, but Segfault is not an exception, is it? I don't think anything is thrown when segfault happens. You have to explicitly "throw" something to use try.... catch.... as far as I know.

Thanks so much, I am quite new to C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恍梦境° 2024-11-14 08:05:13

您不能 在分段违规后可靠地恢复执行。如果您的程序必须保持运行,请在单独的进程中隔离有问题的库,并通过管道与其进行通信。当发生分段违规时,您的程序将注意到关闭的管道。

You cannot reliably resume execution after a segmentation violation. If your program must remain running, fence off the offending library in a separate process and communicate with it over a pipe. When it takes a segmentation violation, your program will notice the closed pipe.

听你说爱我 2024-11-14 08:05:13

不幸的是,你不能让程序继续下去。导致 SIGSEGV 的错误代码通常会触发未定义的行为,例如取消引用空指针或读取垃圾内存。如果您的代码对无效数据进行操作,您将无法继续。

你可以处理信号,但你最多能做的就是转储堆栈跟踪并死掉。

C 和 C++ 本质上 不安全,无法处理由 undefined 触发的错误行为并让程序继续执行。

Unfortunately, you cannot make the program continue. The buggy code that resulted in SIGSEGV usually triggers undefined behaviour like dereferencing a null pointer or reading garbage memory. You cannot possibly continue if your code operates on invalid data.

You can handle the signal, but the most you can do is dump the stack trace and die.

C and C++ are inherently unsafe, you cannot handle errors triggered by undefined behaviour and let the program continue.

若能看破又如何 2024-11-14 08:05:13

您可以使用信号处理程序。但实际上并不推荐这样做,因为您不能保证已经消除了问题的原因。最好的办法是将其隔离在一个单独的进程中 - 这就是 Google Chrome 所采取的方法。

如果它是 FOSS,最简单的方法就是调试它。

You can use signal handlers. It's not really recommended though because you can't guarantee that you've eliminated the cause of the problem. The best thing to do would be to isolate it in a separate process- this is the approach Google Chrome takes.

If it's FOSS, the easiest thing to do would be to just debug it.

ぽ尐不点ル 2024-11-14 08:05:13

如果您可以访问源代码,那么在 GDB 之类的调试器中运行编程器可能会很有用。 GDB 在导致段错误的行处停止。

如果您确实想捕获信号,则需要使用 信号系统调用。不过,我可能会坚持使用调试器。

编辑:
既然您写了库段错误,我只想指出 编程的第一条规则:错总是你的。特别是如果您是 C++ 新手,则可能会发生段错误,因为您以某种方式错误地使用了该库。 C++ 是一种非常微妙的语言,很容易做一些你不想做的事情。

If you have access to the source, it might be useful to run the programmer in a debugger like GDB. GDB stops at the line which causes the segfault.

If you really want to catch the signal though, you need to hook up a signal handler, using the signal system call. I would probably just stick to the debugger though.

EDIT:
Since you write that the library segfaults, I would just like to point out the first rule of programming: It's always your fault. Especially if you are a new to C++, the segfault probably happens because you have used the library incorrectly in some way. C++ is a very subtle language and it is easy to do things you don't intend.

眼眸印温柔 2024-11-14 08:05:13

正如此处所述,您无法使用 try 块捕获段错误信号或将段违规“映射”到任何内容。自己处理 SIGSEGV 确实是个坏主意。 C++ 代码中的 SEGV 是一个严重错误。您可以使用 gdb 找出原因并解决它。

As mentioned over here you can’t catch segfault signals with try blocks or “map” segment violations to anything. It’s really bad idea to handle SIGSEGV yourself. SEGV from C++ code is a severe error. You can use gdb to figure it out why and solve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文