From fb823ae164f6d47a1d64f0dd498ff36b59ab812b Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Wed, 12 Mar 2014 17:07:56 +0000 Subject: [PATCH] [0.3.x] added : typeInfo.fields methos ( return list of tuple ( filedName, fieldType ) ) git-svn-id: https://pykd.svn.codeplex.com/svn@87546 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/pymod.cpp | 4 +++- pykd/pytypeinfo.cpp | 30 ++++++++++++++++++++++++++++++ pykd/pytypeinfo.h | 2 ++ test/scripts/typeinfo.py | 10 ++++++---- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index b82ccae..40163b3 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -528,6 +528,8 @@ BOOST_PYTHON_MODULE( pykd ) "Return field's type" ) .def( "fieldName", TypeInfoAdapter::getElementName, "Return name of struct field by index" ) + .def( "fields", TypeInfoAdapter::getFields, + "Return list of tuple ( filedName, fieldType )" ) .def( "deref", TypeInfoAdapter::deref, "Return type of pointer" ) .def( "append", TypeInfoAdapter::appendField, @@ -580,7 +582,7 @@ BOOST_PYTHON_MODULE( pykd ) "Return field of structure as an object attribute" ) .def("field", TypedVarAdapter::getElementByIndex, "Return field of structure as an object attribute" ) - .add_property( "fields", TypedVarAdapter::getFields, + .def( "fields", TypedVarAdapter::getFields, "Return list of tuple ( filedName, fieldOffset, fieldValue )" ) .def( "fieldName", TypedVarAdapter::getElementName, "Return name of struct field by index" ) diff --git a/pykd/pytypeinfo.cpp b/pykd/pytypeinfo.cpp index 5c992a1..2b9e1ea 100644 --- a/pykd/pytypeinfo.cpp +++ b/pykd/pytypeinfo.cpp @@ -72,4 +72,34 @@ python::tuple findSymbolAndDisp( ULONG64 offset ) /////////////////////////////////////////////////////////////////////////////// +python::list TypeInfoAdapter::getFields( kdlib::TypeInfo &typeInfo ) +{ + typedef boost::tuple FieldTuple; + + std::list lst; + + do { + + AutoRestorePyState pystate; + + for ( size_t i = 0; i < typeInfo.getElementCount(); ++i ) + { + std::wstring name = typeInfo.getElementName(i); + kdlib::TypeInfoPtr val = typeInfo.getElement(i); + + lst.push_back( FieldTuple( name, val ) ); + } + + } while(false); + + python::list pylst; + + for ( std::list::const_iterator it = lst.begin(); it != lst.end(); ++it) + pylst.append( python::make_tuple( it->get<0>(), it->get<1>() ) ); + + return pylst; +} + +/////////////////////////////////////////////////////////////////////////////// + } // pykd namespace diff --git a/pykd/pytypeinfo.h b/pykd/pytypeinfo.h index a6b5782..5852cc2 100644 --- a/pykd/pytypeinfo.h +++ b/pykd/pytypeinfo.h @@ -206,6 +206,8 @@ struct TypeInfoAdapter : public kdlib::TypeInfo { return typeInfo.str(); } + static python::list getFields( kdlib::TypeInfo &typeInfo ); + }; struct BaseTypesEnum { diff --git a/test/scripts/typeinfo.py b/test/scripts/typeinfo.py index 97eb6f4..d8f9381 100644 --- a/test/scripts/typeinfo.py +++ b/test/scripts/typeinfo.py @@ -154,10 +154,6 @@ class TypeInfoTest( unittest.TestCase ): self.assertEqual( 0, ti.fieldOffset("m_structValue") ) self.assertEqual( ti.size(), ti.m_structValue.size() ) - def testAsMap(self): - ti = target.module.type("enumType") - self.assertEqual( { 1 : "ONE", 2 : "TWO", 3 : "THREE" }, ti.asMap() ) - def testDeref(self): ti = pykd.typeInfo("Int1B*") self.assertEqual( "Int1B", ti.deref().name() ) @@ -223,6 +219,12 @@ class TypeInfoTest( unittest.TestCase ): for field in ti: str( field ) + def testEnumSubscribe(self): + ti = pykd.typeInfo( "enumType" ) + self.assertEqual( 3, len(ti) ) + self.assertEqual( [ 1, 2, 3], [ long(field) for field in ti ] ) + self.assertEqual( [ ( "ONE", 1), ("TWO", 2), ("THREE", 3) ], ti.fields() ) + def testStructNullSize(self): ti = target.module.type("structNullSize") self.assertEqual( 0, len(ti) )