PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) { PetscFunctionBegin; if (flag == PETSC_FP_TRAP_ON) { handle_sigfpes(_ON,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0); } else { handle_sigfpes(_OFF,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0); } _trapmode = flag; PetscFunctionReturn(0); }
static void ieee0(Void) { int i; for(i=1; i<=4; i++){ sigfpe_[i].count = 1000; sigfpe_[i].trace = 1; sigfpe_[i].repls = _USER_DETERMINED; } sigfpe_[1].repls = _ZERO; /* underflow */ handle_sigfpes( _ON, _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID, ieeeuserhand,_ABORT_ON_ERROR,ieeeuserhand2); }
void FPinit(void) { union fpc_csr csr; int i; for(i=1; i<=4; i++) { sigfpe_[i].repls = _USER_DETERMINED; sigfpe_[i].abort = 2; } handle_sigfpes(_ON, _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID, trapFPE, _ABORT_ON_ERROR, 0); }
static void ieee0(bool enableTrap) { TEUCHOS_TEST_FOR_EXCEPTION( enableTrap == false, std::logic_error, "Error, don't know how to turn off trap for MIPS!" ); int i; for(i=1; i<=4; i++){ sigfpe_[i].count = 1000; sigfpe_[i].trace = 1; sigfpe_[i].repls = _USER_DETERMINED; } sigfpe_[1].repls = _ZERO; /* underflow */ handle_sigfpes( _ON, _EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID, ieeeuserhand,_ABORT_ON_ERROR,ieeeuserhand2); }
void doexceptions(void) { #include <sigfpe.h> #include <signal.h> int hs[] = { SIGILL, SIGFPE, SIGTRAP, SIGEMT, SIGSYS }; int onoff,en_mask,abort_action,i; onoff = _DEBUG; en_mask = _EN_UNDERFL | _EN_OVERFL | _EN_DIVZERO | _EN_INVALID | _EN_INT_OVERFL; abort_action = _ABORT_ON_ERROR; handle_sigfpes(onoff,en_mask,user_routine,abort_action,abort_routine); for(i=0; (i<asize(hs)); i++) signal(hs[i],handle_signals); }
void Foam::sigFpe::set(const bool verbose) { if (oldAction_.sa_handler) { FatalErrorIn ( "Foam::sigFpe::set()" ) << "Cannot call sigFpe::set() more than once" << abort(FatalError); } if (env("FOAM_SIGFPE")) { bool supported = false; # ifdef LINUX_GNUC supported = true; feenableexcept ( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); struct sigaction newAction; newAction.sa_handler = sigHandler; newAction.sa_flags = SA_NODEFER; sigemptyset(&newAction.sa_mask); if (sigaction(SIGFPE, &newAction, &oldAction_) < 0) { FatalErrorIn ( "Foam::sigFpe::set()" ) << "Cannot set SIGFPE trapping" << abort(FatalError); } # elif defined(sgiN32) || defined(sgiN32Gcc) supported = true; sigfpe_[_DIVZERO].abort=1; sigfpe_[_OVERFL].abort=1; sigfpe_[_INVALID].abort=1; sigfpe_[_DIVZERO].trace=1; sigfpe_[_OVERFL].trace=1; sigfpe_[_INVALID].trace=1; handle_sigfpes ( _ON, _EN_DIVZERO | _EN_INVALID | _EN_OVERFL, 0, _ABORT_ON_ERROR, NULL ); # endif if (verbose) { if (supported) { Info<< "sigFpe : Enabling floating point exception trapping" << " (FOAM_SIGFPE)." << endl; } else { Info<< "sigFpe : Floating point exception trapping" << " - not supported on this platform" << endl; } } } if (env("FOAM_SETNAN")) { bool supported = false; # ifdef LINUX_GNUC supported = true; // Set our malloc __malloc_hook = Foam::sigFpe::nanMallocHook_; # endif if (verbose) { if (supported) { Info<< "SetNaN : Initialising allocated memory to NaN" << " (FOAM_SETNAN)." << endl; } else { Info<< "SetNaN : Initialise allocated memory to NaN" << " - not supported on this platform" << endl; } } } }
static void fpe_reset(Sigfunc *handler) { /* Reset the exception handling machinery, and reset the signal * handler for SIGFPE to the given handler. */ /*-- IRIX -----------------------------------------------------------------*/ #if defined(sgi) /* See man page on handle_sigfpes -- must link with -lfpe * My usage doesn't follow the man page exactly. Maybe somebody * else can explain handle_sigfpes to me.... * cc -c -I/usr/local/python/include fpectlmodule.c * ld -shared -o fpectlmodule.so fpectlmodule.o -lfpe */ #include <sigfpe.h> typedef void user_routine (unsigned[5], int[2]); typedef void abort_routine (unsigned long); handle_sigfpes(_OFF, 0, (user_routine *)0, _TURN_OFF_HANDLER_ON_ERROR, NULL); handle_sigfpes(_ON, _EN_OVERFL | _EN_DIVZERO | _EN_INVALID, (user_routine *)0, _ABORT_ON_ERROR, NULL); PyOS_setsig(SIGFPE, handler); /*-- SunOS and Solaris ----------------------------------------------------*/ #elif defined(sun) /* References: ieee_handler, ieee_sun, ieee_functions, and ieee_flags man pages (SunOS or Solaris) cc -c -I/usr/local/python/include fpectlmodule.c ld -G -o fpectlmodule.so -L/opt/SUNWspro/lib fpectlmodule.o -lsunmath -lm */ #include <math.h> #ifndef _SUNMATH_H extern void nonstandard_arithmetic(void); extern int ieee_flags(const char*, const char*, const char*, char **); extern long ieee_handler(const char*, const char*, sigfpe_handler_type); #endif char *mode="exception", *in="all", *out; (void) nonstandard_arithmetic(); (void) ieee_flags("clearall",mode,in,&out); (void) ieee_handler("set","common",(sigfpe_handler_type)handler); PyOS_setsig(SIGFPE, handler); /*-- HPUX -----------------------------------------------------------------*/ #elif defined(__hppa) || defined(hppa) /* References: fpsetmask man page */ /* cc -Aa +z -c -I/usr/local/python/include fpectlmodule.c */ /* ld -b -o fpectlmodule.sl fpectlmodule.o -lm */ #include <math.h> fpsetdefaults(); PyOS_setsig(SIGFPE, handler); /*-- IBM AIX --------------------------------------------------------------*/ #elif defined(__AIX) || defined(_AIX) /* References: fp_trap, fp_enable man pages */ #include <fptrap.h> fp_trap(FP_TRAP_SYNC); fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); PyOS_setsig(SIGFPE, handler); /*-- DEC ALPHA OSF --------------------------------------------------------*/ #elif defined(__alpha) && defined(__osf__) /* References: exception_intro, ieee man pages */ /* cc -c -I/usr/local/python/include fpectlmodule.c */ /* ld -shared -o fpectlmodule.so fpectlmodule.o */ #include <machine/fpu.h> unsigned long fp_control = IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; ieee_set_fp_control(fp_control); PyOS_setsig(SIGFPE, handler); /*-- DEC ALPHA LINUX ------------------------------------------------------*/ #elif defined(__alpha) && defined(linux) #include <asm/fpu.h> unsigned long fp_control = IEEE_TRAP_ENABLE_INV | IEEE_TRAP_ENABLE_DZE | IEEE_TRAP_ENABLE_OVF; ieee_set_fp_control(fp_control); PyOS_setsig(SIGFPE, handler); /*-- DEC ALPHA VMS --------------------------------------------------------*/ #elif defined(__ALPHA) && defined(__VMS) IEEE clrmsk; IEEE setmsk; clrmsk.ieee$q_flags = IEEE$M_TRAP_ENABLE_UNF | IEEE$M_TRAP_ENABLE_INE | IEEE$M_MAP_UMZ; setmsk.ieee$q_flags = IEEE$M_TRAP_ENABLE_INV | IEEE$M_TRAP_ENABLE_DZE | IEEE$M_TRAP_ENABLE_OVF; sys$ieee_set_fp_control(&clrmsk, &setmsk, 0); PyOS_setsig(SIGFPE, handler); /*-- HP IA64 VMS --------------------------------------------------------*/ #elif defined(__ia64) && defined(__VMS) PyOS_setsig(SIGFPE, handler); /*-- Cray Unicos ----------------------------------------------------------*/ #elif defined(cray) /* UNICOS delivers SIGFPE by default, but no matherr */ #ifdef HAS_LIBMSET libmset(-1); #endif PyOS_setsig(SIGFPE, handler); /*-- FreeBSD ----------------------------------------------------------------*/ #elif defined(__FreeBSD__) fpresetsticky(fpgetsticky()); fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL); PyOS_setsig(SIGFPE, handler); /*-- Linux ----------------------------------------------------------------*/ #elif defined(linux) #ifdef __GLIBC__ #include <fpu_control.h> #else #include <i386/fpu_control.h> #endif #ifdef _FPU_SETCW { fpu_control_t cw = 0x1372; _FPU_SETCW(cw); } #else __setfpucw(0x1372); #endif PyOS_setsig(SIGFPE, handler); /*-- Microsoft Windows, NT ------------------------------------------------*/ #elif defined(_MSC_VER) /* Reference: Visual C++ Books Online 4.2, Run-Time Library Reference, _control87, _controlfp */ #include <float.h> unsigned int cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW; (void)_controlfp(0, cw); PyOS_setsig(SIGFPE, handler); /*-- Give Up --------------------------------------------------------------*/ #else fputs("Operation not implemented\n", stderr); #endif }
int main(int argc, char **argv) { iso_primary_descriptor_type ipd; char *dev = default_dev; char vendor[9],model[17],rev[5]; unsigned char reply[1024]; char tmpstr[255]; int replylen=sizeof(reply); int trackno = 0; int info_only = 0; unsigned char *buffer; int buffersize = READBLOCKS*BLOCKSIZE; int start,stop,imagesize=0,tracksize=0; int counter = 0; long readsize = 0; long imagesize_bytes = 0; int drive_block_size, init_bsize; int force_mode = 0; int scanbus_mode = 0; int dump_start, dump_count; MD5_CTX *MD5; char digest[16],digest_text[33]; int md5_mode = 0; int opt_index = 0; int audio_track = 0; int readblocksize = BLOCKSIZE; int file_format = AF_FILE_AIFFC; #ifdef IRIX CDPARSER *cdp = CDcreateparser(); CDFRAME cdframe; #endif int dev_type; int i,c,o; int len; int start_time,cur_time,kbps; if (rcsid); MD5 = malloc(sizeof(MD5_CTX)); buffer=(unsigned char*)malloc(READBLOCKS*AUDIOBLOCKSIZE); if (!buffer || !MD5) die("No memory"); if (argc<2) die("parameter(s) missing\n" "Try '%s --help' for more information.\n",PRGNAME); /* parse command line parameters */ while(1) { if ((c=getopt_long(argc,argv,"SMmvhid:",long_options,&opt_index)) == -1) break; switch (c) { case 'a': file_format=AF_FILE_AIFF; break; case 'A': file_format=AF_FILE_AIFFC; break; case 'v': verbose_mode=1; break; case 'h': p_usage(); break; case 'd': dev=strdup(optarg); break; case 't': if (sscanf(optarg,"%d",&trackno)!=1) trackno=0; break; case 'i': info_only=1; break; case 'c': if (sscanf(optarg,"%d,%d",&dump_start,&dump_count)!=2) die("invalid parameters"); dump_mode=1; break; #ifdef IRIX case 'C': if (sscanf(optarg,"%d,%d",&dump_start,&dump_count)!=2) die("invalid parameters"); dump_mode=2; break; #endif case 'f': if (sscanf(optarg,"%d",&force_mode)!=1) die("invalid parameters"); if (force_mode<1 || force_mode >2) { die("invalid parameters"); } break; case 'm': md5_mode=1; break; case 'M': md5_mode=2; break; case 's': audio_mode=1; break; case 'S': scanbus_mode=1; break; case 'V': printf(PRGNAME " " VERSION " " HOST_TYPE "\nCopyright (c) Timo Kokkonen, 1997-1998.\n\n"); exit(0); break; case '?': break; default: die("error parsing parameters"); } } if (!info_only) { if (md5_mode==2) outfile=fopen("/dev/null","w"); else outfile=fopen(argv[optind],"w"); if (!outfile) { if (argv[optind]) die("cannot open output file '%s'",argv[optind]); info_only=1; } } printf("readiso(9660) " VERSION "\n"); /* open the scsi device */ if (scsi_open(dev)) die("error opening scsi device '%s'",dev); if (scanbus_mode) { printf("\n"); scan_bus(); exit(0); } memset(reply,0,sizeof(reply)); if ((dev_type=inquiry(vendor,model,rev))<0) die("error accessing scsi device"); if (verbose_mode) { printf("device: %s\n",dev); printf("Vendor: %s\nModel: %s\nRevision: %s\n",vendor,model,rev); } if ( (dev_type&0x1f) != 0x5 ) { die("Device doesn't seem to be a CD-ROM!"); } #ifdef IRIX if (strcmp(vendor,"TOSHIBA")) { warn("NOTE! Audio track reading probably not supported on this device.\n"); } #endif test_ready(); if (test_ready()!=0) { sleep(2); if (test_ready()!=0) die("device not ready"); } fprintf(stderr,"Initializing...\n"); if (audio_mode) { #ifdef IRIX audioport=ALopenport("readiso","w",0); if (!audioport) { warn("Cannot initialize audio."); audio_mode=0; } #else audio_mode=0; #endif } #ifdef IRIX /* Make sure we get sane underflow exception handling */ sigfpe_[_UNDERFL].repls = _ZERO; handle_sigfpes(_ON, _EN_UNDERFL, NULL, _ABORT_ON_ERROR, NULL); #endif /* set_removable(1); */ #if 0 replylen=255; if (mode_sense10(reply,&replylen)==0) { printf("replylen=%d blocks=%d blocklen=%d\n",replylen, V3(&reply[8+1]),V3(&reply[8+5])); PRINT_BUF(reply,replylen); } replylen=255; /* sizeof(reply); */ if (mode_sense(reply,&replylen)==0) { printf("replylen=%d blocks=%d blocklen=%d\n",replylen, V3(&reply[4+1]),V3(&reply[4+5])); PRINT_BUF(reply,replylen); } #endif if (dump_mode==2) init_bsize=AUDIOBLOCKSIZE; else init_bsize=BLOCKSIZE; start_stop(0); if ( (drive_block_size=get_block_size()) < 0 ) { warn("cannot get current block size"); drive_block_size=init_bsize; } if (drive_block_size != init_bsize) { mode_select(init_bsize,(dump_mode==2?0x82:0x00)); drive_block_size=get_block_size(); if (drive_block_size!=init_bsize) warn("cannot set drive block size."); } start_stop(1); if (dump_mode && !info_only) { #ifdef IRIX CDFRAME buf; if (dump_mode==2) { if (cdp) { CDaddcallback(cdp, cd_audio, (CDCALLBACKFUNC)playaudio, 0); } else die("No audioparser"); } #endif fprintf(stderr,"Dumping %d sector(s) starting from LBA=%d\n", dump_count,dump_start); for (i=dump_start;i<dump_start+dump_count;i++) { len=buffersize; read_10(i,1,buffer,&len); if (len<init_bsize) break; #ifdef IRIX if (dump_mode==2) { memcpy(&buf,buffer,CDDA_BLOCKSIZE); CDparseframe(cdp,&buf); } #endif fwrite(buffer,len,1,outfile); fprintf(stderr,"."); } fprintf(stderr,"\ndone.\n"); goto quit; } fprintf(stderr,"Reading disc TOC..."); replylen=sizeof(reply); read_toc(reply,&replylen,verbose_mode); printf("\n"); if (trackno==0) { /* try to find first data track */ for (i=0;i<(reply[3]-reply[2]+1);i++) { o=4+i*8; if (reply[o+1]&DATA_TRACK) { trackno=i+1; break; } } if (trackno==0) die("No data track(s) found."); } fprintf(stderr,"Reading track %d...\n",trackno); if ( (trackno < reply[2]) || (trackno > reply[3]) ) die("Invalid track specified."); if ( ((reply[(trackno-1)*8+4+1]&DATA_TRACK)==0) ) { fprintf(stderr,"Not a data track.\n"); mode_select(AUDIOBLOCKSIZE,0x82); if (mode_sense(reply,&replylen)!=0) die("cannot get sense data"); drive_block_size=V3(&reply[9]); fprintf(stderr,"Selecting CD-DA mode, output file format: %s\n", file_format==AF_FILE_AIFFC?"AIFFC":"AIFF"); audio_track=1; } else { audio_track=0; } start=V4(&reply[(trackno-1)*8+4+4]); stop=V4(&reply[(trackno)*8+4+4]); tracksize=abs(stop-start); /* if (verbose_mode) printf("Start LBA=%d\nStop LBA=%d\n",start,stop); */ len=buffersize; read_10(start-0,1,buffer,&len); /* PRINT_BUF(buffer,32); */ if (!audio_track) { /* read the iso9660 primary descriptor */ fprintf(stderr,"Reading ISO9660 primary descriptor...\n"); len=buffersize; read_10(start+16,1,buffer,&len); if (len<sizeof(ipd)) die("cannot read iso9660 primary descriptor."); memcpy(&ipd,buffer,sizeof(ipd)); imagesize=ISONUM(ipd.volume_space_size); /* we should really check here if we really got a valid primary descriptor or not... */ if ( (imagesize>(stop-start)) || (imagesize<1) ) { fprintf(stderr,"\aInvalid ISO primary descriptor!!!\n"); if (!info_only) fprintf(stderr,"Copying entire track to image file.\n"); force_mode=2; } if (force_mode==1) {} /* use size from ISO primary descriptor */ else if (force_mode==2) imagesize=tracksize; /* use size from TOC */ else { if ( ( (tracksize-imagesize) > MAX_DIFF_ALLOWED ) || ( imagesize > tracksize ) ) { fprintf(stderr,"ISO primary descriptor has suspicious volume size" " (%d blocks)\n",imagesize); imagesize=tracksize; fprintf(stderr, "Using track size from TOC record (%d blocks) instead.\n", imagesize); fprintf(stderr, "(option -f can be used to override this behaviour.)\n"); } } imagesize_bytes=imagesize*BLOCKSIZE; if (verbose_mode||info_only) { printf("ISO9660 image info:\n"); printf("Type: %02xh\n",ipd.type[0]); ISOGETSTR(tmpstr,ipd.id,5); printf("ID: %s\n",tmpstr); printf("Version: %u\n",ipd.version[0]); ISOGETSTR(tmpstr,ipd.system_id,32); printf("System id: %s\n",tmpstr); ISOGETSTR(tmpstr,ipd.volume_id,32); printf("Volume id: %s\n",tmpstr); ISOGETSTR(tmpstr,ipd.volume_set_id,128); if (strlen(tmpstr)>0) printf("Volume set id: %s\n",tmpstr); ISOGETSTR(tmpstr,ipd.publisher_id,128); if (strlen(tmpstr)>0) printf("Publisher id: %s\n",tmpstr); ISOGETSTR(tmpstr,ipd.preparer_id,128); if (strlen(tmpstr)>0) printf("Preparer id: %s\n",tmpstr); ISOGETSTR(tmpstr,ipd.application_id,128); if (strlen(tmpstr)>0) printf("Application id: %s\n",tmpstr); ISOGETDATE(tmpstr,ipd.creation_date); printf("Creation date: %s\n",tmpstr); ISOGETDATE(tmpstr,ipd.modification_date); if (!NULLISODATE(ipd.modification_date)) printf("Modification date: %s\n",tmpstr); ISOGETDATE(tmpstr,ipd.expiration_date); if (!NULLISODATE(ipd.expiration_date)) printf("Expiration date: %s\n",tmpstr); ISOGETDATE(tmpstr,ipd.effective_date); if (!NULLISODATE(ipd.effective_date)) printf("Effective date: %s\n",tmpstr); printf("Image size: %02d:%02d:%02d, %d blocks (%ld bytes)\n", LBA_MIN(ISONUM(ipd.volume_space_size)), LBA_SEC(ISONUM(ipd.volume_space_size)), LBA_FRM(ISONUM(ipd.volume_space_size)), ISONUM(ipd.volume_space_size), (long)ISONUM(ipd.volume_space_size)*BLOCKSIZE ); printf("Track size: %02d:%02d:%02d, %d blocks (%ld bytes)\n", LBA_MIN(tracksize), LBA_SEC(tracksize), LBA_FRM(tracksize), tracksize, (long)tracksize*BLOCKSIZE ); } } else { #ifdef IRIX /* if reading audio track */ imagesize=tracksize; imagesize_bytes=imagesize*CDDA_DATASIZE; buffersize = READBLOCKS*AUDIOBLOCKSIZE; readblocksize = AUDIOBLOCKSIZE; if (cdp) { CDaddcallback(cdp, cd_audio, (CDCALLBACKFUNC)playaudio, 0); } else die("No audioparser"); fclose(outfile); aiffsetup=AFnewfilesetup(); AFinitrate(aiffsetup,AF_DEFAULT_TRACK,44100.0); /* 44.1 kHz */ AFinitfilefmt(aiffsetup,file_format); /* set file format */ AFinitchannels(aiffsetup,AF_DEFAULT_TRACK,2); /* stereo */ AFinitsampfmt(aiffsetup,AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP,16); /* 16bit */ aiffoutfile=AFopenfile(argv[optind],"w",aiffsetup); if (!aiffoutfile) die("Cannot open target file (%s).",argv[optind]); #endif } /* read the image */ if (md5_mode) MD5Init(MD5); if (!info_only) { start_time=(int)time(NULL); fprintf(stderr,"Reading %s (%ldMb)...\n", audio_track?"audio track":"ISO9660 image", imagesize_bytes/(1024*1024)); do { len=buffersize; if(readsize/readblocksize+READBLOCKS>imagesize) { read_10(start+counter,imagesize-readsize/readblocksize,buffer,&len); } else read_10(start+counter,READBLOCKS,buffer,&len); if ((counter%(1024*1024/readblocksize))<READBLOCKS) { cur_time=(int)time(NULL); if ((cur_time-start_time)>0) { kbps=(readsize/1024)/(cur_time-start_time); } else { kbps=0; } fprintf(stderr,"%3dM of %dM read. (%d kb/s) \r", counter/512,imagesize/512,kbps); } counter+=READBLOCKS; readsize+=len; if (!audio_track) { fwrite(buffer,len,1,outfile); } else { #ifdef IRIX /* audio track */ for(i=0;i<(len/CDDA_BLOCKSIZE);i++) { CDparseframe(cdp,(CDFRAME*)&buffer[i*CDDA_BLOCKSIZE]); } #endif } if (md5_mode) MD5Update(MD5,buffer,(readsize>imagesize_bytes? len-(readsize-imagesize_bytes):len) ); } while (len==readblocksize*READBLOCKS&&readsize<imagesize*readblocksize); fprintf(stderr,"\n"); if (!audio_track) { if (readsize > imagesize_bytes) ftruncate(fileno(outfile),imagesize_bytes); if (readsize < imagesize_bytes) fprintf(stderr,"Image not complete!\n"); else fprintf(stderr,"Image complete.\n"); fclose(outfile); } else { #ifdef IRIX AFclosefile(aiffoutfile); #endif } } if (md5_mode && !info_only) { MD5Final((unsigned char*)digest,MD5); md2str((unsigned char*)digest,digest_text); fprintf(stderr,"MD5 (%s) = %s\n",(md5_mode==2?"'image'":argv[optind]), digest_text); } quit: start_stop(0); /* set_removable(1); */ /* close the scsi device */ scsi_close(); return 0; }