Fortran 90 是否有可能因 GDB 中的运行时错误而中断?我在这里展示了一个简单例程的 MWE测试.f90这会引发越界错误:
program main
implicit none
integer :: i
integer, parameter :: npt = 10
real, dimension(npt) :: A
real :: B
!
do i = 1,npt
A(i) = i
enddo
B = A(npt+1)
!
end program main
我编译如下:
gfortran test.f90 -O0 -fbacktrace -fbounds-check -g -Wall -w -o test.x
基本上如预期的那样,这里给出了回溯:
Error termination. Backtrace:
#0 0x7ffff7a0f2ed in ???
#1 0x7ffff7a0fed5 in ???
#2 0x7ffff7a102a7 in ???
#3 0x55555555480e in MAIN__
at ~/test.f90:11
#4 0x555555554844 in main
at ~/test.f90:13
当我在 GDB 中运行时,我设置了catch catch
捕获catch throw
点,但是在运行 GDB 时仍然让程序终止,并且我没有框架可供查看。
(gdb) catch catch
Catchpoint 1 (catch)
(gdb) catch throw
Catchpoint 2 (throw)
(gdb) r test.x
`~/test.x' has changed; re-reading symbols.
Starting program: ~/test.x test.x
warning: Probes-based dynamic linker interface failed.
Reverting to original interface.
[Inferior 1 (process 3769) exited normally]
(gdb) where
No stack.
(gdb) bt
No stack.
(gdb)
我怎样才能让 GDB 捕获这样的错误并从罪魁祸首开始调试?显然,在较小的脚本中识别有用的断点很容易,但在较大的遗留 Fortran 代码中,它可以节省大量的时间和精力。
答案1
我不知道你是否可以用 来做到这一点catch
,但你可以尝试在 上设置一个中断_gfortran_runtime_error_at
,然后从回溯中识别罪魁祸首行(然后在那里设置一个断点等):
$ gdb -q ./test.x
Reading symbols from ./test.x...done.
(gdb) br _gfortran_runtime_error_at
Breakpoint 1 at 0x1030
(gdb) r
Starting program: /tmp/test.x
Breakpoint 1, 0x00007ffff7d5e670 in _gfortran_runtime_error_at ()
from /lib/x86_64-linux-gnu/libgfortran.so.5
(gdb) bt
#0 0x00007ffff7d5e670 in _gfortran_runtime_error_at ()
from /lib/x86_64-linux-gnu/libgfortran.so.5
#1 0x00005555555551fa in MAIN__ () at test.f90:11
(gdb) fram 1
#1 0x00005555555551fa in MAIN__ () at test.f90:11
11 B = A(npt+1)