Debugging 调试时打印变量-gdb无法打印在另一个子例程中计算的变量

Debugging 调试时打印变量-gdb无法打印在另一个子例程中计算的变量,debugging,gdb,fortran,Debugging,Gdb,Fortran,我目前正在调试用fortran编写并用gfortran编译的代码 我一直面临使用gdb打印一些变量的问题 例如,当我在一个子例程中,我想打印一个来自“外部”的变量,其大小取决于另一个子例程中计算的参数时,gdb似乎无法识别矩阵的大小,也无法打印它 我在下面给出了一个简化的示例,以便说明: subroutine stiff(id) implicit real*8 (a-h,o-z) common /a/nnp dimension id(5,nnp) 以下是gdb给出的一些结果 (

我目前正在调试用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)   
$gfortran-g-fbacktrace test.for
$gdb./a.out
GNU gdb(gdb)7.0.1-debian
版权所有(C)2009免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-linux-gnu”。
有关错误报告说明,请参阅:
...  
从/test_print/a.out读取符号…完成。
(gdb)断裂试验。适用于:36
0x400a7d处的断点1:file test.for,第36行。
(gdb)运行
启动程序:/test\u print/a.out
输入nnn:
2.
断点1,在测试时输入(id=…)。for:37
37 i=1,5吗
当前语言:自动
当前源语言为“自动;当前为fortran”。
(gdb)什么是id
类型=整数(种类=4)(5,0)
(gdb)打印id
$1 = ()  
(gdb)打印nnn
$2 = 2  
(gdb)

对我来说,您的代码与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) 
[testy]$gfortran-g-fbacktrace common2.f90
[testy]$gdb./a.out
GNU gdb(gdb)Red Hat Enterprise Linux(7.2-48.el6)
版权所有(C)2010免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-redhat-linux-gnu”。
有关错误报告说明,请参阅:
...
从/home/x/f/testy/a.out读取符号…完成。
(gdb)常见故障2.f90:73
0x400a2d处的断点1:文件common2.f90,第73行。
(gdb)运行
启动程序:/home/x/f/testy/a.out
输入nnn:
4.
断点1,在common2处输入(id=…)。f90:73
73 i=1,5吗
(gdb)什么是id
类型=整数(种类=4)(5,4)
(gdb)打印id
$1 = (( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) ( 0, 0, 0, 0, 0) )
(gdb)打印nnn
$2 = 4
(gdb)

我试过了,我的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,而是整数,因为隐式键入。