plk
plk

Reputation: 1

Creating a Crash Log

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++.

Upvotes: 0

Views: 2720

Answers (1)

Collin Dauphinee
Collin Dauphinee

Reputation: 13973

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.

Upvotes: 2

Related Questions