diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index c6a4b89..8b71abd 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -450,6 +450,8 @@ BOOST_PYTHON_MODULE( pykd ) "Return bit field's length" ) .def( "field", TypeInfoAdapter::getElementByName, "Return field's type" ) + .def( "fieldName", &kdlib::TypeInfo::getElementName, + "Return name of struct field by index" ) //.def( "asMap", &kdlib::TypeInfo::asMap, // "Return type as python dict ( for enum types )" ) .def( "deref", &kdlib::TypeInfo::deref, @@ -480,6 +482,12 @@ BOOST_PYTHON_MODULE( pykd ) "Return target field offset" ) .def("field", TypedVarAdapter::getField, "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, + "Return list of tuple ( filedName, fieldOffset, fieldValue )" ) + .def( "fieldName", &kdlib::TypedVar::getElementName, + "Return name of struct field by index" ) //.def( "dataKind", &kdlib::TypedVar::getDataKind, // "Retrieves the variable classification of a data: DataIsXxx") .def("deref", &kdlib::TypedVar::deref, diff --git a/pykd/typedvar.h b/pykd/typedvar.h index 204a35c..c61c714 100644 --- a/pykd/typedvar.h +++ b/pykd/typedvar.h @@ -1,5 +1,9 @@ #pragma once +#include +#include +namespace python = boost::python; + #include "kdlib/typedvar.h" namespace pykd { @@ -42,7 +46,20 @@ struct TypedVarAdapter { static kdlib::TypedVarPtr containingRecordByType( kdlib::MEMOFFSET_64 offset, kdlib::TypeInfoPtr &typeInfo, const std::wstring &fieldName ) { return kdlib::containingRecord( offset, typeInfo, fieldName ); + } + static python::list getFields( kdlib::TypedVar& typedVar ) + { + python::list lst; + for ( size_t i = 0; i < typedVar.getElementCount(); ++i ) + { + std::wstring name = typedVar.getElementName(i); + kdlib::MEMOFFSET_32 offset = typedVar.getElementOffset(i); + kdlib::TypedVarPtr val = typedVar.getElement(i); + lst.append( python::make_tuple( name, offset, val ) ); + } + + return lst; } }; diff --git a/test/scripts/typedvar.py b/test/scripts/typedvar.py index 3cbae59..67dd35b 100644 --- a/test/scripts/typedvar.py +++ b/test/scripts/typedvar.py @@ -38,11 +38,11 @@ class TypedVarTest( unittest.TestCase ): tvDiaStruct = pykd.typedVar( target.module.type("structTest").ptrTo(), target.module.offset("g_structTestPtr") ) self.assertEqual( 500, tvDiaStruct.deref().m_field1 ) - customStructTest = pykd.typeBuilder().createStruct("customStructTest", 4) - customStructTest.append("m_field0", pykd.typeInfo("UInt4B")) - customStructTest.append("m_field1", pykd.typeInfo("UInt8B")) - tvCustomStruct = pykd.typedVar( customStructTest.ptrTo(), target.module.offset("g_structTestPtr") ) - self.assertEqual( 500, tvCustomStruct.deref().m_field1 ) + #customStructTest = pykd.typeBuilder().createStruct("customStructTest", 4) + #customStructTest.append("m_field0", pykd.typeInfo("UInt4B")) + #customStructTest.append("m_field1", pykd.typeInfo("UInt8B")) + #tvCustomStruct = pykd.typedVar( customStructTest.ptrTo(), target.module.offset("g_structTestPtr") ) + #self.assertEqual( 500, tvCustomStruct.deref().m_field1 ) def testArrayOf(self): arrayType = pykd.typeInfo("UInt8B").arrayOf(5) @@ -302,11 +302,14 @@ class TypedVarTest( unittest.TestCase ): def testUdtSubscribe(self): tv = pykd.typedVar( "g_virtChild" ) self.assertEqual( 6, len(tv) ) - fieldName, fieldVal = tv[4] + fieldName = tv.fieldName(5) + fieldVal = tv.field(5) + self.assertEqual( fieldName, "m_baseField" ) self.assertEqual( fieldVal, tv.m_baseField ) - for field in tv: - str( field ) + + for field in tv.fields: + str( field ) def testDeadlockList(self): lst = []