Gdb

如何在 GDB 中擷取和中斷 Fortran 90 執行時錯誤?

  • August 19, 2021

是否有可能在 GDB 中出現執行時錯誤時讓 Fortran 90 中斷?我在這裡展示了一個用於簡單常式test.f90的 MWE,它會引發越界錯誤:

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 catchcatch 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 程式碼中,它可以節省大量時間和精力。

我不知道你是否可以這樣做catch,但你可以嘗試設置一個 break on _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)

引用自:https://unix.stackexchange.com/questions/665191