#define
ing _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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With