问题
I'm using Eclipse (Neon.3 Release 4.6.3) with gdb 7.11.1 and gfortran 5.4.0, to debug an executable, but it only seems possible to watch local subroutine variables and simple external variables properly. Consider this simplified example:
module ext_class
type extstruct_type
integer(kind=4), ::svar1
integer(kind=4), ::svar2
end type extstruct_type
integer(kind=4), save :: extvar
integer(kind=4), dimension(4), save :: extarray
type (extstruct_type), save :: extstruct
end
module mod
subroutine foo(invar)
use ext_class, only : extvar, extarray, extstruct
type (real::8), intent(in) :: invar
integer(kind=4) :: i
...
!Debugger breakpoint inserted here to check variable visibility
end
end
Eclipse's variable list will properly show the local variables (i
) and the inputs (invar
) even if they're modules/arrays, but any external/global variables (extvar, extarray, extstruct
) don't show up in the list. If I try typing them manually into the "expressions" view, it gives errors about being unable to evaluate missing symbols:
Multiple errors reported.
1) Failed to execute MI command: -var-create - * extvar Error message from debugger back end: -var-create: unable to create variable object
2) Unable to create variable object
3) Failed to execute MI command: -data-evaluate-expression extvar Error message from debugger back end: No symbol "extvar" in current context.
4) Failed to execute MI command: -var-create - * extvar Error message from debugger back end: -var-create: unable to create variable object
I discovered the special notation used by the compiler to store these global variables in the binary executable using the command:
nm <binaryname> | grep <modulename>
I can then generally see the global module members in gdb by typing:
print __<modulename>_MOD_<membername>
However, it only works for simple member types in the module! For example I can see the integer member properly:
print __ext_class_MOD_extvar
$1 = 0
For a static array of integers, it improperly only prints the first element, thus preventing me from viewing any of the member array's other elements:
print __ext_class_MOD_extarray
$2 = 0
print __ext_class_MOD_extarray(1:4)
Cannot perform substring on this type
For a structure type, it improperly only prints the first member (svar1
), thus preventing me from viewing any of the structure's other members:
print __ext_class_MOD_extstruct
$3 = 0
print __ext_class_MOD_extstruct%svar2
Attempt to extract a component of a value that is not a structure.
I read here that this might actually be a problem with gfortran, not gdb, because it works fine when using Intel compilers. Is there possibly an extra flag I need to set when compiling? I already use -g -O0
回答1:
EDIT: This issue has been resolved in some versions (gfortran 6.2.0 with gdb 7.12 on MacOS, but not on the same versions in Ubuntu). Update to the latest version before trying the steps below.
I found a workaround that's also compatible in Eclipse. It seems that the binary isn't tracking information about the variable types, just their addresses in memory. So the variables can be viewed by casting them to the proper type. Enter __ext_class_MOD_extstruct
into Eclipse's "expressions" tab and then right-click the entry and chose "Cast to type..." entering extstruct_type
! Alternatively, simply enter
(extstruct_type)__ext_class_MOD_extstruct
in the "expression" tab. Note that asterisks are omitted (differing from the C syntax). The same can be achieved at the gdb command line and individual members are obtainable by name using the %
delimiter:
print ((extstruct_type)(__ext_class_MOD_extstruct)
$5 = (0, 0)
print ((extstruct_type)(__ext_class_MOD_extstruct)%svar2
$6 = 0
Eclipse's "expression" option "Display as Array..." fails as it seems to be using C syntax pointer arithmetic (*
) that isn't compatible with gdb for fortran, but it works when manually omitting the asterisks:
print __ext_class_MOD_extarray@4
$7 = (0, 0, 0, 0)
print __ext_class_MOD_extarray(2)
Cannot perform substring on this type
Note that accessing individual array elements still fails in some versions. Let's hope that they fix this issue once and for all in the newer releases.
来源:https://stackoverflow.com/questions/46141549/global-arrays-structs-not-accessible-in-gdb-while-debugging-gfortran-executable