"But always my debugger is getting hanged"
It is look like infinite loop. May be the debbuger is not hanged but hard working? Try to added debug output in the loop.
"sometimes its crashing"
can you get a crash dump?
In my experience, scripts like yours work unstable inside windbg. Windbg has special loop for waiting and handling debug events. The normal algorithm for windbg:
1)wait for debug event ( break )
3) wait for user command
4) if command is g, p, t - change execution status of the target and go to point 1)
But when you call pykd routines like step() this algorithm is changed: pykd has own loop for debug events waiting.
Try to move you work outside windbg. Pykd can work within standalone python applicaton. All you need to added some code for starting debugging:
startProcess( "my_target_app param1 param2" ) #start debugging
setBp( my_func ) #set breakpoint on the investigated code
go() #go until breakpoint is hit
You can try to run your script with karmadbg (
Type %run script_name in the command line, may be you will more fortunate with karmadbg ( and you will have one benefit with karmadbg: visual script debugging with %rund macro command )