0

当我的 KDE 程序崩溃时(这很常见 :( ),我可以生成一个事后回溯(我想这不是真正事后的,只是错误被捕获并保存)并使用它来提交错误报告,如果我安装了调试符号。它是如何工作的,我如何将该功能添加到我自己的程序中?

我现在遇到了一个问题,我编写的程序(用 C++ 编写)间歇性地崩溃,显然是由于内存管理不善。运行这个程序对我来说是非常不切实际的,gdb因为它是一个需要几个小时才能运行的大规模模拟,并且只有在系统规模非常大时才会出现崩溃。能够自动将回溯转储到文件中将为我节省很多时间。

我认为它涉及将所有内容包装在某种try{}catch(){}例程中,但我如何从中获得有用的回溯?有没有更好的办法?我所有的编程都在 Linux 上,如果这会影响答案的话。

4

3 回答 3

3

You can't use try/catch, because those require a correct program, and the crashes you experience are because your program is ill-formed/incorrect/broken. You cannot in general use deterministic programming to work around non-deterministic broken code. Avoid writing broken code in the first place (use tools like Clang's asan/tsan/ubsan, and Valgrind, and write tests), or use a language that doesn't become ill-formed when you make programming errors (like Java or Python).

What normally happens is that when the operating system kills your process because of some illegal operation (e.g. illegal instructions or illegal memory access), it creates a core dump of the process just before it gets removed from the system. The core dump consists of the entire memory content (more or less), and it includes the stack traces of all the running threads.

Some contemporary OSs pipe the coredump to a program that handles it, e.g. by uploading it to a software vendor for analysis. You could do something similar, though you'll have to arrange that with your OS. It's probably enough just to send the stack traces of the running threads rather than the entire memory, though.

You can also create stack traces of a running program, either by using a library (libunwind?) or by attaching a debugger, which interrupts the program, but there's generally not much use for this - the only stack trace that is interesting is the one from where the illegal operation happened, because you want to know what that operation was.

于 2013-08-29T08:45:05.213 回答
0

根据您遇到的崩溃类型,您还可以在程序终止处理程序中执行某种日志记录操作。

那时有多少可用的调试信息很大程度上取决于您的编译器及其设置,当然不能保证这在您可能遇到的所有情况下都能正常工作,例如内存不足。

于 2013-08-29T09:11:26.753 回答
0

您可以集成google breakpad。或者你至少可以看看来源。AFAIK 它通过提供各种信号处理程序来创建转储。

于 2013-08-29T11:23:18.033 回答