This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: stopping gdb at runtime errors.


I am using gfortran with gdb, and I would like to set a single break
point that stoppped the program just before it crashed due to any
runtime error. That would allow me a last view at my variables before
a any crash.

You can set a breakpoint to _gfortrani_sys_exit: it is the libgfortran function that is called to exit the program when a runtime error is met (libgfortran is the helper library linked with code compile by gfortran). Then go to the frame of interest in your program, as in the following commented example (comment inside <...>):

<we have a code that raises a runtime error>

$ cat a.f90
 integer :: i, j, k, l
 i = 1 ; j = 2 ; k = 3 ; l = 4
 print *, i, j, k, l
 write(*,"(I)") i
 end
$ gfortran a.f90 -g -w -static && ./a.out
          1           2           3           4
At line 4 of file a.f90
Fortran runtime error: Nonnegative width required in format
(I)
 ^

<we run it under gdb, and set a breakpoint to _gfortrani_sys_exit>

(gdb) b _gfortrani_sys_exit
Breakpoint 1 at 0x8048840: file
/home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c,
line 73.

<we run the program>

(gdb) r
Starting program: /tmp/a.out
Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x40000000
          1           2           3           4
At line 4 of file a.f90
Fortran runtime error: Nonnegative width required in format
(I)
 ^

Breakpoint 1, 0x08048840 in *_gfortrani_sys_exit (code=2)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c:73
73      /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c:
No such file or directory.
       in /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c

<we look at the function call backtrace>

(gdb) where
#0  0x08048840 in *_gfortrani_sys_exit (code=2)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c:73
#1  0x08048a79 in *_gfortrani_generate_error (cmp=Variable "cmp" is
not available.
)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/runtime/error.c:477
#2  0x08053a53 in *_gfortrani_format_error (dtp=0xbfe319f4, f=0x0,
   message=0x80b3d60 "Nonnegative width required in format")
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/io/format.c:950
#3  0x08053c0c in *_gfortrani_parse_format (dtp=0xbfe319f4)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/io/format.c:992
#4  0x0804bc79 in data_transfer_init (dtp=0xbfe319f4, read_flag=0)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/io/transfer.c:1788
#5  0x0804832d in MAIN__ () at a.f90:4
#6  0x08048389 in main (argc=Cannot access memory at address 0x0
)
   at /home/fxcoudert/gfortran_nightbuild/trunk/libgfortran/fmain.c:22

<we switch to the frame where the error actually occured (here, in the
MAIN__ program in a.f90, at line 4)>

(gdb) frame 5
#5  0x0804832d in MAIN__ () at a.f90:4
4         write(*,"(I)") i
Current language:  auto; currently fortran

<now, we can inspect the various variables available>

(gdb) p i
$1 = 1
(gdb) p k
$2 = 3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]