From ba2a699ce46ce8faee465b3a7651027dc1e4cca1 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Fri, 2 Nov 2012 07:03:34 +0000 Subject: [PATCH] [0.2.x] fixed : print typedVar with invalid address will not raise MemoryException ( ???? printed instead ) git-svn-id: https://pykd.svn.codeplex.com/svn@80770 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgmem.h | 1 + pykd/typedvar.cpp | 73 ++++++++++++++++++++++++++++------------ pykd/win/memory.cpp | 27 +++++++++++++++ test/scripts/typedvar.py | 7 +++- 4 files changed, 86 insertions(+), 22 deletions(-) diff --git a/pykd/dbgmem.h b/pykd/dbgmem.h index 86a4fdf..9ac34a4 100644 --- a/pykd/dbgmem.h +++ b/pykd/dbgmem.h @@ -6,6 +6,7 @@ namespace pykd { ULONG64 addr64( ULONG64 offset ); void readMemory( ULONG64 offset, PVOID buffer, ULONG length, bool phyAddr = FALSE, ULONG *readed = NULL ); +bool readMemoryUnsafe( ULONG64 offset, PVOID buffer, ULONG length, bool phyAddr = FALSE, ULONG *readed = NULL ); bool isVaValid( ULONG64 addr ); bool compareMemory( ULONG64 addr1, ULONG64 addr2, ULONG length, bool phyAddr = FALSE ); diff --git a/pykd/typedvar.cpp b/pykd/typedvar.cpp index 02f6699..c056270 100644 --- a/pykd/typedvar.cpp +++ b/pykd/typedvar.cpp @@ -166,10 +166,18 @@ std::string BasicTypedVar::print() std::string BasicTypedVar::printValue() { - std::stringstream sstr; + std::stringstream sstr; + + try { - sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); - sstr << " (" << boost::apply_visitor( VariantToStr(), getValue() ) << ")"; + sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); + sstr << " (" << boost::apply_visitor( VariantToStr(), getValue() ) << ")"; + + } + catch( MemoryException& ) + { + sstr << "????"; + } return sstr.str(); } @@ -205,9 +213,17 @@ std::string PtrTypedVar::print() std::string PtrTypedVar::printValue() { - std::stringstream sstr; + std::stringstream sstr; - sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); + try { + + sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); + + } + catch( MemoryException& ) + { + sstr << "????"; + } return sstr.str(); } @@ -406,9 +422,16 @@ BaseTypeVariant BitFieldVar::getValue() std::string BitFieldVar::printValue() { std::stringstream sstr; - - sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); - sstr << " (" << boost::apply_visitor( VariantToStr(), getValue() ) << ")"; + + try + { + sstr << "0x" << boost::apply_visitor( VariantToHex(), getValue() ); + sstr << " (" << boost::apply_visitor( VariantToStr(), getValue() ) << ")"; + } + catch( MemoryException& ) + { + sstr << "????"; + } return sstr.str(); } @@ -442,23 +465,31 @@ std::string EnumTypedVar::printValue() { std::stringstream sstr; - ULONG val = boost::apply_visitor( VariantToULong(), getValue() ); + try { - for ( ULONG i = 0; i < m_typeInfo->getFieldCount(); ++i ) - { - ULONG val1 = boost::apply_visitor( VariantToULong(), m_typeInfo->getFieldByIndex(i)->getValue() ); + ULONG val = boost::apply_visitor( VariantToULong(), getValue() ); - if ( val == val1 ) - { - sstr << m_typeInfo->getFieldNameByIndex(i); - sstr << "(0x" << std::hex << val << ")"; + for ( ULONG i = 0; i < m_typeInfo->getFieldCount(); ++i ) + { + ULONG val1 = boost::apply_visitor( VariantToULong(), m_typeInfo->getFieldByIndex(i)->getValue() ); + + if ( val == val1 ) + { + sstr << m_typeInfo->getFieldNameByIndex(i); + sstr << "(0x" << std::hex << val << ")"; + + return sstr.str(); + } + } + + sstr << "0x" << std::hex << val; + sstr << " ( No matching name )"; - return sstr.str(); - } } - - sstr << "0x" << std::hex << val; - sstr << " ( No matching name )"; + catch( MemoryException& ) + { + sstr << "????"; + } return sstr.str(); } diff --git a/pykd/win/memory.cpp b/pykd/win/memory.cpp index 14c2ac3..e91b277 100644 --- a/pykd/win/memory.cpp +++ b/pykd/win/memory.cpp @@ -92,6 +92,33 @@ void readMemory( ULONG64 offset, PVOID buffer, ULONG length, bool phyAddr, ULONG /////////////////////////////////////////////////////////////////////////////////// +bool readMemoryUnsafe( ULONG64 offset, PVOID buffer, ULONG length, bool phyAddr, ULONG *readed ) +{ + PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate ); + + HRESULT hres; + if ( phyAddr == false ) + { + offset = addr64NoSafe( offset ); + + // workitem/10473 workaround + ULONG64 nextAddress; + hres = g_dbgEng->dataspace->GetNextDifferentlyValidOffsetVirtual( offset, &nextAddress ); + + DBG_UNREFERENCED_LOCAL_VARIABLE(nextAddress); + + hres = g_dbgEng->dataspace->ReadVirtual( offset, buffer, length, readed ); + } + else + { + hres = g_dbgEng->dataspace->ReadPhysical( offset, buffer, length, readed ); + } + + return hres == S_OK; +} + +/////////////////////////////////////////////////////////////////////////////////// + std::string loadCStr( ULONG64 offset ) { PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate ); diff --git a/test/scripts/typedvar.py b/test/scripts/typedvar.py index 073afc8..94a5c47 100644 --- a/test/scripts/typedvar.py +++ b/test/scripts/typedvar.py @@ -238,7 +238,12 @@ class TypedVarTest( unittest.TestCase ): self.assertTrue( str(target.module.typedVar( "g_voidPtr" ) ) ) self.assertTrue( str(target.module.typedVar( "g_arrOfPtrToFunc" ) ) ) self.assertTrue( str(target.module.typedVar( "g_unTypedPtrToFunction" ) ) ) - + + def testNotValidPrint(self): + types = ("structTest", ) #, "ULong[100]", "Ulong*" ) + for ti in types: + self.assertTrue( str(pykd.typedVar( target.module.type(ti), 0 ) ) ) + def testStaticField(self): tv = pykd.typedVar( "g_classChild" ) self.assertEqual( 200, tv.m_staticField )