From ead4054ccc2dd5c1ed82ae21a73a1ba2dadcfdcf Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Sun, 11 Aug 2013 15:27:13 +0000 Subject: [PATCH] [0.3.x] added : onException event handler git-svn-id: https://pykd.svn.codeplex.com/svn@84645 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgengine.cpp | 32 ++++++++++++++++++++++++++ pykd/dbgengine.h | 4 ++++ pykd/eventhandler.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++ pykd/eventhandler.h | 1 + pykd/pymod.cpp | 35 ++++++++++++++--------------- 5 files changed, 106 insertions(+), 18 deletions(-) diff --git a/pykd/dbgengine.cpp b/pykd/dbgengine.cpp index ff0f031..c238a39 100644 --- a/pykd/dbgengine.cpp +++ b/pykd/dbgengine.cpp @@ -177,4 +177,36 @@ std::wstring printSystemVersion( kdlib::SystemInfo& sysInfo ) /////////////////////////////////////////////////////////////////////////////// +python::list getExceptionInfoParameters( kdlib::ExceptionInfo& exceptionInfo ) +{ + python::list lst; + for ( unsigned long i = 0; i < exceptionInfo.parameterCount; ++i ) + lst.append( exceptionInfo.parameters[i] ); + return lst; +} + +/////////////////////////////////////////////////////////////////////////////// + +std::wstring printExceptionInfo( kdlib::ExceptionInfo& exceptionInfo ) +{ + std::wstringstream sstream; + + sstream << L"FirstChance= " << (exceptionInfo.firstChance ? "True" : "False") << std::endl; + + sstream << L"ExceptionCode= 0x" << std::hex << exceptionInfo.exceptionCode << std::endl; + sstream << L"ExceptionFlags= 0x" << std::hex << exceptionInfo.exceptionFlags << std::endl; + sstream << L"ExceptionRecord= 0x" << std::hex << exceptionInfo.exceptionRecord << std::endl; + sstream << L"ExceptionAddress= 0x" << std::hex << exceptionInfo.exceptionAddress << std::endl; + + for (ULONG i = 0; i < exceptionInfo.parameterCount; ++i) + { + sstream << L"Param[" << std::dec << i << L"]= 0x"; + sstream << std::hex << exceptionInfo.parameters[i] << std::endl; + } + + return sstream.str(); +} + +/////////////////////////////////////////////////////////////////////////////// + } //end namespace pykd diff --git a/pykd/dbgengine.h b/pykd/dbgengine.h index 07180f6..8b4745c 100644 --- a/pykd/dbgengine.h +++ b/pykd/dbgengine.h @@ -29,6 +29,10 @@ kdlib::SystemInfo getSystemVersion(); std::wstring printSystemVersion( kdlib::SystemInfo& sysInfo ); +python::list getExceptionInfoParameters( kdlib::ExceptionInfo& exceptionInfo ); + +std::wstring printExceptionInfo( kdlib::ExceptionInfo& exceptionInfo ); + /////////////////////////////////////////////////////////////////////////////// } //end namespace pykd \ No newline at end of file diff --git a/pykd/eventhandler.cpp b/pykd/eventhandler.cpp index c886ab8..123b8d2 100644 --- a/pykd/eventhandler.cpp +++ b/pykd/eventhandler.cpp @@ -198,4 +198,56 @@ void EventHandler::onExecutionStatusChange( kdlib::ExecutionStatus executionStat /////////////////////////////////////////////////////////////////////////////// +kdlib::DebugCallbackResult EventHandler::onException( const kdlib::ExceptionInfo &exceptionInfo ) +{ + kdlib::DebugCallbackResult result = kdlib::DebugCallbackNoChange; + + PyEval_RestoreThread( m_pystate ); + + try { + + do { + + python::override pythonHandler = get_override( "onException" ); + if ( !pythonHandler ) + { + result = kdlib::EventHandler::onException( exceptionInfo ); + break; + } + + python::object resObj = pythonHandler( exceptionInfo ); + + if ( resObj.is_none() ) + { + result = kdlib::DebugCallbackNoChange; + break; + } + + int retVal = python::extract( resObj ); + + if ( retVal >= kdlib::DebugCallbackMax ) + { + result = kdlib::DebugCallbackBreak; + break; + } + + result = kdlib::DebugCallbackResult(retVal); + + } while( FALSE ); + + } + catch (const python::error_already_set &) + { + printException(); + result = kdlib::DebugCallbackBreak; + } + + m_pystate = PyEval_SaveThread(); + + return result; +} + +/////////////////////////////////////////////////////////////////////////////// + + } // end namespace pykd diff --git a/pykd/eventhandler.h b/pykd/eventhandler.h index 67d9209..54a8e1e 100644 --- a/pykd/eventhandler.h +++ b/pykd/eventhandler.h @@ -44,6 +44,7 @@ public: virtual kdlib::DebugCallbackResult onBreakpoint( kdlib::BREAKPOINT_ID bpId ); virtual void onExecutionStatusChange( kdlib::ExecutionStatus executionStatus ); + virtual kdlib::DebugCallbackResult onException( const kdlib::ExceptionInfo &exceptionInfo ); private: diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index 385b26e..2a04015 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -574,7 +574,7 @@ BOOST_PYTHON_MODULE( pykd ) // .def("__getitem__", &ScopeVars::getVarByIndex ) // .def("__getitem__", &ScopeVars::getVarByName ); - python::class_< kdlib::SystemInfo>( + python::class_( "systemVersion", "Operation system version", python::no_init) //.def_readonly( "platformId", &SystemVersion::platformId, // "Platform ID: VER_PLATFORM_WIN32_NT for NT-based Windows") @@ -594,23 +594,22 @@ BOOST_PYTHON_MODULE( pykd ) "Return object as a string"); - // python::class_< ExceptionInfo, ExceptionInfoPtr, boost::noncopyable >( - // "exceptionInfo", "Exception information", python::no_init ) - // .def_readonly( "FirstChance", &ExceptionInfo::FirstChance, - // "Specifies whether this exception has been previously encountered") - // .def_readonly( "ExceptionCode", &ExceptionInfo::ExceptionCode, - // "The reason the exception occurred") - // .def_readonly( "ExceptionFlags", &ExceptionInfo::ExceptionFlags, - // "The exception flags") - // .def_readonly( "ExceptionRecord", &ExceptionInfo::ExceptionRecord, - // "A pointer to an associated EXCEPTION_RECORD structure") - // .def_readonly( "ExceptionAddress", &ExceptionInfo::ExceptionAddress, - // "The address where the exception occurred") - // .add_property( "Parameters", &ExceptionInfo::getParameters, - // "An array of additional arguments that describe the exception") - // .def( "__str__", &ExceptionInfo::print, - // "Return object as a string"); - + python::class_( + "exceptionInfo", "Exception information", python::no_init ) + .def_readonly( "firstChance", &kdlib::ExceptionInfo::firstChance, + "Specifies whether this exception has been previously encountered") + .def_readonly( "exceptionCode", &kdlib::ExceptionInfo::exceptionCode, + "The reason the exception occurred") + .def_readonly( "exceptionFlags", &kdlib::ExceptionInfo::exceptionFlags, + "The exception flags") + .def_readonly( "exceptionRecord", &kdlib::ExceptionInfo::exceptionRecord, + "A pointer to an associated EXCEPTION_RECORD structure") + .def_readonly( "exceptionAddress", &kdlib::ExceptionInfo::exceptionAddress, + "The address where the exception occurred") + .add_property( "parameters", &getExceptionInfoParameters, + "An array of additional arguments that describe the exception") + .def( "__str__", &printExceptionInfo, + "Return object as a string"); // python::enum_("eventType", "Type of debug event") // .value("Breakpoint", EventTypeBreakpoint)