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. Obviously gdb is capable of far more than what’s listed here, down to stack registers and assembly instructions, but this is written for the science/engineering programmer who more typically uses a high-level language.

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

Start GDB debug of Fortran

assuming you have Fortran compiled program myprog with arguments hello and 3:

gdb --args ./myprog hello 3

start the program (perhaps after setting breakpoints) with

r

Setting breakpoints

Breakpoints are where the debugger stops running until you type c.

Set breakpoints by functionName:lineNumber. So for function myfun (gdb will find where it is) you might type

b myfun:32

If you’re using modules, let’s suppose mymod:

b mymod::myfun:32

List all variables

list all variables as two separate steps–one for local variables, and another for the arguments to the function.

Variable shape (elements/dim) and size (bytes/element)

variable type, per-element size in bytes, and shape are available for each variable “var” by

    whatis var

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

would be for a real64 3-D array.

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

    whatis var%prop

List values/names of function local variables

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

info locals

List values/names of function arguments

This lists the names and values of all arguments to the function you’re currently in.

info args

For example, if you’re 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, etc. you will see array values printed as well.