]> SALOME platform Git repositories - modules/kernel.git/blob - src/Utils/Utils_ExceptHandlers.cxx
Salome HOME
[EDF17470] : huge bug correction.
[modules/kernel.git] / src / Utils / Utils_ExceptHandlers.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        Utils_ExceptHandler.cxx
24 // Created:     Mon Mar 15 10:23:41 2004
25 // Author:      Oksana TCHEBANOVA
26 //              <ota@localhost.localdomain>
27 //
28 #include "Utils_ExceptHandlers.hxx"
29 #include "Utils_CorbaException.hxx"
30 #include "Utils_SALOME_Exception.hxx"
31
32 #include <sstream>
33 #include <execinfo.h>
34 #include <dlfcn.h>
35 #include <cxxabi.h>
36
37 #include <SALOMEconfig.h>
38 #include CORBA_SERVER_HEADER(SALOME_Exception)
39
40 //#define NBLINES_BACKTRACE 64
41
42 void printBacktrace(void **stacklines, int nbLines, std::stringstream& txt)
43 {
44   char **stackSymbols = backtrace_symbols(stacklines, nbLines);
45   for (int i = 0; i < nbLines; i++)
46     {
47       Dl_info infodl;
48       if (dladdr(stacklines[i], &infodl))
49         {
50           txt << i << " " << infodl.dli_fname << " " << infodl.dli_fbase << " ";
51           char *demangled = NULL;
52           int status = 0;
53           demangled = abi::__cxa_demangle(infodl.dli_sname, NULL, 0, &status);
54           if (status == 0 && demangled != NULL)
55             {
56               std::string demangstr = demangled; // copy
57               txt << demangstr;
58             }
59           else
60             {
61               if (infodl.dli_sname != 0 && infodl.dli_sname[0] != 0)
62                 {
63                   std::string sname = infodl.dli_sname;
64                   if (sname.size() > 0)
65                     txt << infodl.dli_sname;
66                 }
67             }
68           txt << " " << infodl.dli_saddr;
69           txt << std::endl;
70           free(demangled);
71         }
72       else
73         txt << i << " " << stackSymbols[i] << std::endl;
74     }
75   free(stackSymbols);
76 }
77
78 void SalomeException ()
79 {
80   void *stacklines[64];
81   size_t nbLines;
82   nbLines = backtrace(stacklines, 64);
83   std::stringstream txt;
84   txt << "Salome Exception" << std::endl;
85   printBacktrace(stacklines, nbLines, txt);
86   throw SALOME_Exception(txt.str().c_str());
87 }
88
89 void SALOME_SalomeException()
90 {
91   void *stacklines[64];
92   size_t nbLines;
93   nbLines = backtrace(stacklines, 64);
94   std::stringstream txt;
95   txt << "INTERNAL_ERROR, backtrace stack:" << nbLines << std::endl;
96   printBacktrace(stacklines, nbLines, txt);
97   THROW_SALOME_CORBA_EXCEPTION(txt.str().c_str(), SALOME::INTERNAL_ERROR);
98 }
99