FreeFOAM The Cross-Platform CFD Toolkit
sigSegvImpl.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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/error.H>
27 #include "sigSegvImpl.H"
28 #include <OpenFOAM/JobInfo.H>
29 #include <OpenFOAM/IOstreams.H>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 struct sigaction Foam::sigSegvImpl::oldAction_;
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 void Foam::sigSegvImpl::sigSegvHandler(int)
38 {
39  // Reset old handling
40  if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
41  {
43  (
44  "Foam::sigSegvImpl::sigSegvHandler()"
45  ) << "Cannot reset SIGSEGV trapping"
46  << abort(FatalError);
47  }
48 
49  // Update jobInfo file
51 
52  Perr << "\n"
53  "************************************************************\n"
54  "* FreeFOAM crashed. See below for a backtrace to help *\n"
55  "* locating the problem. *\n"
56  "************************************************************\n";
57  error::printStack(Perr);
58  Perr << "************************************************************\n"
59  "\n";
60 
61  // Throw signal (to old handler)
62  raise(SIGSEGV);
63 }
64 
65 
66 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
67 
69 {
70  oldAction_.sa_handler = NULL;
71 }
72 
73 
74 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
75 
77 {
78  // Reset old handling
79  if (sigaction(SIGSEGV, &oldAction_, NULL) < 0)
80  {
82  (
83  "Foam::sigSegvImpl::~sigSegvImpl()"
84  ) << "Cannot reset SIGSEGV trapping"
85  << abort(FatalError);
86  }
87 }
88 
89 
90 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
91 
92 void Foam::sigSegvImpl::set(const bool verbose)
93 {
94  if (oldAction_.sa_handler)
95  {
97  (
98  "Foam::sigSegvImpl::set()"
99  ) << "Cannot call sigSegvImpl::set() more than once"
100  << abort(FatalError);
101  }
102 
103  struct sigaction newAction;
104  newAction.sa_handler = sigSegvHandler;
105  newAction.sa_flags = SA_NODEFER;
106  sigemptyset(&newAction.sa_mask);
107  if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0)
108  {
110  (
111  "Foam::sigSegvImpl::set()"
112  ) << "Cannot set SIGSEGV trapping"
113  << abort(FatalError);
114  }
115 }
116 
117 
118 // ************************ vim: set sw=4 sts=4 et: ************************ //