From 33761324e335d4d7e4e75e8e710701d7089bf340 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Fri, 7 Mar 2014 07:16:10 +0000
Subject: [PATCH] [0.3.x] added : onModuleLoad/onModuleUnload  methods

git-svn-id: https://pykd.svn.codeplex.com/svn@87486 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/pyeventhandler.cpp | 102 ++++++++++++++++++++++++++++++++++++++++
 pykd/pyeventhandler.h   |   2 +
 pykd/pykdver.h          |   2 +-
 pykd/pymod.cpp          |  12 ++---
 4 files changed, 111 insertions(+), 7 deletions(-)

diff --git a/pykd/pyeventhandler.cpp b/pykd/pyeventhandler.cpp
index 781fbb1..e4270d1 100644
--- a/pykd/pyeventhandler.cpp
+++ b/pykd/pyeventhandler.cpp
@@ -249,5 +249,107 @@ kdlib::DebugCallbackResult EventHandler::onException( const kdlib::ExceptionInfo
 
 ///////////////////////////////////////////////////////////////////////////////
 
+kdlib::DebugCallbackResult  EventHandler::onModuleLoad( kdlib::MEMOFFSET_64 offset, const std::wstring &name )
+{
+    kdlib::DebugCallbackResult  result = kdlib::DebugCallbackNoChange;
+
+    PyEval_RestoreThread( m_pystate );
+
+    try {
+
+        do {
+
+            python::override pythonHandler = get_override( "onModuleLoad" );
+            if ( !pythonHandler )
+            {
+                result = kdlib::EventHandler::onModuleLoad( offset, name );
+                break;
+            }
+
+            python::object  resObj = pythonHandler( offset, name );
+
+            if ( resObj.is_none() )
+            {
+                result = kdlib::DebugCallbackNoChange;
+                break;
+            }
+
+            int retVal = python::extract<int>( resObj );
+
+            if ( retVal >= kdlib::DebugCallbackMax )
+            {
+                result = kdlib::DebugCallbackBreak;
+                break;
+            }
+                
+            result = kdlib::DebugCallbackResult(retVal);
+
+        } while( FALSE );
+
+    }
+    catch (const python::error_already_set &) 
+    {
+        printException();
+        result =  kdlib::DebugCallbackBreak;
+    }
+
+    m_pystate = PyEval_SaveThread();
+
+    return result;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+kdlib::DebugCallbackResult  EventHandler::onModuleUnload( kdlib::MEMOFFSET_64 offset, const std::wstring &name )
+{
+    kdlib::DebugCallbackResult  result = kdlib::DebugCallbackNoChange;
+
+    PyEval_RestoreThread( m_pystate );
+
+    try {
+
+        do {
+
+            python::override pythonHandler = get_override( "onModuleUnload" );
+            if ( !pythonHandler )
+            {
+                result = kdlib::EventHandler::onModuleUnload( offset, name );
+                break;
+            }
+
+            python::object  resObj = pythonHandler( offset, name );
+
+            if ( resObj.is_none() )
+            {
+                result = kdlib::DebugCallbackNoChange;
+                break;
+            }
+
+            int retVal = python::extract<int>( resObj );
+
+            if ( retVal >= kdlib::DebugCallbackMax )
+            {
+                result = kdlib::DebugCallbackBreak;
+                break;
+            }
+                
+            result = kdlib::DebugCallbackResult(retVal);
+
+        } while( FALSE );
+
+    }
+    catch (const python::error_already_set &) 
+    {
+        printException();
+        result =  kdlib::DebugCallbackBreak;
+    }
+
+    m_pystate = PyEval_SaveThread();
+
+    return result;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 
 } // end namespace pykd
diff --git a/pykd/pyeventhandler.h b/pykd/pyeventhandler.h
index a34a37c..310508f 100644
--- a/pykd/pyeventhandler.h
+++ b/pykd/pyeventhandler.h
@@ -75,6 +75,8 @@ public:
     virtual kdlib::DebugCallbackResult onBreakpoint( kdlib::BREAKPOINT_ID bpId );
     virtual void onExecutionStatusChange( kdlib::ExecutionStatus executionStatus );
     virtual kdlib::DebugCallbackResult onException( const kdlib::ExceptionInfo &exceptionInfo );
+    virtual kdlib::DebugCallbackResult onModuleLoad( kdlib::MEMOFFSET_64 offset, const std::wstring &name );
+    virtual kdlib::DebugCallbackResult onModuleUnload( kdlib::MEMOFFSET_64 offset, const std::wstring &name );
 
 private:
 
diff --git a/pykd/pykdver.h b/pykd/pykdver.h
index b994ab2..2086993 100644
--- a/pykd/pykdver.h
+++ b/pykd/pykdver.h
@@ -2,7 +2,7 @@
 #define PYKD_VERSION_MAJOR      0
 #define PYKD_VERSION_MINOR      3
 #define PYKD_VERSION_SUBVERSION 0
-#define PYKD_VERSION_BUILDNO    1
+#define PYKD_VERSION_BUILDNO    2
 
 #define __VER_STR2__(x) #x
 #define __VER_STR1__(x) __VER_STR2__(x)
diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp
index 9daad97..b82ccae 100644
--- a/pykd/pymod.cpp
+++ b/pykd/pymod.cpp
@@ -840,12 +840,12 @@ BOOST_PYTHON_MODULE( pykd )
          .def( "onBreakpoint", &EventHandler::onBreakpoint,
             "Triggered breakpoint event. Parameter is int: ID of breakpoint\n"
             "For ignore event method must return eventResult.noChange" )
-   //     .def( "onModuleLoad", &EventHandlerWrap::OnModuleLoad,
-   //         "Triggered module load event. Parameter are long: module base, string: module name\n"
-   //         "For ignore event method must return eventResult.noChange" )
-   //     .def( "onModuleUnload", &EventHandlerWrap::OnModuleUnload,
-   //         "Triggered module unload event. Parameter are  long: module base, string: module name\n"
-   //         "For ignore event method must return eventResult.noChange" )
+        .def( "onModuleLoad", &EventHandler::onModuleLoad,
+            "Triggered module load event. Parameter are long: module base, string: module name\n"
+            "For ignore event method must return eventResult.noChange" )
+        .def( "onModuleUnload", &EventHandler::onModuleUnload,
+            "Triggered module unload event. Parameter are  long: module base, string: module name\n"
+            "For ignore event method must return eventResult.noChange" )
         .def( "onException", &EventHandler::onException,
             "Triggered exception event. Parameter - exceptionInfo\n"
             "For ignore event method must return eventResult.noChange" )