![]() ![]() Program received signal SIGFPE, Arithmetic exception.Ġx0000000000400887 in divide (d=0, e=1) at bugs.f90:24 If we simply continue with execution, GDB will stop the program when a floating-point error occurs: (gdb) cont By default, GDB will stop when it sees signals that indicate errors – in C code we often we see this when a SIGSEGV occurs due to a pointer-related bug. Now we’ve recompiled to trap on floating point exceptions we will receive a SIGFPE if a floating point error happens. We could just do that until the error occurs – but it would be nice to jump to the exact moment of the floating point error we’re expecting. This is just as we’d expect from our C / C++ debugging experience – GDB is using the debug information from gfortran to step through lines of source code, so we can see how the state is changing. Now we can step through each line of Fortran source code by pressing n: (gdb) n a.outīreakpoint 1 at 0x4008c4: file bugs.f90, line 8. The reason we need to use MAIN_ instead of main (as we would usually use with C) is that main actually runs some startup code to set up the enviroment. To stop execution on the first line of our program we use b MAIN_. To start debugging our program we invoke gdb PROGRAM. Just as for a C program, we start our Fortran executable under control of the debugger. Lets build our executable with these new options: $> gfortran -g -ffpe-trap=zero,invalid,overflow,underflow bugs.f90 Debugging in GDB The -ffpe-trap compilation flag enables exception traps for the various floating point errors we can catch these with GDB to find the exact cause of our bug. The gfortran debug options can help us here. The Infinity output suggests that we have an error in our floating point maths, most likely a divide by zero. As with other compilers from the gcc family, we need to supply the -g flag to generate debug information. To diagnose this fault, we’re going to run the program under GDB. If we compile and run this code, we see the output contains unexpected text – the Infinity printed on the first line: $> gfortran bugs.f90 ! Calculate and print ratios of successive integers. ! Initialise c with successive integer values. It’s supposed to calculate the ratios between successive integers but it contains a bug: program bugs Today we’ll see a simple example of how we can debug Fortran programs, using features provided by GDB and gfortran. Debug info formats like DWARF, along with some language-specific extensions, allow GDB to support most of the commonly-used compiled languages. We are used to using GDB for debugging C and C++ but it can also be used to debug other languages including Fortran, D, Go and Ada. ![]() Jonathan Laver (engineer-in-training) and Mark Williamson (senior software engineer at Undo) write: ![]()
0 Comments
Leave a Reply. |