Sample Python script for Mdbg-Python extension
#----------------------------------------------------
# Python Script for MDbg commands
#----------------------------------------------------
#----------------------------------------------------
# Support for accessing the root Shell object.
# We can get at everything else from this.
#----------------------------------------------------
#Shell = None
# Helper to give this module access to the "Shell" Object
# Since globals are public, you can set this directly too.
def SetShell(x):
#global g_Shell
#Shell = x
pass
def GetShell():
# global Shell
return Shell
def GetDebugger():
return GetShell().Debugger;
# Explicitly print to Mdbg shells' Output.
# @todo - Python "Print" command should do this too.
# currently, Print goes to sys.stdout.
def ShPrint(x):
GetShell().IO.WriteOutput("STDOUT", x)
# Execute an Mdbg command.
def ShExe(cmd):
MDbgUtil.ExecuteCommand(cmd)
def fib(n):
print 'n =', n
if n > 1:
return n * fib(n - 1)
else:
print 'end of the line'
return 1
# Helper alias to get the current MDbgProcess object
# Throws if none available.
def CurProc():
return GetDebugger().Processes.Active
# Helper alias to get the current MDbgThread object
# Throws if none available.
def CurThread(): return CurProc().Threads.Active
# Helper to get the current frame.
# Throws if none available.
def CurFrame(): return CurThread().CurrentFrame
def CurFrame2(): return CurThread().CurrentFrame.CorFrame
#----------------------------------------------------
# Helper to print the callstack.
# Uses MDbgFrame.ToString()
#----------------------------------------------------
def PrintStack():
i = 0
for x in CurThread().Frames:
print "%d) %s" % (i, x)
i = i + 1
print '------------'
#----------------------------------------------------
# Get locals for the current frame.
# Returns an array of MdbgValue[]
#----------------------------------------------------
def CurLocals():
return CurFrame().Function.GetActiveLocalVars(CurFrame())
#----------------------------------------------------
# Get the current arguments (aka parameters) to the current frame
#----------------------------------------------------
def CurArgs():
return CurFrame().Function.GetArguments(CurFrame())
#----------------------------------------------------
# pretty print a value. May be multiline result
# Don't use fun-eval.
# V can be either a single MDgValue or an MDbgValue[]
#----------------------------------------------------
def PrintVal(v):
# Check for MDbgValue[]
if str(type(v))=="<type 'Microsoft.Samples.Debugging.MdbgEngine.MDbgValue[]'>":
for x in v:
PrintVal(x)
else:
PrintValHelper(v, "")
# Helper to print with indent.
def PrintValHelper(v, indent):
if (v.IsComplexType):
print indent + v.Name + "=Complex Type: (" + v.TypeName + ")"
for x in v.GetFields():
PrintValHelper(x, indent+" ")
elif (v.IsArrayType):
print indent + v.Name + "=array Type: (" + v.TypeName + ")"
for x in v.GetArrayItems():
PrintValHelper(x, indent+" ")
else:
print indent + v.Name + "=" + v.GetStringValue(0, False) + "(" + v.TypeName + ")";
#----------------------------------------------------
# Get an Mdbg value for the given name.
# This is short because it will likely be a common operator.
# Can be a local or parameter
# This provides __repr__ on the output value so that we
# have a pretty string-izing operator.
# returns none if not found (@todo - perhaps throw?)
#----------------------------------------------------
def E(name):
v = _CheckValArray(name, CurLocals())
if v == None:
v = _CheckValArray(name, CurArgs())
# if v != None v.__repr__ = _ValToString
return v
# Get a value as a simple pretty string:
def _ValToString(v):
return v.GetStringValue(0, False)
def E2(name):
return _ValToString(E(name))
# Helper to see if any items in v array have the given name.
# V is an MDbgValue[]
def _CheckValArray(name, v):
for x in v:
if x.Name == name:
return x
return None
# Test for hit counter breakpoints.
# @todo - they all share global right now - uses Classes to give each their own data?
g_Total = 0;
def q1(hits):
global g_Total
g_Total = g_Total + 1
print "Inside hit counter bp:%d (of %d hits)" % (g_Total, hits)
if g_Total < hits:
return None
else:
g_Total = 0
return "Stop at %d" % (hits)
# Test class
class MyC:
def __init__(self): self.val=0
def Inc(self): self.val = self.val + 1
def Dev(self): self.val = self.val - 1
Comments
- Anonymous
August 31, 2005
I hear IronPython is a great managed scripting language to embed in other managed apps, so I thought... - Anonymous
September 01, 2005
Previously, I added IronPython scripting support to a real existing application, MDbg (a managed debugger...