From ebaccc0e4a845a57d5d7b422902b1147e7bd717b Mon Sep 17 00:00:00 2001 From: Greg Zharun Date: Tue, 10 Feb 2015 20:32:16 +0200 Subject: [PATCH] [Summary] Added possibility to print out stacktrace directly from code on Windows [Reviewed by] Paul Davis --- libs/pbd/pool.cc | 2 ++ libs/pbd/stacktrace.cc | 57 ++++++++++++++++++++++++++++++++++++++++++ wscript | 6 +++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/libs/pbd/pool.cc b/libs/pbd/pool.cc index e09a4706c8..28981c5dd2 100644 --- a/libs/pbd/pool.cc +++ b/libs/pbd/pool.cc @@ -25,6 +25,7 @@ #include "pbd/pool.h" #include "pbd/pthread_utils.h" +#include "pbd/stacktrace.h" #include "pbd/error.h" #include "pbd/debug.h" #include "pbd/compose.h" @@ -183,6 +184,7 @@ PerThreadPool::per_thread_pool () { CrossThreadPool* p = _key.get(); if (!p) { + PBD::stacktrace(std::cout, 100); fatal << "programming error: no per-thread pool \"" << _name << "\" for thread " << pthread_name() << endmsg; /*NOTREACHED*/ } diff --git a/libs/pbd/stacktrace.cc b/libs/pbd/stacktrace.cc index c74dd946f7..8b21c9e6ae 100644 --- a/libs/pbd/stacktrace.cc +++ b/libs/pbd/stacktrace.cc @@ -20,10 +20,19 @@ #include "libpbd-config.h" #include "pbd/stacktrace.h" +#include "pbd/compose.h" +#include "pbd/pthread_utils.h" + #include #include #include +#ifdef PLATFORM_WINDOWS +#include +#include +#endif + + void PBD::trace_twb () { @@ -103,6 +112,54 @@ PBD::stacktrace (std::ostream& out, int levels) } } +#elif defined (PLATFORM_WINDOWS) + +std::string +PBD::demangle (std::string const & l) /* JE - !!!! 'PBD' namespace might possibly get removed (except it's still used in 'libs/canvas/item.cc') */ +{ + return std::string(); +} + +void +PBD::stacktrace( std::ostream& out, int) +{ + const size_t levels = 62; // does not support more then 62 levels of stacktrace + unsigned int i; + void * stack[ levels ]; + unsigned short frames; + SYMBOL_INFO * symbol; + HANDLE process; + + process = GetCurrentProcess(); + out << "+++++Backtrace process: " << pthread_self() << std::endl; + + SymInitialize( process, NULL, TRUE ); + + frames = CaptureStackBackTrace( 0, levels, stack, NULL ); + + out << "+++++Backtrace frames: " << frames << std::endl; + + symbol = ( SYMBOL_INFO * )calloc( sizeof( SYMBOL_INFO ) + 256 * sizeof( char ), 1 ); + symbol->MaxNameLen = 255; + symbol->SizeOfStruct = sizeof( SYMBOL_INFO ); + + for( i = 0; i < frames; i++ ) + { + SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol ); + out << string_compose( "%1: %2 - %3\n", frames - i - 1, symbol->Name, symbol->Address ); + } + + out.flush(); + + free( symbol ); +} + +void +c_stacktrace () +{ + PBD::stacktrace (std::cout); +} + #else std::string diff --git a/wscript b/wscript index 230e0cc3a8..94483b41b4 100755 --- a/wscript +++ b/wscript @@ -710,8 +710,10 @@ def configure(conf): lib='regex', uselib_store="REGEX", define_name='HAVE_REGEX_H') # TODO put this only where it is needed conf.env.append_value('LIB', 'regex') - - # work around GdkDrawable BitBlt performance issue on windows + # For debugging + conf.env.append_value('LIB', 'dbghelp') + + # work around GdkDrawable BitBlt performance issue on windows # see http://gareus.org/wiki/ardour_windows_gdk_and_cairo conf.env.append_value('CFLAGS', '-DUSE_CAIRO_IMAGE_SURFACE') conf.env.append_value('CXXFLAGS', '-DUSE_CAIRO_IMAGE_SURFACE')