Search code examples
fortrangdbsignalssigintraise

How to set a breakpoint programmatically in Fortran / Raise a SIGINT in Fortran


I want the debugger (Gdb) attached to my Fortran program to stop under some programmed circumstances. In C/C++ this is easily possible: Set breakpoint in C or C++ code programmatically for gdb on Linux

How to raise such a signal in Fortran?


Solution

  • Just call the same C system function as in the linked answer

      use iso_c_binding, only: c_int
      
      implicit none
    
      interface
        function raise(sig) bind(C, name="raise")
          use iso_c_binding, only: c_int
          integer(c_int) :: raise
          integer(c_int), value :: sig
        end function
      end interface
    
      integer(c_int), parameter :: SIGINT = 2
      
      
      print *, raise(SIGINT)
        
    end
    

    It is probably not easy to avoid entering the SIGINT integer value manually. Consult your signal.h or man raise. The value 2 is for POSIX. On POSIX systems you can also use SIGTRAP = 5.