1992-01-16 13:50:21 +00:00
|
|
|
# pdb.py -- finally, a Python debugger!
|
|
|
|
# See file pdb.doc for instructions.
|
1992-01-14 18:30:15 +00:00
|
|
|
|
|
|
|
# To do:
|
1992-01-16 13:50:21 +00:00
|
|
|
# - It should be possible to intercept KeyboardInterrupt
|
|
|
|
# - Handle return events differently -- always printing the r.v. can be bad!
|
|
|
|
# - Merge with tb, to get a single debugger for active and post-mortem usage
|
|
|
|
# - Solve bugs in termination (e.g., 'continue' after the program
|
|
|
|
# is done proceeds to debug the debugger; 'quit' sometimes complains
|
|
|
|
# about the PdbQuit exception...)
|
1992-01-12 23:32:55 +00:00
|
|
|
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
import string
|
|
|
|
import sys
|
|
|
|
import linecache
|
1992-01-16 13:50:21 +00:00
|
|
|
from cmd import Cmd
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
# A specialization of Cmd for use by the debugger
|
|
|
|
|
1992-01-12 23:32:55 +00:00
|
|
|
PdbQuit = 'pdb.PdbQuit' # Exception to give up
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
class Pdb(Cmd):
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
self = Cmd.init(self)
|
|
|
|
self.prompt = '(Pdb) '
|
|
|
|
self.reset()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def reset(self):
|
1992-01-16 13:50:21 +00:00
|
|
|
self.quitting = 0
|
1992-01-12 23:32:55 +00:00
|
|
|
self.breaks = {}
|
1992-01-10 14:54:42 +00:00
|
|
|
self.botframe = None
|
1992-01-12 23:32:55 +00:00
|
|
|
self.stopframe = None
|
1992-01-16 13:55:21 +00:00
|
|
|
self.lastretval = None
|
1992-01-10 14:54:42 +00:00
|
|
|
self.forget()
|
|
|
|
|
|
|
|
def forget(self):
|
1992-01-16 13:50:21 +00:00
|
|
|
self.setup(None, None)
|
1992-01-10 14:54:42 +00:00
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
def setup(self, f, t):
|
1992-01-10 14:54:42 +00:00
|
|
|
self.lineno = None
|
1992-01-16 13:50:21 +00:00
|
|
|
self.stack = []
|
1992-01-16 13:55:21 +00:00
|
|
|
self.curindex = 0
|
|
|
|
self.curframe = None
|
|
|
|
if f is None and t is None:
|
|
|
|
return
|
1992-01-16 13:50:21 +00:00
|
|
|
if t and t.tb_frame is f:
|
|
|
|
t = t.tb_next
|
1992-01-16 13:55:21 +00:00
|
|
|
while f is not None:
|
1992-01-16 13:50:21 +00:00
|
|
|
self.stack.append((f, f.f_lineno))
|
1992-01-16 13:55:21 +00:00
|
|
|
if f is self.botframe:
|
|
|
|
break
|
1992-01-16 13:50:21 +00:00
|
|
|
f = f.f_back
|
1992-01-16 13:55:21 +00:00
|
|
|
else:
|
|
|
|
print '[Weird: bottom frame not in stack trace.]'
|
1992-01-16 13:50:21 +00:00
|
|
|
self.stack.reverse()
|
|
|
|
self.curindex = max(0, len(self.stack) - 1)
|
1992-01-16 13:55:21 +00:00
|
|
|
while t is not None:
|
1992-01-16 13:50:21 +00:00
|
|
|
self.stack.append((t.tb_frame, t.tb_lineno))
|
|
|
|
t = t.tb_next
|
1992-01-16 13:55:21 +00:00
|
|
|
self.curframe = self.stack[self.curindex][0]
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
def run(self, cmd):
|
1992-01-12 23:32:55 +00:00
|
|
|
import __main__
|
|
|
|
dict = __main__.__dict__
|
|
|
|
self.runctx(cmd, dict, dict)
|
|
|
|
|
|
|
|
def runctx(self, cmd, globals, locals):
|
1992-01-16 13:55:21 +00:00
|
|
|
t = None
|
1992-01-10 14:54:42 +00:00
|
|
|
self.reset()
|
|
|
|
sys.trace = self.dispatch
|
|
|
|
try:
|
1992-01-12 23:32:55 +00:00
|
|
|
exec(cmd + '\n', globals, locals)
|
1992-01-10 14:54:42 +00:00
|
|
|
except PdbQuit:
|
1992-01-16 13:55:21 +00:00
|
|
|
return
|
1992-01-16 13:50:21 +00:00
|
|
|
except:
|
|
|
|
print '***', sys.exc_type + ':', `sys.exc_value`
|
1992-01-16 13:55:21 +00:00
|
|
|
t = sys.exc_traceback
|
1992-01-16 13:50:21 +00:00
|
|
|
finally:
|
1992-01-16 13:55:21 +00:00
|
|
|
self.trace = None
|
|
|
|
del self.trace
|
|
|
|
self.forget()
|
|
|
|
print '!!! Post-mortem debugging'
|
|
|
|
self.pmd(t)
|
|
|
|
|
|
|
|
def pmd(self, traceback):
|
|
|
|
t = traceback
|
|
|
|
if self.botframe is not None:
|
|
|
|
while t is not None:
|
|
|
|
if t.tb_frame is not self.botframe:
|
|
|
|
break
|
|
|
|
t = t.tb_next
|
|
|
|
else:
|
|
|
|
t = sys.exc_traceback
|
|
|
|
try:
|
|
|
|
self.ask_user(self.botframe, t)
|
|
|
|
except PdbQuit:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.forget()
|
1992-01-10 14:54:42 +00:00
|
|
|
|
1992-01-12 23:32:55 +00:00
|
|
|
def dispatch(self, frame, event, arg):
|
1992-01-16 13:50:21 +00:00
|
|
|
if self.quitting:
|
|
|
|
return None
|
1992-01-12 23:32:55 +00:00
|
|
|
if event == 'line':
|
|
|
|
return self.dispatch_line(frame)
|
|
|
|
if event == 'call':
|
|
|
|
return self.dispatch_call(frame, arg)
|
|
|
|
if event == 'return':
|
|
|
|
return self.dispatch_return(frame, arg)
|
|
|
|
if event == 'exception':
|
|
|
|
return self.dispatch_exception(frame, arg)
|
|
|
|
print '*** dispatch: unknown event type', `event`
|
|
|
|
return self.dispatch
|
|
|
|
|
|
|
|
def dispatch_line(self, frame):
|
|
|
|
if self.stop_here(frame) or self.break_here(frame):
|
1992-01-16 13:50:21 +00:00
|
|
|
self.ask_user(frame, None)
|
1992-01-12 23:32:55 +00:00
|
|
|
return self.dispatch
|
|
|
|
|
|
|
|
def dispatch_call(self, frame, arg):
|
1992-01-10 14:54:42 +00:00
|
|
|
if self.botframe is None:
|
1992-01-16 13:55:21 +00:00
|
|
|
# First call dispatch since reset()
|
1992-01-10 14:54:42 +00:00
|
|
|
self.botframe = frame
|
1992-01-16 13:55:21 +00:00
|
|
|
return None
|
1992-01-12 23:32:55 +00:00
|
|
|
if not (self.stop_here(frame) or self.break_anywhere(frame)):
|
1992-01-16 13:55:21 +00:00
|
|
|
return None
|
|
|
|
if arg is None:
|
|
|
|
print '[Entering non-function block.]'
|
|
|
|
else:
|
|
|
|
frame.f_locals['__args__'] = arg
|
1992-01-12 23:32:55 +00:00
|
|
|
return self.dispatch
|
|
|
|
|
|
|
|
def dispatch_return(self, frame, arg):
|
1992-01-16 13:55:21 +00:00
|
|
|
self.lastretval = arg
|
|
|
|
return None
|
1992-01-12 23:32:55 +00:00
|
|
|
|
|
|
|
def dispatch_exception(self, frame, arg):
|
|
|
|
if self.stop_here(frame):
|
1992-01-16 13:55:21 +00:00
|
|
|
print '!!!', arg[0] + ':', `arg[1]`
|
1992-01-16 13:50:21 +00:00
|
|
|
self.ask_user(frame, arg[2])
|
1992-01-10 14:54:42 +00:00
|
|
|
return self.dispatch
|
|
|
|
|
1992-01-12 23:32:55 +00:00
|
|
|
def stop_here(self, frame):
|
|
|
|
if self.stopframe is None:
|
|
|
|
return 1
|
|
|
|
if frame is self.stopframe:
|
|
|
|
return 1
|
1992-01-16 13:50:21 +00:00
|
|
|
while frame is not None and frame is not self.stopframe:
|
|
|
|
if frame is self.botframe:
|
1992-01-12 23:32:55 +00:00
|
|
|
return 1
|
|
|
|
frame = frame.f_back
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def break_here(self, frame):
|
|
|
|
if not self.breaks.has_key(frame.f_code.co_filename):
|
|
|
|
return 0
|
|
|
|
if not frame.f_lineno in \
|
|
|
|
self.breaks[frame.f_code.co_filename]:
|
|
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def break_anywhere(self, frame):
|
|
|
|
return self.breaks.has_key(frame.f_code.co_filename)
|
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
def ask_user(self, frame, traceback):
|
|
|
|
self.setup(frame, traceback)
|
|
|
|
self.printframelineno(self.stack[self.curindex])
|
|
|
|
self.cmdloop()
|
1992-01-12 23:32:55 +00:00
|
|
|
self.forget()
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
def default(self, line):
|
|
|
|
if not line:
|
1992-01-12 23:32:55 +00:00
|
|
|
return self.do_next('')
|
1992-01-10 14:54:42 +00:00
|
|
|
else:
|
|
|
|
if line[0] == '!': line = line[1:]
|
|
|
|
try:
|
|
|
|
exec(line + '\n', \
|
|
|
|
self.curframe.f_globals, \
|
|
|
|
self.curframe.f_locals)
|
|
|
|
except:
|
1992-01-12 23:32:55 +00:00
|
|
|
print '***', sys.exc_type + ':',
|
1992-01-10 14:54:42 +00:00
|
|
|
print `sys.exc_value`
|
|
|
|
|
|
|
|
do_h = Cmd.do_help
|
|
|
|
|
|
|
|
def do_break(self, arg):
|
|
|
|
if not arg:
|
|
|
|
print self.breaks # XXX
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
lineno = int(eval(arg))
|
|
|
|
except:
|
|
|
|
print '*** Error in argument:', `arg`
|
|
|
|
return
|
|
|
|
filename = self.curframe.f_code.co_filename
|
|
|
|
line = linecache.getline(filename, lineno)
|
|
|
|
if not line:
|
|
|
|
print '*** That line does not exist!'
|
|
|
|
return
|
|
|
|
if not self.breaks.has_key(filename):
|
|
|
|
self.breaks[filename] = []
|
|
|
|
list = self.breaks[filename]
|
|
|
|
if lineno in list:
|
|
|
|
print '*** There is already a break there!'
|
|
|
|
return
|
|
|
|
list.append(lineno)
|
|
|
|
do_b = do_break
|
|
|
|
|
|
|
|
def do_clear(self, arg):
|
|
|
|
if not arg:
|
1992-01-12 23:32:55 +00:00
|
|
|
try:
|
|
|
|
reply = raw_input('Clear all breaks? ')
|
|
|
|
except EOFError:
|
|
|
|
reply = 'no'
|
|
|
|
reply = string.lower(string.strip(reply))
|
|
|
|
if reply in ('y', 'yes'):
|
|
|
|
self.breaks = {}
|
1992-01-10 14:54:42 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
lineno = int(eval(arg))
|
|
|
|
except:
|
|
|
|
print '*** Error in argument:', `arg`
|
|
|
|
return
|
|
|
|
filename = self.curframe.f_code.co_filename
|
|
|
|
try:
|
|
|
|
self.breaks[filename].remove(lineno)
|
|
|
|
except (ValueError, KeyError):
|
|
|
|
print '*** There is no break there!'
|
|
|
|
return
|
1992-01-12 23:32:55 +00:00
|
|
|
if not self.breaks[filename]:
|
|
|
|
del self.breaks[filename]
|
|
|
|
do_cl = do_clear # 'c' is already an abbreviation for 'continue'
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
def do_where(self, arg):
|
1992-01-16 13:50:21 +00:00
|
|
|
self.printstacktrace()
|
1992-01-10 14:54:42 +00:00
|
|
|
do_w = do_where
|
|
|
|
|
|
|
|
def do_up(self, arg):
|
1992-01-16 13:50:21 +00:00
|
|
|
if self.curindex == 0:
|
|
|
|
print '*** Oldest frame'
|
1992-01-10 14:54:42 +00:00
|
|
|
else:
|
1992-01-16 13:50:21 +00:00
|
|
|
self.curindex = self.curindex - 1
|
|
|
|
self.curframe = self.stack[self.curindex][0]
|
|
|
|
self.printframelineno(self.stack[self.curindex])
|
1992-01-10 14:54:42 +00:00
|
|
|
do_u = do_up
|
|
|
|
|
|
|
|
def do_down(self, arg):
|
1992-01-16 13:50:21 +00:00
|
|
|
if self.curindex + 1 == len(self.stack):
|
|
|
|
print '*** Newest frame'
|
1992-01-10 14:54:42 +00:00
|
|
|
else:
|
1992-01-16 13:50:21 +00:00
|
|
|
self.curindex = self.curindex + 1
|
|
|
|
self.curframe = self.stack[self.curindex][0]
|
|
|
|
self.printframelineno(self.stack[self.curindex])
|
1992-01-10 14:54:42 +00:00
|
|
|
do_d = do_down
|
|
|
|
|
|
|
|
def do_step(self, arg):
|
1992-01-12 23:32:55 +00:00
|
|
|
self.stopframe = None
|
|
|
|
return 1
|
1992-01-10 14:54:42 +00:00
|
|
|
do_s = do_step
|
|
|
|
|
|
|
|
def do_next(self, arg):
|
1992-01-12 23:32:55 +00:00
|
|
|
self.stopframe = self.curframe
|
|
|
|
return 1
|
1992-01-10 14:54:42 +00:00
|
|
|
do_n = do_next
|
|
|
|
|
1992-01-12 23:32:55 +00:00
|
|
|
def do_return(self, arg):
|
|
|
|
self.stopframe = self.curframe.f_back
|
|
|
|
return 1
|
|
|
|
do_r = do_return
|
|
|
|
|
1992-01-16 13:55:21 +00:00
|
|
|
def do_retval(self, arg):
|
|
|
|
print self.lastretval
|
|
|
|
do_rv = do_retval
|
|
|
|
|
1992-01-10 14:54:42 +00:00
|
|
|
def do_continue(self, arg):
|
1992-01-12 23:32:55 +00:00
|
|
|
self.stopframe = self.botframe
|
|
|
|
return 1
|
1992-01-10 14:54:42 +00:00
|
|
|
do_c = do_cont = do_continue
|
|
|
|
|
|
|
|
def do_quit(self, arg):
|
1992-01-16 13:50:21 +00:00
|
|
|
self.quitting = 1
|
|
|
|
sys.trace = None; del sys.trace
|
1992-01-12 23:32:55 +00:00
|
|
|
raise PdbQuit
|
1992-01-10 14:54:42 +00:00
|
|
|
do_q = do_quit
|
|
|
|
|
|
|
|
def do_list(self, arg):
|
1992-01-12 23:32:55 +00:00
|
|
|
self.lastcmd = 'list'
|
1992-01-10 14:54:42 +00:00
|
|
|
last = None
|
|
|
|
if arg:
|
|
|
|
try:
|
|
|
|
x = eval(arg, {}, {})
|
|
|
|
if type(x) == type(()):
|
|
|
|
first, last = x
|
|
|
|
first = int(first)
|
|
|
|
last = int(last)
|
|
|
|
if last < first:
|
|
|
|
# Assume it's a count
|
|
|
|
last = first + last
|
|
|
|
else:
|
|
|
|
first = int(x)
|
|
|
|
except:
|
|
|
|
print '*** Error in argument:', `arg`
|
|
|
|
return
|
|
|
|
elif self.lineno is None:
|
|
|
|
first = max(1, self.curframe.f_lineno - 5)
|
|
|
|
else:
|
|
|
|
first = self.lineno + 1
|
|
|
|
if last is None:
|
|
|
|
last = first + 10
|
|
|
|
filename = self.curframe.f_code.co_filename
|
|
|
|
if self.breaks.has_key(filename):
|
|
|
|
breaklist = self.breaks[filename]
|
|
|
|
else:
|
|
|
|
breaklist = []
|
|
|
|
try:
|
|
|
|
for lineno in range(first, last+1):
|
|
|
|
line = linecache.getline(filename, lineno)
|
|
|
|
if not line:
|
|
|
|
print '[EOF]'
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
s = string.rjust(`lineno`, 3)
|
|
|
|
if len(s) < 4: s = s + ' '
|
|
|
|
if lineno in breaklist: s = s + 'B'
|
|
|
|
else: s = s + ' '
|
|
|
|
if lineno == self.curframe.f_lineno:
|
|
|
|
s = s + '->'
|
|
|
|
print s + '\t' + line,
|
|
|
|
self.lineno = lineno
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
do_l = do_list
|
1992-01-12 23:32:55 +00:00
|
|
|
|
|
|
|
def do_args(self, arg):
|
|
|
|
try:
|
|
|
|
value = eval('__args__', self.curframe.f_globals, \
|
|
|
|
self.curframe.f_locals)
|
|
|
|
except:
|
|
|
|
print '***', sys.exc_type + ':', `sys.exc_value`
|
|
|
|
return
|
|
|
|
print `value`
|
|
|
|
do_a = do_args
|
|
|
|
|
|
|
|
def do_p(self, arg):
|
|
|
|
try:
|
|
|
|
value = eval(arg, self.curframe.f_globals, \
|
|
|
|
self.curframe.f_locals)
|
|
|
|
except:
|
|
|
|
print '***', sys.exc_type + ':', `sys.exc_value`
|
|
|
|
return
|
|
|
|
print `value`
|
1992-01-10 14:54:42 +00:00
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
# Print a traceback starting at the top stack frame.
|
|
|
|
# Note that the most recently entered frame is printed last;
|
|
|
|
# this is different from dbx and gdb, but consistent with
|
|
|
|
# the Python interpreter's stack trace.
|
|
|
|
# It is also consistent with the up/down commands (which are
|
|
|
|
# compatible with dbx and gdb: up moves towards 'main()'
|
|
|
|
# and down moves towards the most recent stack frame).
|
1992-01-10 14:54:42 +00:00
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
def printstacktrace(self):
|
|
|
|
for x in self.stack:
|
|
|
|
self.printframelineno(x)
|
|
|
|
|
|
|
|
def printframelineno(self, (frame, lineno)):
|
1992-01-10 14:54:42 +00:00
|
|
|
if frame is self.curframe: print '->',
|
|
|
|
code = frame.f_code
|
|
|
|
filename = code.co_filename
|
|
|
|
print filename + '(' + `lineno` + ')',
|
|
|
|
line = linecache.getline(filename, lineno)
|
1992-01-16 13:50:21 +00:00
|
|
|
print string.strip(line),
|
1992-01-10 14:54:42 +00:00
|
|
|
print
|
|
|
|
|
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
def run(statement):
|
|
|
|
Pdb().init().run(statement)
|
|
|
|
|
|
|
|
def runctx(statement, globals, locals):
|
|
|
|
Pdb().init().runctx(statement, globals, locals)
|
|
|
|
|
|
|
|
|
1992-01-10 14:54:42 +00:00
|
|
|
# --------------------- testing ---------------------
|
|
|
|
|
|
|
|
# The Ackermann function -- a highly recursive beast
|
1992-01-16 13:50:21 +00:00
|
|
|
cheat = 2
|
1992-01-10 14:54:42 +00:00
|
|
|
cache = {}
|
|
|
|
def ack(x, y):
|
|
|
|
key = `(long(x), long(y))`
|
|
|
|
if cache.has_key(key):
|
|
|
|
res = cache[key]
|
|
|
|
else:
|
|
|
|
if x == 0:
|
|
|
|
res = 1L
|
|
|
|
elif y == 0:
|
|
|
|
if x == 1:
|
|
|
|
res = 2L
|
|
|
|
else:
|
|
|
|
res = 2L + x
|
|
|
|
elif y == 1 and cheat >= 1:
|
|
|
|
res = 2L * x
|
|
|
|
elif y == 2 and cheat >= 2:
|
|
|
|
res = pow(2L, x)
|
|
|
|
else:
|
|
|
|
res = ack(ack(x-1, y), y-1)
|
|
|
|
cache[key] = res
|
|
|
|
return res
|
|
|
|
|
|
|
|
def foo(n):
|
|
|
|
print 'foo', n
|
|
|
|
x = bar(n*2)
|
|
|
|
print 'bar returned', x
|
1992-01-16 13:50:21 +00:00
|
|
|
y = ack(4, 3)
|
|
|
|
return y
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
def bar(a):
|
|
|
|
print 'bar', a
|
|
|
|
return a*10
|
|
|
|
|
1992-01-16 13:50:21 +00:00
|
|
|
def melt(n):
|
|
|
|
print 1.0/n
|
|
|
|
melt(n-1)
|
|
|
|
|
1992-01-10 14:54:42 +00:00
|
|
|
def test():
|
|
|
|
linecache.checkcache()
|
1992-01-16 13:50:21 +00:00
|
|
|
runctx('from pdb import foo; foo(12)', {}, {})
|
|
|
|
runctx('from pdb import melt; melt(5)', {}, {})
|
1992-01-10 14:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------------------- main ---------------------
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if sys.argv[1:]:
|
|
|
|
file = sys.argv[1]
|
|
|
|
head, tail = os.path.split(file)
|
|
|
|
if tail[-3:] != '.py':
|
|
|
|
print 'Sorry, file arg must be a python module'
|
|
|
|
print '(i.e., must end in \'.py\')'
|
|
|
|
# XXX Or we could copy it to a temp file
|
|
|
|
sys.exit(2)
|
|
|
|
del sys.argv[0]
|
|
|
|
sys.path.insert(0, head)
|
1992-01-12 23:32:55 +00:00
|
|
|
run('import ' + tail[:-3])
|
1992-01-10 14:54:42 +00:00
|
|
|
else:
|
1992-01-16 13:50:21 +00:00
|
|
|
run(raw_input('Python statement to debug: '))
|