调试时打印变量 - 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)
I am currently debugging a code written in fortran and compiled with gfortran.
I have been facing problems to print some variables with gdb.
For example, when I am inside of a subroutine and I want to print a variable which comes from "outside" and has a size which depends of a parameter calculated in another subroutine, it looks like gdb doesn't recognize the size of the matrix and can't print it.
I give below a simplified example so that it is clear :
subroutine stiff(id)
implicit real*8 (a-h,o-z)
common /a/nnp
dimension id(5,nnp)
Here are some results given by gdb
(gdb) print id
$6 = ()
(gdb) whatis id
type = integer(kind=4) (5,0)
(gdb) print nnp
$7 = 15
Is there a way to fix this or is it inherent to the way of programming? This code has been developed by someone else and is huge, so I can change all the way of declaring variables.
Thanks in advance for any help.
edit :
See below a simple program (the most simple I could). It has globally the same structure as the code I am working on and the same behaviour with gdb that I described previously. When being into the input subroutine I can't print the "id" variable.
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
Here is what I get with gfortran 4.4.5 and 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

对我来说,你的代码与 gdb 配合得很好。使用 gfortran 4.5.5 和 gdb 7.2 进行测试。它不应该是这种编程风格的任何固有限制。
For me your code works well with gdb. Tested with gfortran 4.5.5 and gdb 7.2. It should not be any inherent limitation of that style of programming.