Mohamed Iqzas
Mohamed Iqzas

Reputation: 1046

get stacktrace during an application crash - c/c++

I am a java developer trying my hands on C-C++ code. Basically I have some build script that uses Visual Studio components to build the application libraries - dlls. We do not use Visual Studio IDE to debug.

But whenever we face some crash in our application, we have to enter the debug statements line by line and need to check the exact line of code in which it is crashing.

Is there any API in C/C++ which would write the stack trace into a file during the crash of our program?

Some kind of event listener that would be called during a program exit and that could print the stack trace and error information into a log file.

I have seen many questions related to this but I am not able to get how to handle this in code rather than debugging tools. I feel Java is very much advanced with respect to error handling.

Thanks in advance!

Upvotes: 4

Views: 5723

Answers (3)

Tamás Szelei
Tamás Szelei

Reputation: 23921

The Standard does not give you any facilites for this. That said, you can use OS-specific APIs to get what you want. On windows, the easiest is probably to use StackWalker. You do need to take care of SEH oddities yourself. I suggest this article for more information.

On POSIX platforms, you can use the backtrace function from glibc.

In general, both require your program to be compiled with debug information. It is also possible to create crash dumps without symbol names (see Minidumps on Windows), but you will need to decode them (which Visual Studio does for you for example, but you mentioned in your post that you don't use it).

Also, keep in mind that writing crash dumps from a crashing process is very error-prone (since you really have no idea what state your application is at that time). You might get more reliable results if you write the crash dumps from another process. Earlier I've put together a simple example out-of-process crash handler that demonstrates how to implement that.

Upvotes: 5

Jesper Juhl
Jesper Juhl

Reputation: 31447

You can use std::set_terminate to define a function to be called when an exception leaves main() additionally you can install a signal handler for SIGSEGV to catch segmentation faults.

You can get a stack trace (on Linux) using libunwind ( http://www.nongnu.org/libunwind/ ) or use the backtrace() function from libc.

One thing I sometimes do in my own exception classes is to grab and store a stacktrace in the constructor that I can then obtain where I catch the exception and print the trace of where it was thrown from.

Upvotes: 2

Natos
Natos

Reputation: 13

You could use <errno.h> in C to handle your error and log them into a log file. The standard flux for the errors is stderr.

One solution to get the error is to use the function perror included in <errno.h> that will display the exact error.

To use it, put it in an error catcher.

if (!fileIsOpen())
   perror("File not opened");

I hope it helped.

Upvotes: 0

Related Questions