【问题标题】:Can GDB be used to print values of allocatable arrays of a derived type in Fortran 90? [duplicate]GDB 可以用于在 Fortran 90 中打印派生类型的可分配数组的值吗? [复制]
【发布时间】:2023-03-30 02:55:01
【问题描述】:

我在 Fortran90 程序中有以下数据结构:

TYPE derivedType
  CHARACTER(100)     :: name      = ' '
  INTEGER            :: type      = 0
  REAL(KIND(1.0D0))  :: property  = 0.0
END TYPE derivedType

TYPE (derivedType), ALLOCATABLE, DIMENSION(:) :: arrayOfDerivedTypes

当我尝试在 GDB 中调试和打印值时:

(gdb) p arrayOfDerivedTypes(1)%name

我得到无意义的值(通常是零、正斜杠和字母的字符串)或完全错误的值(如 arrayOfDerivedTypes(1)%name = 9,当我知道它是 = 2 时)。如何让 GDB 打印正确的值?

背景

我知道:

我不想麻烦编译一个单独的 GDB 分支来测试它是否能解决这个问题,如果有人已经知道它不会或者是否有更好的解决方案可用。

我很难想象目前还没有解决方案。 fortran 社区还没有更好的免费调试器解决方案吗?

【问题讨论】:

    标签: gdb fortran fortran90 gfortran derived-types


    【解决方案1】:

    您使用的是哪个版本的 gdb 和 fortran 编译器(gfortran?)? 因为我没有问题

    • gdb - GNU gdb (GDB) Red Hat Enterprise Linux (7.2-56.el6)
    • gfortran - GNU Fortran (GCC) 4.4.6 20120305(红帽 4.4.6-4)

    这是测试程序:

    program test
            implicit none
    
            TYPE derivedType
                    CHARACTER(100)     :: name      = ' '
                    INTEGER            :: type      = 0
                    REAL(KIND(1.0D0))  :: property  = 0.0
            END TYPE derivedType
    
            TYPE (derivedType), ALLOCATABLE, DIMENSION(:) :: arrayOfDerivedTypes
    
            allocate(arrayOfDerivedTypes(10))
    
            write(6,*) arrayOfDerivedTypes(1)%type
    
    end program test
    

    我把它编译成

    gfortran -o test -g -O0 -Wall test.f90
    

    然后启动调试器,设置断点并运行

    $ gdb test
    (gdb) break test.f90:14
    Breakpoint 1 at 0x402c8a: file test.f90, line 14.
    (gdb) r
    [Thread debugging using libthread_db enabled]
    
    Breakpoint 1, test () at test.f90:14
    14              write(6,*) arrayOfDerivedTypes(1)%type
    (gdb) p arrayOfDerivedTypes
    $3 = (( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ), ( ' ' <repeats 100 times>, 0, 0 ))
    (gdb) p arrayOfDerivedTypes(1)
    $4 = ( ' ' <repeats 100 times>, 0, 0 )
    (gdb) p arrayOfDerivedTypes(1)%property
    $5 = 0
    (gdb) p arrayOfDerivedTypes(1)%name
    $6 = ' ' <repeats 100 times>
    

    我可以看到一切。

    还有http://brulermavie.org/2012/02/how-to-debug-fortran-programs-using-gdb/ 对我没有帮助,因为我没有看到问题。

    【讨论】:

    【解决方案2】:

    我知道可能答案有点不对,但 Sun studio (sdb) 和 intel fortran 也带有调试器

    【讨论】:

      猜你喜欢
      • 2015-05-26
      • 2020-03-17
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2013-08-13
      相关资源
      最近更新 更多