Methods of debugging Fortran projects

It’s well known that Fortran in nature is a type of compiled language. To make matters worse, there are very few IDEs specially developed for Fortran, which is especially true on Linux systems.

So, I want to know if there are some elegant methods of debugging the Fortran project on Linux operating system, especially when using Intel oneAPI Toolkits.

Regards,
HZ

Intel has one of the more extensive suites of tools for debugging and optimizing codes, with much of the documentation available on their web site as well as a good amount that comes with the tools when you install them; there are a few IDEs for Fortran on Linux, and I would hazard to say that TotalView is one of the most commonly used commercial debuggers. Apparently gdb is not what you have in mind? If you are interested primarily in Intel products have you tried their web site already? If you compile with the major debug flags on you can often detect problems without the need for a debugger. Debuggers are useful for investigating logic problems in code but generally fail on actual bugs; the compiler flags are generally more useful with that category (over and under-indexing arrays, type mismatches, …).

Read this first …

Tips for Debugging Run-time Failures in Intel Fortran Applications

After that, it depends on whether you are using Intel processors, whether you are interested in parallel processing, whether tuning the code is part of what you consider to be debugging, … this can take a few days to read:

gdb(1) is worth trying to master in line mode, and a lot of GUI users get converted over to CLI users using it; but there are a lot of wrappers for gdb; ddd may already be on your system, but also take a look at
http://sourceware.org/gdb/wiki/GDB%20Front%20Ends

If you are an fpm(1) user you can easily run in the TDU world half-way between GUI and CLI with something like this:

fpm run --runner ‘gdb -tui’

What editor do you use? If you are a vim(1) user edit your code and enter

:packadd termdebug
:Termdebug

If you use emacs(1) there are web books on using gdb(1) and everything else in there (but I am a vi/vim user unless working with an emacs(1) fan so I’ll leave that up to others – no editor holy wars please).

2 Likes

Thank you for sharing your opinion with me.

Though gdb is powerful, it’s a bit complicated and difficult to understand.

This is just one of the core things I’m focusing on.

Thank you for telling me this great website. Though there are so many tools listed above, most of them are not designed for Fortran, IMO.

Do you mean this tool?

The Fortran Tools list has a Text Editors, Integrated Development Environments, and Plugins section, as does fortran-lang.

2 Likes

Hi @Hongyi, for Linux regardless of if you are using ifort or gfortran you should be able to use VS Code + the Modern Fortran extension. Please use the pre-Release version and allow it to install fortls.

That should give you an IDE level debugging functionality with breaking points, hover, watch windows, stepping, etc. OpenMP threads should also work, but no MPI, conditional or functional breakpoint support.

FYI You will need to make sure you use Intel’s GDB version in your launch.json, see the docs for how to do that

1 Like

Thank you for your insightful tips. I mainly use Emacs.

Regards,
HZ

I use VS code with Modern Fortran, it is very nice and user-friendly. I did not use it for debugging purposes.

However, whenever I try to create a new program, the program template appears with 2 cursors and leads to simultaneous writing in two-place (though useful), which leads to the wrong syntax. (for clarity see the attached image file). Maybe someone has a better experience (and is willing to share it) in using VS code with Modern Fortran.

This is because you are using the program snippet of the extension, which generates the template. Snippets insert code with placeholders, which the user can then navigate using Tab. The two cursors are basically VS Code’s way of typing at the same time the program name at both the start and end of the scope. Once done, press Tab to move to the body of the scope.

For more info on how to use snippets see: Snippets in Visual Studio Code

I should also add, that snippets will not necessarily fit all your cases, so it does not always make sense to use them. Let me know if you would like to see certain snippets in the extension.

Also, you can define your own custom snippets in VS Code:Snippets in Visual Studio Code

Are you using GUI emacs or terminal emacs? I find the built-in gdb integration reasonably nice to use in GUI emacs. Open up your main program, then M-x gdb to start a session, then M-x gdb-many-windows to get buffers for tracking variables and the call stack. You can set breakpoints by clicking in the gutter of your source file buffer, like in most IDEs. Control the debugger with the gdb prompt (like terminal) or with the menu.

Thanks for the suggestion. I will try them. Further, custom snippets seem pretty new to me and hopefully fantastic in the future as I continue using Modern Fortran in more depth.

Thank you very much for your tips. I’m using GUI Emacs. Do you have experience working with Emacs using Intel oneAPI?

Regards,
HZ

Emacs is just using gdb, so if you are able to debug the executable with gdb in your terminal, then it will also work when using emacs. I have debugged programs built with ifort and gfortran this way. I would guess ifx works the same, but I’ve never done it myself.

What do you mean by saying ifx?

It is the new LLVM-based compiler of Intel, as described here.

Really. I’ve installed it, but I haven’t noticed it until now:

$ module load compiler
$ which ifx
/opt/intel/oneapi/compiler/2022.0.2/linux/bin/ifx