[pykd] refactored: disasm class

git-svn-id: https://pykd.svn.codeplex.com/svn@69944 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2011-09-22 06:51:22 +00:00
parent 9603376e29
commit 26c2b2d117
3 changed files with 54 additions and 36 deletions

View File

@ -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" );
// èñêëþ÷åíèÿ

View File

@ -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;
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////

View File

@ -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 );
/////////////////////////////////////////////////////////////////////////////////