From 31b46b6c8ed0372698fa847538f4cf2d05714ce4 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Sat, 8 Dec 2012 14:18:00 +0000 Subject: [PATCH] [0.2.x] added : snippet stkdelta ( show stack consumption ) git-svn-id: https://pykd.svn.codeplex.com/svn@81714 9b283d60-5439-405e-af05-b73fd8c4d996 --- snippets/stkdelta.py | 94 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 snippets/stkdelta.py diff --git a/snippets/stkdelta.py b/snippets/stkdelta.py new file mode 100644 index 0000000..be63d66 --- /dev/null +++ b/snippets/stkdelta.py @@ -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()