Sample Header Ad - 728x90

How to catch and break on a Fortran 90 runtime error in GDB?

0 votes
1 answer
573 views
Is it possible to have Fortran 90 break at a runtime error in GDB? I show here a MWE for a simple routine *test.f90* which throws an out of bounds error:
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
I compile as follows:
gfortran test.f90 -O0  -fbacktrace  -fbounds-check -g -Wall -w -o test.x
Which, mostly as expected, gives the backtrace here:
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
When I run in GDB I set
catch
and
throw
catchpoints, but upon running GDB still lets the program terminate and I don't have frames to look at.
(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)
How can I get GDB to catch errors like this and start debugging from the culprit line? Obviously it's easy to identify useful break points in smaller scripts but in a larger legacy Fortran code it can save a ton of time and effort.
Asked by tdstoff (3 rep)
Aug 18, 2021, 12:50 PM
Last activity: Aug 19, 2021, 03:47 PM