[[+] added : __str__ method for typedVar class, so it can be outputed by print operator

git-svn-id: https://pykd.svn.codeplex.com/svn@60159 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2011-01-14 17:58:28 +00:00
parent 0e12bf9fab
commit efdf59111b
5 changed files with 162 additions and 86 deletions

View File

@ -145,9 +145,10 @@ BOOST_PYTHON_MODULE( pykd )
boost::python::def( "setCurrentProcess", &setCurrentProcess ); boost::python::def( "setCurrentProcess", &setCurrentProcess );
boost::python::def( "getProcessorMode", &getProcessorMode ); boost::python::def( "getProcessorMode", &getProcessorMode );
boost::python::def( "setProcessorMode", &setProcessorMode ); boost::python::def( "setProcessorMode", &setProcessorMode );
boost::python::class_<typedVarClass>( "typedVarClass" ) boost::python::class_<typedVarClass, boost::shared_ptr<typedVarClass> >( "typedVarClass" )
.def("getAddress", &typedVarClass::getAddress ) .def("getAddress", &typedVarClass::getAddress )
.def("sizeof", &typedVarClass::size ); .def("sizeof", &typedVarClass::size )
.def("__str__", &typedVarClass::print);
boost::python::class_<dbgModuleClass>( "dbgModuleClass" ) boost::python::class_<dbgModuleClass>( "dbgModuleClass" )
.def("begin", &dbgModuleClass::getBegin ) .def("begin", &dbgModuleClass::getBegin )
.def("end", &dbgModuleClass::getEnd ) .def("end", &dbgModuleClass::getEnd )
@ -478,3 +479,4 @@ pythonpath( PDEBUG_CLIENT4 client, PCSTR args )
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////

View File

@ -1,6 +1,6 @@
#include "stdafx.h" #include "stdafx.h"
#include <map> #include <sstream>
#include "dbgext.h" #include "dbgext.h"
#include "dbgtype.h" #include "dbgtype.h"
@ -285,7 +285,7 @@ TypeInfo::TypeInfo( const std::string &moduleName, const std::string &typeName
|| fieldTypeNameStr.find("<unnamed-tag>") < fieldTypeNameStr.size() ) || fieldTypeNameStr.find("<unnamed-tag>") < fieldTypeNameStr.size() )
continue; continue;
m_fields.insert( make_pair( fieldName, TypeField( get(moduleName, fieldTypeName), fieldSize, fieldOffset ) ) ); m_fields.push_back( TypeField( fieldName, get(moduleName, fieldTypeName), fieldSize, fieldOffset ) );
} }
} }
@ -310,25 +310,30 @@ TypeInfo::load( ULONG64 addr ) const
if ( m_baseType ) if ( m_baseType )
return loadBaseType( addr ); return loadBaseType( addr );
boost::python::object var( typedVarClass( addr, m_size ) ); boost::shared_ptr<typedVarClass> ptr( new typedVarClass( *this, addr, m_size ) );
boost::python::object var( ptr );
ptr->setPyObj( var );
TypeFieldMap::const_iterator it = m_fields.begin(); TypeFieldList::const_iterator field = m_fields.begin();
for ( it = m_fields.begin(); it != m_fields.end(); ++it ) for ( field = m_fields.begin(); field != m_fields.end(); ++field )
{ {
const TypeField &field = it->second;
if ( field.size == field.type.size() ) if ( field->size == field->type.size() )
{ {
var.attr( it->first.c_str() ) = field.type.load( addr + field.offset ); var.attr( field->name.c_str() ) = field->type.load( addr + field->offset );
// boost::python::object obj = var.attr( field->name.c_str() );
//int a;
} }
else else
{ {
boost::python::dict arr; boost::python::dict arr;
for ( unsigned int i = 0; i < field.size / field.type.size(); ++i ) for ( unsigned int i = 0; i < field->size / field->type.size(); ++i )
arr[i] = field.type.load( addr + field.offset + i * field.type.size() ); arr[i] = field->type.load( addr + field->offset + i * field->type.size() );
var.attr( it->first.c_str() ) = arr; var.attr( field->name.c_str() ) = arr;
} }
} }
@ -413,3 +418,46 @@ valueLoader( ULONG64 address, ULONG size )
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
std::string
typedVarClass::print() const
{
stringstream sstr;
for (
TypeInfo::TypeFieldList::const_iterator field = m_typeInfo.getFields().begin();
field != m_typeInfo.getFields().end();
field++ )
{
sstr << "\t" << hex << "+" << field->offset << " " << field->name << " ";
if ( field->type.isComplex() )
sstr << field->type.name();
else
{
if ( field->size == field->type.size() )
{
boost::python::object attr = m_pyobj.attr( field->name.c_str() );
if ( attr.ptr() == Py_None )
{
sstr << "memory error";
}
else
{
unsigned __int64 val = boost::python::extract<unsigned __int64>( attr );
sstr << hex << "0x" << val << dec << " ( " << val << " )";
}
}
}
sstr << std::endl;
}
return sstr.str();
}
/////////////////////////////////////////////////////////////////////////////////

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <list>
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/python/object.hpp> #include <boost/python/object.hpp>
@ -10,37 +11,7 @@
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class typedVarClass {
public:
typedVarClass()
{}
typedVarClass( ULONG64 addr, ULONG size ) :
m_addr( addr ),
m_size( size )
{}
ULONG64
getAddress() const {
return m_addr;
}
ULONG
size() const {
return m_size;
}
private:
ULONG64 m_addr;
ULONG m_size;
};
/////////////////////////////////////////////////////////////////////////////////
boost::python::object boost::python::object
loadTypedVar( const std::string &moduleName, const std::string &typeName, ULONG64 address ); loadTypedVar( const std::string &moduleName, const std::string &typeName, ULONG64 address );
@ -59,6 +30,44 @@ sizeofType( const std::string &moduleName, const std::string &typeName );
class TypeInfo { class TypeInfo {
public:
template< typename TTypeInfo>
struct TypeFieldT {
ULONG size;
ULONG offset;
TTypeInfo type;
std::string name;
TypeFieldT( const std::string &name_, const TTypeInfo &type_, ULONG size_, ULONG offset_ ) :
name( name_ ),
size( size_ ),
offset( offset_ ),
type( type_ )
{}
};
struct TypeName {
std::string module;
std::string symbol;
TypeName( const std::string &module_, const std::string &symbol_ ) :
module( module_ ),
symbol( symbol_ )
{}
bool
operator < ( const TypeName &typeName ) const {
return ( typeName.module <= module ) && ( typeName.symbol < symbol );
}
};
typedef TypeFieldT<TypeInfo> TypeField;
typedef std::map<TypeName, TypeInfo> TypeInfoMap;
typedef std::list<TypeField> TypeFieldList;
public: public:
TypeInfo() : TypeInfo() :
@ -87,44 +96,17 @@ public:
static const TypeInfo& static const TypeInfo&
get( const std::string &moduleName, const std::string &typeName ); get( const std::string &moduleName, const std::string &typeName );
const TypeFieldList&
private: getFields() const {
return m_fields;
template< typename TTypeInfo> }
struct TypeFieldT {
ULONG size;
ULONG offset;
TTypeInfo type;
TypeFieldT( const TTypeInfo &type_, ULONG size_, ULONG offset_ ) :
size( size_ ),
offset( offset_ ),
type( type_ )
{}
};
struct TypeName {
std::string module;
std::string symbol;
TypeName( const std::string &module_, const std::string &symbol_ ) :
module( module_ ),
symbol( symbol_ )
{}
bool bool
operator < ( const TypeName &typeName ) const { isComplex() const {
return ( typeName.module <= module ) && ( typeName.symbol < symbol ); return !m_baseType;
} }
};
private:
typedef TypeFieldT<TypeInfo> TypeField;
typedef std::map<TypeName, TypeInfo> TypeInfoMap;
typedef std::map<std::string, TypeField> TypeFieldMap;
static TypeInfoMap g_typeInfoCache; static TypeInfoMap g_typeInfoCache;
@ -145,7 +127,7 @@ private:
bool m_pointer; bool m_pointer;
TypeFieldMap m_fields; TypeFieldList m_fields;
std::string m_typeName; std::string m_typeName;
@ -153,3 +135,47 @@ private:
}; };
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
class typedVarClass {
public:
typedVarClass()
{}
typedVarClass( const TypeInfo &typeInfo, ULONG64 addr, ULONG size ) :
m_typeInfo( typeInfo ),
m_addr( addr ),
m_size( size )
{}
ULONG64
getAddress() const {
return m_addr;
}
ULONG
size() const {
return m_size;
}
std::string
print() const;
void
setPyObj( const boost::python::object &obj ) {
m_pyobj = obj;
}
private:
ULONG64 m_addr;
ULONG m_size;
TypeInfo m_typeInfo;
boost::python::object m_pyobj;
};
/////////////////////////////////////////////////////////////////////////////////