[Dune] Debugging tricks
Sven Marnach
sven at pantoffel-wg.de
Tue Dec 11 13:20:56 CET 2007
Hi all,
though a bit off-topic, I'd like to share two solutions of problems I
stumbled on again and again while debugging, so there is a chance that
you sometimes have the same problems.
1. Floating point exceptions
By default, the FPU doesn't throw floating exceptions on errors.
When, for example, dividing by zero, the FPU just returns "nan" and
the program continues computing. That's usually not the kind of
behaviour a programmer of numerical software wants.
glibc users can enable floating point excetions by putting
#include <fenv.h>
//...
int main()
{
feenableexcept (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
//...
}
in the source code. Now, your program receives SIGFPE each time a
floating point exception of one of the specified types occurs. By
default, this will make the program abort and print an error
message. You could also install a signal handler using signal(2).
Note that the FPU runs asynchronously, so your program receives the
signal not exactly at the instruction that caused the exception (so
the backtrace in gdb might be corrupted).
You can also put the call to feenableexcept into the initialization
code of a library and link your program with that library, see
http://gcc.gnu.org/onlinedocs/gcc-3.3.6/g77/Floating_002dpoint-Exception-Handling.html
2. Printing STL vectors in GDB
...is quite inconvenient. It will get a whole lot easier if you
put the following lines in your ~/.gdbinit:
define pvec
p *&$arg0[0]@$arg0.size()
end
document pvec
pvec <vector>: print an STL vector in readable format
end
You can customise the display format by
set print array-indexes
set print array
Greetings from Heidelberg,
Sven
More information about the Dune
mailing list