'GDB not catching SIGFPE

I am using gcc (11.2.0-mingw-w64ucrt-9.0.0-r5), trying to debug Fortran code with CodeBlocks on Windows 10.

For some reason gdb is not catching / halting on SIGFPEs. I don't remember having this issue on previous installs (I changed jobs a few months ago and unfortunately can't check anymore).

For the Debug build, I am using the following options :

-g -fcheck=all -fcheck=bounds -fbacktrace -Og -fno-automatic -fdefault-real-8 -fdefault-double-8 -fno-align-commons -Wline-truncation -Wsurprising -ffpe-trap=invalid,zero,overflow -march=native -fmax-errors=3

Here is the CodeBlocks IDE console output when calling GDB (to see gdb arguments) :

Starting debugger: D:\Logiciels\GCC\bin\gdb.exe -nx -fullname -quiet  -args D:/Work/Baro/bin/Debug/BARO.exe
done

And then the rest of the console output (removing version info etc)

[debug]>>>>>>cb_gdb:
[debug]> set confirm off

Debugger name and version: GNU gdb (GDB for MinGW-W64 x86_64, built by Brecht Sanders) 11.2

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 200
[debug]>>>>>>cb_gdb:
[debug]> set new-console on
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> directory D:/Work/Baro/
[debug]Source directories searched: D:/Work/Baro;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> py print(1+2)
[debug]3
[debug]>>>>>>cb_gdb:
[debug]> set $fortran_script_dir = 'D:\Logiciels\CodeBlocks\share\codeblocks/images/fortranproject/'
[debug]>>>>>>cb_gdb:
[debug]> source D:\Logiciels\CodeBlocks\share\codeblocks/images/fortranproject/gdb_fortran_extensions_v1.0.py
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: D:\Work\Baro\bin\Debug\BARO.exe 

Child process PID: 21196

[debug][New Thread 21196.0x1408]
[debug][New Thread 21196.0x11f4]
[debug][New Thread 21196.0x51c4]
[debug][Thread 21196.0x51c4 exited with code 2]
[debug][Thread 21196.0x11f4 exited with code 2]
[debug][Thread 21196.0x1408 exited with code 2]
[debug][Inferior 1 (process 21196) exited with code 02]
[debug]>>>>>>cb_gdb:

[Inferior 1 (process 21196) exited with code 02]

[debug]> quit

Debugger finished with status 0

I see some "exit with code 02", which looks like it matches a SIGFPE error ? But GDB / the IDE never halted execution or pointed me where there was an issue (like I'd expect), it is just terminating the program, which is useless.

Am I missing something ? Is there some additional argument to pass to gdb, or something else ?

Edit : I have tried the gdb command info signal SIGFPE, which returns :

[debug]> info signal SIGFPE
[debug]Signal        Stop   Print   Pass to program Description
[debug]SIGFPE        Yes    Yes Yes     Arithmetic exception

I have also tried handle SIGFPE stop nopass, handle SIGFPE nostop nopass, and handle SIGFPE nostop pass (issuing them before and after connection).

The console replies accordingly, but I don't see any change in debugging behavior.

Edit 2 : Hmm... I've tried inserting a line early in the beginning that I know should result in a SIGFPE (dividing by a variable that is equal to zero), and with handle SIGFPE stop nopass, GDB does halt execution and points me toward the culprit.

(I hope posting in the Fortran section is the right thing to do, apologies if another section would be more suitable)



Solution 1:[1]

Ok, so there were a few things.

  1. Exit code 02 is actually not a SIGFPE (although I did have the same kind of issue with SIGFPE). It looks like SIGFPEs are exit code 03. In my case, exit code 02 was due to an out-of-bound error (trying to access a table out of bounds).

  2. Adding handle SIGFPE stop nopass to the additional gdb commands did help me with the SIGFPE issue. My IDE does now seem to always break on these (even though it should have before already ?)

  3. For out of bounds, the -fcheck=all -fcheck=bounds was logically leading to the exit - as it should.

I tried removing it, to see if gdb would now catch the issue (instead of it maybe having been already "terminated" due to these flags ?), but it didn't change anything.

The solution for this issue was to add break exit to the additional gdb commands, which did then correctly halted the program and pointed me to the exact line where the out of bound occurred.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Parker Lewis