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/debug.h"
24 #include "pbd/compose.h"
25 #include "pbd/pthread_utils.h"
31 #ifdef PLATFORM_WINDOWS
41 /* Obtain a backtrace and print it to stdout. */
49 symbol_demangle (const std::string& l)
55 char* realname = abi::__cxa_demangle (l.c_str(), 0, 0, &status);
56 std::string d (realname);
59 } catch (std::exception) {
67 PBD::demangle (std::string const & l)
69 std::string::size_type const b = l.find_first_of ("(");
71 if (b == std::string::npos) {
72 return symbol_demangle (l);
75 std::string::size_type const p = l.find_last_of ("+");
76 if (p == std::string::npos) {
77 return symbol_demangle (l);
81 return symbol_demangle (l);
84 std::string const fn = l.substr (b + 1, p - b - 1);
86 return symbol_demangle (fn);
90 PBD::stacktrace (std::ostream& out, int levels)
97 size = backtrace (array, 200);
100 strings = backtrace_symbols (array, size);
104 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
105 out << " " << demangle (strings[i]) << std::endl;
111 out << "no stacktrace available!" << std::endl;
115 #elif defined (PLATFORM_WINDOWS)
117 #if defined DEBUG && !defined CaptureStackBackTrace
118 #define CaptureStackBackTrace RtlCaptureStackBackTrace
121 __declspec(dllimport) USHORT WINAPI CaptureStackBackTrace (
123 ULONG FramesToCapture,
131 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
133 return std::string();
137 PBD::stacktrace( std::ostream& out, int)
140 const size_t levels = 62; // does not support more then 62 levels of stacktrace
142 void * stack[ levels ];
143 unsigned short frames;
144 SYMBOL_INFO * symbol;
147 process = GetCurrentProcess();
148 out << "+++++Backtrace process: " << DEBUG_THREAD_SELF << std::endl;
150 SymInitialize( process, NULL, TRUE );
152 frames = CaptureStackBackTrace( 0, levels, stack, NULL );
154 out << "+++++Backtrace frames: " << frames << std::endl;
156 symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
157 symbol->MaxNameLen = 255;
158 symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
160 for( i = 0; i < frames; i++ )
162 SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
163 out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address );
175 PBD::stacktrace (std::cout);
181 PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */
183 return std::string();
187 PBD::stacktrace (std::ostream& out, int /*levels*/)
189 out << "stack tracing is not enabled on this platform" << std::endl;
195 PBD::stacktrace (std::cout);
198 #endif /* HAVE_EXECINFO */