【问题标题】:Printing variables when debugging - gdb can not print variables calculated in another subroutine调试时打印变量 - gdb 无法打印在另一个子程序中计算的变量
【发布时间】:2011-12-13 17:26:59
【问题描述】:

我目前正在调试一个用 fortran 编写并用 gfortran 编译的代码。

我在使用 gdb 打印一些变量时遇到了问题。

例如,当我在一个子程序内部并且我想打印一个来自“外部”的变量并且其大小取决于在另一个子程序中计算的参数时,看起来 gdb 无法识别大小矩阵,无法打印。

我在下面给出一个简化的例子,以便清楚:

subroutine stiff(id)  
implicit real*8 (a-h,o-z)  
common /a/nnp  
dimension id(5,nnp)  

这是gdb给出的一些结果

(gdb) print id  
$6 = ()  
(gdb) whatis id  
type = integer(kind=4) (5,0)  
(gdb) print nnp  
$7 = 15  

有没有办法解决这个问题,还是编程方式固有的?这段代码是别人开发的,而且很大,所以我可以改变所有声明变量的方式。
提前感谢您的帮助。

编辑:

看下面一个简单的程序(我能做到的最简单的)。它与我正在处理的代码具有全局相同的结构,并且与我之前描述的 gdb 具有相同的行为。当进入输入子程序时,我无法打印“id”变量。

implicit real*8 (a-h,o-z)
dimension a(1000)
call markaz(a)
stop
end

subroutine markaz(a)

implicit real*8 (a-h,o-z)
dimension a(1000)    
common /a/nnn

call dim1(l1,l2)

call input(a(l1))

return
end

subroutine dim1(l1,l2)
implicit real*8 (a-h,o-z)
common /a/nnn     

print*, 'enter nnn:  ';read(*,*) nnn

l1=1
l2=l1+(nnn*5+1)/2

return
end

subroutine input(id)
implicit real*8 (a-h,o-z)
common /a/nnn     
dimension id(5,nnn)

do i=1,5
do j=1,nnn 
id(i,j)=1.0
enddo
enddo

return
end

这是我使用 gfortran 4.4.5 和 gdb 7.0.1 得到的结果

$ gfortran -g -fbacktrace test.for  
$ gdb ./a.out  
GNU gdb (GDB) 7.0.1-debian  
Copyright (C) 2009 Free Software Foundation, Inc.  
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>  
This is free software: you are free to change and redistribute it.  
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"  
and "show warranty" for details.  
This GDB was configured as "x86_64-linux-gnu".  
For bug reporting instructions, please see:  
<http://www.gnu.org/software/gdb/bugs/>...  
Reading symbols from /test_print/a.out...done.  
(gdb) break test.for :36  
Breakpoint 1 at 0x400a7d: file test.for, line 36.  
(gdb) run  
Starting program: /test_print/a.out   
 enter nnn:    
2  

Breakpoint 1, input (id=...) at test.for:37  
37        do i=1,5  
Current language:  auto  
The current source language is "auto; currently fortran".  
(gdb) whatis id  
type = integer(kind=4) (5,0)  
(gdb) print id  
$1 = ()  
(gdb) print nnn  
$2 = 2  
(gdb)   

【问题讨论】:

  • 我试过了,我的 gdb 打印了正确的答案。你能补充更多细节吗?也许一些最小的测试程序?

标签: debugging gdb fortran


【解决方案1】:

对我来说,您的代码与 gdb 配合得很好。使用 gfortran 4.5.5 和 gdb 7.2 测试。它不应该是这种编程风格的任何固有限制。

[testy]$ gfortran -g -fbacktrace common2.f90

[testy]$ gdb ./a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-48.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/x/f/testy/a.out...done.
(gdb) break common2.f90:73
Breakpoint 1 at 0x400a2d: file common2.f90, line 73.
(gdb) run
Starting program: /home/x/f/testy/a.out 
 enter nnn:  
4

Breakpoint 1, input (id=...) at common2.f90:73
73      do i=1,5
(gdb) whatis id
type = integer(kind=4) (5,4)
(gdb) print id
$1 = (( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) )
(gdb) print nnn
$2 = 4
(gdb) 

【讨论】:

  • 和你做的一模一样,看看我在编辑我的帖子时得到了什么。那么,这可能是由于 gfortran 和 gdb 的版本造成的吗?还是完全是个谜?
  • 可能是版本不同。可能是gdb的更多问题,而且你的gfortran版本很旧。
  • 用 gfortran 4.6.2 和 gdb 7.3.1 更新这个:gfortran 首先抱怨id 的类型(应该声明为real*8,在修复该gdb 后报告类型id 为type = real(kind=8) (5,*)
  • 这很奇怪。由于隐式类型,它不应该是实数*8,而是整数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多