[0.1.x] refactored: exceptions classes

git-svn-id: https://pykd.svn.codeplex.com/svn@73081 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2012-01-11 07:30:51 +00:00 committed by Mikhail I. Izmestev
parent d0c6b024e1
commit 43cc22a89f
7 changed files with 156 additions and 324 deletions

View File

@ -25,7 +25,7 @@ void Registers::getI386Context(
HRESULT hres = advanced->GetThreadContext(&Context, sizeof(Context)); HRESULT hres = advanced->GetThreadContext(&Context, sizeof(Context));
if (S_OK != hres) if (S_OK != hres)
throw Exception( pykd::buildExceptDesc("IDebugAdvanced2::GetThreadContext", hres) ); throw Exception( "IDebugAdvanced2::GetThreadContext", hres );
m_regValues[CV_REG_DR0] = Context.Dr0; m_regValues[CV_REG_DR0] = Context.Dr0;
m_regValues[CV_REG_DR1] = Context.Dr1; m_regValues[CV_REG_DR1] = Context.Dr1;
@ -73,7 +73,7 @@ void Registers::getAmd64Context(
HRESULT hres = advanced->GetThreadContext(&Context, sizeof(Context)); HRESULT hres = advanced->GetThreadContext(&Context, sizeof(Context));
if (S_OK != hres) if (S_OK != hres)
throw Exception( pykd::buildExceptDesc("IDebugAdvanced2::GetThreadContext", hres) ); throw Exception( "IDebugAdvanced2::GetThreadContext", hres);
m_regValues[CV_AMD64_MXCSR] = Context.MxCsr; m_regValues[CV_AMD64_MXCSR] = Context.MxCsr;
@ -122,7 +122,7 @@ Registers::Registers(
{ {
HRESULT hres = control->GetExecutingProcessorType(&m_processorType); HRESULT hres = control->GetExecutingProcessorType(&m_processorType);
if (S_OK != hres) if (S_OK != hres)
throw Exception( pykd::buildExceptDesc("IDebugControl::GetExecutingProcessorType", hres) ); throw Exception( "IDebugControl::GetExecutingProcessorType", hres );
switch (m_processorType) switch (m_processorType)
{ {

View File

@ -42,7 +42,7 @@ private:
T retValue; T retValue;
HRESULT hres = (m_control->*method)(&retValue); HRESULT hres = (m_control->*method)(&retValue);
if (S_OK != hres) if (S_OK != hres)
throw DbgException( buildExceptDesc(methodName, hres) ); throw DbgException( methodName, hres);
return retValue; return retValue;
} }
#define getDbgControl(method) \ #define getDbgControl(method) \

View File

@ -1,87 +1,16 @@
#include "stdafx.h" #include "stdafx.h"
#include "dbgexcept.h" #include "dbgexcept.h"
#include "diawrapper.h"
namespace pykd { namespace pykd {
//////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
PyObject *DbgException::baseExceptTypeObject = NULL; PyObject *ExceptionTranslator<DbgException>::exceptTypeObject = NULL;
PyObject *MemoryException::memoryExceptionTypeObject = NULL; PyObject *ExceptionTranslator<MemoryException>::exceptTypeObject = NULL;
PyObject *WaitEventException::waitEventExceptTypeObject = NULL; PyObject *ExceptionTranslator<WaitEventException>::exceptTypeObject = NULL;
PyObject *ExceptionTranslator<pyDia::Exception>::exceptTypeObject = NULL;
///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
void DbgException::exceptionTranslate( const DbgException &e )
{
python::object pyExcept(e);
PyErr_SetObject( baseExceptTypeObject, pyExcept.ptr() );
}
/////////////////////////////////////////////////////////////////////////////////
void MemoryException::exceptionTranslate( const MemoryException &e )
{
python::object pyExcept(e);
PyErr_SetObject( memoryExceptionTypeObject, pyExcept.ptr() );
}
/////////////////////////////////////////////////////////////////////////////////
std::string buildExceptDesc(PCSTR routineName, HRESULT hres)
{
std::stringstream sstream;
sstream << "Call " << routineName << " failed\n";
sstream << "HRESULT 0x" << std::hex << hres;
return sstream.str();
}
/////////////////////////////////////////////////////////////////////////////////
void WaitEventException::exceptionTranslate( const WaitEventException &e )
{
python::object pyExcept(e);
PyErr_SetObject( waitEventExceptTypeObject, pyExcept.ptr() );
}
/////////////////////////////////////////////////////////////////////////////////
}; // end namespace pykd }; // end namespace pykd
//PyObject *eventExceptionType = NULL;
//PyObject *typeExceptionType = NULL;
//PyObject *memoryExceptionType = NULL;
//
//void WaitEventException::exceptionTranslate( const WaitEventException &e )
//{
// boost::python::object pyExcept(e);
//
// PyErr_SetObject( eventExceptionType, pyExcept.ptr());
//}
//
///////////////////////////////////////////////////////////////////////////////////
//
//void TypeException::exceptionTranslate( const TypeException &e )
//{
// boost::python::object pyExcept(e);
//
// PyErr_SetObject( typeExceptionType, pyExcept.ptr());
//}
//
///////////////////////////////////////////////////////////////////////////////////
//
//void MemoryException::translate( const MemoryException &e )
//{
// boost::python::object pyExcept(e);
//
// PyErr_SetObject( memoryExceptionType, pyExcept.ptr());
//}
//
///////////////////////////////////////////////////////////////////////////////////

View File

@ -5,30 +5,51 @@
namespace pykd { namespace pykd {
/////////////////////////////////////////////////////////////////////////////////
template<typename TExcept>
class ExceptionTranslator {
public:
static
void
exceptionTranslate(const TExcept &e ) {
python::object pyExcept(e);
PyErr_SetObject( exceptTypeObject, pyExcept.ptr() );
}
static void setTypeObject(PyObject *p) {
exceptTypeObject = p;
python::register_exception_translator<TExcept>( &exceptionTranslate );
}
private:
static PyObject *exceptTypeObject;
};
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class PyException class PyException : public std::exception
{ {
public: public:
PyException( PyObject* pyObj, const std::string &desc ) : PyException( PyObject* pyObj, const std::string &desc ) :
m_typeObj( pyObj ), std::exception( desc.c_str() ),
m_desc( desc ) m_typeObj( pyObj )
{} {}
static static
void void
exceptionTranslate(const PyException &e ) { exceptionTranslate(const PyException &e ) {
PyErr_SetString( e.m_typeObj, e.m_desc.c_str() ); PyErr_SetString( e.m_typeObj, e.what() );
} }
private: private:
PyObject* m_typeObj; PyObject* m_typeObj;
std::string m_desc;
}; };
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -41,27 +62,38 @@ public:
std::exception( desc.c_str() ) std::exception( desc.c_str() )
{} {}
DbgException( const std::string &methodName, HRESULT hres ) :
std::exception( buildHresultDesc( methodName, hres ).c_str() )
{}
const char* getDesc() const { const char* getDesc() const {
return what(); return what();
} }
static
void
exceptionTranslate(const DbgException &e );
static void setTypeObject(PyObject *p) {
baseExceptTypeObject = p;
}
std::string print() {
return what();
}
private: private:
static PyObject *baseExceptTypeObject;
std::string buildHresultDesc( const std::string &methodName, HRESULT hres )
{
std::stringstream sstream;
sstream << "Call " << methodName << " failed\n";
sstream << "HRESULT 0x" << std::hex << hres;
return sstream.str();
}
}; };
/////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class WaitEventException : public DbgException
{
public:
WaitEventException()
: DbgException( "None of the targets could generate events" )
{}
};
/////////////////////////////////////////////////////////////////////////////////
class MemoryException : public DbgException class MemoryException : public DbgException
{ {
@ -69,17 +101,9 @@ public:
MemoryException( ULONG64 targetAddr, bool phyAddr = false ) : MemoryException( ULONG64 targetAddr, bool phyAddr = false ) :
m_targetAddress( targetAddr ), m_targetAddress( targetAddr ),
DbgException( MemoryException::DescMaker( targetAddr, phyAddr ).desc() ) DbgException( buildDesc( targetAddr, phyAddr ) )
{} {}
static
void
exceptionTranslate( const MemoryException &e );
static void setTypeObject(PyObject *p) {
memoryExceptionTypeObject = p;
}
ULONG64 ULONG64
getAddress() const { getAddress() const {
return m_targetAddress; return m_targetAddress;
@ -89,175 +113,19 @@ private:
ULONG64 m_targetAddress; ULONG64 m_targetAddress;
static PyObject *memoryExceptionTypeObject; std::string buildDesc( ULONG64 addr, bool phyAddr )
class DescMaker {
public:
DescMaker( ULONG64 addr, bool phyAddr )
{
std::stringstream sstr;
if ( phyAddr )
sstr << "Memory exception at 0x" << std::hex << addr << " target physical address";
else
sstr << "Memory exception at 0x" << std::hex << addr << " target virtual address";
m_desc = sstr.str();
}
const std::string&
desc() const {
return m_desc;
}
private:
std::string m_desc;
};
};
std::string buildExceptDesc(PCSTR routineName, HRESULT hres);
///////////////////////////////////////////////////////////////////////////////////
class WaitEventException : public DbgException
{
public:
WaitEventException()
: DbgException( "None of the targets could generate events" )
{ {
} std::stringstream sstr;
if ( phyAddr )
sstr << "Memory exception at 0x" << std::hex << addr << " target physical address";
else
sstr << "Memory exception at 0x" << std::hex << addr << " target virtual address";
static void exceptionTranslate(const WaitEventException &e); return sstr.str();
static void setTypeObject(PyObject *p) {
waitEventExceptTypeObject = p;
} }
private:
static PyObject *waitEventExceptTypeObject;
}; };
/////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
}; // namespace pykd }; // namespace pykd
///////////////////////////////////////////////////////////////////////////////////
//
//class WaitEventException : public DbgException
//{
//public:
// WaitEventException()
// : DbgException( "none of the targets could generate events" )
// {
// }
//
// static void exceptionTranslate(const WaitEventException &e);
//};
//
///////////////////////////////////////////////////////////////////////////////////
//
//class TypeException : public DbgException
//{
//public:
//
// TypeException() :
// DbgException( "type operation invalid" )
// {}
//
// TypeException( const std::string &desc ) :
// DbgException( desc )
// {}
//
// static
// void
// exceptionTranslate(const TypeException &e );
//
//};
//
///////////////////////////////////////////////////////////////////////////////////
//
//class IndexException : public DbgException
//{
//public:
//
// IndexException() :
// DbgException( "Index out of range" )
// {}
//
// static
// void
// translate(const IndexException &e ) {
// PyErr_SetString(PyExc_IndexError, "Index out of range");
// }
//};
//
///////////////////////////////////////////////////////////////////////////////////
//
//class MemoryException : public DbgException
//{
//public:
//
// MemoryException( ULONG64 targetAddr ) :
// m_targetAddress( targetAddr ),
// DbgException( MemoryException::DescMaker( targetAddr, false ).desc() )
// {}
//
// MemoryException( ULONG64 targetAddr, bool phyAddr ) :
// m_targetAddress( targetAddr ),
// DbgException( MemoryException::DescMaker( targetAddr, phyAddr ).desc() )
// {}
//
// static
// void
// translate( const MemoryException &e );
//
// ULONG64
// getAddress() const {
// return m_targetAddress;
// }
//
//private:
//
// ULONG64 m_targetAddress;
//
// class DescMaker {
// public:
// DescMaker( ULONG64 addr, bool phyAddr )
// {
// std::stringstream sstr;
// if ( phyAddr )
// sstr << "Memory exception at 0x" << std::hex << addr << " target physical address";
// else
// sstr << "Memory exception at 0x" << std::hex << addr << " target virtual address";
// m_desc = sstr.str();
// }
//
// const std::string&
// desc() const {
// return m_desc;
// }
//
// private:
// std::string m_desc;
// };
//};
//
///////////////////////////////////////////////////////////////////////////////////
//
//extern PyObject *baseExceptionType;
//extern PyObject *eventExceptionType;
//extern PyObject *typeExceptionType;
//extern PyObject *memoryExceptionType;
//
///////////////////////////////////////////////////////////////////////////////////

View File

@ -786,53 +786,88 @@ 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<pykd::PyException>( &PyException::exceptionTranslate ); python::register_exception_translator<PyException>( &PyException::exceptionTranslate );
#define _DECL_BASE_EXCEPT_STR .def( "__repr__", &pykd::DbgException::print ) // BaseException
ExceptionTranslator<DbgException>::setTypeObject(
python::class_<DbgException>( "BaseException", "Pykd base exception class", python::no_init )
.def( "__str__", &DbgException::getDesc )
.def( "__repr__", &DbgException::getDesc )
.ptr()
);
// base exception // DIA exception
python::class_<pykd::DbgException> dbgExceptionClass( "BaseException", ExceptionTranslator<pyDia::Exception>::setTypeObject(
"Pykd base exception class", python::class_<pyDia::Exception, python::bases<DbgException> >(
python::no_init ); "DiaException", "Debug interface access exception", python::no_init)
dbgExceptionClass .ptr()
.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 // Memory exception
python::class_<pykd::MemoryException, python::bases<DbgException> > memException( ExceptionTranslator<MemoryException>::setTypeObject(
"MemoryException", "Target memory access exception class", python::class_<MemoryException, python::bases<DbgException> >(
python::no_init ); "MemoryException", "Target memory access exception class", python::no_init )
memException.def( "getAddress", &pykd::MemoryException::getAddress, "Return a target address where the exception occurs" ); .def( "getAddress", &pykd::MemoryException::getAddress, "Return a target address where the exception occurs" )
pykd::MemoryException::setTypeObject( memException.ptr() ); .ptr()
python::register_exception_translator<pykd::MemoryException>( );
&pykd::MemoryException::exceptionTranslate );
// Wait debug event exception // WaitEventException
python::class_<WaitEventException, python::bases<DbgException> > waitEventException( ExceptionTranslator<WaitEventException>::setTypeObject(
"WaitEventException", "Debug interface access exception", python::no_init); python::class_<WaitEventException, python::bases<DbgException> >(
waitEventException "WaitEventException", "Debug interface access exception", python::no_init)
_DECL_BASE_EXCEPT_STR; .ptr()
WaitEventException::setTypeObject( waitEventException.ptr() ); );
python::register_exception_translator<WaitEventException>(
&WaitEventException::exceptionTranslate );
//
//#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

@ -42,7 +42,7 @@ void SyntheticSymbols::add(
DEBUG_ADDSYNTHSYM_DEFAULT, DEBUG_ADDSYNTHSYM_DEFAULT,
&dbgModuleAndId); &dbgModuleAndId);
if ( FAILED( hres ) ) if ( FAILED( hres ) )
throw DbgException(buildExceptDesc("IDebugSymbols3::AddSyntheticSymbol", hres)); throw DbgException("IDebugSymbols3::AddSyntheticSymbol", hres);
// add/update symbol for target module (in internal map) // add/update symbol for target module (in internal map)
SymbolsScopedLock lock(*m_allSymbolsLock); SymbolsScopedLock lock(*m_allSymbolsLock);
@ -93,7 +93,7 @@ ULONG SyntheticSymbols::removeByMask(
moduleName + "!" + symName, moduleName + "!" + symName,
dbgSymbols); dbgSymbols);
if (FAILED(hres)) if (FAILED(hres))
throw DbgException(buildExceptDesc("IDebugSymbols3::GetSymbolEntriesByName", hres)); throw DbgException("IDebugSymbols3::GetSymbolEntriesByName", hres);
if (dbgSymbols.empty()) if (dbgSymbols.empty())
return 0; return 0;

View File

@ -69,7 +69,7 @@ SyntheticSymbols::ModuleId SynSymHelper::modByBase(ULONG64 moduleBase)
0, 0,
&dbgModuleParameters); &dbgModuleParameters);
if ( FAILED( hres ) ) if ( FAILED( hres ) )
throw DbgException(buildExceptDesc("IDebugSymbols3::GetModuleParameters", hres)); throw DbgException( "IDebugSymbols3::GetModuleParameters", hres );
return SyntheticSymbols::ModuleId(dbgModuleParameters); return SyntheticSymbols::ModuleId(dbgModuleParameters);
} }
@ -84,7 +84,7 @@ SyntheticSymbols::ModuleId SynSymHelper::modByOffset(
HRESULT hres = HRESULT hres =
m_symbols.GetModuleByOffset(moduleOffset, 0, NULL, &moduleBase); m_symbols.GetModuleByOffset(moduleOffset, 0, NULL, &moduleBase);
if ( FAILED( hres ) ) if ( FAILED( hres ) )
throw DbgException(buildExceptDesc("IDebugSymbols3::GetModuleByOffset", hres)); throw DbgException( "IDebugSymbols3::GetModuleByOffset", hres );
return modByBase(moduleBase); return modByBase(moduleBase);
} }