add stacktrace() support for Windows (Grygorii Zharun)
authorPaul Davis <paul@linuxaudiosystems.com>
Wed, 6 May 2015 10:55:05 +0000 (06:55 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Mon, 29 Jun 2015 18:16:42 +0000 (14:16 -0400)
libs/pbd/stacktrace.cc
wscript

index c74dd946f78d7896246d4dfe36f19df64440bc32..cf6b4a2df44eb6d1463868372a2c8831fcbc3149 100644 (file)
 #include "libpbd-config.h"
 
 #include "pbd/stacktrace.h"
+#include "pbd/compose.h"
+#include "pbd/pthread_utils.h"
+
 #include <cstdio>
 #include <iostream>
 #include <string>
 
+#ifdef PLATFORM_WINDOWS
+#include <Windows.h>
+#include <DbgHelp.h>
+#endif
+
 void
 PBD::trace_twb ()
 {
@@ -103,6 +111,56 @@ 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)
+{
+#ifdef DEBUG
+       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 );
+#endif
+}
+
+void
+c_stacktrace ()
+{
+       PBD::stacktrace (std::cout);
+}
+
 #else
 
 std::string 
diff --git a/wscript b/wscript
index 1626f845569583e1dcc2f6e049f30ff697460570..70678c321a9a1c32e25121703c27902a64746cb4 100644 (file)
--- a/wscript
+++ b/wscript
@@ -889,7 +889,9 @@ 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')
-
+        # TODO this should only be necessary for a debug build
+        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')