From e19c2df8721025b2a8b757cbc0e876ad10f63148 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Mon, 19 Sep 2011 07:05:22 +0000
Subject: [PATCH] [0.1.x] added : Module class ( Class representing executable
 module )

git-svn-id: https://pykd.svn.codeplex.com/svn@69867 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/dbgclient.cpp                |  6 ++++-
 pykd/dbgclient.h                  |  7 ++++++
 pykd/dbgext.cpp                   | 13 ++++++++--
 pykd/dbgobj.h                     | 41 +++++++++++++++++++++++++++++++
 pykd/module.cpp                   | 34 +++++++++++++++++++++++++
 pykd/module.h                     | 41 ++++++++++++++++++++++++++++---
 pykd/pykd_2008.vcproj             |  6 ++++-
 pykd_2008.sln => pykd_01_2008.sln |  0
 test/scripts/moduletest.py        | 24 ++++++++++++++++++
 test/scripts/pykdtest.py          |  6 ++---
 10 files changed, 167 insertions(+), 11 deletions(-)
 create mode 100644 pykd/dbgobj.h
 rename pykd_2008.sln => pykd_01_2008.sln (100%)
 create mode 100644 test/scripts/moduletest.py

diff --git a/pykd/dbgclient.cpp b/pykd/dbgclient.cpp
index 0d4e3c7..4a6d0c8 100644
--- a/pykd/dbgclient.cpp
+++ b/pykd/dbgclient.cpp
@@ -16,7 +16,11 @@ DebugClient::DebugClient()
 
     hres = m_client->QueryInterface( __uuidof(IDebugControl4), (void**)&m_control );
     if ( FAILED( hres ) )
-        throw DbgException("QueryInterface IDebugControl4  failed");     
+        throw DbgException("QueryInterface IDebugControl4  failed");    
+
+    hres = m_client->QueryInterface( __uuidof(IDebugSymbols3), (void**)&m_symbols );
+    if ( FAILED( hres ) )
+        throw DbgException("QueryInterface IDebugSymbols3  failed");    
 }
 
 ///////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgclient.h b/pykd/dbgclient.h
index e684d7e..04e6222 100644
--- a/pykd/dbgclient.h
+++ b/pykd/dbgclient.h
@@ -5,6 +5,7 @@
 #include <dbghelp.h>
 
 #include "dbgexcept.h"
+#include "module.h"
 
 /////////////////////////////////////////////////////////////////////////////////
 
@@ -28,10 +29,16 @@ public:
 
     void attachKernel( const std::wstring  &param );
 
+    Module loadModule( const std::string  &moduleName ) {
+        return Module( m_client, moduleName );
+    }
+
+
 private:
 
     CComPtr<IDebugClient5>      m_client;     
     CComPtr<IDebugControl4>     m_control;
+    CComPtr<IDebugSymbols3>     m_symbols;
 };
 
 /////////////////////////////////////////////////////////////////////////////////
diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp
index ab8b668..519342d 100644
--- a/pykd/dbgext.cpp
+++ b/pykd/dbgext.cpp
@@ -82,10 +82,19 @@ BOOST_PYTHON_MODULE( pykd )
         .def( "loadDump", &pykd::DebugClient::loadDump, "Load crash dump" )
         .def( "startProcess", &pykd::DebugClient::startProcess, "Start process for debugging" )
         .def( "attachProcess", &pykd::DebugClient::attachProcess, "Attach debugger to a exsisting process" )
-        .def( "attachKernel", &pykd::DebugClient::attachKernel, "Attach debugger to a target's kernel" );
+        .def( "attachKernel", &pykd::DebugClient::attachKernel, "Attach debugger to a target's kernel" )
+        .def( "loadModule", &pykd::DebugClient::loadModule, "Create instance of Module class" );
 
     python::class_<pykd::Module>("module", "Class representing executable module", python::no_init )
-        .def( python::init<std::string>( "constructor" ) );
+         .def("begin", &pykd::Module::getBase,
+             "Return start address of the module" )
+         .def("end", &pykd::Module::getEnd,
+             "Return end address of the module" )
+         .def("size", &pykd::Module::getSize,
+              "Return size of the module" )
+         .def("name", &pykd::Module::getName,
+             "Return name of the module" );
+        
 
     python::def( "diaOpenPdb", &pyDia::GlobalScope::openPdb, 
         "Open pdb file for quering debug symbols. Return DiaSymbol of global scope");
diff --git a/pykd/dbgobj.h b/pykd/dbgobj.h
new file mode 100644
index 0000000..6875ca1
--- /dev/null
+++ b/pykd/dbgobj.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <dbgeng.h>
+#include "dbgexcept.h"
+
+namespace pykd {
+
+///////////////////////////////////////////////////////////////////////////////////
+
+class DbgObject {
+
+protected:
+
+    DbgObject( IDebugClient5 *client ) {
+
+        HRESULT    hres;
+        hres = client->QueryInterface( __uuidof(IDebugClient5), (void **)&m_client );
+        if ( FAILED( hres ) )
+            throw DbgException("DebugCreate failed");
+
+        hres = client->QueryInterface( __uuidof(IDebugControl4), (void**)&m_control );
+        if ( FAILED( hres ) )
+            throw DbgException("QueryInterface IDebugControl4  failed");    
+
+        hres = client->QueryInterface( __uuidof(IDebugSymbols3), (void**)&m_symbols );
+        if ( FAILED( hres ) )
+            throw DbgException("QueryInterface IDebugSymbols3  failed");  
+    }
+    
+    virtual ~DbgObject() {};
+
+    CComPtr<IDebugClient5>      m_client;     
+    CComPtr<IDebugControl4>     m_control;
+    CComPtr<IDebugSymbols3>     m_symbols;
+};
+
+
+
+///////////////////////////////////////////////////////////////////////////////////
+
+}; // end of namespace pykd
\ No newline at end of file
diff --git a/pykd/module.cpp b/pykd/module.cpp
index a7f619f..4af45db 100644
--- a/pykd/module.cpp
+++ b/pykd/module.cpp
@@ -1,5 +1,39 @@
 #include "stdafx.h"
 
