14
votes

I'm debugging a larger numerical program that I have added on to. It is written in fortran90, compiled with gfortran (the latest version available for Mac) and I am debugging it using gdb (again the latest version available for Mac).

My additions have a bug somewhere and I am trying to locate it, which is clear as running the program does not produce the expected result. When I run it in gdb, I get the following output at the end:

Note: The following floating-point exceptions are signalling: IEEE_INVALID_FLAG IEEE_DIVIDE_BY_ZERO IEEE_UNDERFLOW_FLAG IEEE_DENORMAL [Inferior 1 (process 83843) exited normally]

I would like to identify exactly where this FPE occurs, but it seems that a floating point exception does not cause the program to crash. I tested this by explicitly dividing by 0 in my code - it did not cause the program to stop running, but led to unexpected behavior.

What is the proper flag for either gdb or gfortran to ensure that the program stops running (ideally with a backtrace) when it reaches a floating point exception? I tried following the instructions here but it did not appear to change anything.

1
There are compiler flags mentioned in stackoverflow.com/questions/10210759/… which may help. However, would you please be able to mention version numbers in the question? "Latest" will be of less use to readers in the future.francescalus

1 Answers

15
votes

Probably you need to add these flags when compiling your code:

gfortran -g -fbacktrace -ffpe-trap=zero,overflow,underflow youcode.f90 -o run.exe



Explanation for compiler flags from gfortran manual:

-g       

to include debug data

-fbacktrace

Specify that, when a runtime error is encountered or a deadly signal is emitted (segmentation fault, illegal instruction, bus error or floating-point exception), the Fortran runtime library should output a backtrace of the error. This option only has influence for compilation of the Fortran main program.

-ffpe-trap=list

Specify a list of IEEE exceptions when a Floating Point Exception (FPE) should be raised. On most systems, this will result in a SIGFPE signal being sent and the program being interrupted, producing a core file useful for debugging. list is a (possibly empty) comma-separated list of the following IEEE exceptions: invalid (invalid floating point operation, such as SQRT(-1.0)), zero (division by zero), overflow (overflow in a floating point operation), underflow (underflow in a floating point operation), precision (loss of precision during operation) and denormal (operation produced a denormal value). Some of the routines in the Fortran runtime library, like ‘CPU_TIME’, are likely to to trigger floating point exceptions when ffpe-trap=precision is used. For this reason, the use of ffpe-trap=precision is not recommended.

Take a look at these two places for more info:

https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gfortran.pdf http://faculty.washington.edu/rjl/uwamath583s11/sphinx/notes/html/gfortran_flags.html