Can I force a trap for a denormal exception?

I understand that the note generated for the IEEE_DENORMAL exception is not an error message, however I would still like to learn where in my program that exception is happening. I don’t think anything in my program should generate such an exception, and am concerned that it may represent a real error. It has in the past. I have tried adding

-ffpe-trap=zero,invalid,overflow,underflow,denormal

as an option to gfortran 11.5.0, but the event is not trapped. I still see

Note: The following floating-point exceptions are signalling: IEEE_INVALID_FLAGIEEE_DENORMAL

only when the run completes. Or does “trapped” not mean “end execution with traceback”? Playing around with example programs I find that 1/zero is +infinity, and does not abend, with or without the -ffpe-trap options. It seems I am missing something. Any guidance appreciated.


1 Like

I get an accurate backtrace for floating point errors with gfortran on my personal computer (Ubuntu Linux):

$ cat fpe.f90 
program fpe

implicit none

print *, 1.0/huge(1.0)

end program fpe
$ gfortran --version
GNU Fortran (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gfortran -Og -g -fbacktrace -fcheck=all -ffpe-trap=invalid,zero,overflow,underflow,denormal fpe.f90 
$ ./a.out 

Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x7a1e175f1e59 in ???
#1  0x7a1e175f0e75 in ???
#2  0x7a1e1731832f in ???
    at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
#3  0x7a1e17894f73 in ???
#4  0x7a1e178979a6 in ???
#5  0x7a1e17897e75 in ???
#6  0x7a1e178992c2 in ???
#7  0x5f6147f9e1f5 in fpe
    at /home/ben/ramdisk/fpe.f90:5
#8  0x5f6147f9e251 in main
    at /home/ben/ramdisk/fpe.f90:7
Floating point exception (core dumped)

Maybe some of the other compiler flags I use are needed?

Also: At work, I use Windows and the version of gfortran I have there doesn’t display backtraces. As I recall, it’s a bug in an upstream package that was fixed upstream but didn’t make it to the version of gfortran my work uses yet. I would use GDB at work to get the backtrace. GDB seems to work in this case:

$ gdb ./a.out 
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
(gdb) run
Starting program: /home/ben/ramdisk/a.out 

This GDB supports auto-downloading debuginfo from the following URLs:
  <https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) n
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGFPE, Arithmetic exception.
0x00007ffff7f51f73 in ?? ()
   from /lib/x86_64-linux-gnu/libgfortran.so.5
(gdb) bt
#0  0x00007ffff7f51f73 in ?? ()
   from /lib/x86_64-linux-gnu/libgfortran.so.5
#1  0x00007ffff7f549a7 in ?? ()
   from /lib/x86_64-linux-gnu/libgfortran.so.5
#2  0x00007ffff7f54e76 in ?? ()
   from /lib/x86_64-linux-gnu/libgfortran.so.5
#3  0x00007ffff7f562c3 in ?? ()
   from /lib/x86_64-linux-gnu/libgfortran.so.5
#4  0x00005555555551f6 in fpe () at fpe.f90:5
(gdb) exit
A debugging session is active.

    Inferior 1 [process 93393] will be killed.

Quit anyway? (y or n) y
1 Like