Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make GCC print helpful RUNTIME error messages?

Tags:

c++

gcc

g++

#defineing _GLIBCXX_DEBUG forces GCC to catch a large class of runtime errors in C++, such as out-of-bounds STL access, invalid iterators, etc.

Unfortunately, when the error happens, the message printed is not very helpful. I know how to print a backtrace with a function, and __FILE__ and __LINE__ with a macro myself.

Is there an easy way to convince GCC to do that, or to specify a function/macro for it to call when the kind of errors that _GLIBCXX_DEBUG catches actually occur?

like image 835
Oleg2718281828 Avatar asked Jul 25 '12 21:07

Oleg2718281828


2 Answers

I assume you mean you want messages that print the context of use in your code, rather than the filename and line number of some internal header file used by GCC.

There appears to be a single macro in .../debug/macros.h that all the checking code uses called _GLIBCXX_DEBUG_VERIFY. You could modify it to suit your needs.

Edit: Jonathan Wakely points out that all checks are fatal.

like image 137
jxh Avatar answered Oct 22 '22 06:10

jxh


When a Debug Mode check fails it calls abort(), so it dumps a core file which you can easily examine with a debugger to see where it failed. If you run the program in a debugger it will stop when it aborts and you can print the stack trace with backtrace.

To make that automatic you would need to change the call to abort() (in libstdc++-v3/src/c++11/debug.cc). I think you could change it to call std::terminate() and then install your own terminate_handler with set_terminate to make it print a backtrace.

like image 3
Jonathan Wakely Avatar answered Oct 22 '22 06:10

Jonathan Wakely