Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Backtrace.cc

Go to the documentation of this file.
00001 // $Id: Backtrace.cc 1790 2011-06-06 16:32:21Z tho $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "Backtrace.hh"
00027 //#include "Backtrace.ih"
00028 
00029 // Custom includes
00030 #include <senf/config.hh>
00031 #ifdef SENF_BACKTRACE
00032     #include <execinfo.h>
00033     #include <errno.h>
00034 #endif
00035 #include <cxxabi.h>
00036 #include <boost/regex.hpp>
00037 #include "Buffer.hh"
00038 
00039 //#include "Backtrace.mpp"
00040 #define prefix_
00041 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00042 
00043 prefix_ void senf::formatBacktrace(std::ostream & os, void ** backtrace, int numEntries)
00044 {
00045 #ifdef SENF_BACKTRACE
00046     char ** symbols (::backtrace_symbols(backtrace, numEntries));
00047     if (symbols == NULL) {
00048         os << "error on translating backtrace addresses with ::backtrace_symbols: " << std::strerror(errno);
00049         return;
00050     }
00051 
00052     static boost::regex const backtraceRx
00053         ("(.*)\\((.*)\\+(0x[0-9a-f]+)\\) \\[(0x[0-9a-f]+)\\]");
00054     enum { File = 1,
00055            Symbol = 2,
00056            Offset = 3,
00057            Address = 4 };
00058 
00059     for (int i=0; i<numEntries; ++i) {
00060         std::string sym (symbols[i]);
00061         boost::smatch match;
00062         if (regex_match(sym, match, backtraceRx)) {
00063             std::string symbol (match[Symbol]);
00064             int status (0);
00065             char * demangled ( abi::__cxa_demangle(symbol.c_str(), 0, 0, &status) );
00066             if (demangled) {
00067                 symbol = std::string(demangled);
00068                 free(demangled);
00069             }
00070             os << "    " << symbol << " + " << match[Offset]
00071                << "\n        in " << match[File] << " [" << match[Address] << "]\n";
00072         }
00073         else if (sym == "[0xffffe410]")
00074             os << "    __kernel_vsyscall [0xffffe410]\n";
00075         else if (sym == "[0xffffe420]")
00076             os << "    __kernel_sigreturn [0xffffe410]\n";
00077         else if (sym == "[0xffffe440]")
00078             os << "    __kernel_rt_sigreturn [0xffffe440]\n";
00079         else
00080             os << "    " << sym << "\n";
00081     }
00082     free(symbols);
00083 #endif
00084 #ifndef SENF_DEBUG
00085    os << "no backtrace available please compile SENF without final=1\n";
00086 #endif
00087 
00088 }
00089 
00090 prefix_ void senf::backtrace(std::ostream & os, int numEntries)
00091 {
00092 #ifdef SENF_BACKTRACE
00093    SENF_SCOPED_BUFFER( void*, entries, numEntries);
00094    int n ( ::backtrace(entries, numEntries) );
00095    senf::formatBacktrace(os, entries, n);
00096 #endif
00097 }
00098 
00099 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00100 #undef prefix_
00101 //#include "Backtrace.mpp"
00102 
00103 
00104 // Local Variables:
00105 // mode: c++
00106 // fill-column: 100
00107 // comment-column: 40
00108 // c-file-style: "senf"
00109 // indent-tabs-mode: nil
00110 // ispell-local-dictionary: "american"
00111 // compile-command: "scons -u test"
00112 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research