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