hopefully fixes for backtrace handling
[senf.git] / senf / Utils / Backtrace.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Backtrace non-inline non-template implementation */
25
26 #include "Backtrace.hh"
27 //#include "Backtrace.ih"
28
29 // Custom includes
30 #include <senf/config.hh>
31 #ifdef SENF_BACKTRACE
32     #include <execinfo.h>
33     #include <errno.h>
34 #endif
35 #include <cxxabi.h>
36 #include <boost/regex.hpp>
37 #include "Buffer.hh"
38
39 //#include "Backtrace.mpp"
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 prefix_ void senf::formatBacktrace(std::ostream & os, void ** backtrace, int numEntries)
44 {
45 #ifdef SENF_BACKTRACE
46     char ** symbols (::backtrace_symbols(backtrace, numEntries));
47     if (symbols == NULL) {
48         os << "error on translating backtrace addresses with ::backtrace_symbols: " << std::strerror(errno);
49         return;
50     }
51
52     static boost::regex const backtraceRx
53         ("(.*)\\((.*)\\+(0x[0-9a-f]+)\\) \\[(0x[0-9a-f]+)\\]");
54     enum { File = 1,
55            Symbol = 2,
56            Offset = 3,
57            Address = 4 };
58
59     for (int i=0; i<numEntries; ++i) {
60         std::string sym (symbols[i]);
61         boost::smatch match;
62         if (regex_match(sym, match, backtraceRx)) {
63             std::string symbol (match[Symbol]);
64             int status (0);
65             char * demangled ( abi::__cxa_demangle(symbol.c_str(), 0, 0, &status) );
66             if (demangled) {
67                 symbol = std::string(demangled);
68                 free(demangled);
69             }
70             os << "    " << symbol << " + " << match[Offset]
71                << "\n        in " << match[File] << " [" << match[Address] << "]\n";
72         }
73         else if (sym == "[0xffffe410]")
74             os << "    __kernel_vsyscall [0xffffe410]\n";
75         else if (sym == "[0xffffe420]")
76             os << "    __kernel_sigreturn [0xffffe410]\n";
77         else if (sym == "[0xffffe440]")
78             os << "    __kernel_rt_sigreturn [0xffffe440]\n";
79         else
80             os << "    " << sym << "\n";
81     }
82     free(symbols);
83 #endif
84 #ifndef SENF_DEBUG
85    os << "no backtrace available please compile SENF without final=1\n";
86 #endif
87
88 }
89
90 prefix_ void senf::backtrace(std::ostream & os, int numEntries)
91 {
92 #ifdef SENF_BACKTRACE
93    SENF_SCOPED_BUFFER( void*, entries, numEntries);
94    int n ( ::backtrace(entries, numEntries) );
95    senf::formatBacktrace(os, entries, n);
96 #endif
97 }
98
99 //-/////////////////////////////////////////////////////////////////////////////////////////////////
100 #undef prefix_
101 //#include "Backtrace.mpp"
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // fill-column: 100
107 // comment-column: 40
108 // c-file-style: "senf"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -u test"
112 // End: