From 3333bdac13d231e4e463a25100a607ad4a88fbda Mon Sep 17 00:00:00 2001 From: "SND\\EreTIk_cp" Date: Wed, 21 Sep 2011 12:41:05 +0000 Subject: [PATCH] [*] pyDia: methods of Symbols prepared for C++ calls, macro call -> callSymbolT method git-svn-id: https://pykd.svn.codeplex.com/svn@69927 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgext.cpp | 2 +- pykd/diawrapper.cpp | 66 ++++++++++++--------------------------------- pykd/diawrapper.h | 40 ++++++++++++++++++++++++--- pykd/utils.h | 19 ++++++++++++- 4 files changed, 72 insertions(+), 55 deletions(-) diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index 1feec3f..798140b 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -109,7 +109,7 @@ BOOST_PYTHON_MODULE( pykd ) "Open pdb file for quering debug symbols. Return DiaSymbol of global scope"); python::class_("DiaSymbol", "class wrapper for MS DIA Symbol" ) - .def( "findEx", &pyDia::Symbol::findChildrenImpl, + .def( "findEx", &pyDia::Symbol::findChildrenEx, "Retrieves the children of the symbol" ) .def( "find", &pyDia::Symbol::findChildren, "Retrieves the children of the symbol" ) diff --git a/pykd/diawrapper.cpp b/pykd/diawrapper.cpp index 976b177..be2af2d 100644 --- a/pykd/diawrapper.cpp +++ b/pykd/diawrapper.cpp @@ -1,9 +1,6 @@ #include "stdafx.h" -#include -#include - #include "diawrapper.h" #include "utils.h" @@ -96,38 +93,8 @@ const size_t Symbol::cntBasicTypeName = _countof(Symbol::basicTypeName); //////////////////////////////////////////////////////////////////////////////// -#define callSymbol(retType, method) \ -do { \ - throwIfNull(__FUNCTION__); \ - retType retValue; \ - HRESULT hres = m_symbol->##method(&retValue); \ - if (S_OK != hres) \ - throw Exception("Call IDiaSymbol::" #method, hres); \ - return retValue; \ -} while(false) - -#define callSymbolDword(method) callSymbol(DWORD, method) -#define callSymbolUlonglong(method) callSymbol(ULONGLONG, method) - -#define callSymbolObjectFromSymbol(method) \ -do { \ - throwIfNull(__FUNCTION__); \ - DiaSymbolPtr retSymbol; \ - HRESULT hres = m_symbol->##method(&retSymbol); \ - if (S_OK != hres) \ - throw Exception("Call IDiaSymbol::" #method, hres); \ - return python::object( Symbol(retSymbol) ); \ -} while(false) - -#define callSymbolStr(method) \ -do { \ - throwIfNull(__FUNCTION__); \ - autoBstr bstrName; \ - HRESULT hres = m_symbol->##method(&bstrName); \ - if (S_OK != hres) \ - throw Exception("Call IDiaSymbol" #method, hres); \ - return bstrName.asStr(); \ -} while(false) +#define callSymbol(method) \ + callSymbolT( &IDiaSymbol::##method, __FUNCTION__, #method) //////////////////////////////////////////////////////////////////////////////// @@ -171,7 +138,7 @@ void Exception::exceptionTranslate( const Exception &e ) //////////////////////////////////////////////////////////////////////////////// -python::list Symbol::findChildrenImpl( +std::list< Symbol > Symbol::findChildrenImpl( ULONG symTag, const std::string &name, DWORD nameCmpFlags @@ -189,12 +156,12 @@ python::list Symbol::findChildrenImpl( if (S_OK != hres) throw Exception("Call IDiaSymbol::findChildren", hres); - python::list childList; + std::list< Symbol > childList; DiaSymbolPtr child; ULONG celt; while ( SUCCEEDED(symbols->Next(1, &child, &celt)) && (celt == 1) ) - childList.append( Symbol(child) ); + childList.push_back( Symbol(child) ); return childList; } @@ -203,49 +170,50 @@ python::list Symbol::findChildrenImpl( ULONGLONG Symbol::getSize() { - callSymbolUlonglong(get_length); + return callSymbol(get_length); } //////////////////////////////////////////////////////////////////////////////// std::string Symbol::getName() { - callSymbolStr(get_name); + autoBstr retValue( callSymbol(get_name) ); + return retValue.asStr(); } //////////////////////////////////////////////////////////////////////////////// -python::object Symbol::getType() +Symbol Symbol::getType() { - callSymbolObjectFromSymbol(get_type); + return Symbol( callSymbol(get_type) ); } //////////////////////////////////////////////////////////////////////////////// -python::object Symbol::getIndexType() +Symbol Symbol::getIndexType() { - callSymbolObjectFromSymbol(get_arrayIndexType); + return Symbol( callSymbol(get_arrayIndexType) ); } //////////////////////////////////////////////////////////////////////////////// ULONG Symbol::getSymTag() { - callSymbolDword(get_symTag); + return callSymbol(get_symTag); } //////////////////////////////////////////////////////////////////////////////// ULONG Symbol::getRva() { - callSymbolDword(get_relativeVirtualAddress); + return callSymbol(get_relativeVirtualAddress); } //////////////////////////////////////////////////////////////////////////////// ULONG Symbol::getLocType() { - callSymbolDword(get_locationType); + return callSymbol(get_locationType); } //////////////////////////////////////////////////////////////////////////////// @@ -313,14 +281,14 @@ bool Symbol::isBasicType() ULONG Symbol::getBaseType() { - callSymbolDword(get_baseType); + return callSymbol(get_baseType); } //////////////////////////////////////////////////////////////////////////////// ULONG Symbol::getBitPosition() { - callSymbolDword(get_bitPosition); + return callSymbol(get_bitPosition); } //////////////////////////////////////////////////////////////////////////////// diff --git a/pykd/diawrapper.h b/pykd/diawrapper.h index f990da2..3add540 100644 --- a/pykd/diawrapper.h +++ b/pykd/diawrapper.h @@ -2,6 +2,8 @@ #pragma once #include + +#include "utils.h" #include "dbgexcept.h" namespace pyDia { @@ -56,26 +58,35 @@ class Symbol { public: Symbol() {} - python::list findChildrenImpl( + std::list< Symbol > findChildrenImpl( ULONG symTag, const std::string &name, DWORD nameCmpFlags ); + python::list findChildrenEx( + ULONG symTag, + const std::string &name, + DWORD nameCmpFlags + ) + { + return toPyList( findChildrenImpl(symTag, name, nameCmpFlags) ); + } + python::list findChildren( const std::string &name ) { - return findChildrenImpl(SymTagNull, name, nsfCaseSensitive); + return toPyList( findChildrenImpl(SymTagNull, name, nsfCaseSensitive) ); } ULONGLONG getSize(); std::string getName(); - python::object getType(); + Symbol getType(); - python::object getIndexType(); + Symbol getIndexType(); ULONG getSymTag(); @@ -107,6 +118,23 @@ public: protected: + template + TRet callSymbolT( + HRESULT(STDMETHODCALLTYPE IDiaSymbol::*method)(TRet *), + const char *funcName, + const char *methodName + ) + { + throwIfNull(funcName); + + TRet retValue; + HRESULT hres = (m_symbol->*method)(&retValue); + if (S_OK != hres) + throw Exception(std::string("Call IDiaSymbol::") + methodName); + + return retValue; + } + void throwIfNull(const char *desc) { if (!m_symbol) @@ -117,6 +145,10 @@ protected: m_symbol = _symbol.Detach(); } + Symbol(__in IDiaSymbol *_symbol) { + m_symbol = _symbol; + } + DiaSymbolPtr m_symbol; }; diff --git a/pykd/utils.h b/pykd/utils.h index 5766106..1b82716 100644 --- a/pykd/utils.h +++ b/pykd/utils.h @@ -1,6 +1,9 @@ #pragma once +#include +#include + //////////////////////////////////////////////////////////////////////////////// // std::string -> const WCHAR * //////////////////////////////////////////////////////////////////////////////// @@ -25,7 +28,7 @@ private: //////////////////////////////////////////////////////////////////////////////// class autoBstr { public: - autoBstr() : m_bstr(NULL) {} + autoBstr(BSTR bstr = NULL) : m_bstr(bstr) {} ~autoBstr() { free(); } @@ -76,3 +79,17 @@ public: private: BSTR m_bstr; }; + +//////////////////////////////////////////////////////////////////////////////// +// std::list -> python::list +//////////////////////////////////////////////////////////////////////////////// +template +python::list toPyList(const std::list< TElem > &stdList) +{ + python::list pyList; + for (std::list< TElem >::const_iterator it = stdList.begin(); it != stdList.end(); ++it) + pyList.append( *it ); + return pyList; +} + +////////////////////////////////////////////////////////////////////////////////