sigSegv.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sigSegv.H"
27 #include "error.H"
28 #include "JobInfo.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 struct sigaction Foam::sigSegv::oldAction_;
34 
35 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 void Foam::sigSegv::sigHandler(int)
39 {
40  // Reset old handling
41  if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
42  {
44  (
45  "Foam::sigSegv::sigHandler()"
46  ) << "Cannot reset SIGSEGV trapping"
47  << abort(FatalError);
48  }
49 
50  // Update jobInfo file
52 
53  error::printStack(Perr);
54 
55  // Throw signal (to old handler)
56  raise(SIGSEGV);
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
63 {
64  oldAction_.sa_handler = NULL;
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
69 
71 {
72  // Reset old handling
73  if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
74  {
76  (
77  "Foam::sigSegv::~sigSegv()"
78  ) << "Cannot reset SIGSEGV trapping"
79  << abort(FatalError);
80  }
81 }
82 
83 
84 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
85 
86 void Foam::sigSegv::set(const bool)
87 {
88  if (oldAction_.sa_handler)
89  {
91  (
92  "Foam::sigSegv::set()"
93  ) << "Cannot call sigSegv::set() more than once"
94  << abort(FatalError);
95  }
96 
97  struct sigaction newAction;
98  newAction.sa_handler = sigHandler;
99  newAction.sa_flags = SA_NODEFER;
100  sigemptyset(&newAction.sa_mask);
101  if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0)
102  {
104  (
105  "Foam::sigSegv::set()"
106  ) << "Cannot set SIGSEGV trapping"
107  << abort(FatalError);
108  }
109 }
110 
111 
112 // ************************************************************************* //
sigSegv()
Construct null.
Definition: sigSegv.C:62
prefixOSstream Perr(cerr,"Perr")
Definition: IOstreams.H:54
void signalEnd() const
Definition: JobInfo.C:176
Namespace for OpenFOAM.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
~sigSegv()
Destructor.
Definition: sigSegv.C:70
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
JobInfo jobInfo
Definition: JobInfo.C:35
error FatalError
void set(const bool verbose)
Activate SIGSEGV signal handler.
Definition: sigSegv.C:86