From b8811c855440c9d90a5ee3cb20d6aca6a73f2ac1 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Tue, 27 Nov 2012 06:49:15 +0000
Subject: [PATCH] [0.2.x] added : print exception stack from event callback

git-svn-id: https://pykd.svn.codeplex.com/svn@81516 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/dbgexcept.cpp    | 33 ++++++++++++++++++++++++++++++++-
 pykd/dbgexcept.h      |  4 ++++
 pykd/dbgext.cpp       | 25 +------------------------
 pykd/eventhandler.cpp |  1 +
 pykd/eventhandler.h   |  8 +++++---
 pykd/pykdver.h        |  2 +-
 6 files changed, 44 insertions(+), 29 deletions(-)

diff --git a/pykd/dbgexcept.cpp b/pykd/dbgexcept.cpp
index a022644..d8c8b3c 100644
--- a/pykd/dbgexcept.cpp
+++ b/pykd/dbgexcept.cpp
@@ -1,6 +1,6 @@
 #include "stdafx.h"
 #include "dbgexcept.h"
-//#include "diawrapper.h"
+#include "dbgengine.h"
 
 namespace pykd {
 
@@ -17,4 +17,35 @@ python::handle<> exceptPyType<AddSyntheticSymbolException>::pyExceptType;
 python::handle<> exceptPyType<ImplementException>::pyExceptType;
 
 ///////////////////////////////////////////////////////////////////////////////////
+
+void printException()
+{
+    // ������ � �������
+    PyObject  *errtype = NULL, *errvalue = NULL, *traceback = NULL;
+
+    PyErr_Fetch( &errtype, &errvalue, &traceback );
+
+    PyErr_NormalizeException( &errtype, &errvalue, &traceback );
+
+    python::object  tracebackModule = python::import("traceback");
+
+    std::wstringstream  sstr;
+
+    python::object   lst = 
+        python::object( tracebackModule.attr("format_exception" ) )( 
+            python::handle<>( errtype ),
+            python::handle<>( python::allow_null( errvalue ) ),
+            python::handle<>( python::allow_null( traceback ) ) );
+
+    sstr << std::endl << std::endl;
+
+    for ( long i = 0; i < python::len(lst); ++i )
+        sstr << std::wstring( python::extract<std::wstring>(lst[i]) ) << std::endl;
+
+    eprintln( sstr.str() );
+}
+
+
+///////////////////////////////////////////////////////////////////////////////////
+
 }; // end namespace pykd
diff --git a/pykd/dbgexcept.h b/pykd/dbgexcept.h
index 62ebb2f..397c3e8 100644
--- a/pykd/dbgexcept.h
+++ b/pykd/dbgexcept.h
@@ -226,5 +226,9 @@ private:
 
 /////////////////////////////////////////////////////////////////////////////////
 
+void printException();
+
+/////////////////////////////////////////////////////////////////////////////////
+
 }; // namespace pykd
 
diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp
index 233b532..e481f95 100644
--- a/pykd/dbgext.cpp
+++ b/pykd/dbgext.cpp
@@ -208,9 +208,6 @@ py( PDEBUG_CLIENT4 client, PCSTR args )
 
         global["globalEventHandler"] = EventHandlerPtr( new EventHandlerImpl() );
 
-        // ����������� ������ ��������� ���������� ( ����� ��� ������ traceback � )
-        python::object       tracebackModule = python::import("traceback");
-
         // ������ ����������
         typedef  boost::escaped_list_separator<char>    char_separator_t;
         typedef  boost::tokenizer< char_separator_t >   char_tokenizer_t;  
@@ -262,27 +259,7 @@ py( PDEBUG_CLIENT4 client, PCSTR args )
         }
         catch( boost::python::error_already_set const & )
         {
-            // ������ � �������
-            PyObject    *errtype = NULL, *errvalue = NULL, *traceback = NULL;
-
-            PyErr_Fetch( &errtype, &errvalue, &traceback );
-
-            PyErr_NormalizeException( &errtype, &errvalue, &traceback );
-
-            std::wstringstream       sstr;
-
-            python::object   lst = 
-                python::object( tracebackModule.attr("format_exception" ) )( 
-                    python::handle<>( errtype ),
-                    python::handle<>( python::allow_null( errvalue ) ),
-                    python::handle<>( python::allow_null( traceback ) ) );
-
-            sstr << std::endl << std::endl;
-
-            for ( long i = 0; i < python::len(lst); ++i )
-                sstr << std::wstring( python::extract<std::wstring>(lst[i]) ) << std::endl;
-
-            eprintln( sstr.str() );
+            printException();
         }
     }
     catch(...)
diff --git a/pykd/eventhandler.cpp b/pykd/eventhandler.cpp
index ae6d55d..9c7af3c 100644
--- a/pykd/eventhandler.cpp
+++ b/pykd/eventhandler.cpp
@@ -50,6 +50,7 @@ DEBUG_CALLBACK_RESULT EventHandlerImpl::OnBreakpoint( ULONG bpId )
         return DEBUG_CALLBACK_RESULT(retVal);
     }
     catch (const python::error_already_set &) {
+        printException();
     }
 
     return DebugCallbackBreak;
diff --git a/pykd/eventhandler.h b/pykd/eventhandler.h
index 13f6230..990e396 100644
--- a/pykd/eventhandler.h
+++ b/pykd/eventhandler.h
@@ -6,6 +6,7 @@
 
 #include "dbgengine.h"
 #include "bpoint.h"
+#include "dbgexcept.h"
 
 namespace pykd {
 
@@ -62,7 +63,7 @@ private:
                 return pythonHandler(arg1);
             }
             catch (const python::error_already_set &)  {
-                //onHandlerException();
+                printException();
             }
         }
         return DebugCallbackNoChange;
@@ -77,7 +78,7 @@ private:
                 return pythonHandler(arg1,arg2);
             }
             catch (const python::error_already_set &)  {
-                //onHandlerException();
+                printException();
             }
         }
         return DebugCallbackNoChange;
@@ -91,12 +92,13 @@ private:
                 return pythonHandler();
             }
             catch (const python::error_already_set &) {
-                //onHandlerException();
+                printException();
             }
         }
 
         return DebugCallbackNoChange;
     }
+
 }; 
 
 typedef boost::shared_ptr<EventHandlerWrap> EventHandlerPtr;
diff --git a/pykd/pykdver.h b/pykd/pykdver.h
index 123c98e..6cf2226 100644
--- a/pykd/pykdver.h
+++ b/pykd/pykdver.h
@@ -2,7 +2,7 @@
 #define PYKD_VERSION_MAJOR      0
 #define PYKD_VERSION_MINOR      2
 #define PYKD_VERSION_SUBVERSION 0
-#define PYKD_VERSION_BUILDNO    4
+#define PYKD_VERSION_BUILDNO    6
 
 
 #define __VER_STR2__(x) #x