EoaCore.cxx
Go to the documentation of this file.00001 #include <cstring>
00002 #include <cstdlib>
00003
00004 #include "EoaCore.hxx"
00005 #include "Demangle.hxx"
00006
00007 #ifdef _GNU_SOURCE
00008 #include <execinfo.h>
00009 #endif
00010
00011 unsigned int EoaCore::gBacktraceSymbols = 5;
00012
00013 EoaCore::EoaCore() {
00014 fWhat[0] = 0;
00015 #ifdef _GNU_SOURCE
00016 if (gBacktraceSymbols>0) {
00017 const unsigned int bufferSize=100;
00018 if (gBacktraceSymbols>bufferSize) gBacktraceSymbols = bufferSize;
00019 void *buffer[bufferSize];
00020 unsigned int frames = bufferSize;
00021 if (gBacktraceSymbols<frames) frames = gBacktraceSymbols;
00022 int nFrames = backtrace(buffer,frames);
00023 char **symbols = backtrace_symbols(buffer,nFrames);
00024 if (symbols != NULL) {
00025 unsigned int len = 0;
00026 std::strcat(fWhat,"Backtrace:\n");
00027 for (int i = 0; i<nFrames; ++i) {
00028 std::string sym(symbols[i]);
00029 std::string lib;
00030 std::string::size_type s = sym.find_last_of('/');
00031 if (s != std::string::npos) sym = sym.substr(s+1);
00032 s = sym.find_first_of('(');
00033 if (s != std::string::npos) lib = sym.substr(0,s);
00034 sym = sym.substr(s+1);
00035 std::string::size_type t = sym.find_first_of(')');
00036 if (s != std::string::npos
00037 && t != std::string::npos) sym = sym.substr(0,t);
00038 else sym="";
00039 if (lib.empty() && sym.empty()) continue;
00040 len += lib.length();
00041 len += sym.length() + 6;
00042 if (2*len>sizeof(fWhat)) break;
00043 std::strcat(fWhat," --> ");
00044 if (!lib.empty()) std::strcat(fWhat,lib.c_str());
00045 if (!sym.empty()) {
00046 std::strcat(fWhat,": ");
00047 std::strcat(fWhat,sym.c_str());
00048 }
00049 std::strcat(fWhat,"\n");
00050 }
00051 std::free(symbols);
00052 }
00053 }
00054 #endif
00055 std::strcat(fWhat,"Exception: EoaCore");
00056 }
00057
00058 void EoaCore::AppendWhat(const char* child) {
00059 unsigned int wLen = std::strlen(fWhat);
00060 unsigned int cLen = std::strlen(child);
00061 if (wLen+cLen < sizeof(fWhat)-5) {
00062 std::strcat(fWhat,"::");
00063 std::strcat(fWhat,child);
00064 }
00065 }