mirror of
https://github.com/ivellioscolin/pykd.git
synced 2025-04-19 19:13:22 +08:00
[0.2.x] added : snippet stkdelta ( show stack consumption )
git-svn-id: https://pykd.svn.codeplex.com/svn@81714 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
parent
af34c5da49
commit
31b46b6c8e
94
snippets/stkdelta.py
Normal file
94
snippets/stkdelta.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
|
||||||
|
from pykd import *
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def printThreadInfo():
|
||||||
|
|
||||||
|
nt = module("nt")
|
||||||
|
|
||||||
|
thread = nt.typedVar( "_KTHREAD", getImplicitThread() )
|
||||||
|
|
||||||
|
stackPointer = reg("rsp") if is64bitSystem() else reg("esp")
|
||||||
|
|
||||||
|
dprintln("")
|
||||||
|
|
||||||
|
dprintln( "Stack Base: %x Limit: %x Current: %x Used: %x Unused: %x" %
|
||||||
|
( thread.InitialStack, thread.StackLimit, stackPointer, thread.InitialStack - stackPointer, stackPointer - thread.StackLimit ) )
|
||||||
|
|
||||||
|
|
||||||
|
def printDeltaStat():
|
||||||
|
|
||||||
|
dprintln("")
|
||||||
|
|
||||||
|
printThreadInfo()
|
||||||
|
|
||||||
|
stk = getStack()
|
||||||
|
|
||||||
|
moduleLst = {}
|
||||||
|
funcLst = {}
|
||||||
|
|
||||||
|
for i in range( 0, len(stk) -1 ):
|
||||||
|
|
||||||
|
try:
|
||||||
|
mname = module( stk[i].returnOffset ).name()
|
||||||
|
except BaseException:
|
||||||
|
continue
|
||||||
|
|
||||||
|
delta = stk[i+1].frameOffset - stk[i].frameOffset
|
||||||
|
if delta > 0:
|
||||||
|
|
||||||
|
if mname in moduleLst:
|
||||||
|
moduleLst[mname] = moduleLst[mname] + delta
|
||||||
|
else:
|
||||||
|
moduleLst[mname] = delta
|
||||||
|
|
||||||
|
func = findSymbol( stk[i].returnOffset )
|
||||||
|
|
||||||
|
if func in funcLst:
|
||||||
|
funcLst[func] = funcLst[func] + delta
|
||||||
|
else:
|
||||||
|
funcLst[func] = delta
|
||||||
|
|
||||||
|
nt = module("nt")
|
||||||
|
|
||||||
|
thread = nt.typedVar( "_KTHREAD", getImplicitThread() )
|
||||||
|
|
||||||
|
stackSize = thread.InitialStack - thread.StackLimit
|
||||||
|
|
||||||
|
dprintln("")
|
||||||
|
dprintln( "%12s\t%s" % ("Stack usage:", "Module:" ) )
|
||||||
|
dprintln( "="*30 )
|
||||||
|
|
||||||
|
for mod,delta in sorted( moduleLst.iteritems(), key=lambda x: x[1], reverse=True ):
|
||||||
|
dprintln( "%7d%5s\t%s" % (delta, "(%%%d)" % (delta*100/stackSize), mod ) )
|
||||||
|
|
||||||
|
dprintln("")
|
||||||
|
dprintln( "%12s\t%s" % ("Stack usage:", "Function" ) )
|
||||||
|
dprintln( "="*30 )
|
||||||
|
|
||||||
|
for func,delta in sorted( funcLst.iteritems(), key=lambda x: x[1], reverse=True ):
|
||||||
|
dprintln( "%7d%5s\t%s" % (delta, "(%%%d)" % (delta*100/stackSize), func ) )
|
||||||
|
|
||||||
|
def printDeltaStack():
|
||||||
|
|
||||||
|
printThreadInfo()
|
||||||
|
|
||||||
|
stk = getStack()
|
||||||
|
|
||||||
|
dprintln( "Stack Delta:\tFunction:")
|
||||||
|
|
||||||
|
for i in range( 0, len(stk) -1 ):
|
||||||
|
dprint( "%12s\t" % long( stk[i+1].frameOffset - stk[i].frameOffset) )
|
||||||
|
dprintln( findSymbol( stk[i].returnOffset ) )
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
if sys.argv[1] == "stat":
|
||||||
|
printDeltaStat()
|
||||||
|
return
|
||||||
|
|
||||||
|
printDeltaStack()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user