Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping the debugger when a NaN floating point number is produced without a code change

Tags:

c++

c

nan

sigfpe

I read this and this. The quintessence is that one can throw a SIGFPE if a nan is produced by including fenv.h and enabling all floating point exceptions but FE_INEXACT by feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);

Thus, the code changes form

int main () {
   double dirty = 0.0;
   double nanvalue = 0.0/dirty;
   return 0;
 }

to

 #include <fenv.h>
 int main () {
     feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);  // Enable all floating point exceptions but FE_INEXACT
     double dirty = 0.0;
     double nanvalue = 0.0/dirty;
     return 0;
 }

This works fine, but you have to change the code. I have the problem, that in a huge c and c++ code base, somewhere a nan is produced and I don't know where. It is not an option to apply the above change to hunderts of files and track the error.

Is there a way to enable the all floating point exceptions, WITHOUT a code change? Is there a compile option I am not aware of?

We use the intel icc version 15.0.3 compiler.

like image 737
schorsch312 Avatar asked Sep 12 '17 07:09

schorsch312


1 Answers

No matter how many files your code spans, you only need to add feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT) once only, at the first line of your main() function.

It will enable the exceptions for your whole program until you disable the exceptions by calling another function such as fedisableexcept().

like image 174
Bernard Avatar answered Oct 23 '22 20:10

Bernard