Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <OpenFOAM/error.H>
00033 #include <OpenFOAM/OStringStream.H>
00034
00035 #include <cxxabi.h>
00036 #include <execinfo.h>
00037
00038
00039
00040
00041 void tokenizeSymbols( const Foam::string& symbols, Foam::string& module,
00042 Foam::string& address, Foam::string& function,
00043 Foam::string& offset )
00044 {
00045 size_t s(0), e;
00046
00047
00048 #ifdef darwin
00049
00050 std::string blanks(" \t");
00051
00052
00053 s = symbols.find_first_of(blanks,s);
00054
00055 s = symbols.find_first_not_of(blanks,s+1);
00056 e = symbols.find_first_of(blanks,s+1);
00057 module = symbols.substr(s,e-s);
00058
00059 s = symbols.find_first_not_of(blanks,e+1);
00060 e = symbols.find_first_of(blanks,s+1);
00061 address = symbols.substr(s,e-s);
00062
00063 s = symbols.find_first_not_of(blanks,e+1);
00064 e = symbols.find_first_of(blanks,s+1);
00065 function = symbols.substr(s,e-s);
00066
00067 s = symbols.find_first_not_of(blanks,e+1);
00068 e = symbols.find_first_of(blanks,s+1);
00069
00070 s = symbols.find_first_not_of(blanks,e+1);
00071 e = symbols.find_first_of(blanks,s+1);
00072 offset = symbols.substr(s,e-s);
00073 #else
00074
00075
00076
00077 e = symbols.find('(',s);
00078 module = symbols.substr(s,e-s);
00079
00080 s = e;
00081 e = symbols.find('+',s+1);
00082 function = symbols.substr(s+1,e-s-1);
00083
00084 s = e;
00085 e = symbols.find(')',s+1);
00086 offset = symbols.substr(s+1,e-s-1);
00087
00088 s = symbols.find('[',s+1);
00089 e = symbols.find(']',s+2);
00090 address = symbols.substr(s+1,e-s-1);
00091 #endif
00092 }
00093
00094
00095 void Foam::error::printStack(Foam::Ostream& os)
00096 {
00097 os << " Stack trace:\n";
00098
00099
00100 const size_t max_frames = 100;
00101 void* addrlist[max_frames];
00102
00103
00104 int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
00105
00106 if (addrlen == 0) {
00107 os << "* <EMPTY BACKTRACE, POSSIBLY CORRUPT STACK.> *\n";
00108 } else {
00109
00110
00111 char** symbollist = backtrace_symbols(addrlist, addrlen);
00112
00113
00114 size_t funcnamesize = 256;
00115 char* funcname = new char[funcnamesize];
00116
00117
00118
00119 for (size_t i = 2; i < addrlen; i++)
00120 {
00121
00122 string module, function, address, offset;
00123 tokenizeSymbols( symbollist[i], module, address, function, offset );
00124
00125 if (! (module.empty() || address.empty() || function.empty() || offset.empty()) )
00126 {
00127
00128 int status;
00129 char* ret = abi::__cxa_demangle(function.c_str(), NULL, 0, &status);
00130 if (status == 0) {
00131 function.assign( ret );
00132 free( ret );
00133 os << " " << int(i-2) << " " << module.c_str() << " [" << address.c_str() << "]: "
00134 << function.c_str() << " + " << offset.c_str() << "\n";
00135 }
00136 else {
00137
00138
00139 os << " " << int(i-2) << " " << module.c_str() << " [" << address.c_str() << "]: "
00140 << function.c_str() << " + " << offset.c_str() << "\n";
00141 }
00142 }
00143 else
00144 {
00145
00146 os << " " << int(i-2) << " " << symbollist[i] << "\n";
00147 }
00148 }
00149
00150 delete[] funcname;
00151 free(symbollist);
00152 }
00153 }
00154
00155