switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Backtrace.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Backtrace non-inline non-template implementation */
30
31 #include "Backtrace.hh"
32 //#include "Backtrace.ih"
33
34 // Custom includes
35 #include <senf/config.hh>
36 #ifdef SENF_BACKTRACE
37     #include <execinfo.h>
38     #include <errno.h>
39 #endif
40 #include <cxxabi.h>
41 #include <boost/regex.hpp>
42 #include "Buffer.hh"
43
44 //#include "Backtrace.mpp"
45 #define prefix_
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 prefix_ void senf::formatBacktrace(std::ostream & os, void ** backtrace, int numEntries)
49 {
50 #ifdef SENF_BACKTRACE
51     char ** symbols (::backtrace_symbols(backtrace, numEntries));
52     if (symbols == NULL) {
53         os << "error on translating backtrace addresses with ::backtrace_symbols: " << std::strerror(errno);
54         return;
55     }
56
57     static boost::regex const backtraceRx
58         ("(.*)\\((.*)\\+(0x[0-9a-f]+)\\) \\[(0x[0-9a-f]+)\\]");
59     enum { File = 1,
60            Symbol = 2,
61            Offset = 3,
62            Address = 4 };
63
64     for (int i=0; i<numEntries; ++i) {
65         std::string sym (symbols[i]);
66         boost::smatch match;
67         if (regex_match(sym, match, backtraceRx)) {
68             std::string symbol (match[Symbol]);
69             int status (0);
70             char * demangled ( abi::__cxa_demangle(symbol.c_str(), 0, 0, &status) );
71             if (demangled) {
72                 symbol = std::string(demangled);
73                 free(demangled);
74             }
75             os << "    " << symbol << " + " << match[Offset]
76                << "\n        in " << match[File] << " [" << match[Address] << "]\n";
77         }
78         else if (sym == "[0xffffe410]")
79             os << "    __kernel_vsyscall [0xffffe410]\n";
80         else if (sym == "[0xffffe420]")
81             os << "    __kernel_sigreturn [0xffffe410]\n";
82         else if (sym == "[0xffffe440]")
83             os << "    __kernel_rt_sigreturn [0xffffe440]\n";
84         else
85             os << "    " << sym << "\n";
86     }
87     free(symbols);
88 #endif
89 #ifndef SENF_DEBUG
90    os << "no backtrace available please compile SENF without final=1\n";
91 #endif
92
93 }
94
95 prefix_ void senf::backtrace(std::ostream & os, int numEntries)
96 {
97 #ifdef SENF_BACKTRACE
98    SENF_SCOPED_BUFFER( void*, entries, numEntries);
99    int n ( ::backtrace(entries, numEntries) );
100    senf::formatBacktrace(os, entries, n);
101 #endif
102 }
103
104 //-/////////////////////////////////////////////////////////////////////////////////////////////////
105 #undef prefix_
106 //#include "Backtrace.mpp"
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // comment-column: 40
113 // c-file-style: "senf"
114 // indent-tabs-mode: nil
115 // ispell-local-dictionary: "american"
116 // compile-command: "scons -u test"
117 // End: