- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <math.h>
- #include <fenv.h>
- #include <stdexcept>
- #include <iostream>
- static sigjmp_buf jmp_env;
- static volatile siginfo_t saved_siginfo;
- static void sigfpe_handler(int num, siginfo_t *info, void *ucontext)
- {
- (void)num;
- (void)ucontext;
- (void)feclearexcept(FE_ALL_EXCEPT);
- (void)memcpy((void *)&saved_siginfo, info, sizeof(saved_siginfo));
- #if 0
- siglongjmp(jmp_env, 1);
- #else
- throw std::invalid_argument( "you violated the fp math rules" );
- #endif
- }
- int main(int ac, char *av[])
- {
- struct sigaction action;
- int ret;
- int i = 0, j = 0;
- (void)puts("#start.");
- (void)memset(&action, 0, sizeof action);
- action.sa_sigaction = sigfpe_handler;
- (void)sigemptyset(&action.sa_mask);
- action.sa_flags = SA_SIGINFO;
- (void)sigaction(SIGFPE, &action, NULL);
- try
- {
- ret = sigsetjmp(jmp_env, 1);
- if (ret) {
- (void)puts("#got_signal");
- }
- else
- {
- (void)puts("#begin_math");
- i = i / j;
- (void)puts("#end_math");
- }
- }
- catch( const std::invalid_argument &e )
- {
- std::cout << std::string("#got_exception ")+e.what()+" \n";
- }
- (void)printf("i=%d, j=%d\n", i, j);
- (void)puts("#end.");
- return EXIT_SUCCESS;
- }
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)
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.