2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include "libpbd-config.h"
22 #include "pbd/stacktrace.h"
23 #include "pbd/compose.h"
24 #include "pbd/pthread_utils.h"
30 #ifdef PLATFORM_WINDOWS
40 /* Obtain a backtrace and print it to stdout. */
48 symbol_demangle (const std::string& l)
54 char* realname = abi::__cxa_demangle (l.c_str(), 0, 0, &status);
55 std::string d (realname);
58 } catch (std::exception) {
66 PBD::demangle (std::string const & l)
68 std::string::size_type const b = l.find_first_of ("(");
70 if (b == std::string::npos) {
71 return symbol_demangle (l);
74 std::string::size_type const p = l.find_last_of ("+");
75 if (p == std::string::npos) {
76 return symbol_demangle (l);
80 return symbol_demangle (l);
83 std::string const fn = l.substr (b + 1, p - b - 1);
85 return symbol_demangle (fn);
89 PBD::stacktrace (std::ostream& out, int levels)
96 size = backtrace (array, 200);
99 strings = backtrace_symbols (array, size);
103 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
104 out << " " << demangle (strings[i]) << std::endl;
110 out << "no stacktrace available!" << std::endl;
114 #elif defined (PLATFORM_WINDOWS)
117 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
119 return std::string();
123 PBD::stacktrace( std::ostream& out, int)
126 const size_t levels = 62; // does not support more then 62 levels of stacktrace
128 void * stack[ levels ];
129 unsigned short frames;
130 SYMBOL_INFO * symbol;
133 process = GetCurrentProcess();
134 out << "+++++Backtrace process: " << pthread_self() << std::endl;
136 SymInitialize( process, NULL, TRUE );
138 frames = CaptureStackBackTrace( 0, levels, stack, NULL );
140 out << "+++++Backtrace frames: " << frames << std::endl;
142 symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
143 symbol->MaxNameLen = 255;
144 symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
146 for( i = 0; i < frames; i++ )
148 SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
149 out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address );
161 PBD::stacktrace (std::cout);
167 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
169 return std::string();
173 PBD::stacktrace (std::ostream& out, int /*levels*/)
175 out << "stack tracing is not enabled on this platform" << std::endl;
181 PBD::stacktrace (std::cout);
184 #endif /* HAVE_EXECINFO */