How to stop a fortran program abnormally

When an exception is thrown, I would like to terminate my program abnormally. Right now, when an exception occurs, the statement write

with the explanatory statement is called and then the statement is called stop

.

I am debugging a program with idb

(intel debugger), when an exception occurs, I get a statement write

, but it idb

treats the program as terminated normally. I would like the program to abnormally abort when an exception is thrown so that I can look into memory with help backtrace

at the point where the exception occurred.

I tried changing stop

to stop 1

so that a nonzero value is returned, but that doesn't work

EDIT:

I have implemented the solution in one of the answers:

 interface
    subroutine abort() bind(C, name="abort")
    end subroutine
 end interface

 print *,1
 call abort()
 print *,2
end

      

with this solution, I still don't get a backtrace when I use it ifort 13.0.1

, but it works fine with ifort 14.0.2

.

I used idb

instead gdb

, because often the latter cannot read the values ​​of the allocated arrays into fortran

.

+4


source to share


4 answers


There are non-standard extensions for this. Gfortran uses backtrace()

to print it anywhere, to see Intel equivalent. A wander95 fooobar.com/questions/2228347 / ... .

In ifort and gfortran, you can call a subroutine abort()

and you will get a backtrace if you used -traceback

(Intel) or -g -fbacktrace

(gfortran).



You can also call C abort()

directly using C compatibility (also non-standard and may not work in all circumstances):

  interface
    subroutine abort() bind(C, name="abort")
    end subroutine
  end interface

  print *,1
  call abort()
  print *,2
end

      

+3


source


Fortran 2008 introduced the operator ERROR STOP

. It is mainly used for Coarray Fortran programs to trigger error breaks on all images.



+3


source


Found this old question by accident. If you want abnormal termination with the Intel compiler, you can use the tracebackqq routine . The sequence of calls can be:

     call TRACEBACKQQ(string=string,user_exit_code=user_exit_code)

      

To quote the manual:

Provides trace information. Uses the Intel® Fortran Runtime Library Trace Tool to create a stack trace showing the program call stack as it appeared during a TRACEBACKQQ () call

+1


source


I have never used idb

, I have used gdb

, so this may not work. I just put the read statement at the point of the error, so the program stops and waits for input. Then I can use CTRL-C, which forces gdb

execution to pause, from which I can get a backtrace, move up and down the stack, view variables, etc.

0


source







All Articles