【问题标题】:Print values out of a Fortran polymorphic derived data type in GDB从 GDB 中的 Fortran 多态派生数据类型中打印值
【发布时间】:2014-05-23 19:13:43
【问题描述】:

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

!!类定义

  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

【问题讨论】:

  • 这将是一个大问题。 GDB 可能不理解 gfortran 多态描述符。试着在某处找出它的结构。

标签: gdb fortran


【解决方案1】:

这可能只适用于较新版本的 GDB,但根据Pretty print Fortran dynamic type in gdb?,您似乎可以使用隐藏的_data 组件来访问结构组件。类似的东西

print this%_data%int1

(对于像我这样通过 Google 搜索轻松找到此帖子但无法轻松找到链接帖子的人来说,此答案可能很有用。链接帖子主要讨论一个几乎不相关的主题,但他提供了一个示例使用GDB打印Fortran中类实例的成员变量。我在网上找不到其他类似的例子。)

我也只是为我自己的 Fortran 代码尝试过,它似乎可以工作。

使用上面的示例代码(我不得不将虚拟变量“this”更改为“temp”,因为当我尝试打印此内容时,由于某种原因,我一直在 GDB 中遇到段错误),这就是我从 GDB 得到的(Ubuntu 上的版本 7.11.1):

(gdb) print temp
$1 = ( 0x601070 <struct>, 0x4009c0 <__class_test_MOD___vtab_class_test_Mytype> )
(gdb) print temp%_data
$2 = (PTR TO -> ( Type mytype
integer(kind=4) :: int1
integer(kind=4) :: int2
integer(kind=4) :: int3
integer(kind=4) :: int4
End Type mytype )) 0x601070 <struct>
(gdb) print temp%_data%int1
$3 = 2
(gdb) print temp%_data%int2
$4 = 3

【讨论】:

  • 对不起,我好像一直没抓住重点。您能否尝试在答案中澄清这一点,以免其他人犯同样的错误?
  • 我添加了一些来自 GDB 的示例输出来阐明我的观点;我希望这会有所帮助。请随时编辑帖子以提高其清晰度——我认为你的 Fortran 词汇比我的更好,更一致。
【解决方案2】:

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

(gdb) break class_test::fun
(gdb) run
(gdb) info args

这揭示了与虚拟 this 关联的参数 struct 的名称和地址:

this = ( 0x601080 <struct>, 0x400ae0 <__class_test_MOD___vtab_class_test_Mytype> )

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

(gdb) x/4u 0x601080

输出是什么

0x601080 <struct.1905>: 2       3       4       5

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

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

【讨论】:

    猜你喜欢
    • 2022-11-14
    • 2023-03-30
    • 1970-01-01
    • 2013-11-17
    • 2013-10-07
    • 2012-11-27
    • 2012-10-23
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多