Tutorial 5: Debugging Fortran code with Visual Studio Code (VSCode)

Tags: VSCode

This takes a bit more initial configuration (that may be less than obvious) than some other languages, so we’ll walk you through this.

Tip

Click on the boxes throughout this tutorial labeled “More Detail (click here to show/hide)” in order to see more detailed information and/or helpful hints. Click again to hide the info again.

You will learn to …

  • Configure VSCode for debugging Fortran code

  • Create tasks in VSCode

  • Perform basic debugging of Fortran code in VSCode

Prerequisites

  • The Modern Fortran extension for VSCode (This requires and will automatically install the C/C++ extension as well)

  • (As an alternative to the above, we have also successfully used the Fortran Breakpoint Support extension with the C/C++ extension)

  • subMIT already has both gfortran and gdb already installed natively, so you don’t need to worry about those. However, if you wish to use a custom version of gfortran in a conda environment, make sure you install gdb in that conda environment as well, as these are both necessary (e.g. conda create --name gfort_dbg -c conda-forge gfortran gdb).

Configuring VSCode to debug Fortran

  1. Follow the directions outlined directions to open a session connecting Visual Studio Code on your laptop (or desktop) to subMIT. (But do not open a folder yet once connected to subMIT).

  2. From the top menu, select “File” -> “New Text File …”

    A new editor tab titled “Untitled-1” will appear (it may take a moment).

    Image of "Untitled-1" editor tab in VSCode
  3. Copy & paste the following code into that editor window.

    1program hello
    2  integer :: i
    3  i = 0
    4  i = i + 1
    5  i = i + 1
    6  print *, 'Hello, World!'
    7  print *, 'Hi again'
    8end program hello
    
  4. From the menu, select “File” -> “Save”.

    A drop-down menu will appear at the top of your screen suggesting a filename in your home directory on submit. It will look something like: /home/submit/username/program hello.md, where “username” is your subMIT (kerberos) username.

    Change this to (without the quotes) “/home/submit/username/tutorial_vscode_dbgfort/hello.f90”, but replace “username” with your subMIT (kerberos) username. Then hit OK.

    ../_images/ConfirmFort.png
  5. VSCode will now prompt you, “The folder tutorial_vscode_dbgfort does not exist. Would you like to create it?”. Hit the “OK” button.

  6. In the menu (top of screen), select “File” -> “Open Folder…”

    In the text box that appears at the top of your screen, type (without the quotes) “/home/submit/username/tutorial_vscode_dbgfort/” but replace “username” with your subMIT (kerberos) username. (It is likely already pre-filled).

    Hit OK.

    VSCode will re-establish your connection to subMIT and may take a moment.

  7. Create a custom task (we will later use this as a preLaunchTask) by following these steps:

    1. Select “File” -> “New Text File” from the menu.

    2. Copy and paste the following into the editor for this new blank file:

      {
          "version": "2.0.0",
          "tasks": [
              {
                  "type": "shell",
                  "label": "gfbuild",
                  "command": "gfortran hello.f90 -g3 -O0 -fbacktrace -fcheck=all -ffpe-trap=zero,overflow,underflow -Wall -Wextra -Warray-temporaries -Wconversion -ffree-line-length-0",
              }
          ]
      }
      
    3. Select “File” -> “Save” from the menu.

    4. In the box that appears at the top of the screen, enter (without the quotes) “/home/submit/username/tutorial_vscode_dbgfort/.vscode/tasks.json”, but replace “username” with your subMIT (kerberos) username. Then hit OK. (Also hit OK when it asks if you want to create the folder “.vscode”).

      You should now see tasks.json appear in the Explorer sidebar (“View”->”Explorer” from the top menu, or click on the sidebar icon)

      ../_images/fort_tasks_json.png
  8. Create a launch configuration by performing the following steps:

    1. “File” -> “New Text File” from the top menu

    2. Copy & Paste the following into the edtior for this new blank file:

      {
          "version": "0.2.0",
          "configurations": [
            {
              "name": "Debug Fortran (gdb)",
              "type": "cppdbg",
              "request": "launch",
              "preLaunchTask": "gfbuild",
              "program": "${workspaceFolder}/a.out",
              "cwd":     "${workspaceFolder}",
              "args": [],
              "environment": [],
              "stopAtEntry": false,
              "externalConsole": false,
              "MIMode": "gdb",
              "setupCommands": [
                {
                  "description": "Enable pretty-printing for gdb",
                  "text": "-enable-pretty-printing",
                  "ignoreFailures": true
                }
              ]
            }
          ]
      }
      

      Source: The above code is a slight variation of that provided by the Modern Fortran VSCode extension documentation.

    3. Select “File” -> “Save” from the top menu.

    4. In the box that appears at the top of the screen, enter (without the quotes) “/home/submit/username/tutorial_vscode_dbgfort/.vscode/launch.json”, but replace “username” with your subMIT (kerberos) username. Then hit OK.

      You should now see launch.json appear in the Explorer sidebar (“View”->”Explorer” from the top menu, or click on the sidebar icon)

      ../_images/fort_launch_json.png
  9. Go back to the hello.f90 editor (“View”->”Explorer” in top menu, then click on “hello.f90”) and create a breakpoint by doing the following:

    Left-Click to the left of line 4. This should create a red dot to the left of line 4. (This red dot should persist after you move your cursor away).

    ../_images/fort_breakpoint.png
  10. Select “Run” -> “Start Debugging” from the top menu to actually start debugging.

    Your screen should then look like the screen shot below.

    ../_images/fort_debug_4.png

    The Debug sidebar will open on the left and a terminal on the bottom of the screen.

    Note that the yellow arrow to the left of line 4 (and highlighting) indicate that the execution is pause on line 4 (due to the breakpoint we set above).

    In the upper left “Variables” section we can see that, at this point in execution, the variable i has the value 0. (Hovering the mouse over any instance of the variable i reveals the same).

    In the bottom left, we can navigate the call stack (this is helpful when code makes heavy use of functions).

    The screen output of the program (e.g. print statements) will be displayed in the Terminal at the bottom.

    The debug navigation bar at the top of the screen (or the “Run” top menu) can be used to control the debug execution (e.g. Step Over/Into/Out, Continue, Stop).

    You are now all set to debug your Fortran application on subMIT!

    (To see an example of stepping through this program, click the “More Detail” below).