Fortran gdb/gdb-ia Debugging

This is a “cheat sheet” for debugging Fortran code with gdb (and Intel’s fork gdb-ia that replaces idb), targeted to those accustomed to Matlab or Python code debugging. gdb is capable of far more than what’s listed here, down to stack registers and assembly instructions. This gdb introduction by example is written for the science/engineering programmer who more typically uses a high-level language.

Note: In these descriptions, “function” is synonymous for program, function, and subroutine.

Start GDB Fortran debugger

  1. assuming executable myprog with arguments hello and 3:
    gdb --args ./myprog hello 3
    
  2. Run program in gdb (perhaps after setting breakpoints) with
    r
    

Setting breakpoints

Breakpoints are where the debugger stops running until you type

c

Set breakpoints by functionName:lineNumber. Example: Set a breakpoint in function myfun on line 32

b myfun:32

For breakpoints in Fortran modules in this example line 32 of a module named mymod in function myfun:

b mymod::myfun:32

List all variables

List all scope variables as two separate steps.

  1. local variables
  2. arguments to the function.

Variable shape/size

variable type, size (bytes/element), and shape (elements/dim) are available for each variable “var” by

whatis var

Example: a Fortran iso_fortran_env real64 3-D array of size 220 x 23 x 83:

var = REAL(8) (220,23,83)

If “var” is a “derived type” (similar to “struct” in other languages), get the same information about each record (akin to “property”) of the derived type by

whatis var%prop

function local variables

Local variables are variables used only within the scope of the function–excluding arguments to the function.

info locals

function arguments

List the names and values of all arguments to the current function:

info args

Example: in integer function myfun(a,b) or subroutine mysub(a,b), upon info args you’d see perhaps

a = 1.5 b = 0.2

If a or b are arrays or structs, array values are printed as well.

Leave a Comment