我正在尝试使用 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,但根据 Pretty print Fortran dynamic type in gdb ?,看来您可以使用隐藏的 _data
组件来访问结构组件。就像是
print this%_data%int1
(这个答案可能对像我这样通过谷歌搜索很容易找到这篇文章但无法轻松找到链接的帖子的人有用。链接的帖子主要讨论一个几乎不相关的主题,但他提供了一个使用 GDB 的示例在 Fortran 中打印出类实例的成员变量。我在 Internet 上找不到任何其他类似的示例。)
我也只是为我自己的 Fortran 代码尝试了它,它似乎可以工作。
使用上面的示例代码(我不得不将虚拟变量“this”更改为“temp”,因为当我尝试打印此内容时,由于某种原因,我一直在 GDB 中遇到段错误),这是我从 GDB 得到的(版本 7.11 .1 在 Ubuntu 上):
(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 - 从 GDB 中的 Fortran 多态派生数据类型中打印值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836882/