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/demangle.h"
25 #include "pbd/compose.h"
26 #include "pbd/pthread_utils.h"
32 #ifdef PLATFORM_WINDOWS
42 /* Obtain a backtrace and print it to stdout. */
49 PBD::stacktrace (std::ostream& out, int levels)
56 size = backtrace (array, 200);
59 strings = backtrace_symbols (array, size);
63 for (i = 0; i < size && (levels == 0 || i < size_t(levels)); i++) {
64 out << " " << demangle (strings[i]) << std::endl;
70 out << "no stacktrace available!" << std::endl;
74 #elif defined (PLATFORM_WINDOWS)
76 #if defined DEBUG && !defined CaptureStackBackTrace
77 #define CaptureStackBackTrace RtlCaptureStackBackTrace
80 __declspec(dllimport) USHORT WINAPI CaptureStackBackTrace (
82 ULONG FramesToCapture,
90 PBD::stacktrace( std::ostream& out, int)
93 const size_t levels = 62; // does not support more then 62 levels of stacktrace
95 void * stack[ levels ];
96 unsigned short frames;
100 process = GetCurrentProcess();
101 out << "+++++Backtrace process: " << DEBUG_THREAD_SELF << std::endl;
103 SymInitialize( process, NULL, TRUE );
105 frames = CaptureStackBackTrace( 0, levels, stack, NULL );
107 out << "+++++Backtrace frames: " << frames << std::endl;
109 symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 );
110 symbol->MaxNameLen = 255;
111 symbol->SizeOfStruct = sizeof( SYMBOL_INFO );
113 for( i = 0; i < frames; i++ )
115 SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
116 out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address );
128 PBD::stacktrace (std::ostream& out, int /*levels*/)
130 out << "stack tracing is not enabled on this platform" << std::endl;
138 PBD::stacktrace (std::cout);