Printing members of parent type from an instance of an extended type

Viewed 185

Scenario (see the code below): consider some Fortran module my_module which contains the definition of two derived types: test (parent) and sub_test (extends test, subclass). In addition to the two members a and b of the parent class, sub_test defines a new member c. Eventually, this module my_module is to used in some program my_program.

module my_module

implicit none

private
public :: test, sub_test

type :: test
  integer :: a = 1
  integer :: b = 2
end type test

type, extends(test) :: sub_test
  integer :: c = 3
contains
  procedure, public :: increment
end type sub_test

contains

subroutine increment (this)
class(sub_test) :: this
this % a = this % a + 1
write(*, *) "Incrementation complete!"
end subroutine increment

end module my_module
!---------------------------------------
program my_program

use my_module

implicit none

type(test)  :: parent
type(sub_test) :: child

write(*, *) "Printing parent members"
write(*, *) parent % a, parent % b
write(*, *) "Printing child members"
write(*, *) child % a, child % b, child % c

call child % increment

end program my_program

Objective: When debugging, using gdb, we want to print the values of the parent members a and b of some variable of the child (extended) type sub_test. In sum, we're looking for a successful completion of something like:

(gdb) print child % a

yet, apparently, it is not that trivial.

Setup before trying to print:

  • Compile the program (here called test.f90) with the -g flag, convenient for the application of gdb with gfortran -g test.f90 -o a.out

  • Run the executable with gdb ./a.out

  • Insert a breakpoint a breakpoint in the last write(*, *) statement of the program and in the unique write(*, *) statement in subroutine increment and run the program

(gdb) break test.f90:41
Breakpoint 1 at 0xb12: file test.f90, line 41.
(gdb) break test.f90:24
Breakpoint 2 at 0x955: file test.f90, line 24.
(gdb) run

What I have (unsuccessfully) tried so far:

  • Print directly the members from the child variable (member c prints flawlessly)
Breakpoint 1, my_program () at test.f90:41
41  write(*, *) child % a, child % b, child % c
(gdb) p child%a
There is no member named a.
(gdb) p child%b
There is no member named b.
(gdb) p child%c
$1 = 3
  • Print the test (parent) member of the child variable, as gdb shows it exists
  • Print the so-wanted members from the test member of child
Breakpoint 1, my_program () at test.f90:41
41  write(*, *) child % a, child % b, child % c
(gdb) p child
$2 = ( test = ( a = 1, b = 2 ), c = 3 )
(gdb) p child%test
A syntax error in expression, near `test'.
(gdb) p child%test%a
A syntax error in expression, near `test%a'.
(gdb) p child%test%b
A syntax error in expression, near `test%b'.
  • The same steps above, but this time applied at the breakpoint inside the subroutine, therefore, trying to print the members of the dummy variable this. In this case, gdb clearly changes the pitch regarding the variable and, for instance, c can only be accessed through the hidden member _dat, yet a and b continue unreachable.
Breakpoint 2, my_module::increment (this=...) at test.f90:24
24  write(*, *) "Incrementation complete!"
(gdb) p this
$3 = ( _data = 0x555555756018 <child>, _vptr = 0x555555755d20 <__my_module_MOD___vtab_my_module_Sub_test> )
(gdb) p this%_data
$4 = (PTR TO -> ( Type sub_test )) 0x555555756018 <child>
(gdb) p this%_data%a
There is no member named a.
(gdb) p this%_data%b
There is no member named b.
(gdb) p this%_data%c
$5 = 3
(gdb) p this%_data%test%a
A syntax error in expression, near `test%a'.
(gdb) p this%_data%test%b
A syntax error in expression, near `test%b'.

Thank you for reading: I feel like reaching a dead-end in this regards, I deeply hope someone could lend me a hand. gdb seems incredible but, without being able to inspect this information, I sadly fear it won't suit my needs properly.

More information: I am using gdb-9.2 and gfortran 7.5.0.

0 Answers
Related