+#include "module.h"
+
+using namespace pykd;
+
+///////////////////////////////////////////////////////////////////////////////////
+
+Module::Module( IDebugClient5 *client, const std::string& moduleName ) : DbgObject( client )
+{
+    HRESULT    hres;
+
+    m_name = moduleName;
+
+    hres = m_symbols->GetModuleByModuleName( moduleName.c_str(), 0, NULL, &m_base );
+    if ( FAILED( hres ) )
+        throw DbgException( "IDebugSymbol::GetModuleByModuleName  failed" ); 
+
+    DEBUG_MODULE_PARAMETERS     moduleParam = { 0 };
+    hres = m_symbols->GetModuleParameters( 1, &m_base, 0, &moduleParam );
+    if ( FAILED( hres ) )
+         throw DbgException( "IDebugSymbol::GetModuleParameters  failed" );    
+
+    m_size = moduleParam.Size;
+}
+
+///////////////////////////////////////////////////////////////////////////////////
+
+
+
+
+
+
+
+
+
 //#include <boost/format.hpp>
 //
 //#include "dbgext.h"
diff --git a/pykd/module.h b/pykd/module.h
index b7ccb6a..2df26f5 100644
--- a/pykd/module.h
+++ b/pykd/module.h
@@ -1,15 +1,41 @@
 #pragma once
 
+#include <string>
+
+#include "dbgobj.h"
+
 namespace pykd {
 
 ///////////////////////////////////////////////////////////////////////////////////
 
-class Module {
+class Module : private DbgObject {
 
 public:
+    
+    Module( IDebugClient5 *client, const std::string& moduleName );
+
+    std::string  getName() {
+        return m_name;
+    }
+
+    ULONG64  getBase() {
+        return m_base;
+    }
+
+    ULONG64 getEnd() {
+        return m_base + m_size;
+    }
+
+    ULONG  getSize() {
+        return m_size;
+    }
+
+private:
+
+    std::string     m_name;
+    ULONG64         m_base;
+    ULONG           m_size;
 
-    Module( const std::string  &moduleName )
-    {}
 };
 
 ///////////////////////////////////////////////////////////////////////////////////
@@ -17,6 +43,15 @@ public:
 };
 
 
+
+
+
+
+
+
+
+
+
 //#include <string>
 //#include <map>
 //
diff --git a/pykd/pykd_2008.vcproj b/pykd/pykd_2008.vcproj
index a5dc5b2..5f27921 100644
--- a/pykd/pykd_2008.vcproj
+++ b/pykd/pykd_2008.vcproj
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="windows-1251"?>
 <VisualStudioProject
 	ProjectType="Visual C++"
-	Version="9.00"
+	Version="9,00"
 	Name="pykd"
 	ProjectGUID="{FE961905-666F-4908-A212-961465F46F13}"
 	RootNamespace="pykd"
@@ -423,6 +423,10 @@
 				RelativePath=".\dbgexcept.h"
 				>
 			</File>
+			<File
+				RelativePath=".\dbgobj.h"
+				>
+			</File>
 			<File
 				RelativePath=".\diawrapper.h"
 				>
diff --git a/pykd_2008.sln b/pykd_01_2008.sln
similarity index 100%
rename from pykd_2008.sln
rename to pykd_01_2008.sln
diff --git a/test/scripts/moduletest.py b/test/scripts/moduletest.py
new file mode 100644
index 0000000..96e72e4
--- /dev/null
+++ b/test/scripts/moduletest.py
@@ -0,0 +1,24 @@
+#
+#
+#
+
+import unittest
+import target
+import pykd
+
+class ModuleTest( unittest.TestCase ):
+    
+    def testCtor( self ):
+         self.assertRaises( RuntimeError, pykd.module )
+         
+    def testName( self ):
+         self.assertEqual( target.moduleName, target.module.name() )
+         
+    def testSize( self ):
+         self.assertNotEqual( 0, target.module.size() )
+         
+    def testBegin( self ):
+         self.assertNotEqual( 0, target.module.begin() )
+             
+    def testEnd( self ):
+         self.assertEqual( target.module.size(), target.module.end() - target.module.begin() )
diff --git a/test/scripts/pykdtest.py b/test/scripts/pykdtest.py
index 63d861a..393a407 100644
--- a/test/scripts/pykdtest.py
+++ b/test/scripts/pykdtest.py
@@ -16,8 +16,6 @@ import typeinfo
 import regtest
 import moduletest
 
-print dir(pykd)
-
 def getTestSuite( singleName = "" ):
     if singleName == "":
         return unittest.TestSuite(
@@ -38,11 +36,11 @@ if __name__ == "__main__":
     print "\nTest module: %s" % targetAppPath
     
     dbg = pykd.dbgClient()
-    
+   
     dbg.startProcess( targetAppPath )
 #    pykd.go()
 
-#    target.module = pykd.loadModule( target.moduleName )
+    target.module = dbg.loadModule( target.moduleName )
     
     suite = getTestSuite()