[0.3.x] branch : gdt.py

git-svn-id: https://pykd.svn.codeplex.com/svn@85190 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2013-09-13 08:24:30 +00:00 committed by Mikhail I. Izmestev
parent b8eff4c69a
commit 4572e0927f
6 changed files with 266 additions and 2 deletions

View File

@ -52,6 +52,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "snippets", "snippets", "{AA
snippets\cr4.py = snippets\cr4.py
snippets\ctlcode.py = snippets\ctlcode.py
snippets\export.py = snippets\export.py
snippets\gdt.py = snippets\gdt.py
snippets\iat.py = snippets\iat.py
EndProjectSection
EndProject

View File

@ -8,7 +8,7 @@ from pykd import *
def cr0( value = 0 ):
if value == 0:
value = reg( "cr0" ) & 0xFFFFFFFF
value = reg( "cr0" )
dprintln( "CR0: %x (" % value + "".join( [ ( value & ( 1 << ( 31 - i ) ) ) and "1" or "0" for i in range(0,32) ] ) + ")" )

View File

@ -8,7 +8,7 @@ from pykd import *
def cr4( value = 0 ):
if value == 0:
value = reg( "cr4" ) & 0xFFFFFFFF
value = reg( "cr4" )
dprintln( "CR4: %x (" % value + "".join( [ ( value & ( 1 << ( 31 - i ) ) ) and "1" or "0" for i in range(0,32) ] ) + ")" )

69
snippets/gdt.py Normal file
View File

@ -0,0 +1,69 @@
#
#
#
import sys
from pykd import *
def printGdtEntry( addr ):
dprintln( "GDT Entry: %x" % addr )
attr = ptrByte( addr + 5 ) + ( ( ptrByte( addr + 6 ) & 0xF0 ) << 4 )
limit = ptrWord( addr ) + ( ( ptrByte( addr + 6 ) & 0xF ) << 16 )
base = ptrWord( addr + 2 ) + ( ptrByte( addr + 4) << 16 ) + ( ptrByte( addr + 7 ) << 24 )
if attr & 0x10:
pass
else:
if is64bitSystem():
base = ( ptrDWord( addr + 8 ) << 32 ) + base
if attr & 0x800:
limit = ( limit << 12 ) | 0xFFF
dprint( "attr: %x ( " % attr + "".join( [ ( attr & ( 1 << ( 11 - i ) ) ) and "1" or "0" for i in range(0,12) ] ) + " )" )
dprint( " base: %x" % base )
dprintln( " limit: %x" % limit )
def printGdt( gdtr ):
for s in ( "cs", "es", "ds", "ss", "gs", "fs", "tr" ):
dprintln( s + " (%x):" % reg(s) )
printGdtEntry( gdtr + ( reg( s ) & 0xFFF8 ) )
dprintln("")
def printGdtHelp():
dprintln( "Usage:" )
dprintln( "!py gdt help - Print out this message" )
dprintln( "!py gdt x - Print out gdt entry. The gdt entry's base is got from gdtr" )
dprintln( "!py gdt x y - Print out gdt entry. The gdt entry's base is x, y - offset" )
if __name__ == "__main__":
if not isWindbgExt():
print "script is launch out of windbg"
quit( 0 )
gdtr = reg("gdtr")
if len( sys.argv)==1:
printGdt( gdtr )
elif sys.argv[1] == "help":
printGdtHelp()
elif len( sys.argv )==2:
printGdtEntry( gdtr + ( int( sys.argv[1], 16 ) & 0xFFF8 ) )
else:
printGdtEntry( int( sys.argv[1], 16 ) + ( int( sys.argv[2], 16 ) & 0xFFF8 ) )

61
snippets/help.py Normal file
View File

@ -0,0 +1,61 @@
#
#
#
import sys
import pykd
from pykd import dprintln
def getFuncs():
funcs = sorted( [ item for item in pykd.__dict__.values() if type(item).__name__ == "function" ],
key=lambda x: x.__name__ )
return funcs
def getClasses():
classes = sorted( [ item for item in pykd.__dict__.values() if type(item).__name__ == "class" ],
key=lambda x: x.__name__ )
return classes
def printContent():
dprintln( "\nPYKD API Help\n")
funcs = getFuncs()
dprintln( "\n<u>FUNCTIONS</u>\n", True )
for f in funcs:
dprintln( "<link cmd=\"!py help %s\">%s</link>" %( f.__name__, f.__name__ ), True )
classes = getClasses()
dprintln( "\n<u>CLASSES</u>\n", True )
for c in classes:
dprintln( "<link cmd=\"!py help %s\">%s</link>" %( c.__name__, c.__name__ ), True )
def printDetail( name ):
if name in pykd.__dict__:
help( "pykd.%s" % name )
dprintln( "\n\n<link cmd=\"!py help\">View content</link>", True )
if __name__ == "__main__":
if len(sys.argv)<=1:
printContent()
if len(sys.argv)==2:
printDetail(sys.argv[1])

