For some general tips on writing and debugging programs in any language, see Debugging and debuggers.
Adding print statements to a program is a tried and true method of debugging, and the only method that many programmers use. Not because it’s the best method, but it’s sometimes the simplest way to examine what’s going on at a particular point in a program.
Print statements can be added almost anywhere in a Fortran code to print things out to the terminal window as it goes along.
You might want to put some special symbols in debugging statements to flag them as such, which makes it easier to see what output is your debug output, and also makes it easier to find them again later to remove from the code, e.g. you might use “+++” or “DEBUG”.
There are a number of flags you can use when compiling your code that will make it easier to debug.
Here’s a generic set of options you might try:
$ gfortran -g -W -Wall -fbounds-check -pedantic-errors \
-ffpe-trap=zero,invalid,overflow,underflow program.f90
See Useful gfortran flags for more information. Most of these options indicate that the program should give warnings or die if certain bad things happen.
Compiling with the -g flag indicates that information should be generated and saved during compilation that can be used to help debug the code using a debugger such as gdb or totalview. You generally have to compile with this option to use a debugger.
This is the Gnu open source debugger for Gnu compilers such as gfortran. Unfortunately it works very poorly for Fortran.
Totalview is a commercial debugger that works quite well on Fortran codes together with various compilers, including gfortran. It also works with other languages, and for parallel computing.