From 26c2b2d11740b29331801bfbdacbe79c42743830 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Thu, 22 Sep 2011 06:51:22 +0000
Subject: [PATCH] [pykd] refactored: disasm class

git-svn-id: https://pykd.svn.codeplex.com/svn@69944 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/dbgext.cpp |  7 ++++---
 pykd/disasm.cpp | 56 ++++++++++++++++++++++++++++++-------------------
 pykd/disasm.h   | 27 ++++++++++++++----------
 3 files changed, 54 insertions(+), 36 deletions(-)

diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp
index 9a01a05..9d6fc40 100644
--- a/pykd/dbgext.cpp
+++ b/pykd/dbgext.cpp
@@ -221,7 +221,6 @@ BOOST_PYTHON_MODULE( pykd )
         "Delete synthetic symbols by virtual address" );
     boost::python::def( "delSynSymbolsMask", &delSyntheticSymbolsMask, 
         "Delete synthetic symbols by mask of module and symbol name");
-    boost::python::def( "assembly", &assembly, "Assemble a single processor instruction" );
 
     boost::python::class_<TypeInfo>( "typeInfo",
         "Class representing non-primitive type info: structure, union, etc. attributes is a fields of non-primitive type" )
@@ -380,12 +379,14 @@ BOOST_PYTHON_MODULE( pykd )
     boost::python::class_<disasm>("disasm", "Class disassemble a processor instructions", boost::python::no_init )
         .def( boost::python::init<>( "constructor" ) )
         .def( boost::python::init<ULONG64>( boost::python::args("offset"), "constructor" ) )
-        .def( "next", &disasm::next, "Disassemble next instruction" )
+        .def( "disasm", &disasm::disassemble, "Disassemble next instruction" )
+        .def( "sasm", &disasm::assembly, "Insert assemblied instuction to current offset" )
         .def( "begin", &disasm::begin, "Return begin offset" )
         .def( "current", &disasm::current, "Return current offset" )
         .def( "length", &disasm::length, "Return current instruction length" )
         .def( "instruction", &disasm::instruction, "Returm current disassembled instruction" )
-        .def( "ea", &disasm::ea, "Return effective address for last disassembled instruction or 0" );
+        .def( "ea", &disasm::ea, "Return effective address for last disassembled instruction or 0" )
+        .def( "reset", &disasm::reset, "Reset current offset to begin" );
 
 
     // ����������
diff --git a/pykd/disasm.cpp b/pykd/disasm.cpp
index 52498fb..4780633 100644
--- a/pykd/disasm.cpp
+++ b/pykd/disasm.cpp
@@ -2,7 +2,26 @@
 #include "dbgext.h"
 #include "disasm.h"
 #include "dbgexcept.h"
-#include "dbgmem.h"
+
+/////////////////////////////////////////////////////////////////////////////////
+
+disasm::disasm( ULONG64 offset )
+{
+    HRESULT     hres;
+
+    m_beginOffset = addr64(offset);
+
+    if ( m_beginOffset == 0 )
+    {
+        hres = dbgExt->registers->GetInstructionOffset( &m_beginOffset );
+        if ( FAILED( hres ) )
+            throw DbgException( "IDebugRegisters::GetInstructionOffset failed" );
+    }
+
+    m_currentOffset = m_beginOffset;
+
+    doDisasm();
+}
 
 /////////////////////////////////////////////////////////////////////////////////
 
@@ -11,23 +30,11 @@ void disasm::doDisasm()
     HRESULT     hres;
     char        buffer[0x100];
     ULONG       disasmSize = 0;
-    ULONG64     offset = addr64(m_currentOffset);
     ULONG64     endOffset = 0;
-
-    if ( m_beginOffset == 0 )
-    {
-        ULONG64     currentOffset = 0;
-
-        hres = dbgExt->registers->GetInstructionOffset( &currentOffset );
-        if ( FAILED( hres ) )
-            throw DbgException( "IDebugRegisters::GetInstructionOffset failed" );
-
-        offset += currentOffset;
-    }
-
+    
     hres = 
         dbgExt->control->Disassemble(
-            offset,
+            m_currentOffset,
             DEBUG_DISASM_EFFECTIVE_ADDRESS,
             buffer,
             sizeof(buffer),
@@ -41,24 +48,29 @@ void disasm::doDisasm()
     if ( FAILED( hres ) )
         m_ea = 0;
 
-    m_length = (ULONG)(endOffset - offset);
+    m_length = (ULONG)(endOffset - m_currentOffset);
 
     m_disasm = std::string( buffer, disasmSize - 2);
 }
 
 /////////////////////////////////////////////////////////////////////////////////
 
-ULONG64
-assembly( ULONG64 offset, const std::string &instr )
+std::string
+disasm::assembly( const std::string &instr )
 {
     HRESULT     hres;
 
     ULONG64     endOffset = 0;
-    hres = dbgExt->control->Assemble( offset, instr.c_str(), &endOffset );
+    hres = dbgExt->control->Assemble( m_currentOffset, instr.c_str(), &endOffset );
     if ( FAILED( hres ) )
-        throw DbgException( "IDebugControl::Assemble failed" );      
+        throw DbgException( "IDebugControl::Assemble failed" );
 
-    return endOffset;
+    m_currentOffset = endOffset;
+
+    doDisasm();
+
+    return m_disasm;
 }
 
-/////////////////////////////////////////////////////////////////////////////////
\ No newline at end of file
+/////////////////////////////////////////////////////////////////////////////////
+
diff --git a/pykd/disasm.h b/pykd/disasm.h
index 890e672..5c54503 100644
--- a/pykd/disasm.h
+++ b/pykd/disasm.h
@@ -1,23 +1,31 @@
 #pragma once
 
+#include "dbgmem.h"
+
 /////////////////////////////////////////////////////////////////////////////////
 
 class disasm {
 
 public:
 
-    disasm( ULONG64 offset = 0) :
-      m_beginOffset( offset ),
-      m_currentOffset( offset ) {
-          doDisasm();
-      }
+    disasm( ULONG64 offset = 0);
 
-      std::string    next() {
+    std::string  disassemble() {
+        std::string  s = m_disasm;
         m_currentOffset += m_length;
         doDisasm();
-        return m_disasm;
-      }
+        return s;
+    }
 
+    std::string  reset() {
+
+        m_currentOffset = m_beginOffset;
+        doDisasm();
+        return m_disasm;
+    }
+
+    std::string
+    assembly( const std::string &instr );
 
     std::string    instruction() const {
         return m_disasm;
@@ -51,7 +59,4 @@ private:
     std::string     m_disasm;
 };
 
-ULONG64
-assembly( ULONG64 offset, const std::string &instr );
-
 /////////////////////////////////////////////////////////////////////////////////