从GDB中的Fortran多态派生数据类型中打印值

use*_*991 5 fortran gdb

我正在尝试使用gdb(GNU gdb(Ubuntu / Linaro 7.4-2012.04-0ubuntu2.1)和gfortran(gcc版本4.6.3)调试以下代码。如果我启动gdb并逐步完成子例程的乐趣,我想在模块class_test中打印派生类型“ mytype”的变量使用命令:print class_test :: int很容易在模块“ class_test”中打印变量“ int”,我的问题是如何打印变量int1,int2 ... int4如果gdb逐步执行了子例程fun?

!! 类定义

  module class_test


 integer :: int = 1


 type, public :: mytype

    private

    integer :: int1  = 2

    integer :: int2  = 3

    integer :: int3  = 4

    integer :: int4  = 5

  contains


  procedure, pass(this) :: fun

  end type mytype

  contains

  subroutine fun ( this )

  class(mytype) :: this

  write (*,*) "subroutine" 

  end subroutine fun

  end module class_test


  !! Program
  program test 

 use class_test

 type(mytype) :: struct

 write (*,*) "whateveryouwant"

 call struct%fun()


  end program
Run Code Online (Sandbox Code Playgroud)

sig*_*gma 1

这是可能的,但我认为您无法避免手动检查内存。我采取了以下步骤:

(gdb) break class_test::fun
(gdb) run
(gdb) info args
Run Code Online (Sandbox Code Playgroud)

struct这揭示了与虚拟变量相关的参数的名称和地址this

this = ( 0x601080 <struct>, 0x400ae0 <__class_test_MOD___vtab_class_test_Mytype> )
Run Code Online (Sandbox Code Playgroud)

尝试打印甚至制表符完成struct都会导致 gdb 出现段错误。所以我使用(Fortran 2008)函数storage_size查找this需要128位内存。打印从地址 0x601080 开始的四个 4 字节块的内容,格式为无符号整数:

(gdb) x/4u 0x601080
Run Code Online (Sandbox Code Playgroud)

其输出是

0x601080 <struct.1905>: 2       3       4       5
Run Code Online (Sandbox Code Playgroud)

显示struct包含预期的整数 2、3、4 和 5。

当然,如果您的派生类型包括各种数据类型甚至其他派生类型,这可能不会那么容易。