OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

sigFpe.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
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     // Restore all old hooks
00065     __malloc_hook = old_malloc_hook;
00066 
00067     // Call recursively
00068     result = malloc (size);
00069 
00070     // initialize to signalling nan
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     // Restore our own hooks
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     // Reset old handling
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     // Update jobInfo file
00123     jobInfo.signalEnd();
00124 
00125     error::printStack(Perr);
00126 
00127     // Throw signal (to old handler)
00128     raise(SIGFPE);
00129 }
00130 
00131 #endif
00132 
00133 
00134 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00135 
00136 Foam::sigFpe::sigFpe()
00137 {
00138     oldAction_.sa_handler = NULL;
00139 }
00140 
00141 
00142 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00143 
00144 Foam::sigFpe::~sigFpe()
00145 {
00146     if (env("FOAM_SIGFPE"))
00147     {
00148 #       ifdef LINUX_GNUC
00149 
00150         // Reset signal
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         // Reset to standard malloc
00168         if (oldAction_.sa_handler)
00169         {
00170             __malloc_hook = old_malloc_hook;
00171         }
00172 
00173 #       endif
00174     }
00175 }
00176 
00177 
00178 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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         // Set our malloc
00258         __malloc_hook = Foam::sigFpe::my_malloc_hook;
00259 
00260 #       endif
00261     }
00262 }
00263 
00264 
00265 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd