[0.1.x] fixed : issue #10149 ( handling pykd exceptions )

git-svn-id: https://pykd.svn.codeplex.com/svn@73250 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2012-01-17 16:30:15 +00:00 committed by Mikhail I. Izmestev
parent 75c3423666
commit 6876c4ee3d
9 changed files with 143 additions and 166 deletions

View File

@ -6,12 +6,14 @@ namespace pykd {
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
PyObject *ExceptionTranslator<DbgException>::exceptTypeObject = NULL; python::handle<> exceptPyType<python::detail::not_specified>::pyExceptType;
PyObject *ExceptionTranslator<MemoryException>::exceptTypeObject = NULL; python::handle<> exceptPyType<DbgException>::pyExceptType;
PyObject *ExceptionTranslator<WaitEventException>::exceptTypeObject = NULL; python::handle<> exceptPyType<MemoryException>::pyExceptType;
PyObject *ExceptionTranslator<pyDia::Exception>::exceptTypeObject = NULL; python::handle<> exceptPyType<WaitEventException>::pyExceptType;
PyObject *ExceptionTranslator<AddSyntheticSymbolException>::exceptTypeObject = NULL; python::handle<> exceptPyType<SymbolException>::pyExceptType;
python::handle<> exceptPyType<pyDia::Exception>::pyExceptType;
python::handle<> exceptPyType<TypeException>::pyExceptType;
python::handle<> exceptPyType<AddSyntheticSymbolException>::pyExceptType;
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
}; // end namespace pykd }; // end namespace pykd

View File

