eddd222313bcb7c7e35246a9dd99d12054134e20
[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 #ifdef SENF_DEBUG
31     #include <execinfo.h>
32 #endif
33 #include <cxxabi.h>
34 #include <boost/regex.hpp>
35 #include "Buffer.hh"
36
37 //#include "Backtrace.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 prefix_ void senf::formatBacktrace(std::ostream & os, void ** backtrace, unsigned numEntries)
42 {
43 #ifdef SENF_DEBUG
44     char ** symbols (::backtrace_symbols(backtrace, numEntries));
45
46     static boost::regex const backtraceRx
47         ("(.*)\\((.*)\\+(0x[0-9a-f]+)\\) \\[(0x[0-9a-f]+)\\]");
48     enum { File = 1,
49            Symbol = 2,
50            Offset = 3,
51            Address = 4 };
52
53     for (unsigned i=0; i<numEntries; ++i) {
54         std::string sym (symbols[i]);
55         boost::smatch match;
56         if (regex_match(sym, match, backtraceRx)) {
57             std::string symbol (match[Symbol]);
58             int status (0);
59             char * demangled ( abi::__cxa_demangle(symbol.c_str(), 0, 0, &status) );
60             if (demangled) {
61                 symbol = std::string(demangled);
62                 free(demangled);
63             }
64             os << "    " << symbol << " + " << match[Offset]
65                << "\n        in " << match[File] << " [" << match[Address] << "]\n";
66         }
67         else if (sym == "[0xffffe410]")
68             os << "    __kernel_vsyscall [0xffffe410]\n";
69         else if (sym == "[0xffffe420]")
70             os << "    __kernel_sigreturn [0xffffe410]\n";
71         else if (sym == "[0xffffe440]")
72             os << "    __kernel_rt_sigreturn [0xffffe440]\n";
73         else
74             os << "    " << sym << "\n";
75     }
76     free(symbols);
77 #endif
78 #ifndef SENF_DEBUG
79    os << "no backtrace available please compile SENF without final=1\n";
80 #endif
81
82 }
83
84 prefix_ void senf::backtrace(std::ostream & os, unsigned numEntries)
85 {
86 #ifdef SENF_DEBUG
87    SENF_SCOPED_BUFFER( void*, entries, numEntries);
88    unsigned n ( ::backtrace(entries, numEntries) );
89    senf::formatBacktrace(os, entries, n);
90 #endif
91 }
92
93 ///////////////////////////////cc.e////////////////////////////////////////
94 #undef prefix_
95 //#include "Backtrace.mpp"
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // End: