[*] 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
This commit is contained in:
SND\EreTIk_cp 2011-09-21 12:41:05 +00:00 committed by Mikhail I. Izmestev
parent c91abde950
commit 3333bdac13
4 changed files with 72 additions and 55 deletions

View File

@ -109,7 +109,7 @@ BOOST_PYTHON_MODULE( pykd )
"Open pdb file for quering debug symbols. Return DiaSymbol of global scope"); "Open pdb file for quering debug symbols. Return DiaSymbol of global scope");
python::class_<pyDia::Symbol>("DiaSymbol", "class wrapper for MS DIA Symbol" ) python::class_<pyDia::Symbol>("DiaSymbol", "class wrapper for MS DIA Symbol" )
.def( "findEx", &pyDia::Symbol::findChildrenImpl, .def( "findEx", &pyDia::Symbol::findChildrenEx,
"Retrieves the children of the symbol" ) "Retrieves the children of the symbol" )
.def( "find", &pyDia::Symbol::findChildren, .def( "find", &pyDia::Symbol::findChildren,
"Retrieves the children of the symbol" ) "Retrieves the children of the symbol" )

View File

@ -1,9 +1,6 @@
#include "stdafx.h" #include "stdafx.h"
#include <vector>
#include <memory>
#include "diawrapper.h" #include "diawrapper.h"
#include "utils.h" #include "utils.h"
@ -96,38 +93,8 @@ const size_t Symbol::cntBasicTypeName = _countof(Symbol::basicTypeName);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#define callSymbol(retType, method) \ #define callSymbol(method) \
do { \ callSymbolT( &IDiaSymbol::##method, __FUNCTION__, #method)
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)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -171,7 +138,7 @@ void Exception::exceptionTranslate( const Exception &e )
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
python::list Symbol::findChildrenImpl( std::list< Symbol > Symbol::findChildrenImpl(
ULONG symTag, ULONG symTag,
const std::string &name, const std::string &name,
DWORD nameCmpFlags DWORD nameCmpFlags
@ -189,12 +156,12 @@ python::list Symbol::findChildrenImpl(
if (S_OK != hres) if (S_OK != hres)
throw Exception("Call IDiaSymbol::findChildren", hres); throw Exception("Call IDiaSymbol::findChildren", hres);
python::list childList; std::list< Symbol > childList;
DiaSymbolPtr child; DiaSymbolPtr child;
ULONG celt; ULONG celt;
while ( SUCCEEDED(symbols->Next(1, &child, &celt)) && (celt == 1) ) while ( SUCCEEDED(symbols->Next(1, &child, &celt)) && (celt == 1) )
childList.append( Symbol(child) ); childList.push_back( Symbol(child) );
return childList; return childList;
} }
@ -203,49 +170,50 @@ python::list Symbol::findChildrenImpl(
ULONGLONG Symbol::getSize() ULONGLONG Symbol::getSize()
{ {
callSymbolUlonglong(get_length); return callSymbol(get_length);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Symbol::getName() 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() ULONG Symbol::getSymTag()
{ {
callSymbolDword(get_symTag); return callSymbol(get_symTag);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ULONG Symbol::getRva() ULONG Symbol::getRva()
{ {
callSymbolDword(get_relativeVirtualAddress); return callSymbol(get_relativeVirtualAddress);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ULONG Symbol::getLocType() ULONG Symbol::getLocType()
{ {
callSymbolDword(get_locationType); return callSymbol(get_locationType);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -313,14 +281,14 @@ bool Symbol::isBasicType()
ULONG Symbol::getBaseType() ULONG Symbol::getBaseType()
{ {
callSymbolDword(get_baseType); return callSymbol(get_baseType);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
ULONG Symbol::getBitPosition() ULONG Symbol::getBitPosition()
{ {
callSymbolDword(get_bitPosition); return callSymbol(get_bitPosition);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -2,6 +2,8 @@
#pragma once #pragma once
#include <cvconst.h> #include <cvconst.h>
#include "utils.h"
#include "dbgexcept.h" #include "dbgexcept.h"
namespace pyDia { namespace pyDia {
@ -56,26 +58,35 @@ class Symbol {
public: public:
Symbol() {} Symbol() {}
python::list findChildrenImpl( std::list< Symbol > findChildrenImpl(
ULONG symTag, ULONG symTag,
const std::string &name, const std::string &name,
DWORD nameCmpFlags DWORD nameCmpFlags
); );
python::list findChildrenEx(
ULONG symTag,
const std::string &name,
DWORD nameCmpFlags
)
{
return toPyList( findChildrenImpl(symTag, name, nameCmpFlags) );
}
python::list findChildren( python::list findChildren(
const std::string &name const std::string &name
) )
{ {
return findChildrenImpl(SymTagNull, name, nsfCaseSensitive); return toPyList( findChildrenImpl(SymTagNull, name, nsfCaseSensitive) );
} }
ULONGLONG getSize(); ULONGLONG getSize();
std::string getName(); std::string getName();
python::object getType(); Symbol getType();
python::object getIndexType(); Symbol getIndexType();
ULONG getSymTag(); ULONG getSymTag();
@ -107,6 +118,23 @@ public:
protected: protected:
template <typename TRet>
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) void throwIfNull(const char *desc)
{ {
if (!m_symbol) if (!m_symbol)
@ -117,6 +145,10 @@ protected:
m_symbol = _symbol.Detach(); m_symbol = _symbol.Detach();
} }
Symbol(__in IDiaSymbol *_symbol) {
m_symbol = _symbol;
}
DiaSymbolPtr m_symbol; DiaSymbolPtr m_symbol;
}; };

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <vector>
#include <list>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// std::string -> const WCHAR * // std::string -> const WCHAR *
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -25,7 +28,7 @@ private:
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class autoBstr { class autoBstr {
public: public:
autoBstr() : m_bstr(NULL) {} autoBstr(BSTR bstr = NULL) : m_bstr(bstr) {}
~autoBstr() { ~autoBstr() {
free(); free();
} }
@ -76,3 +79,17 @@ public:
private: private:
BSTR m_bstr; BSTR m_bstr;
}; };
////////////////////////////////////////////////////////////////////////////////
// std::list -> python::list
////////////////////////////////////////////////////////////////////////////////
template <typename TElem>
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;
}
////////////////////////////////////////////////////////////////////////////////