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

1

6

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.

Proceed answered 12/9, 2017 at 7:30 Comment(3)
You just need to apply the code change once, at the first line of main() right?Palladin
I will try it. I thought that I need to include it everywhere.Proceed
As feenableexcept() changes the behaviour of the run-time system it needs to be called once only to take global effect.Dowser
P
4

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().

Palladin answered 12/9, 2017 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.