From 89c524741a87bd810b3ab474991981762b23c7d8 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 28 Jul 2011 08:10:10 +0000 Subject: [PATCH] [pykd] added : class disasm git-svn-id: https://pykd.svn.codeplex.com/svn@68358 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgext.cpp | 12 ++++++++++ pykd/disasm.cpp | 49 +++++++++++++++++++++++++++++++++++++++ pykd/disasm.h | 54 +++++++++++++++++++++++++++++++++++++++++++ pykd/pykd.vcproj | 8 +++++++ pykd/pykd_2008.vcproj | 18 +++++++++++---- 5 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 pykd/disasm.cpp create mode 100644 pykd/disasm.h diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index f629a85..35c3a0f 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -28,6 +28,7 @@ #include "dbgbreak.h" #include "dbgio.h" #include "intbase.h" +#include "disasm.h" ////////////////////////////////////////////////////////////////////////////// @@ -346,6 +347,17 @@ BOOST_PYTHON_MODULE( pykd ) "Unload module event. Parameter is instance of dbgModuleClass. " "For ignore event method must return DEBUG_STATUS_NO_CHANGE value" ); + boost::python::class_("disasm", "Class disassemble a processor instructions", boost::python::no_init ) + .def( boost::python::init<>( "constructor" ) ) + .def( boost::python::init( boost::python::args("offset"), "constructor" ) ) + .def( "next", &disasm::next, "Disassemble next instruction" ) + .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" ); + + // исключения boost::python::class_ dbgExceptionClass( "BaseException", "Pykd base exception class", diff --git a/pykd/disasm.cpp b/pykd/disasm.cpp new file mode 100644 index 0000000..b2b3b5f --- /dev/null +++ b/pykd/disasm.cpp @@ -0,0 +1,49 @@ +#include "stdafx.h" +#include "dbgext.h" +#include "disasm.h" +#include "dbgexcept.h" +#include "dbgmem.h" + +///////////////////////////////////////////////////////////////////////////////// + +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( ¤tOffset ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugRegisters::GetInstructionOffset failed" ); + + offset += currentOffset; + } + + hres = + dbgExt->control->Disassemble( + offset, + DEBUG_DISASM_EFFECTIVE_ADDRESS, + buffer, + sizeof(buffer), + &disasmSize, + &endOffset ); + + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::Disassemble failed" ); + + hres = dbgExt->control->GetDisassembleEffectiveOffset( &m_ea ); + if ( FAILED( hres ) ) + m_ea = 0; + + m_length = (ULONG)(endOffset - offset); + + m_disasm = std::string( buffer, disasmSize - 2); +} + +///////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/pykd/disasm.h b/pykd/disasm.h new file mode 100644 index 0000000..95ba472 --- /dev/null +++ b/pykd/disasm.h @@ -0,0 +1,54 @@ +#pragma once + +///////////////////////////////////////////////////////////////////////////////// + +class disasm { + +public: + + disasm( ULONG64 offset = 0) : + m_beginOffset( offset ), + m_currentOffset( offset ) { + doDisasm(); + } + + std::string next() { + m_currentOffset += m_length; + doDisasm(); + return m_disasm; + } + + + std::string instruction() const { + return m_disasm; + } + + ULONG64 begin() const { + return m_beginOffset; + } + + ULONG64 current() const { + return m_currentOffset; + } + + ULONG length() const { + return m_length; + } + + ULONG64 ea() const { + return m_ea; + } + +private: + + void doDisasm(); + + ULONG64 m_beginOffset; + ULONG64 m_currentOffset; + ULONG64 m_ea; + ULONG m_length; + + std::string m_disasm; +}; + +///////////////////////////////////////////////////////////////////////////////// diff --git a/pykd/pykd.vcproj b/pykd/pykd.vcproj index 458d194..41a9c66 100644 --- a/pykd/pykd.vcproj +++ b/pykd/pykd.vcproj @@ -424,6 +424,10 @@ RelativePath=".\dbgtype.cpp" > + + @@ -550,6 +554,10 @@ RelativePath=".\dbgtype.h" > + + diff --git a/pykd/pykd_2008.vcproj b/pykd/pykd_2008.vcproj index 296c39c..aa7b0a7 100644 --- a/pykd/pykd_2008.vcproj +++ b/pykd/pykd_2008.vcproj @@ -1,14 +1,10 @@ @@ -417,6 +413,10 @@ RelativePath=".\dbgtype.cpp" > + + @@ -539,6 +539,14 @@ RelativePath=".\dbgtype.h" > + + + +