@ -3,34 +3,65 @@
#include <exception> #include <exception>
#include <string> #include <string>
///////////////////////////////////////////////////////////////////////////////
namespace pykd { namespace pykd {
/////////////////////////////////////////////////////////////////////////////////
template<typename TExcept> template< class TExcept >
class ExceptionTranslator { struct exceptPyType{
static python::handle<> pyExceptType;
};
template< class TExcept, class TBaseExcept = python::detail::not_specified >
class exception {
public: public:
exception( const std::string& className, const std::string& classDesc )
{
python::handle<> basedtype;
if ( boost::is_same<TBaseExcept, python::detail::not_specified>::value )
{
basedtype = python::handle<>(PyExc_Exception);
}
else
{
basedtype = exceptPyType<TBaseExcept>::pyExceptType;
}
python::dict ob_dict;
ob_dict["__doc__"] = classDesc;
python::tuple ob_bases = python::make_tuple( basedtype );
python::object ob = python::object( python::handle<>(Py_TYPE(basedtype.get()) ) )( className, ob_bases, ob_dict );
python::scope().attr( className.c_str() ) = ob;
exceptPyType<TExcept>::pyExceptType = python::handle<>( ob.ptr() );
python::register_exception_translator<TExcept>( &exceptionTranslate );
}
static static
void void
exceptionTranslate(const TExcept &e ) { exceptionTranslate(const TExcept &e ) {
python::object pyExcept(e);
PyErr_SetObject( exceptTypeObject, pyExcept.ptr() ); python::object exceptObj = python::object( exceptPyType<TExcept>::pyExceptType )( e.what() );
PyErr_SetObject( exceptPyType<TExcept>::pyExceptType.get(), exceptObj.ptr());
} }
static void setTypeObject(PyObject *p) {
exceptTypeObject = p;
python::register_exception_translator<TExcept>( &exceptionTranslate );
}
private:
static PyObject *exceptTypeObject;
}; };
///////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
class PyException : public std::exception class PyException : public std::exception
{ {
@ -84,6 +115,18 @@ private:
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class SymbolException : public DbgException
{
public:
SymbolException( const std::string &desc ) :
DbgException( desc.c_str() )
{}
};
/////////////////////////////////////////////////////////////////////////////////
class WaitEventException : public DbgException class WaitEventException : public DbgException
{ {
public: public:
@ -95,6 +138,26 @@ public:
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class TypeException : public SymbolException
{
public:
TypeException( const std::string &typeName, const std::string &errorStr )
: SymbolException( buildDesc( typeName, errorStr ) )
{}
private:
std::string buildDesc( const std::string &typeName, const std::string &errorStr )
{
std::stringstream sstr;
sstr << typeName << " : " << errorStr;
return sstr.str();
}
};
/////////////////////////////////////////////////////////////////////////////////
class MemoryException : public DbgException class MemoryException : public DbgException
{ {
public: public:

View File

@ -801,92 +801,19 @@ BOOST_PYTHON_MODULE( pykd )
python::scope().attr("diaAmd64Regs") = python::scope().attr("diaAmd64Regs") =
genDict(pyDia::Symbol::amd64RegName, pyDia::Symbol::cntAmd64RegName); genDict(pyDia::Symbol::amd64RegName, pyDia::Symbol::cntAmd64RegName);
// exception: // exception:
// wrapper for standart python exceptions // wrapper for standart python exceptions
python::register_exception_translator<PyException>( &PyException::exceptionTranslate ); python::register_exception_translator<PyException>( &PyException::exceptionTranslate );
// BaseException
ExceptionTranslator<DbgException>::setTypeObject(
python::class_<DbgException>( "BaseException", "Pykd base exception class", python::no_init )
.def( "__str__", &DbgException::getDesc )
.def( "__repr__", &DbgException::getDesc )
.ptr()
);
// DIA exception
ExceptionTranslator<pyDia::Exception>::setTypeObject(
python::class_<pyDia::Exception, python::bases<DbgException> >(
"DiaException", "Debug interface access exception", python::no_init)
.ptr()
);
// Memory exception pykd::exception<DbgException>( "BaseException", "Pykd base exception class" );
ExceptionTranslator<MemoryException>::setTypeObject( pykd::exception<MemoryException,DbgException>( "MemoryException", "Target memory access exception class" );
python::class_<MemoryException, python::bases<DbgException> >( pykd::exception<WaitEventException,DbgException>( "WaitEventException", "Debug interface access exception" );
"MemoryException", "Target memory access exception class", python::no_init ) pykd::exception<SymbolException,DbgException>( "SymbolException", "Symbol exception" );
.def( "getAddress", &pykd::MemoryException::getAddress, "Return a target address where the exception occurs" ) pykd::exception<pyDia::Exception,SymbolException>( "DiaException", "Debug interface access exception" );
.ptr() pykd::exception<TypeException,SymbolException>( "TypeException", "type exception" );
); pykd::exception<AddSyntheticSymbolException,DbgException>( "AddSynSymbolException", "synthetic symbol exception" );
// WaitEventException
ExceptionTranslator<WaitEventException>::setTypeObject(
python::class_<WaitEventException, python::bases<DbgException> >(
"WaitEventException", "Debug interface access exception", python::no_init)
.ptr()
);
// AddSyntheticSymbolException
ExceptionTranslator<AddSyntheticSymbolException>::setTypeObject(
python::class_<AddSyntheticSymbolException, python::bases<DbgException> >(
"AddSynSymbolException", "Add new synthetic symbol exception", python::no_init)
.ptr()
);
//
//#define _DECL_BASE_EXCEPT_STR .def( "__repr__", &pykd::DbgException::print )
//
// // base exception
// python::class_<pykd::DbgException> dbgExceptionClass( "BaseException",
// "Pykd base exception class",
// python::no_init );
// dbgExceptionClass
// .def( python::init<std::string>( python::args("desc"), "constructor" ) )
// .def( "desc", &pykd::DbgException::getDesc,
// "Get exception description" )
// _DECL_BASE_EXCEPT_STR;
// pykd::DbgException::setTypeObject( dbgExceptionClass.ptr() );
// python::register_exception_translator<pykd::DbgException>(
// &pykd::DbgException::exceptionTranslate );
//
// // DIA exceptions
// python::class_<pyDia::Exception, python::bases<DbgException> > diaException(
// "DiaException", "Debug interface access exception", python::no_init);
// diaException
// .def( "hres", &pyDia::Exception::getRes )
// _DECL_BASE_EXCEPT_STR;
// pyDia::Exception::setTypeObject( diaException.ptr() );
// python::register_exception_translator<pyDia::Exception>(
// &pyDia::Exception::exceptionTranslate );
//
// // Memory exception
// python::class_<pykd::MemoryException, python::bases<DbgException> > memException(
// "MemoryException", "Target memory access exception class",
// python::no_init );
// memException.def( "getAddress", &pykd::MemoryException::getAddress, "Return a target address where the exception occurs" );
// pykd::MemoryException::setTypeObject( memException.ptr() );
// python::register_exception_translator<pykd::MemoryException>(
// &pykd::MemoryException::exceptionTranslate );
//
// // Wait debug event exception
// python::class_<WaitEventException, python::bases<DbgException> > waitEventException(
// "WaitEventException", "Debug interface access exception", python::no_init);
// waitEventException
// _DECL_BASE_EXCEPT_STR;
// WaitEventException::setTypeObject( waitEventException.ptr() );
// python::register_exception_translator<WaitEventException>(
// &WaitEventException::exceptionTranslate );
DEF_PY_CONST_ULONG( DEBUG_CLASS_UNINITIALIZED ); DEF_PY_CONST_ULONG( DEBUG_CLASS_UNINITIALIZED );
DEF_PY_CONST_ULONG( DEBUG_CLASS_KERNEL ); DEF_PY_CONST_ULONG( DEBUG_CLASS_KERNEL );

View File

@ -8,7 +8,7 @@ namespace pyDia {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
PyObject *Exception::diaExceptTypeObject = NULL; //PyObject *Exception::diaExceptTypeObject = NULL;
const std::string Exception::descPrefix("pyDia: "); const std::string Exception::descPrefix("pyDia: ");
@ -50,15 +50,6 @@ std::string Exception::makeFullDesc(const std::string &desc, HRESULT hres)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Exception::exceptionTranslate( const Exception &e )
{
boost::python::object pyExcept(e);
PyErr_SetObject( diaExceptTypeObject, pyExcept.ptr() );
}
////////////////////////////////////////////////////////////////////////////////
SymbolPtrList Symbol::findChildrenImpl( SymbolPtrList Symbol::findChildrenImpl(
ULONG symTag, ULONG symTag,
const std::string &name, const std::string &name,

View File

@ -18,16 +18,16 @@ typedef CComPtr< IDiaSession > DiaSessionPtr;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// DIA Exceptions // DIA Exceptions
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class Exception : public pykd::DbgException { class Exception : public pykd::SymbolException {
public: public:
Exception(const std::string &desc, HRESULT hres) Exception(const std::string &desc, HRESULT hres)
: DbgException( makeFullDesc(desc, hres) ) : SymbolException( makeFullDesc(desc, hres) )
, m_hres(hres) , m_hres(hres)
{ {
} }
Exception(const std::string &desc) Exception(const std::string &desc)
: DbgException(descPrefix + desc) : SymbolException(descPrefix + desc)
, m_hres(S_FALSE) , m_hres(S_FALSE)
{ {
} }
@ -35,19 +35,10 @@ public:
HRESULT getRes() const { HRESULT getRes() const {
return m_hres; return m_hres;
} }
static void exceptionTranslate(const Exception &e);
static void setTypeObject(PyObject *p) {
diaExceptTypeObject = p;
}
private: private:
static const std::string descPrefix; static const std::string descPrefix;
static PyObject *diaExceptTypeObject;
static std::string makeFullDesc(const std::string &desc, HRESULT hres); static std::string makeFullDesc(const std::string &desc, HRESULT hres);
HRESULT m_hres; HRESULT m_hres;

View File

@ -28,7 +28,7 @@ TypeInfoPtr TypeInfo::getTypeInfo( pyDia::SymbolPtr &typeSym )
return TypeInfoPtr( new EnumTypeInfo( typeSym ) ); return TypeInfoPtr( new EnumTypeInfo( typeSym ) );
} }
throw DbgException( "type name invalid" ); throw TypeException( typeSym->getName(), "this type is not supported" );
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
@ -36,7 +36,7 @@ TypeInfoPtr TypeInfo::getTypeInfo( pyDia::SymbolPtr &typeSym )
BaseTypeVariant TypeInfo::getValue() BaseTypeVariant TypeInfo::getValue()
{ {
if ( !m_constant ) if ( !m_constant )
throw DbgException( "The type is not a constant and has not a value" ); throw TypeException( getName(), "this type is not a constant and has not a value" );
switch( m_constantValue.vt ) switch( m_constantValue.vt )
{ {
@ -65,7 +65,7 @@ BaseTypeVariant TypeInfo::getValue()
return (LONG64)m_constantValue.llVal; return (LONG64)m_constantValue.llVal;
} }
throw DbgException( "Failed to convert constant type" ); throw TypeException( getName(), "Failed to convert constant type to any integer type" );
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
@ -337,7 +337,7 @@ TypeInfoPtr TypeInfo::getComplexType( pyDia::SymbolPtr &symScope, const std::str
boost::cmatch matchResult; boost::cmatch matchResult;
if ( !boost::regex_match( symName.c_str(), matchResult, typeMatch ) ) if ( !boost::regex_match( symName.c_str(), matchResult, typeMatch ) )
DbgException( "type name invalid" ); TypeException( symName, "type name is invalid" );
TypeInfoPtr lowestTypeInfo = getTypeInfo( symScope, std::string( matchResult[1].first, matchResult[1].second ) ); TypeInfoPtr lowestTypeInfo = getTypeInfo( symScope, std::string( matchResult[1].first, matchResult[1].second ) );

View File

@ -37,7 +37,7 @@ public:
virtual ULONG getSize() = 0; virtual ULONG getSize() = 0;
virtual TypeInfoPtr getField( const std::string &fieldName ) { virtual TypeInfoPtr getField( const std::string &fieldName ) {
throw DbgException( "there is no fields" ); throw TypeException( getName(), "type is not a struct" );
} }
virtual BaseTypeVariant getValue(); virtual BaseTypeVariant getValue();
@ -67,11 +67,11 @@ public:
} }
virtual ULONG getCount() { virtual ULONG getCount() {
throw DbgException( "there is no element" ); throw TypeException( getName(), "type is not an array" );
} }
virtual TypeInfoPtr getElementType() { virtual TypeInfoPtr getElementType() {
throw DbgException( "there is no element" ); throw TypeException( getName(), "type is not an array" );
} }
virtual ULONG getBitOffset() { virtual ULONG getBitOffset() {
@ -83,11 +83,11 @@ public:
} }
virtual python::dict asMap() { virtual python::dict asMap() {
throw DbgException( "there is no fields" ); throw TypeException( getName(), "type cannot be converted to a dict" );
} }
virtual TypeInfoPtr deref() { virtual TypeInfoPtr deref() {
throw DbgException( "type is not a pointer" ); throw TypeException( getName(), "type is not a pointer" );
} }
ULONG getOffset() { ULONG getOffset() {
@ -114,7 +114,7 @@ protected:
static static
TypeInfoPtr getRecurciveComplexType( TypeInfoPtr &lowestType, std::string &suffix, ULONG ptrSize ); TypeInfoPtr getRecurciveComplexType( TypeInfoPtr &lowestType, std::string &suffix, ULONG ptrSize );
ULONG m_offset; ULONG m_offset;
bool m_constant; bool m_constant;
@ -141,10 +141,6 @@ private:
return sizeof(T); return sizeof(T);
} }
virtual TypeInfoPtr getField( const std::string &fieldName ) {
throw DbgException( "there is no such field" );
}
virtual bool isBasicType() { virtual bool isBasicType() {
return true; return true;
} }
@ -278,10 +274,6 @@ public:
virtual ULONG getSize(); virtual ULONG getSize();
virtual TypeInfoPtr getField( const std::string &fieldName ) {
throw DbgException( "there is no such field" );
}
virtual bool isPointer() { virtual bool isPointer() {
return true; return true;
} }
@ -320,10 +312,6 @@ public:
virtual ULONG getSize(); virtual ULONG getSize();
virtual TypeInfoPtr getField( const std::string &fieldName ) {
throw DbgException( "there is no such field" );
}
virtual bool isArray() { virtual bool isArray() {
return true; return true;
} }

View File

@ -21,6 +21,8 @@ class TypedVarTest( unittest.TestCase ):
self.assertEqual( -2, target.module.typedVar( "g_shortValue" ) ) self.assertEqual( -2, target.module.typedVar( "g_shortValue" ) )
self.assertEqual( -4, target.module.typedVar( "g_longValue" ) ) self.assertEqual( -4, target.module.typedVar( "g_longValue" ) )
self.assertEqual( -8, target.module.typedVar( "g_longlongValue" ) ) self.assertEqual( -8, target.module.typedVar( "g_longlongValue" ) )
def testConst(self):
try: try:
self.assertEqual( True, target.module.typedVar( "g_constBoolValue" ) ) self.assertEqual( True, target.module.typedVar( "g_constBoolValue" ) )
except pykd.BaseException: except pykd.BaseException:

View File

@ -13,17 +13,6 @@ class TypeInfoTest( unittest.TestCase ):
try: pykd.typeInfo() try: pykd.typeInfo()
except RuntimeError: pass except RuntimeError: pass
def testBaseTypes(self):
self.assertEqual("Int1B", target.module.type( "Int1B" ).name() )
self.assertEqual("Int2B", target.module.type( "Int2B" ).name() )
self.assertEqual("Int4B", target.module.type( "Int4B" ).name() )
self.assertEqual("Int8B", target.module.type( "Int8B" ).name() )
self.assertEqual("UInt1B", target.module.type( "UInt1B" ).name() )
self.assertEqual("UInt2B", target.module.type( "UInt2B" ).name() )
self.assertEqual("UInt4B", target.module.type( "UInt4B" ).name() )
self.assertEqual("UInt8B", target.module.type( "UInt8B" ).name() )
def testCreateByName( self ): def testCreateByName( self ):
""" creating typeInfo by the type name """ """ creating typeInfo by the type name """
self.assertEqual( "structTest", target.module.type( "structTest" ).name() ) self.assertEqual( "structTest", target.module.type( "structTest" ).name() )
@ -52,14 +41,41 @@ class TypeInfoTest( unittest.TestCase ):
except pykd.BaseException: pass except pykd.BaseException: pass
def testBaseTypes( self ): def testBaseTypes( self ):
self.assertEqual( 1, target.module.type("Char").size() )
self.assertEqual( 2, target.module.type("WChar").size() ) self.assertEqual("Int1B", target.module.type( "Int1B" ).name() )
self.assertEqual("Int2B", target.module.type( "Int2B" ).name() )
self.assertEqual("Int4B", target.module.type( "Int4B" ).name() )
self.assertEqual("Int8B", target.module.type( "Int8B" ).name() )
self.assertEqual("UInt1B", target.module.type( "UInt1B" ).name() )
self.assertEqual("UInt2B", target.module.type( "UInt2B" ).name() )
self.assertEqual("UInt4B", target.module.type( "UInt4B" ).name() )
self.assertEqual("UInt8B", target.module.type( "UInt8B" ).name() )
self.assertEqual("Long", target.module.type( "Long" ).name() )
self.assertEqual("ULong", target.module.type( "ULong" ).name() )
self.assertEqual("Bool", target.module.type( "Bool" ).name() )
self.assertEqual("Char", target.module.type("Char").name() )
self.assertEqual("WChar", target.module.type("WChar").name() )
self.assertEqual( 1, target.module.type("Int1B").size() )
self.assertEqual( 1, target.module.type("UInt1B").size() )
self.assertEqual( 2, target.module.type("Int2B").size() ) self.assertEqual( 2, target.module.type("Int2B").size() )
self.assertEqual( 2, target.module.type("UInt2B").size() ) self.assertEqual( 2, target.module.type("UInt2B").size() )
self.assertEqual( 4, target.module.type("Int4B").size() ) self.assertEqual( 4, target.module.type("Int4B").size() )
self.assertEqual( 4, target.module.type("UInt4B").size() ) self.assertEqual( 4, target.module.type("UInt4B").size() )
self.assertEqual( 8, target.module.type("Int8B").size() ) self.assertEqual( 8, target.module.type("Int8B").size() )
self.assertEqual( 8, target.module.type("UInt8B").size() ) self.assertEqual( 8, target.module.type("UInt8B").size() )
self.assertEqual( 4, target.module.type("Long" ).size() )
self.assertEqual( 4, target.module.type("ULong" ).size() )
self.assertEqual( 1, target.module.type("Bool" ).size() )
self.assertEqual( 1, target.module.type("Char").size() )
self.assertEqual( 2, target.module.type("WChar").size() )
try:
self.assertEqual("Int9B", target.module.type( "Int9B" ).name() )
except pykd.SymbolException:
pass
def testName( self ): def testName( self ):
ti1 = target.module.type( "classChild" ) ti1 = target.module.type( "classChild" )
@ -114,11 +130,8 @@ class TypeInfoTest( unittest.TestCase ):
self.assertEqual( "listStruct1", ti.deref().name() ) self.assertEqual( "listStruct1", ti.deref().name() )
ti = target.module.type("classChild") ti = target.module.type("classChild")
try: self.assertRaises( pykd.BaseException, ti.deref );
ti.deref()
self.assertTrue(False)
except pykd.BaseException:
pass