133
snippets/pytowiki.py Normal file
View File

@ -0,0 +1,133 @@
#
#
#
import sys
def usage():
print "python pytowiki.py module_name output_file"
class CodeplexFormatter:
def endl( self ):
return "\n"
def header1( self, s ):
return "! " + s + self.endl()
def header2( self, s ):
return "!! " + s + self.endl()
def header3( self, s ):
return "!!! " + s + self.endl()
def header4( self, s ):
return "!!!! " + s + self.endl()
def bulletItem( self, s ):
return "* " + s + self.endl()
def escapeMarkup( self, s ):
return "{\"" + s + "\"}"
def link( self, text, link ):
return "[" + text + "|#" + link + "]"
def anchor( self, link ):
return "{anchor:" + link + "}" + self.endl()
class ModuleInfo:
def __init__ (self, module):
self.funcs = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ )
self.classes = sorted( [ item for item in module.__dict__.values() if type(item).__name__ == "class" ], key=lambda x: x.__name__ )
for cls in self.classes:
cls.methods = sorted( [ item for item in cls.__dict__.values() if type(item).__name__ == "function" ], key=lambda x: x.__name__ )
cls.properties = sorted( [ item for item in cls.__dict__.items() if type(item[1]).__name__ == "property" ], key=lambda x: x[0] )
def buildDoc( ioStream, formatter, apiInfo ):
ioStream.write( formatter.header2( "Functions" ) )
for func in apiInfo.funcs:
ioStream.write( formatter.bulletItem( formatter.link( func.__name__, func.__name__ ) ) )
ioStream.write( formatter.header2( "Classes" ) )
for cls in apiInfo.classes:
ioStream.write( formatter.bulletItem( formatter.link( cls.__name__, cls.__name__ ) ) )
for func in apiInfo.funcs:
ioStream.write( formatter.anchor( func.__name__ ) )
ioStream.write( formatter.header3( "Function " + func.__name__ ) )
if func.__doc__ != None:
ioStream.write( formatter.escapeMarkup( func.__doc__) + formatter.endl() )
for cls in apiInfo.classes:
ioStream.write( formatter.anchor( cls.__name__ ) )
ioStream.write( formatter.header3( "Class " + cls.__name__ ) )
if cls.__doc__ != None:
ioStream.write( formatter.escapeMarkup( cls.__doc__) + formatter.endl() )
if cls.properties:
ioStream.write( formatter.header4( "Properties:") )
for p in cls.properties:
ioStream.write( formatter.bulletItem( formatter.link( p[0], cls.__name__ + "." + p[0]) ) )
if cls.methods:
ioStream.write( formatter.header4( "Methods:") )
for m in cls.methods:
if m.__doc__ != None:
ioStream.write( formatter.bulletItem( formatter.link( m.__name__, cls.__name__ + "." + m.__name__) ) )
if cls.properties:
for p in cls.properties:
if p[1].__doc__ != None:
ioStream.write( formatter.anchor( cls.__name__ + "." + p[0] ) )
ioStream.write( formatter.header4( formatter.escapeMarkup( "Property " + cls.__name__ + "." + p[0] ) ) )
ioStream.write( formatter.escapeMarkup( p[1].__doc__ ) + formatter.endl() )
if cls.methods:
for m in cls.methods:
if m.__doc__ != None:
ioStream.write( formatter.anchor( cls.__name__ + "." + m.__name__ ) )
ioStream.write( formatter.header4( formatter.escapeMarkup( "Method " + cls.__name__ + "." + m.__name__ ) ) )
ioStream.write( formatter.escapeMarkup( m.__doc__ ) + formatter.endl() )
def main():
if len(sys.argv) < 3:
usage()
return
moduleName = sys.argv[1]
fileName = sys.argv[2]
try:
module = __import__( moduleName )
with file( fileName, "w" ) as wikiIo:
apiInfo = ModuleInfo( module )
formatter = CodeplexFormatter()
buildDoc( wikiIo, formatter, apiInfo )
except ImportWarning:
print "failed to import module " + moduleName
if __name__ == "__main__":
main()