This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: stopping gdb at runtime errors.
- From: "François-Xavier Coudert" <fxcoudert at gmail dot com>
- To: "Niels L. Ellegaard" <niels dot ellegaard at gmail dot com>
- Cc: fortran at gcc dot gnu dot org
- Date: Mon, 26 Mar 2007 15:54:57 +0200
- Subject: Re: stopping gdb at runtime errors.
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=XUTo0ipbhFU4NKwxAqCntNYkA7a/h/y7h4TRFXl73oYqje16hWquVNDjzvpCFD/H00yKkGgQFmmorKFT/+fe9rbJbfKv6odj3UOpS3dMoK5rjEwRhIrdWD6W50vR8Z2SV8YRkr1M5z3lwZWcswo3KQpoYVMv/bY8/lK7j/z4xpw=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=BiZxIhoWLxK2gO21nI/fYUft+R05pmBG0QY18p0MC3ysS4+9taeeiKfTWhPBODGUNa6W3RwtlJiCKG2ZM1FnSjyoxGfeIMgC3oeQMIRwVibNljraHvpMU0WMpF9Np3KP7Ngpg46bdsIyYPdju7F8yQI1/7wQudC9BT5x7qIKmLk=
- References: <87tzw8nmqv.fsf@gmail.com>
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