How to debug programs in Fortran

In Fortran, debuggers are used to debug programs. These debuggers check the code line by line to search for errors in the programs.

They do so by examining variable values and data object values throughout execution.

Debuggers typically support the following functionalities while debugging program execution:

  • Set up breakpoints
  • Step through the source code
  • Set up watch points

This shot discusses two frequently used debuggers for Fortran.

GDB debugger

The GNU debugger comes with the operating system Linux. For Windows OS, it comes with a graphical user interface. It is called XXGDB in Windows.

The GNU debugger works for many programming languages. GDB is commonly used for debugging C and C++. It can also be used to debug Fortran, D, Go, and Ada.

Some of the commands in GDB are listed below.

Command

Purpose

break

to set up a breakpoint

run

to start execution

cont

to continue execution

next

to execute only the next line of code while preventing stepping into any function call

step

to execute the next line of code while stepping into function in case of a function call

Debugging Fortran programs with GDB

First, the g77 compile command should be modified to include the -g option.

To compile programs with debugging symbols included, run the following:

g77 -o myprogram -g myprogram.f

To set up a breakpoint:

break main__

Run the following to set up a breakpoint at a particular line number in a particular file:

break myprogram.f:mylinenumber

Here, myprogram.f and mylinenumber are the source file and line number where the breakpoints are set.

To clear breakpoints:

clear myprogram.f:mylinenumber

To step through the next line of code and skipping function calls:

next

To step through the next line of code and also step through function calls:

step

To examine variable values:

print myvariable

Here, myvariable is the variable to be examined.

To resume normal program execution:

continue

To exit GDB:

quit

DBX debugger

Another commonly used debugger is DBX. The DBX debugger is used for the operating system Linux.

Some of the commands in DBX are listed below.

Command

Purpose

stop[var]

to set up a breakpoint when value of var changes

stop in[proc]

to stop execution while entering a proc

stop at[line]

to set up a breakpoint at the specified line

run

begin execution of the program

cont

to continue execution

next

to execute only the next line of code while preventing stepping into any function call

step

to execute the next line of code while stepping into function in case of a function call

print <exp>

print the value of the expression

where

print currently active procedures

trace <line#>

trace execution of the linee

trace <proc>

trace calls to the procedure

trace <var>

trace changes to the variable

trace <exp> at <line#>

print <exp> when <line> is reached

status

print trace/stop's in effect

delete <number>

remove trace or stop of given number

screen

switch dbx to another virtual terminal

call <proc>

call a procedure in program

whatis <name>

print the declaration of the name

list <line>, <line>

list source lines

registers

display register set