如何在GDB中探索Fortran数组结构组件?

时间:2016-10-09 19:44:05

标签: arrays types fortran gdb

如何使用gdb打印Fortran类型中定义的可分配数组?

假设我有以下Fortran 90代码:

program test

implicit none

type :: t_buzzer
  real(8), allocatable :: alloc_array(:,:)
end type

integer, parameter :: nx=2, ny=3
integer :: ii, jj
real(8) :: fixed_array(nx,ny)
real(8), allocatable :: alloc_array(:,:)
type(t_buzzer) :: buzz

allocate(alloc_array(nx,ny))
allocate(buzz%alloc_array(nx,ny))

do jj=1,ny
  do ii=1,nx
    fixed_array(ii,jj) = 10.0 * real(ii) + real(jj)
  end do
end do

alloc_array = fixed_array
buzz%alloc_array = alloc_array

write(*,*) fixed_array
write(*,*) alloc_array
write(*,*) buzz%alloc_array

deallocate(alloc_array)
deallocate(buzz%alloc_array)

end program test

使用gdb运行它,我可以使用print函数查看fixed_array的内容,但不能查看alloc_array

(gdb) print fixed_array 
$1 = (( 11, 21) ( 12, 22) ( 13, 23) )
(gdb) print alloc_array
$2 = (( 0) )

要查看alloc_array的内容,我必须使用

(gdb) print *((real_8 *)alloc_array )@6
$3 = (11, 21, 12, 22, 13, 23)

现在我要查看buzz%alloc_array的内容。上述命令都不适用于此:

(gdb) print buzz%alloc_array
$4 = (( 0) )
(gdb) print *((real_8 *)buzz%alloc_array )@6
Attempt to extract a component of a value that is not a structure.

从gdb文档中,我似乎应该使用explore函数。但是,当我尝试时,什么都没有打印出来:

(gdb) explore buzz
The value of 'buzz' is a struct/class of type 'Type t_buzzer
real(kind=8) :: alloc_array(*,*)
End Type t_buzzer' with the following fields:

  alloc_array = <Enter 0 to explore this field of type 'real(kind=8) (*,*)'>

Enter the field number of choice: 0
'buzz.alloc_array' is an array of 'real(kind=8) (*)'.
Enter the index of the element you want to explore in 'buzz.alloc_array': 1,1

Returning to parent value...

The value of 'buzz' is a struct/class of type 'Type t_buzzer
real(kind=8) :: alloc_array(*,*)
End Type t_buzzer' with the following fields:

  alloc_array = <Enter 0 to explore this field of type 'real(kind=8) (*,*)'>

Enter the field number of choice: 
(gdb) 

如何查看buzz%alloc_array的内容?

1 个答案:

答案 0 :(得分:0)

就像使用普通数组一样

(gdb) print alloc_array
$1 = (( 11, 21) ( 12, 22) ( 13, 23) )
(gdb) print buzz%alloc_array
$2 = (( 11, 21) ( 12, 22) ( 13, 23) )

为什么您认为自己需要explore功能?

BTW real(8)很丑陋且不可移植(它并不总是意味着8个字节)。

作为一种解决方法,你可以试试这个,但我无法在你的GDB版本中测试它。也许这只适用于Archer补丁。

(gdb) set lang c
Warning: the current language does not match this frame.
(gdb) print buzz.alloc_array[1]
$10 = {11, 21}
(gdb) print buzz.alloc_array[2]
$11 = {12, 22}
(gdb) print buzz.alloc_array[3]
$12 = {13, 23}