An editor, gfortran and gdb

A Fortran program is a text file, a compiler and a binary, and the three of them form a loop: edit the file, build the binary, run the binary, read what it printed. This lesson sets that loop up with the tools on any machine with a compiler, adds the cheap check that runs before a build, and shows the debugger doing the one thing it does better than a print statement: stopping the program at a line and looking at the values there.

Edit, build, run

The editor matters less than the file name. Save Fortran under .f90 and the compiler reads it as free-form source; the lesson on source forms explains what the other extensions mean. An editor that colours Fortran adds two useful things: it distinguishes a string from a comment, and it keeps the indentation of the previous line, which is how Fortran code stays readable at a glance.

For a beginner, a plain terminal with vim, nano or emacs is enough, and a graphical editor with a Fortran mode is a convenience rather than a requirement. VS Code with a Fortran extension is the common choice on a desktop. What matters is that the editor writes UTF-8 without a byte order mark, because a stocky PROGRAM with three bytes in front of it is a compiler error at column one.

The loop itself is three commands. Build with the three flags from the previous lesson plus -g, which stores the line numbers for the debugger; run the binary; read the output.

gfortran -Wall -Wextra -std=f2018 -g hello.f90 -o hello
./hello

Hello, Fortran!

A shell alias shortens the typing without hiding the flags: alias gf="gfortran -Wall -Wextra -std=f2018 -g". Put it in ~/.bashrc and the whole course fits on one word. The pages here spell the command out in full, because a reader meets it in a build script without the alias.

Check without building

A syntax error costs one compiler start, and a compiler start costs a fraction of a second, so the loop above is already cheap. -fsyntax-only makes it cheaper: the compiler parses the file, reports every warning and error, and writes nothing.

The program below is the one this lesson debugs later. It has an unused declaration and a loop that reads one element past the end of its array, and the syntax check reports both without producing an object file.

gfortran -Wall -Wextra -std=f2018 -fsyntax-only stats.f90

stats.f90:7:27:

6 | do i = 1, 4
| 2
7 | total = total + values(i)
| 1
Warning: Array reference at (1) out of bounds (4 > 3) in loop beginning at (2)

That warning is worth reading as a sentence. The loop counter goes from 1 to 4, the array has three elements, and gfortran can see the mismatch because the bounds are constants. Change the loop to 1, size(values) and the warning disappears.

gdb in batch mode

gdb is the GNU debugger, and it is in the same package manager as the compiler. Interactive use is fine while learning, but the session below runs it in batch mode with -ex, one command per -ex, which is exactly how a CI job and the pages of this course use it.

The program to debug is the one below: it sums three values and divides by three, but the loop runs one iteration too long. Build it with the bounds check on, which turns the out-of-bounds read into a clean runtime error instead of a silent wrong answer.

program stats implicit none integer :: values(3), i, total values = [3, 4, 5] total = 0 do i = 1, 4 total = total + values(i) end do print *, "total:", total / 3 end program stats

gfortran -Wall -Wextra -std=f2018 -g -fcheck=bounds stats.f90 -o stats
./stats

At line 7 of file stats.f90
Fortran runtime error: Index '4' of dimension 1 of array 'values' above upper bound of 3

The run stops with exit status 2, and the two lines name the line of the source and the index that broke the bound. GFORTRAN_ERROR_BACKTRACE=0 turns off the memory addresses that the runtime would print next; they change on every run and say nothing a reader needs at this point.

The message says what went wrong and nothing about how the program got there. The debugger answers that. The session below breaks before line 7, prints the counter and the element the line reads, prints the call stack, and lets the program run to the next iteration.

gdb -batch -ex 'set disable-randomization off' \
    -ex 'break stats.f90:7' -ex run \
    -ex 'print i' -ex 'print values(i)' -ex bt \
    -ex continue -ex 'print i' ./stats

Breakpoint 1 at 0x124e: file stats.f90, line 7.

Breakpoint 1, stats () at stats.f90:7
7 total = total + values(i)
$1 = 1
$2 = 3
#0 stats () at stats.f90:7

Breakpoint 1, stats () at stats.f90:7
7 total = total + values(i)
$3 = 2

Read the session from the top. The breakpoint is set at the statement before it runs, so the first stop happens with i still at 1 and values(i) at 3. The $1 and $2 are the debugger's own names for the values it printed, and they can be used in later commands. bt prints the call stack, one frame here because everything happens in the main program. The second continue runs one iteration and stops at the same line with i at 2.

set disable-randomization off is there because a container usually forbids the system call that turns address randomisation off, and gdb prints a warning about it on every start without this line. On a desktop the line is unnecessary and harmless.

A runtime error and its line

-fcheck=bounds is one of a family of checks, and each one turns a class of silent corruption into a message:

Use them while developing and leave them out of the release build: each check is a branch in the inner loop, and the point of Fortran is the inner loop. A build with all four is the normal first run of a new program.

What this image does not have

Two tools a Fortran reader meets in articles are not installed in the container this course was built in: fprettify, which reformats source, and fortitude, which lints it. This course does not show their output, because there is none to show here; the lesson on style states the rules they enforce instead, and a reader who wants them installs them from their own package or a Python virtual environment.

Exercises

  1. Build the stats program with -fsyntax-only and then with a normal build. Which one writes stats and which one writes nothing?
  2. Set a breakpoint on the print line instead of line 7. What is total at that point, with the loop as it is?
  3. Fix the loop to 1, size(values) and run the program again. What does it print now?
  4. Add -fcheck=all to the fixed program and run it. Does the output change?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does -fsyntax-only produce?

    Answer

    Warnings and errors, and no output file

    The compiler parses and checks the file and stops before code generation.

  2. Which flag stores the source line numbers for gdb?

    Answer

    -g

    -g writes the debug information that maps the binary back to the source.

  3. What does -fcheck=bounds do?

    Answer

    It checks every array subscript against the declared bounds at run time

    The check is a runtime test on each subscript, and it reports the failing line.

  4. What is the exit status of a run that stops on a Fortran runtime error?

    Answer

    2

    The Fortran runtime terminates with status 2 after printing the error.

  5. What does break stats.f90:7 stop before?

    Answer

    The statement on line 7, before it executes

    A breakpoint stops before the statement runs, so its operands are unchanged at the first stop.

  6. Why is set disable-randomization off needed in the container?

    Answer

    The container forbids turning address randomisation off, and gdb warns about it

    gdb prints a warning when the personality call is refused; the command silences it.

  7. What does this program print? program p implicit none integer :: a(3), i, s a = [1, 2, 3] s = 0 do i = 1, 3 s = s + a(i) end do print *, s end program p

    Answer

    6

    The loop reads the three elements once, and the sum is 6.

  8. What does the compiler do with a loop that reads one element past a fixed-size array?

    Answer

    It warns that the reference is out of bounds

    With constant bounds gfortran sees the mismatch and warns; the build still succeeds.

  9. Why leave -fcheck=bounds out of a release build?

    Answer

    Every check is a test in the inner loop, and Fortran is chosen for the inner loop

    The checks are cheap per statement and expensive when a loop runs a billion times.

  10. What does bt print in gdb?

    Answer

    The call stack, one frame per active call

    backtrace lists the frames from the current function outward.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container. The debugger was gdb 16.3 from the same image.

Article author: Arthur Isaev