Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / stacktrace.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "libpbd-config.h"
21
22 #include "pbd/stacktrace.h"
23 #include <cstdio>
24 #include <iostream>
25
26 void
27 PBD::trace_twb ()
28 {
29 }
30
31 /* Obtain a backtrace and print it to stdout. */
32
33 #ifdef HAVE_EXECINFO
34
35 #include <execinfo.h>
36 #include <cxxabi.h>
37
38 static std::string 
39 symbol_demangle (const std::string& l)
40 {
41         int status;
42
43         try {
44                 
45                 char* realname = abi::__cxa_demangle (l.c_str(), 0, 0, &status);
46                 std::string d (realname);
47                 free (realname);
48                 return d;
49         } catch (std::exception) {
50                 
51         }
52
53         return l;
54 }
55
56 std::string 
57 PBD::demangle (std::string const & l)
58 {
59         std::string::size_type const b = l.find_first_of ("(");
60
61         if (b == std::string::npos) {
62                 return symbol_demangle (l);
63         }
64
65         std::string::size_type const p = l.find_last_of ("+");
66         if (p == std::string::npos) {
67                 return symbol_demangle (l);
68         }
69
70         if ((p - b) <= 1) {
71                 return symbol_demangle (l);
72         }
73         
74         std::string const fn = l.substr (b + 1, p - b - 1);
75
76         return symbol_demangle (fn);
77 }
78
79 void
80 PBD::stacktrace (std::ostream& out, int levels)
81 {
82         void *array[200];
83         size_t size;
84         char **strings;
85         size_t i;
86      
87         size = backtrace (array, 200);
88
89         if (size) {
90                 strings = backtrace_symbols (array, size);
91      
92                 if (strings) {
93                         
94                         for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
95                                 out << "  " << demangle (strings[i]) << std::endl;
96                         }
97                         
98                         free (strings);
99                 }
100         } else {
101                 out << "no stacktrace available!" << std::endl;
102         }
103 }
104
105 #else
106
107 void
108 PBD::stacktrace (std::ostream& out, int /*levels*/)
109 {
110         out << "stack tracing is not enabled on this platform" << std::endl;
111 }
112
113 void
114 c_stacktrace ()
115 {
116         PBD::stacktrace (std::cout);
117 }
118
119 #endif /* HAVE_EXECINFO */