I am debugging some code that contains many Fortran modules, some of which share variables between each other. Unfortunately, gdb with VScode seems to have trouble inspecting imported variables when debugging.
Currently when I need to inspect an imported variable, the only way to do so is to stop debugging, and manually alter the code to include a local variable equal to the imported variable. In the example below, to find out what value of foo%bar
is passed to the function a_function
I would have to declare a new variable, like so
module setup
type(customDerived) :: foo
foo%bar = 1
end module setup
module example
use setup, only: foo
integer(ik) :: foobar <-- Stop debugging, add these lines, restart and inspect 'foobar'
foobar = foo%bar <--
a_function(foo%bar)
end module example
This is obviously very time consuming, and I don't know why VSCode should not be able to inspect global variables. Any ideas? The following are the gfortran compiler flags I currently have turned on in the makefile
-Og -g -Wall -Wextra -Wline-truncation -pedantic -fimplicit-none -fcheck=all -fbacktrace
This issue has been partly treated in: Fortran module variables not accessible in debuggers.
Basically, in the WATCH panel from Visual Studio Code, you can "add expression" and watch for a module variable using the syntax module::variable
.
variable
is an (e.g. 2d) array, you can access to its elements individualy with usual fortran indexing like module::variable(12,457)
-exec
prefix to pass gdb instructions, such as: -exec p module::variable@100
to display the 100 first elements of module::variable
.module::variable(1,1)@100
.Useful sources: https://numericalnoob.blogspot.com/2012/08/fortran-allocatable-arrays-and-pointers.html and https://www.gnu.org/software/gdb/documentation/ of course.