2008-05-11 19:40:06 -04:00
|
|
|
#! /usr/bin/env python
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
"""Another Python debugger."""
|
|
|
|
|
|
|
|
import os, pdb, sys, time, traceback
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
|
|
|
|
"post_mortem", "help"]
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
class Edb(pdb.Pdb):
|
|
|
|
def cmdloop(self, intro=None):
|
|
|
|
stop = False
|
|
|
|
while not stop:
|
|
|
|
sys.stdout.write(self.prompt)
|
|
|
|
sys.stdout.flush()
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
line = self.precmd(line)
|
|
|
|
stop = self.onecmd(line)
|
|
|
|
stop = self.postcmd(stop, line)
|
|
|
|
self.postloop()
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
def columnize(self, list, displaywidth=80):
|
|
|
|
pass
|
|
|
|
def do_help(self, arg):
|
|
|
|
pass
|
|
|
|
def print_topics(self, header, cmds, cmdlen, maxcol):
|
|
|
|
pass
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
# Simplified interface
|
|
|
|
def run(statement, globals=None, locals=None):
|
|
|
|
Edb().run(statement, globals, locals)
|
|
|
|
def runeval(expression, globals=None, locals=None):
|
|
|
|
return Edb().runeval(expression, globals, locals)
|
|
|
|
def runctx(statement, globals, locals):
|
|
|
|
run(statement, globals, locals)
|
|
|
|
def runcall(*args, **kwds):
|
|
|
|
return Edb().runcall(*args, **kwds)
|
|
|
|
def set_trace():
|
|
|
|
Edb().set_trace(sys._getframe().f_back)
|
2008-05-10 18:38:27 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
# Post-Mortem interface
|
|
|
|
def post_mortem(t):
|
|
|
|
p = Edb()
|
|
|
|
p.reset()
|
|
|
|
while t.tb_next is not None:
|
|
|
|
t = t.tb_next
|
|
|
|
p.interaction(t.tb_frame, t)
|
|
|
|
def pm():
|
|
|
|
post_mortem(sys.last_traceback)
|
2008-05-10 18:38:27 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if not sys.argv[1:]:
|
|
|
|
print "usage: edb.py scriptfile [arg] ..."
|
|
|
|
sys.exit(2)
|
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
mainpyfile = sys.argv[1]
|
2008-05-10 18:38:27 -04:00
|
|
|
if not os.path.exists(mainpyfile):
|
|
|
|
print 'Error:', mainpyfile, 'does not exist'
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
del sys.argv[0]
|
|
|
|
|
|
|
|
# Replace pdb's dir with script's dir in front of module search path.
|
|
|
|
sys.path[0] = os.path.dirname(mainpyfile)
|
|
|
|
|
|
|
|
edb = Edb()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
edb._runscript(mainpyfile)
|
|
|
|
if edb._user_requested_quit:
|
|
|
|
break
|
|
|
|
print "The program finished and will be restarted"
|
|
|
|
except SystemExit:
|
|
|
|
# In most cases SystemExit does not warrant a post-mortem session.
|
2008-05-11 19:40:06 -04:00
|
|
|
print "The program exited via sys.exit(). Exit status: ",
|
2008-05-10 18:38:27 -04:00
|
|
|
print sys.exc_info()[1]
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
print "Uncaught exception. Entering post mortem debugging"
|
2008-05-11 19:40:06 -04:00
|
|
|
print "Running 'cont' or 'step' will restart the program"
|
2008-05-10 18:38:27 -04:00
|
|
|
t = sys.exc_info()[2]
|
|
|
|
while t.tb_next is not None:
|
|
|
|
t = t.tb_next
|
|
|
|
edb.interaction(t.tb_frame,t)
|
|
|
|
print "Post mortem debugger finished. The "+mainpyfile+" will be restarted"
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
main()
|