- Fortran
- XL Fortran [aix] (IBM p690 copper)
- Intel Fortran [linux]
(Xeon Cluster tungsten, SGI Altix cobalt, TG IA-64 cluster mercury, Intel-64 cluster Abe)
- References
- C
- XL C [aix] (IBM p690 copper)
- Intel C [linux]
(Xeon Cluster tungsten, SGI Altix cobalt, TG IA-64 cluster mercury, Intel-64 cluster Abe)
1. Fortran
The IBM XL Fortran compiler (IBM p690 copper) provides compilation flags that can
help debug floating point exceptions in your code. The -qflttrap
and -qsigtrap
flags can reveal the source line number near the place where a floating
point exception occurs.
The Intel compiler
(Xeon Cluster tungsten, SGI Altix cobalt, TG IA-64 cluster mercury, Intel-64 cluster Abe)
has similar capability when the -fpe0 -traceback options are used.
A couple of examples
are shown below with the commands used in bold type. Note the
obvious
division by zero at line 8 of the sample program and the way each
runtime
environment displays the error when floating point exceptions are
trapped.
1.1 XL Fortran [aix]
|
IBM XL Fortran example
Cu12:~/debug141% cat -n nan.f 1 program main 2 3 real xd 4 real yd 5 6 xd = 0.0 7 yd = 0.0 8 print *, xd/yd 9 end Cu12:~/debug142% xlf -g -qflttrap=zerodivide:invalid:enable -qsigtrap -o nan nan.f ** main === End of Compilation 1 === 1501-510 Compilation successful for file nan.f. Cu12:~/debug143% ./nan
Signal received: SIGTRAP - Trace trap Signal generated for floating-point exception: FP invalid operation
Instruction that generated the exception: fdiv fr01,fr01,fr02 Source Operand values: fr01 = 0.00000000000000e+00 fr02 = 0.00000000000000e+00
Traceback: Offset 0x000000a4 in procedure main, near line 8 in file nan.f --- End of call chain --- Cu12:~/debug144%
|
1.2 Intel Fortran [linux]
Intel Fortran
example [Linux]
[co-login1 ~/fortran]$ cat -n nan.f 1 program main 2 3 real xd 4 real yd 5 6 xd = 0.0 7 yd = 0.0 8 print *, xd/yd 9 end 10 [co-login1 ~/fortran]$ ifort -g -fpe0 -traceback -o nan nan.f [co-login1 ~/fortran]$ ./nan getRegFromUnwindContext: Can't get Gr0 from UnwindContext, using 0 forrtl: error (65): floating invalid Image PC Routine Line Source nan 4000000000002B41 MAIN__ 8 nan.f nan 4000000000002A10 Unknown Unknown Unknown libc.so.6.1 2000000000412970 Unknown Unknown Unknown nan 4000000000002840 Unknown Unknown Unknown Abort [co-login1 ~/fortran]$
|
2. C
C programmers in the Linux environment
(Xeon Cluster tungsten, SGI Altix cobalt, TG IA-64 cluster mercury, Intel-64 cluster Abe) with GNU or Intel compilers
can add a few lines to a routine and trigger floating point traps that
can
be caught when run within a debugger or cause the program to exit and
leave
a core file for later debugging. See the fenv man page for more
information
[type man fenv at a shell prompt].
An IBM XL C (IBM p690 copper) example with xlc and dbx is also shown in the table below.
These short examples show the additional source code lines in italic
and commands in bold.
2.1 XL C [aix]
|
IBM XL C example with
signal handler and dbx
cu11:~/c163% cat nan.c #include <stdio.h>
#include <fenv.h> #include <signal.h> #include <stdlib.h>
void fpehandler(int sig_num) { signal(SIGFPE, fpehandler); printf("SIGFPE: floating point exception occured, exiting.\n"); abort(); }
int main(void) { double x, y;
fenv_t myfenv;
fegetenv(&myfenv); myfenv.trapstate=1; fesetenv(&myfenv); fp_enable_all(); /* fp_enable_all documentation link here */
signal(SIGFPE, fpehandler);
x= 0.0; y= 0.0;
x= x/y;
printf("%lf\n",x); } cu11:~/c164% xlc -g -o nan nan.c -lm cu11:~/c165% ./nan SIGFPE: floating point exception occured, exiting. Abort (core dumped) cu11:~/c166% dbx nan core* Type 'help' for help. warning: The core file is not a fullcore. Some info may not be available. [using memory image in core] reading symbolic information ...
IOT/Abort trap in raise at 0xd03365bc 0xd03365bc (raise+0x40) 80410014 lwz r2,0x14(r1) (dbx) where raise(??) at 0xd03365bc abort() at 0xd0364454 fpehandler(sig_num = 8), line 11 in "nan.c" main(), line 30 in "nan.c" (dbx) list 25,35 25 signal(SIGFPE, fpehandler); 26 27 x= 0.0; 28 y= 0.0; 29 30 x= x/y; 31 32 printf("%lf\n",x); 33 } (dbx) quit
|
2.2 Intel C [linux]
|
Catching SIGFPE while running within a debugger
[Linux]
[co-login1 ~/c]$ cat nan.c #include <stdio.h>
#define _GNU_SOURCE #include <fenv.h>
int main(void) { double x, y;
int feenableexcept(); feenableexcept(FE_ALL_EXCEPT);
x= 0.0; y= 0.0;
x= x/y;
printf("%lf\n",x); } [co-login1 ~/c]$ icc -g -o nan nan.c -lm [co-login1 ~/c]$ gdb nan GNU gdb Red Hat Linux (6.1post-1.20040607.52rh) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "ia64-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run Starting program: /u/ncsa/arnoldg/c/nan
Program received signal SIGFPE, Arithmetic exception. 0x4000000000000981 in main () at nan.c:16 16 x= x/y; (gdb)
|
|
Catching SIGFPE with your own signal handler
&
debugging with resultant core file [Linux]
[tune ~/c]$ cat nan.c #include <stdio.h>
#define _GNU_SOURCE #include <fenv.h> #include <signal.h> #include <stdlib.h>
void fpehandler(int sig_num) { signal(SIGFPE, fpehandler); printf("SIGFPE: floating point exception occured, exiting.\n"); abort(); }
int main(void) { double x, y;
int feenableexcept(); feenableexcept(FE_ALL_EXCEPT); signal(SIGFPE, fpehandler);
x= 0.0; y= 0.0;
x= x/y;
printf("%lf\n",x); } [tune ~/c]$ icc -g -o nan nan.c -lm [tune ~/c]$ ./nan SIGFPE: floating point exception occured, exiting. Abort (core dumped) [tune ~/c]$ gdb nan core* GNU gdb Red Hat Linux (5.3post-0.20021129.18rh) Copyright 2003 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"... Core was generated by `./nan'. Program terminated with signal 6, Aborted. Reading symbols from /usr/local/intel/9.0.026/lib/libimf.so...done. Loaded symbols for /usr/local/intel/9.0.026/lib/libimf.so Reading symbols from /lib/i686/libm.so.6...done. Loaded symbols for /lib/i686/libm.so.6 Reading symbols from /lib/libgcc_s.so.1...done. Loaded symbols for /lib/libgcc_s.so.1 Reading symbols from /lib/i686/libc.so.6...done. Loaded symbols for /lib/i686/libc.so.6 Reading symbols from /lib/libdl.so.2...done. Loaded symbols for /lib/libdl.so.2 Reading symbols from /lib/ld-linux.so.2...done. Loaded symbols for /lib/ld-linux.so.2 #0 0xafcab6a1 in kill () from /lib/i686/libc.so.6 (gdb) where #0 0xafcab6a1 in kill () from /lib/i686/libc.so.6 #1 0xafcab435 in raise () from /lib/i686/libc.so.6 #2 0xafcac97b in abort () from /lib/i686/libc.so.6 #3 0x08048530 in fpehandler (sig_num=8) at nan.c:12 #4 <signal handler called> #5 0x08048581 in main () at nan.c:26 #6 0xafc98a07 in __libc_start_main () from /lib/i686/libc.so.6 (gdb) list 20,30 20 feenableexcept(FE_ALL_EXCEPT); 21 signal(SIGFPE, fpehandler); 22 23 x= 0.0; 24 y= 0.0; 25 26 x= x/y; 27 28 printf("%lf\n",x); 29 } (gdb)
|
|