Printing the values of Fortran allocatable polymorphic variables in gdb is very painful. Given the program below, in order to see the value of alloc_ext, I have to do the following:
(gdb) p alloc_ext
$1 = ( _data = 0x606260, _vptr = 0x400ce0 <__foo_MOD___vtab_foo_My_extended_type> )
(gdb) ptype alloc_ext
type = Type __class_foo_My_base_type_a
PTR TO -> ( Type my_base_type :: _data)
PTR TO -> ( Type __vtype_foo_My_base_type :: _vptr)
End Type __class_foo_My_base_type_a
(gdb) ptype alloc_ext%_data
type = PTR TO -> ( Type my_base_type
character*4 :: base_char
End Type my_base_type )
(gdb) p alloc_ext%_data
$2 = (PTR TO -> ( Type my_base_type )) 0x606260
(gdb) p *(my_extended_type*)(alloc_ext%_data)
$3 = ( my_base_type = ( base_char = 'base' ), extended_char = 'ext ' )
This quickly gets very painful if a derived type contains, say, an array of other polymorphic derived types. I've tried investigating the python pretty printing API, but I still can't seem to get hold of the actual dynamic type, or even the label on the _vptr address, which would be enough information to pretty print something.
I'm using gdb 8.0.1 and gfortran 7.2.1.
MVCE:
module foo
implicit none
type my_base_type
character(len=4) :: base_char = "base"
end type my_base_type
type, extends(my_base_type) :: my_extended_type
character(len=4) :: extended_char = "ext "
end type my_extended_type
contains
subroutine bar(arg)
class(my_base_type), intent(in) :: arg
print*, "breakpoint here"
select type(arg)
type is (my_base_type)
print*, "my_base_type ", arg%base_char
type is (my_extended_type)
print*, "my_extended_type ", arg%base_char, " ", arg%extended_char
end select
end subroutine bar
end module foo
program mvce
use foo
implicit none
type(my_base_type) :: base
type(my_extended_type) :: ext
class(my_base_type), allocatable :: alloc_base
class(my_base_type), allocatable :: alloc_ext
allocate(alloc_base, source=base)
allocate(alloc_ext, source=ext)
call bar(alloc_base)
call bar(alloc_ext)
end program mvce