sigFpe.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "error.H"
00028 #include "sigFpe.H"
00029
00030 #include "JobInfo.H"
00031 #include "OSspecific.H"
00032 #include "IOstreams.H"
00033
00034 #ifdef LINUX_GNUC
00035
00036 # ifndef __USE_GNU
00037 # define __USE_GNU
00038 # endif
00039
00040 # include <fenv.h>
00041 # include <malloc.h>
00042
00043 #elif defined(sgiN32) || defined(sgiN32Gcc)
00044
00045 # include <sigfpe.h>
00046
00047 #endif
00048
00049 #include <stdint.h>
00050
00051
00052
00053 struct sigaction Foam::sigFpe::oldAction_;
00054
00055
00056 #if defined(LINUX)
00057
00058 void *(*Foam::sigFpe::old_malloc_hook)(size_t, const void *) = NULL;
00059
00060 void* Foam::sigFpe::my_malloc_hook(size_t size, const void *caller)
00061 {
00062 void *result;
00063
00064
00065 __malloc_hook = old_malloc_hook;
00066
00067
00068 result = malloc (size);
00069
00070
00071 # ifdef WM_SP
00072
00073 const uint32_t sNAN = 0x7ff7fffflu;
00074
00075 int nScalars = size / sizeof(scalar);
00076
00077 uint32_t* dPtr = reinterpret_cast<uint32_t*>(result);
00078
00079 for (int i = 0; i < nScalars; i++)
00080 {
00081 *dPtr++ = sNAN;
00082 }
00083
00084 # else
00085
00086 const uint64_t sNAN = 0x7ff7ffffffffffffllu;
00087
00088 int nScalars = size/sizeof(scalar);
00089
00090 uint64_t* dPtr = reinterpret_cast<uint64_t*>(result);
00091
00092 for (int i = 0; i < nScalars; i++)
00093 {
00094 *dPtr++ = sNAN;
00095 }
00096
00097 # endif
00098
00099
00100 __malloc_hook = my_malloc_hook;
00101
00102 return result;
00103 }
00104
00105 #endif
00106
00107
00108 #ifdef LINUX_GNUC
00109
00110 void Foam::sigFpe::sigFpeHandler(int)
00111 {
00112
00113 if (sigaction(SIGFPE, &oldAction_, NULL) < 0)
00114 {
00115 FatalErrorIn
00116 (
00117 "Foam::sigSegv::sigFpeHandler()"
00118 ) << "Cannot reset SIGFPE trapping"
00119 << abort(FatalError);
00120 }
00121
00122
00123 jobInfo.signalEnd();
00124
00125 error::printStack(Perr);
00126
00127
00128 raise(SIGFPE);
00129 }
00130
00131 #endif
00132
00133
00134
00135
00136 Foam::sigFpe::sigFpe()
00137 {
00138 oldAction_.sa_handler = NULL;
00139 }
00140
00141
00142
00143
00144 Foam::sigFpe::~sigFpe()
00145 {
00146 if (env("FOAM_SIGFPE"))
00147 {
00148 # ifdef LINUX_GNUC
00149
00150
00151 if (oldAction_.sa_handler && sigaction(SIGFPE, &oldAction_, NULL) < 0)
00152 {
00153 FatalErrorIn
00154 (
00155 "Foam::sigFpe::~sigFpe()"
00156 ) << "Cannot reset SIGFPE trapping"
00157 << abort(FatalError);
00158 }
00159
00160 # endif
00161 }
00162
00163 if (env("FOAM_SETNAN"))
00164 {
00165 # ifdef LINUX_GNUC
00166
00167
00168 if (oldAction_.sa_handler)
00169 {
00170 __malloc_hook = old_malloc_hook;
00171 }
00172
00173 # endif
00174 }
00175 }
00176
00177
00178
00179
00180 void Foam::sigFpe::set(const bool verbose)
00181 {
00182 if (oldAction_.sa_handler)
00183 {
00184 FatalErrorIn
00185 (
00186 "Foam::sigFpe::set()"
00187 ) << "Cannot call sigFpe::set() more than once"
00188 << abort(FatalError);
00189 }
00190
00191 if (env("FOAM_SIGFPE"))
00192 {
00193 if (verbose)
00194 {
00195 Info<< "SigFpe : Enabling floating point exception trapping"
00196 << " (FOAM_SIGFPE)." << endl;
00197 }
00198
00199 # ifdef LINUX_GNUC
00200
00201 feenableexcept
00202 (
00203 FE_DIVBYZERO
00204 | FE_INVALID
00205 | FE_OVERFLOW
00206 );
00207
00208 struct sigaction newAction;
00209 newAction.sa_handler = sigFpeHandler;
00210 newAction.sa_flags = SA_NODEFER;
00211 sigemptyset(&newAction.sa_mask);
00212 if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
00213 {
00214 FatalErrorIn
00215 (
00216 "Foam::sigFpe::set()"
00217 ) << "Cannot set SIGFPE trapping"
00218 << abort(FatalError);
00219 }
00220
00221
00222 # elif defined(sgiN32) || defined(sgiN32Gcc)
00223
00224 sigfpe_[_DIVZERO].abort=1;
00225 sigfpe_[_OVERFL].abort=1;
00226 sigfpe_[_INVALID].abort=1;
00227
00228 sigfpe_[_DIVZERO].trace=1;
00229 sigfpe_[_OVERFL].trace=1;
00230 sigfpe_[_INVALID].trace=1;
00231
00232 handle_sigfpes
00233 (
00234 _ON,
00235 _EN_DIVZERO
00236 | _EN_INVALID
00237 | _EN_OVERFL,
00238 0,
00239 _ABORT_ON_ERROR,
00240 NULL
00241 );
00242
00243 # endif
00244 }
00245
00246
00247 if (env("FOAM_SETNAN"))
00248 {
00249 if (verbose)
00250 {
00251 Info<< "SetNaN : Initialising allocated memory to NaN"
00252 << " (FOAM_SETNAN)." << endl;
00253 }
00254
00255 # ifdef LINUX_GNUC
00256
00257
00258 __malloc_hook = Foam::sigFpe::my_malloc_hook;
00259
00260 # endif
00261 }
00262 }
00263
00264
00265
Copyright © 2000-2009 OpenCFD Ltd