[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;
PyObject *ExceptionTranslator<MemoryException>::exceptTypeObject = NULL;
PyObject *ExceptionTranslator<WaitEventException>::exceptTypeObject = NULL;
PyObject *ExceptionTranslator<pyDia::Exception>::exceptTypeObject = NULL;
PyObject *ExceptionTranslator<AddSyntheticSymbolException>::exceptTypeObject = NULL;
python::handle<> exceptPyType<python::detail::not_specified>::pyExceptType;
python::handle<> exceptPyType<DbgException>::pyExceptType;
python::handle<> exceptPyType<MemoryException>::pyExceptType;
python::handle<> exceptPyType<WaitEventException>::pyExceptType;
python::handle<> exceptPyType<SymbolException>::pyExceptType;
python::handle<> exceptPyType<pyDia::Exception>::pyExceptType;
python::handle<> exceptPyType<TypeException>::pyExceptType;
python::handle<> exceptPyType<AddSyntheticSymbolException>::pyExceptType;
///////////////////////////////////////////////////////////////////////////////////
}; // end namespace pykd

View File

@ -3,34 +3,65 @@
#include <exception>
#include <string>
///////////////////////////////////////////////////////////////////////////////
namespace pykd {
/////////////////////////////////////////////////////////////////////////////////
template<typename TExcept>
class ExceptionTranslator {
template< class TExcept >
struct exceptPyType{
static python::handle<> pyExceptType;
};
template< class TExcept, class TBaseExcept = python::detail::not_specified >
class exception {
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
void
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
{
@ -84,6 +115,18 @@ private:
/////////////////////////////////////////////////////////////////////////////////
class SymbolException : public DbgException
{
public:
SymbolException( const std::string &desc ) :
DbgException( desc.c_str() )
{}
};
/////////////////////////////////////////////////////////////////////////////////
class WaitEventException : public DbgException
{
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
{
public:

View File

@ -801,92 +801,19 @@ BOOST_PYTHON_MODULE( pykd )
python::scope().attr("diaAmd64Regs") =
genDict(pyDia::Symbol::amd64RegName, pyDia::Symbol::cntAmd64RegName);
// exception:
// exception:
// wrapper for standart python exceptions
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
ExceptionTranslator<MemoryException>::setTypeObject(
python::class_<MemoryException, python::bases<DbgException> >(
"MemoryException", "Target memory access exception class", python::no_init )
.def( "getAddress", &pykd::MemoryException::getAddress, "Return a target address where the exception occurs" )
.ptr()
);
// 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 );
pykd::exception<DbgException>( "BaseException", "Pykd base exception class" );
pykd::exception<MemoryException,DbgException>( "MemoryException", "Target memory access exception class" );
pykd::exception<WaitEventException,DbgException>( "WaitEventException", "Debug interface access exception" );
pykd::exception<SymbolException,DbgException>( "SymbolException", "Symbol exception" );
pykd::exception<pyDia::Exception,SymbolException>( "DiaException", "Debug interface access exception" );
pykd::exception<TypeException,SymbolException>( "TypeException", "type exception" );
pykd::exception<AddSyntheticSymbolException,DbgException>( "AddSynSymbolException", "synthetic symbol exception" );
DEF_PY_CONST_ULONG( DEBUG_CLASS_UNINITIALIZED );
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: ");
@ -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(
ULONG symTag,
const std::string &name,

View File

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

View File

@ -28,7 +28,7 @@ TypeInfoPtr TypeInfo::getTypeInfo( pyDia::SymbolPtr &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()
{
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 )
{
@ -65,7 +65,7 @@ BaseTypeVariant TypeInfo::getValue()
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;
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 ) );

View File

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

View File

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

View File

@ -13,17 +13,6 @@ class TypeInfoTest( unittest.TestCase ):
try: pykd.typeInfo()
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 ):
""" creating typeInfo by the type name """
self.assertEqual( "structTest", target.module.type( "structTest" ).name() )
@ -52,14 +41,41 @@ class TypeInfoTest( unittest.TestCase ):
except pykd.BaseException: pass
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("UInt2B").size() )
self.assertEqual( 4, target.module.type("Int4B").size() )
self.assertEqual( 4, target.module.type("UInt4B").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 ):
ti1 = target.module.type( "classChild" )
@ -114,11 +130,8 @@ class TypeInfoTest( unittest.TestCase ):
self.assertEqual( "listStruct1", ti.deref().name() )
ti = target.module.type("classChild")
try:
ti.deref()
self.assertTrue(False)
except pykd.BaseException:
pass
self.assertRaises( pykd.BaseException, ti.deref );