How can I get some information about crashed program in c++ for future debugging

I would like to know is there any way to capture some information when a program crashes in c++. This information can be used for future debugging by developers. For example, this information provides that my program crashes in which line of code and in which function or preferably why.

Upvotes: 2

Views: 1875

Answers (4)

google::InstallFailureSignalHandler(); can provide us the information. It also automatically prints call stack. It is very easy to use. For more information please see http://rpg.ifi.uzh.ch/docs/glog.html

Upvotes: 0

uri zilberberg
uri zilberberg

Reputation: 1

Without detracting from what has been said above, i think that you need to build a signal handler which can perform a backtrace to the place in the code where it crushed. something like that

void signalHandler(int signal)
{
    void *array[500];
    size_t size;

    // get void*'s for all entries on the stack
    size = backtrace(array, 500);
    // print out all the frames to stderr
    
    fprintf(stderr, "Error: signal %d:\n", signal);
    backtrace_symbols_fd(array, size, STDERR_FILENO);
    exit(1);
}


void registerSignalHandler()
{
    signal(SIGSEGV, signalHandler);
}

int main(int argc, char* argv[])
{

    registerSignalHandler();
    
   // your code 
   return 1;
}

Upvotes: 0

How can I get some information about crashed program in c++ for future debugging

This is implementation specific.

(In practice, both compiler specific and operating system specific; I am taking a Linux centered point of view)

C++11 is a programming language, defined by a specification written in English (n3337). It is not a software.

You usually use some compiler to compile your C++ code. I recommend using a recent version of GCC or of Clang. If you use it, compile your C++ code with all warnings and debug info, that is with g++ -Wall -Wextra -g or clang++ -Wall -Wextra -g at least. Some compilers are capable of optimization while emitting debug information (e.g. you could run g++ -Wall -Wextra -g -Og, as we do in RefPerSys). You could sometimes also generate C++ code with tools like ANTLR or GNU bison -or even some other C++ program- (and such an approach is called metaprogramming).

Once you compiled your C++ code with DWARF debug information (perhaps also using a build automation tool like GNU make), you could (on Linux at least) use the GDB debugger. Since gdb(1) can deal with a core(5) dump (see signal(7)).

Of course, if you are coding an operating system kernel in C++ (like this one), things are different.

Upvotes: 3

Thomas Weller
Thomas Weller

Reputation: 59633

The concept is called a crash dump on Windows or core dump on Linux. It's not a feature of C++ but of the operating system. It works well for code that was compiled to native assembler instructions (which is often the case for C++).

The state of the crashed program at the time of the crash will be saved, so you can have a look at call stacks, memory, registers etc. The amout of information can be configured. The analysis is then done with the help of a debugger, e.g. on Windows.

For line numbers, be aware to build symbols along with your binaries. For the analysis you need to bring the crash dump together with the correct symbols to get the line numbers.

Upvotes: 6

Related Questions