pastebin - collaborative debugging tool
rovema.kpaste.net RSS


sig_fpu_trigger1.cpp
Posted by Anonymous on Wed 2nd Feb 2022 10:15
raw | new post
view followups (newest first): sig_fpu_trigger1.cpp by Anonymous and sig_fpu_trigger1.cpp.ltrace.log by Anonymous
modification of post by Anonymous (view diff)

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <math.h>
  7. #include <fenv.h>
  8.  
  9. #include <stdexcept>
  10. #include <iostream>
  11.  
  12.  
  13. static sigjmp_buf jmp_env;
  14. static volatile siginfo_t saved_siginfo;
  15.  
  16. static void sigfpe_handler(int num, siginfo_t *info, void *ucontext)
  17. {
  18.         (void)num;
  19.         (void)ucontext;
  20.  
  21.         (void)feclearexcept(FE_ALL_EXCEPT);
  22.         (void)memcpy((void *)&saved_siginfo, info, sizeof(saved_siginfo));
  23.  
  24. #if 0
  25.         siglongjmp(jmp_env, 1);
  26. #else
  27.         throw std::invalid_argument( "you violated the fp math rules" );
  28. #endif
  29. }
  30.  
  31. int main(int ac, char *av[])
  32. {
  33.         struct sigaction action;
  34.         int ret;
  35.         int i = 0, j = 0;
  36.  
  37.         (void)puts("#start.");
  38.        
  39.         (void)memset(&action, 0, sizeof action);
  40.  
  41.         action.sa_sigaction = sigfpe_handler;
  42.         (void)sigemptyset(&action.sa_mask);
  43.         action.sa_flags = SA_SIGINFO;
  44.         (void)sigaction(SIGFPE, &action, NULL);
  45.        
  46.        
  47.         try
  48.         {
  49.                 ret = sigsetjmp(jmp_env, 1);
  50.                 if (ret) {
  51.                         (void)puts("#got_signal");
  52.                 }
  53.                 else
  54.                 {
  55.                         (void)puts("#begin_math");
  56.                         i = i / j;
  57.                         (void)puts("#end_math");
  58.                 }
  59.         }
  60.         catch( const std::invalid_argument &e )
  61.         {
  62.                 std::cout << std::string("#got_exception ")+e.what()+" \n";
  63.         }
  64.  
  65.         (void)printf("i=%d, j=%d\n", i, j);
  66.  
  67.         (void)puts("#end.");
  68.         return EXIT_SUCCESS;
  69. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at