Usage
- Environment variable configuration
The print information and check items of floating-point exception detection are configured through environment variables.
- The print information is configured using the NAN_REPORT_CONFIG environment variable.
error/warning. error indicates that the system exits directly if an exception occurs. warning indicates that the system generates a warning if an exception occurs and the process continues.
detail/brief: detail indicates that detailed stack information is printed, and the file name and line number are parsed. brief indicates that only the memory address in the stack is printed, and no parsing is performed.
Example: export NAN_REPORT_CONFIG=error+detail.
- The check items are configured using the NAN_EXCEPT_CONFIG environment variable.
By default, all exceptions (0x0000000F) except FE_INEXACT are enabled. You can configure the bitmap corresponding to each exception.
Example: export NAN_EXCEPT_CONFIG=0x00000003 indicates that only FE_INVALID and FE_DIVBYZERO exceptions are detected.
- The print information is configured using the NAN_REPORT_CONFIG environment variable.
- Compilation option
- Fortran applications: The -fp-detect-exceptions -g option is added for compilation.
- C/C++ applications: The -finstrument-functions -lfpe -g option is added for compilation.
- Only the functions specified by users can be detected. To check whether NAN occurs before a position, use the compilation option -lfpe -g and call fpe_nan_check () at the position to be checked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//main.cpp #include <iostream> void foo() { float x = 1; float y = 0; float z = x/y; std::cout << "divide_by_zero: 1/0 =" << z << std::endl; } int main() { foo(); return 0; }
Compilation commands and result are as follows:
1 2 3 4 5 6 7 8 9
$ export NAN_REPORT_CONFIG=error+detail $ clang++ -finstrument-functions -lfpe main.cpp -o a.out -g $ ./a.out divide_by_zero: 1/0 =inf Warning: ieee_divide_by_zero is signaling ##################backtrace################### ./a.out(+0xbd0) [0xaaaab69fbbd0] foo() ../main.cpp:8 ./a.out(+0xc34) [0xaaaab69fbc34] main ../main.cpp:11 ##############################################
- Remarks
The printed stack source code line number may be offset downwards by one line. The reason is that the next assembly instruction address is returned when the bottom-layer library function backtrace is parsed. The instruction may come from the current source code line or the next line. You need to check whether the offset exists based on the printed result.