创建崩溃日志
当我想查找段错误或任何其他导致程序崩溃的错误时,我总是会使用 gdb 检查核心转储。当这样的应用程序在没有安装 gdb 的计算机上运行时,这是相当痛苦的。
几天前,我使用一个程序(JDownloader)编写了一个崩溃日志文件,该文件包含一个堆栈跟踪。我认为这将对我的应用程序有很大的增强。但我还没有找到任何有关如何编写包含崩溃之前的堆栈跟踪的文件的信息。
有可能吗?我该如何在 Linux/Windows 上执行此操作? 我正在使用 C/C++。
When I want to find a segfault or any other error which leads to a crash of a program, i would always inspect the core dump with gdb. This is quite painful when such an application runs on a computer without gdb installed.
So a few days ago I used a program (JDownloader) wich wrote a crash log file, and this file contained a stack trace. I thought this would be a great enhancement to my application. But I haven't found any information on how to write a file which contains the stacktrace just before the crash.
Is it even possible? How would I do this on Linux/Windows?
I'm using C/C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 JDownloader 是用 Java 编写的。我认为该语言允许您在任何时候检索完整的纯文本堆栈跟踪。 C++ 无法做到这一点,因为编译后的可执行文件通常不保留有关用于生成它的代码的任何信息。
Windows API 确实允许您捕获致命异常并创建进程的转储(或进程的一部分,如果您不想处理大文件)。然后可以使用 Windbg、Visual Studio 或您选择的调试器检查此转储。
这样做的缺点是您必须拥有用于构建转储的可执行文件的确切源代码,以及构建期间生成的符号数据库(PDB 文件)。最重要的是,某些代码可以通过某种方式进行优化,从而使调试器无法为您提供准确的堆栈跟踪,即使使用符号数据也是如此。
请参阅 MiniDumpWriteDump细节。如果您打算采用此路线,最佳实践是不在崩溃进程中生成转储,而是生成一个子进程来获取父进程的转储。
还有一些 C 和 C++ 库可以“手动”记录调用堆栈并在运行时为您提供它的文本表示,但我还没有遇到我建议的任何这些。
I believe JDownloader is written in Java. I think the language allows you to retrieve a full plain text stack trace at any point. C++ is unable to do this, because the compiled executable usually doesn't keep any information about the code used to generate it.
Windows API does allows you to catch fatal exceptions and create a dump of the process (or parts of the process, if you don't want to deal with a huge file). This dump can then be inspected with windbg, Visual Studio, or your debugger of choice.
The downside to this is that you must have the exact source code that was used to build the dumped executable, as well as the symbol database (PDB file) that was generated during the build. On top of that, some code can be optimized in ways that makes it impossible for the debugger to give you an accurate stack trace, even with the symbol data.
See MiniDumpWriteDump for details. If you're going to take this route, the best practice is to not generate the dump in the crashing process, but spawn a child process to take a dump of the parent.
There are also C and C++ libraries that can 'manually' record the call stack and give you a textual representation of it at run time, but I haven't encountered any of these that I would suggest.