From be7d58a4e8444cdf30368f53798dc11e52f69ac9 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 16 Aug 2012 15:41:49 +0000 Subject: [PATCH] [0.2.x] added : eval routine git-svn-id: https://pykd.svn.codeplex.com/svn@78887 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgengine.h | 1 + pykd/pymod.cpp | 2 ++ pykd/win/dbgeng.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/pykd/dbgengine.h b/pykd/dbgengine.h index 2e6d362..bbed4cc 100644 --- a/pykd/dbgengine.h +++ b/pykd/dbgengine.h @@ -17,6 +17,7 @@ bool isKernelDebugging(); void debugGo(); void debugBreak(); +ULONG64 evaluate( const std::wstring &expression ); // debug output void dprint( const std::wstring &str, bool dml = false ); diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index 51eaf17..2c00bb4 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -73,6 +73,8 @@ BOOST_PYTHON_MODULE( pykd ) python::def( "breakin", &debugBreak, "Break into debugger" ); + python::def( "expr", &evaluate, + "Evaluate windbg expression" ); python::def( "go", &debugGo, "Go debugging" ); diff --git a/pykd/win/dbgeng.cpp b/pykd/win/dbgeng.cpp index 9636243..623237b 100644 --- a/pykd/win/dbgeng.cpp +++ b/pykd/win/dbgeng.cpp @@ -168,7 +168,7 @@ void debugGo() } while( currentStatus != DEBUG_STATUS_BREAK && currentStatus != DEBUG_STATUS_NO_DEBUGGEE ); } -///////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// void debugBreak() { @@ -184,7 +184,55 @@ void debugBreak() throw DbgException( "IDebugControl::SetInterrupt" ); } -///////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +ULONG64 evaluate( const std::wstring &expression ) +{ + PyThreadState *pystate = PyEval_SaveThread(); + + HRESULT hres; + ULONG64 value = 0; + + DEBUG_VALUE debugValue = {}; + ULONG remainderIndex = 0; + + hres = g_dbgEng->control->IsPointer64Bit(); + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::IsPointer64Bit failed" ); + + if ( hres == S_OK ) + { + hres = g_dbgEng->control->EvaluateWide( + expression.c_str(), + DEBUG_VALUE_INT64, + &debugValue, + &remainderIndex ); + + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::Evaluate failed" ); + + if ( remainderIndex == expression.length() ) + value = debugValue.I64; + } + else + { + hres = g_dbgEng->control->EvaluateWide( + expression.c_str(), + DEBUG_VALUE_INT32, + &debugValue, + &remainderIndex ); + + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::Evaluate failed" ); + + if ( remainderIndex == expression.length() ) + value = debugValue.I32; + } + + return value; +} + +/////////////////////////////////////////////////////////////////////////////// ULONG64 findModuleBase( const std::string &moduleName ) {