Inspect variable imported from another module in VSCode Fortran debugging
Asked Answered
T

1

5

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
Truly answered 7/7, 2021 at 14:31 Comment(0)
A
6

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.

  • Not quite sure whether you can easily watch for derived types with such a syntax.
  • if variable is an (e.g. 2d) array, you can access to its elements individualy with usual fortran indexing like module::variable(12,457)
  • Multiple elements of an array can be accessed via gdb queries in VS Code's "debug console". Use -exec prefix to pass gdb instructions, such as: -exec p module::variable@100 to display the 100 first elements of module::variable.
  • Multiple elements can be displayed in the "watch" panel by specifying a starting index as follows: 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.

Angry answered 6/9, 2021 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.