Fortran module variables not accessible in debuggers
Asked Answered
A

3

11

I've compiled a Fortran code, which contains several modules, using both gfortran 4.4 and intel 11.1 and subsequently tried to debug it using both gdb and DDT. In all cases, I cannot see the values of any variables that are declared in modules. These global variables have values, as the code still runs correctly, but I can't see what the values are in my debuggers. Local variables are fine. I've had trouble finding a solution to this problem elsewhere online, so perhaps there is no straightforward solution, but it's going to be really difficult to debug my code if I can't see the values of any of my global variables.

Abeu answered 22/4, 2012 at 0:50 Comment(4)
Are you compiling with flags -O0 and -g?Kashakashden
I've seen this, and what I did was to enclose the variables in a user type, and keep one global (saved) record. Then you access in the debugger with % (example OT%POINT_COUNT when OT contains my gobals)Assiduity
To Nick Atoms, compiling with -O0 did the trick, but for non-array elements only. I read that optimization can cause global variables to not be accessible, but I thought that if no optimization flags were used, then the compiler did no optimization by default. Apparently, I was wrong. However, I still cannot access any information in my global arrays. Again, they're being read in and assigned values correctly, but I can't print the whole array or any individual elements in the array in the debugger.Abeu
I asked a similar question and later was able to answer it myself. Mainly it involves casting the pointers to different types then you can view them however you'd like (as arrays, structures, etc) #46142049Flabby
C
6

With newer GDBs (7.2 if I recall correctly), debugging modules is simple. Take the following program:

module modname
  integer :: var1 = 1 , var2 = 2
end module modname

use modname, only: newvar => var2
newvar = 7
end

You can now run:

$ gfortran -g -o mytest test.f90; gdb --quiet ./mytest
Reading symbols from /dev/shm/mytest...done.
(gdb) b 6
Breakpoint 1 at 0x4006a0: file test.f90, line 6.
(gdb) run
Starting program: /dev/shm/mytest
Breakpoint 1, MAIN__ () at test.f90:6
6       newvar = 7
(gdb) p newvar
$1 = 2
(gdb) p var1
No symbol "var1" in current context.
(gdb) p modname::var1
$2 = 1
(gdb) p modname::var2
$3 = 2
(gdb) n
7       end
(gdb) p modname::var2
$4 = 7
(gdb)
Curley answered 23/4, 2012 at 9:50 Comment(1)
It must be gdb 7.2 that this works for because I have 7.1 installed on my system and typing "p modname::var" still results in "No symbol in current context". However, note that this whole issue seems to be a gfortran issue and not a gdb issue, as when I built my source code with intel ifort 11.1, gdb had no issue in printing a local or global variable or array. Building with no optimization with gfortran allowed printing global variables but still not global arrays.Abeu
K
3

In gdb, try referencing the global variables with names like __modulename__variablename

You can check that this is the right mangling scheme using nm and grep to find one of your global variables in the symbols of your program.

If that doesn't work, make sure you're using a recent version of gdb.

Here's a thread on this issue: http://gcc.gnu.org/ml/fortran/2005-04/msg00064.html

Kashakashden answered 22/4, 2012 at 2:3 Comment(2)
This works to display non-array elements in addition to compiling with -O0. I can print global variables, but I still can't see the values of global arrays. When I try to print the name of the array as __modulename_MOD_arrayname, some seemingly random large number is returned. If I try to print one element of the array, I get -nan(0xf...). Any idea how to get arrays to print out correctly as well as non array variables?Abeu
Just to note on my progress, I tried building with intel 11.1 (with -O0 this time) and debugging with gdb, and all global variables and global arrays print out properly. I tried building with g95 and debugging with gdb and global variables won't print out even when built with -O0 (I have to use the naming convention listed above to get them). But global arrays still don't print out. So in summary, it seems like it's a problem with gfortran and g95 and not gdb. And building with intel 11.1 works fine.Abeu
L
0

I had the same issue (GNU gdb 7.9 running in parallel with MPI). What worked for me was the following:

p __modname_mod_var

That is: double underscore, the name of the module, underscore, mod, the name of the variable.

Compiling with -gstabs+ instead of -g may also fix some issues (but not the present one).

Lexicologist answered 25/10, 2018 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.