print_trace.C
Go to the documentation of this file.00001 // Code partially copyright Edd Dawson 2007 00002 // 00003 // Boost Software License - Version 1.0 - August 17th, 2003 00004 // 00005 // Permission is hereby granted, free of charge, to any person or organization 00006 // obtaining a copy of the software and accompanying documentation covered by 00007 // this license (the "Software") to use, reproduce, display, distribute, 00008 // execute, and transmit the Software, and to prepare derivative works of the 00009 // Software, and to permit third-parties to whom the Software is furnished to 00010 // do so, all subject to the following: 00011 // 00012 // The copyright notices in the Software and this entire statement, including 00013 // the above license grant, this restriction and the following disclaimer, 00014 // must be included in all copies of the Software, in whole or in part, and 00015 // all derivative works of the Software, unless such copies or derivative 00016 // works are solely in the form of machine-executable object code generated by 00017 // a source language processor. 00018 // 00019 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00022 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00023 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00024 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00025 // DEALINGS IN THE SOFTWARE. 00026 00027 00028 #include "libmesh/libmesh_config.h" 00029 #include "libmesh/print_trace.h" 00030 00031 #if defined(LIBMESH_HAVE_GCC_ABI_DEMANGLE) && defined(LIBMESH_HAVE_GLIBC_BACKTRACE) 00032 00033 #include <iostream> 00034 #include <string> 00035 #include <execinfo.h> 00036 #include <cxxabi.h> 00037 #include <cstdlib> 00038 00039 namespace libMesh 00040 { 00041 00042 std::string process_trace(const char *name) 00043 { 00044 std::string fullname = name; 00045 std::string saved_begin, saved_end; 00046 size_t namestart, nameend; 00047 00055 #ifdef __APPLE__ 00056 namestart = fullname.find("0x"); 00057 if (namestart != std::string::npos) 00058 { 00059 namestart = fullname.find(' ', namestart) + 1; 00060 saved_begin = fullname.substr(0, namestart); 00061 } 00062 else 00063 namestart = 0; 00064 nameend = fullname.find('+'); 00065 if (nameend == std::string::npos || 00066 nameend <= namestart) 00067 nameend = fullname.size(); 00068 else 00069 { 00070 nameend -= 1; 00071 saved_end = fullname.substr(nameend, fullname.length()); 00072 } 00073 #else 00074 namestart = fullname.find('('); 00075 if (namestart == std::string::npos) 00076 return fullname; 00077 else 00078 namestart++; 00079 nameend = fullname.find('+'); 00080 if (nameend == std::string::npos || 00081 nameend <= namestart) 00082 return fullname; 00083 #endif 00084 00085 std::string type_name = fullname.substr(namestart, nameend - namestart); 00086 00087 // Try to demangle now 00088 return saved_begin + demangle(type_name.c_str()) + saved_end; 00089 } 00090 00091 00092 std::string demangle(const char *name) 00093 { 00094 int status = 0; 00095 char *d = 0; 00096 std::string ret = name; 00097 try { if ( (d = abi::__cxa_demangle(name, 0, 0, &status)) ) ret = d; } 00098 catch(...) { } 00099 std::free(d); 00100 return ret; 00101 } 00102 00103 void print_trace(std::ostream &out) 00104 { 00105 void *addresses[40]; 00106 char **strings; 00107 00108 int size = backtrace(addresses, 40); 00109 strings = backtrace_symbols(addresses, size); 00110 out << "Stack frames: " << size << std::endl; 00111 for(int i = 0; i < size; i++) 00112 out << i << ": " << process_trace(strings[i]) << std::endl; 00113 std::free(strings); 00114 } 00115 00116 } // namespace libMesh 00117 00118 #else 00119 00120 namespace libMesh 00121 { 00122 void print_trace(std::ostream &) {} 00123 00124 std::string demangle(const char *name) { return std::string(name); } 00125 } 00126 00127 #endif
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:48 UTC
Hosted By: