3

Is there a command in gdb that I can run to make it automatically stop (break) at the first occurrence of assignment of NaN or inf to a variable, that is when RHS of an executed assignment is NaN or inf (for example when I divide something by zero and try to assign the result to a variable)?

DeLorean88
  • 547
  • 2
  • 5
  • 15
  • It is possible with `feenableexcept` call, but it wil break a bit earlier, when your try to divide by zero. – ks1322 May 21 '20 at 13:19
  • I can't seem to find an example of code using `feenableexcept` online. Care to briefly explain how one would use it? – DeLorean88 May 21 '20 at 13:35

2 Answers2

1

With gcc on Linux you can turn floating-point exceptions to SIGFPE signal. You can call feenableexcept(FE_DIVBYZERO) to catch all subsequent floating point divisions by zero in your code. If you run this code in gdb it will stop on SIGFPE signal. This is default behavior for most other signals as well. This code was taken and modified from here: https://stackoverflow.com/a/2949452/72178

#define _GNU_SOURCE
#include <fenv.h>

int main(void) {
    double x, y, z;
    feenableexcept(FE_DIVBYZERO);

    x = 1;
    y = 0;
    z = x / y;

    return 0;
}

gdb will stop on division by zero:

$ gdb -q ./a.out
Reading symbols from ./a.out...
(gdb) r
Program received signal SIGFPE, Arithmetic exception.
0x0000000000401153 in main () at 1.c:10
10      z = x / y;
(gdb) 
ks1322
  • 33,961
  • 14
  • 109
  • 164
0

Under many aspects, NaN is a value like any other. Therefore, I don't believe that it is possible to have this generate a special event in every case.

Depending on the architecture you are programming for, there may be a division-by-zero event which might be quite easy to catch. Note however that divisions by zero are not the only way to produce a NaN value. (Again depending on the architecture) performing another operation with a NaN operant may raise an event/exception (e. g., "invalid operation"). This may point into some direction to generate a gdb breakpoint by adding some code to the system under debug (if that is an option).

Beside triggers from exception events, conditional watchpoints may be an option: If you aren't just looking for NaN values anywhere, a watchpoint on a certain variable, which is limited to a condition can be a good solution.

Under certain circumstances, both ideas won't help you further: If, for example, the NaN appears at some places in a buggy (or unstable) numeric implementation in HPC context, additional numeric operations and conditional breakpoints/watchpoints normally aren't feasible because the overhead will "choke" the calculation before it even generated NaN values.

HelpingHand
  • 1,294
  • 11
  • 27
  • How would I catch division by zero in gdb (for example, in Linux x64)? – DeLorean88 May 21 '20 at 13:22
  • I've been working mostly with embedded architectures recently, so I'm afraid that I don't have a good suggestion where to find that information. But with Linux/x64 there is likely somebody around who can help you. I'd suggest to add this information to your question. – HelpingHand May 21 '20 at 13:24