fix headers for case-sensitive FS.
[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 "pbd/compose.h"
24 #include "pbd/pthread_utils.h"
25
26 #include <cstdio>
27 #include <iostream>
28 #include <string>
29
30 #ifdef PLATFORM_WINDOWS
31 #include <windows.h>
32 #include <dbghelp.h>
33 #endif
34
35 void
36 PBD::trace_twb ()
37 {
38 }
39
40 /* Obtain a backtrace and print it to stdout. */
41
42 #ifdef HAVE_EXECINFO
43
44 #include <execinfo.h>
45 #include <cxxabi.h>
46
47 static std::string 
48 symbol_demangle (const std::string& l)
49 {
50         int status;
51
52         try {
53                 
54                 char* realname = abi::__cxa_demangle (l.c_str(), 0, 0, &status);
55                 std::string d (realname);
56                 free (realname);
57                 return d;
58         } catch (std::exception) {
59                 
60         }
61
62         return l;
63 }
64
65 std::string 
66 PBD::demangle (std::string const & l)
67 {
68         std::string::size_type const b = l.find_first_of ("(");
69
70         if (b == std::string::npos) {
71                 return symbol_demangle (l);
72         }
73
74         std::string::size_type const p = l.find_last_of ("+");
75         if (p == std::string::npos) {
76                 return symbol_demangle (l);
77         }
78
79         if ((p - b) <= 1) {
80                 return symbol_demangle (l);
81         }
82         
83         std::string const fn = l.substr (b + 1, p - b - 1);
84
85         return symbol_demangle (fn);
86 }
87
88 void
89 PBD::stacktrace (std::ostream& out, int levels)
90 {
91         void *array[200];
92         size_t size;
93         char **strings;
94         size_t i;
95      
96         size = backtrace (array, 200);
97
98         if (size) {
99                 strings = backtrace_symbols (array, size);
100      
101                 if (strings) {
102                         
103                         for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
104                                 out << "  " << demangle (strings[i]) << std::endl;
105                         }
106                         
107                         free (strings);
108                 }
109         } else {
110                 out << "no stacktrace available!" << std::endl;
111         }
112 }
113
114 #elif defined (PLATFORM_WINDOWS)
115
116 std::string 
117 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
118 {
119         return std::string();
120 }
121
122 void
123 PBD::stacktrace( std::ostream& out, int)
124 {
125 #ifdef DEBUG
126         const size_t levels = 62; // does not support more then 62 levels of stacktrace
127         unsigned int   i;
128         void         * stack[ levels ];
129         unsigned short frames;
130         SYMBOL_INFO  * symbol;
131         HANDLE         process;
132
133         process = GetCurrentProcess();
134         out << "+++++Backtrace process: " <<  pthread_self() << std::endl;
135
136         SymInitialize( process, NULL, TRUE );
137
138         frames               = CaptureStackBackTrace( 0, levels, stack, NULL );
139
140         out << "+++++Backtrace frames: " <<  frames << std::endl;
141
142         symbol               = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
143         symbol->MaxNameLen   = 255;
144         symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
145
146         for( i = 0; i < frames; i++ )
147         {
148                 SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
149                 out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address );
150         }
151
152         out.flush();
153
154         free( symbol );
155 #endif
156 }
157
158 void
159 c_stacktrace ()
160 {
161         PBD::stacktrace (std::cout);
162 }
163
164 #else
165
166 std::string 
167 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
168 {
169         return std::string();
170 }
171
172 void
173 PBD::stacktrace (std::ostream& out, int /*levels*/)
174 {
175         out << "stack tracing is not enabled on this platform" << std::endl;
176 }
177
178 void
179 c_stacktrace ()
180 {
181         PBD::stacktrace (std::cout);
182 }
183
184 #endif /* HAVE_EXECINFO */