Salome HOME
Merge branch 'omu/multijob'
[modules/kernel.git] / src / Utils / Utils_ExceptHandlers.cxx
1 // Copyright (C) 2007-2020  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 #ifdef WIN32
34 #include "DbgHelp.h"
35 #include <WinBase.h>
36 #pragma comment(lib, "Dbghelp.lib")
37 #else
38 #include <execinfo.h>
39 #include <dlfcn.h>
40 #include <cxxabi.h>
41 #endif
42
43 #include <SALOMEconfig.h>
44 #include CORBA_SERVER_HEADER(SALOME_Exception)
45
46 //#define NBLINES_BACKTRACE 64
47 #ifdef WIN32
48 void printBacktrace(std::stringstream& txt)
49 {
50         typedef USHORT(WINAPI *CaptureStackBackTraceType)(__in ULONG, __in ULONG, __out PVOID*, __out_opt PULONG);
51
52         CaptureStackBackTraceType func = (CaptureStackBackTraceType)(GetProcAddress(LoadLibraryA("kernel32.dll"), "RtlCaptureStackBackTrace"));
53
54         if (func == NULL)
55                 return;
56         const int kMaxCallers = 128;
57
58         void         * callers_stack[kMaxCallers];
59         unsigned short frames;
60         SYMBOL_INFO  * symbol;
61         HANDLE         process;
62         process = GetCurrentProcess();
63         SymInitialize(process, NULL, TRUE);
64         frames = (func)(0, kMaxCallers, callers_stack, NULL);
65         symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
66         symbol->MaxNameLen = 255;
67         symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
68
69         const unsigned short  MAX_CALLERS_SHOWN = 64;
70         frames = frames < MAX_CALLERS_SHOWN ? frames : MAX_CALLERS_SHOWN;
71         for (unsigned int i = 0; i < frames; i++)
72         {
73                 SymFromAddr(process, (DWORD64)(callers_stack[i]), 0, symbol);
74                 txt << "*** " << i << ": " << callers_stack[i] << " " << symbol->Name << " - 0x" << symbol->Address << std::endl;
75         }
76
77         free(symbol);
78 }
79 #else
80 void printBacktrace(void **stacklines, int nbLines, std::stringstream& txt)
81 {
82   char **stackSymbols = backtrace_symbols(stacklines, nbLines);
83   for (int i = 0; i < nbLines; i++)
84     {
85       Dl_info infodl;
86       if (dladdr(stacklines[i], &infodl))
87         {
88           txt << i << " " << infodl.dli_fname << " " << infodl.dli_fbase << " ";
89           char *demangled = NULL;
90           int status = 0;
91           demangled = abi::__cxa_demangle(infodl.dli_sname, NULL, 0, &status);
92           if (status == 0 && demangled != NULL)
93             {
94               std::string demangstr = demangled; // copy
95               txt << demangstr;
96             }
97           else
98             {
99               if (infodl.dli_sname != 0 && infodl.dli_sname[0] != 0)
100                 {
101                   std::string sname = infodl.dli_sname;
102                   if (sname.size() > 0)
103                     txt << infodl.dli_sname;
104                 }
105             }
106           txt << " " << infodl.dli_saddr;
107           txt << std::endl;
108           free(demangled);
109         }
110       else
111         txt << i << " " << stackSymbols[i] << std::endl;
112     }
113   free(stackSymbols);
114 }
115 #endif
116
117 void SalomeException ()
118 {
119   std::stringstream txt;
120   txt << "Salome Exception" << std::endl;
121 #ifdef WIN32
122   printBacktrace(txt);
123 #else
124   void *stacklines[64];
125   size_t nbLines;
126   nbLines = backtrace(stacklines, 64);
127   printBacktrace(stacklines, nbLines, txt);
128 #endif
129   throw SALOME_Exception(txt.str().c_str());
130 }
131
132 void SALOME_SalomeException()
133 {
134   std::stringstream txt;
135 #ifdef WIN32
136   txt << "INTERNAL_ERROR, backtrace stack:" << std::endl;
137   printBacktrace(txt);
138 #else
139   void *stacklines[64];
140   size_t nbLines;
141   nbLines = backtrace(stacklines, 64);
142   txt << "INTERNAL_ERROR, backtrace stack:" << nbLines << std::endl;
143   printBacktrace(stacklines, nbLines, txt);
144 #endif
145   THROW_SALOME_CORBA_EXCEPTION(txt.str().c_str(), SALOME::INTERNAL_ERROR);
146 }
147