Skip to content

Commit

Permalink
Enable FPE tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
scemama committed Jun 29, 2023
1 parent 5228c28 commit db13db8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ AC_ARG_ENABLE([hpc],
AS_IF([test "x$enable_hpc" = "xyes"],
[AC_DEFINE([HAVE_HPC], [1], [Activate HPC routines])])


AC_ARG_ENABLE([fpe],
[AS_HELP_STRING([--enable-fpe],
[Enable floating-point exceptions])],
[enable_fpe=$enableval],
[enable_fpe=no])

AS_IF([test "x$enable_fpe" = "xyes"],
[AC_DEFINE([HAVE_FPE], [1], [Activate floating-point exceptions])])



AC_ARG_ENABLE([doc],
[AS_HELP_STRING([--disable-doc],
[Disable documentation])],
Expand Down
36 changes: 36 additions & 0 deletions org/qmckl_numprec.org
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ int main() {
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_FPE
#define _GNU_SOURCE
#include <fenv.h>
#include <signal.h>
#include <stdio.h>
#include <execinfo.h>


#define MAX_BACKTRACE_SIZE 100

void floatingPointExceptionHandler(int signal) {
void* backtraceArray[MAX_BACKTRACE_SIZE];
int backtraceSize = backtrace(backtraceArray, MAX_BACKTRACE_SIZE);
char** backtraceSymbols = backtrace_symbols(backtraceArray, backtraceSize);

// Print the backtrace
for (int i = 0; i < backtraceSize; ++i) {
printf("[%d] %s\n", i, backtraceSymbols[i]);
}

// Clean up the memory used by backtrace_symbols
free(backtraceSymbols);

exit(EXIT_FAILURE);
}

static void __attribute__ ((constructor))
trapfpe ()
{
/* Enable some exceptions. At startup all exceptions are masked. */

feenableexcept (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
signal(SIGFPE, floatingPointExceptionHandler);
}
#endif

#include "qmckl.h"
#include "qmckl_context_private_type.h"
#+end_src
Expand Down

0 comments on commit db13db8

Please sign in to comment.