在gdb中打印Fortran可分配的多态变量的值非常痛苦。给定以下程序,为了查看alloc_ext
的值,我必须执行以下操作:
(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 ' )
这在派生类型中包含其他多态派生类型的数组时会变得非常痛苦。我尝试调查Python漂亮打印API,但似乎仍然无法获得实际动态类型,甚至无法获取
_vptr
地址上的标签,这足以打印出某些信息。 我正在使用gdb 8.0.1和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