initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OSspecific / Unix / signals / sigFpe.C
blobe3bcf15a843dd302eea7211d619eaba2ef9aa3b6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "sigFpe.H"
30 #include "JobInfo.H"
31 #include "OSspecific.H"
32 #include "IOstreams.H"
34 #ifdef LINUX_GNUC
36 #   ifndef __USE_GNU
37 #       define __USE_GNU
38 #   endif
40 #   include <fenv.h>
41 #   include <malloc.h>
43 #elif defined(sgiN32) || defined(sgiN32Gcc)
45 #   include <sigfpe.h>
47 #endif
50 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
52 struct sigaction Foam::sigFpe::oldAction_;
55 #if defined(LINUX)
57 void *(*Foam::sigFpe::old_malloc_hook)(size_t, const void *) = NULL;
59 void* Foam::sigFpe::my_malloc_hook(size_t size, const void *caller)
61     void *result;
63     // Restore all old hooks
64     __malloc_hook = old_malloc_hook;
66     // Call recursively
67     result = malloc (size);
69     // initialize to signalling nan
70 #   ifdef SP
72     const uint32_t sNAN = 0x7ff7fffflu; 
74     int nScalars = size / sizeof(scalar);
76     uint32_t* dPtr = reinterpret_cast<uint32_t*>(result);
78     for (int i = 0; i < nScalars; i++)
79     {
80         *dPtr++ = sNAN;
81     }
83 #   else
85     const uint64_t sNAN = 0x7ff7ffffffffffffllu; 
87     int nScalars = size/sizeof(scalar);
89     uint64_t* dPtr = reinterpret_cast<uint64_t*>(result);
91     for (int i = 0; i < nScalars; i++)
92     {
93         *dPtr++ = sNAN;
94     }
96 #   endif
98     // Restore our own hooks
99     __malloc_hook = my_malloc_hook;
101     return result;
104 #endif
107 #ifdef LINUX_GNUC
109 void Foam::sigFpe::sigFpeHandler(int)
111     // Reset old handling
112     if (sigaction(SIGFPE, &oldAction_, NULL) < 0)
113     {
114         FatalErrorIn
115         (
116             "Foam::sigSegv::sigFpeHandler()"
117         )   << "Cannot reset SIGFPE trapping"
118             << abort(FatalError);    
119     }
121     // Update jobInfo file
122     jobInfo.signalEnd();
124     error::printStack(Perr);
126     // Throw signal (to old handler)
127     raise(SIGFPE);
130 #endif
133 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
135 Foam::sigFpe::sigFpe()
137     oldAction_.sa_handler = NULL;
141 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
143 Foam::sigFpe::~sigFpe()
145     if (env("FOAM_SIGFPE"))
146     {
147 #       ifdef LINUX_GNUC
149         // Reset signal
150         if (oldAction_.sa_handler && sigaction(SIGFPE, &oldAction_, NULL) < 0)
151         {
152             FatalErrorIn
153             (
154                 "Foam::sigFpe::~sigFpe()"
155             )   << "Cannot reset SIGFPE trapping"
156                 << abort(FatalError);    
157         }
159 #       endif
160     }
162     if (env("FOAM_SETNAN"))
163     {
164 #       ifdef LINUX_GNUC
166         // Reset to standard malloc
167         if (oldAction_.sa_handler)
168         {
169             __malloc_hook = old_malloc_hook;
170         }
172 #       endif
173     }
177 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
179 void Foam::sigFpe::set()
181     if (oldAction_.sa_handler)
182     {
183         FatalErrorIn
184         (
185             "Foam::sigFpe::set()"
186         )   << "Cannot call sigFpe::set() more than once"
187             << abort(FatalError);
188     }
190     if (env("FOAM_SIGFPE"))
191     {
192 #       ifdef LINUX_GNUC
194         feenableexcept
195         (
196             FE_DIVBYZERO
197           | FE_INVALID
198           | FE_OVERFLOW
199         );
201         struct sigaction newAction;
202         newAction.sa_handler = sigFpeHandler;
203         newAction.sa_flags = SA_NODEFER;
204         sigemptyset(&newAction.sa_mask);
205         if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
206         {
207             FatalErrorIn
208             (
209                 "Foam::sigFpe::set()"
210             )   << "Cannot set SIGFPE trapping"
211                 << abort(FatalError);    
212         }
215 #       elif defined(sgiN32) || defined(sgiN32Gcc)
217         sigfpe_[_DIVZERO].abort=1;
218         sigfpe_[_OVERFL].abort=1;
219         sigfpe_[_INVALID].abort=1;
221         sigfpe_[_DIVZERO].trace=1;
222         sigfpe_[_OVERFL].trace=1;
223         sigfpe_[_INVALID].trace=1;
225         handle_sigfpes
226         (
227             _ON,
228             _EN_DIVZERO
229           | _EN_INVALID
230           | _EN_OVERFL,
231             0,
232             _ABORT_ON_ERROR,
233             NULL
234         );
236 #       endif
237     }
240     if (env("FOAM_SETNAN"))
241     {
242 #       ifdef LINUX_GNUC
244         // Set our malloc
245         __malloc_hook = Foam::sigFpe::my_malloc_hook;
247 #       endif
248     }
252 // ************************************************************************* //