blob: 3796e0d248ac34cbd214c34432276be0649236a2 [file] [log] [blame]
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001# pdb.py -- finally, a Python debugger!
Guido van Rossum92df0c61992-01-14 18:30:15 +00002
Guido van Rossum23efba41992-01-27 16:58:47 +00003# (See pdb.doc for documentation.)
Guido van Rossum921c8241992-01-10 14:54:42 +00004
5import string
6import sys
7import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +00008import cmd
9import bdb
10import repr
Guido van Rossum921c8241992-01-10 14:54:42 +000011
12
Guido van Rossuma558e371994-11-10 22:27:35 +000013# Interaction prompt line will separate file and call info from code
14# text using value of line_prefix string. A newline and arrow may
15# be to your liking. You can set it once pdb is imported using the
16# command "pdb.line_prefix = '\n% '".
17# line_prefix = ': ' # Use this to get the old situation back
18line_prefix = '\n-> ' # Probably a better default
19
Guido van Rossum23efba41992-01-27 16:58:47 +000020class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum921c8241992-01-10 14:54:42 +000021
Guido van Rossum5ef74b81993-06-23 11:55:24 +000022 def __init__(self):
23 bdb.Bdb.__init__(self)
24 cmd.Cmd.__init__(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000025 self.prompt = '(Pdb) '
Guido van Rossum921c8241992-01-10 14:54:42 +000026
27 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000028 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000029 self.forget()
30
31 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000032 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000033 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000034 self.curindex = 0
35 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000036
37 def setup(self, f, t):
38 self.forget()
39 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000040 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000041
Guido van Rossum23efba41992-01-27 16:58:47 +000042 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000043
Guido van Rossum23efba41992-01-27 16:58:47 +000044 def user_line(self, frame):
45 # This function is called when we stop or break at this line
46 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000047
Guido van Rossum23efba41992-01-27 16:58:47 +000048 def user_return(self, frame, return_value):
49 # This function is called when a return trap is set here
50 frame.f_locals['__return__'] = return_value
51 print '--Return--'
52 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000053
Guido van Rossum23efba41992-01-27 16:58:47 +000054 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
55 # This function is called if an exception occurs,
56 # but only if we are to stop at or just below this level
57 frame.f_locals['__exception__'] = exc_type, exc_value
58 print exc_type + ':', repr.repr(exc_value)
59 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000060
Guido van Rossum23efba41992-01-27 16:58:47 +000061 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000062
Guido van Rossum23efba41992-01-27 16:58:47 +000063 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000064 self.setup(frame, traceback)
Guido van Rossumb6aa92e1995-02-03 12:50:04 +000065 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000066 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000067 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000068
Guido van Rossum921c8241992-01-10 14:54:42 +000069 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000070 if line[:1] == '!': line = line[1:]
71 locals = self.curframe.f_locals
72 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000073 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000074 try:
75 exec(line + '\n', globals, locals)
76 except:
77 print '***', sys.exc_type + ':', sys.exc_value
78
79 # Command definitions, called by cmdloop()
80 # The argument is the remaining string on the command line
81 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000082
Guido van Rossum23efba41992-01-27 16:58:47 +000083 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000084
Guido van Rossum921c8241992-01-10 14:54:42 +000085 def do_break(self, arg):
86 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000087 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000088 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000089 # Try line number as argument
90 try:
Guido van Rossum921c8241992-01-10 14:54:42 +000091 lineno = int(eval(arg))
Guido van Rossumb6775db1994-08-01 11:34:53 +000092 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +000093 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +000094 # Try function name as the argument
95 import codehack
96 try:
97 func = eval(arg, self.curframe.f_globals,
98 self.curframe.f_locals)
99 if hasattr(func, 'im_func'):
100 func = func.im_func
101 code = func.func_code
102 except:
103 print '*** Could not eval argument:', arg
104 return
105 lineno = codehack.getlineno(code)
106 filename = code.co_filename
107
108 # now set the break point
Guido van Rossum23efba41992-01-27 16:58:47 +0000109 err = self.set_break(filename, lineno)
110 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +0000111 do_b = do_break
112
113 def do_clear(self, arg):
114 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000115 try:
116 reply = raw_input('Clear all breaks? ')
117 except EOFError:
118 reply = 'no'
119 reply = string.lower(string.strip(reply))
120 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000121 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000122 return
123 try:
124 lineno = int(eval(arg))
125 except:
126 print '*** Error in argument:', `arg`
127 return
128 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000129 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000130 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000131 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000132
133 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000134 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000135 do_w = do_where
136
137 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000138 if self.curindex == 0:
139 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000140 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000141 self.curindex = self.curindex - 1
142 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000143 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000144 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000145 do_u = do_up
146
147 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000148 if self.curindex + 1 == len(self.stack):
149 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000150 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000151 self.curindex = self.curindex + 1
152 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000153 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000154 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000155 do_d = do_down
156
157 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000158 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000159 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000160 do_s = do_step
161
162 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000163 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000164 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000165 do_n = do_next
166
Guido van Rossumb9142571992-01-12 23:32:55 +0000167 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000168 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000169 return 1
170 do_r = do_return
171
Guido van Rossum921c8241992-01-10 14:54:42 +0000172 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000173 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000174 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000175 do_c = do_cont = do_continue
176
177 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000178 self.set_quit()
179 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000180 do_q = do_quit
181
Guido van Rossum23efba41992-01-27 16:58:47 +0000182 def do_args(self, arg):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 if self.curframe.f_locals.has_key('__args__'):
184 print `self.curframe.f_locals['__args__']`
Guido van Rossum23efba41992-01-27 16:58:47 +0000185 else:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186 print '*** No arguments?!'
Guido van Rossum23efba41992-01-27 16:58:47 +0000187 do_a = do_args
188
189 def do_retval(self, arg):
190 if self.curframe.f_locals.has_key('__return__'):
191 print self.curframe.f_locals['__return__']
192 else:
193 print '*** Not yet returned!'
194 do_rv = do_retval
195
196 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000197 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000198 try:
199 value = eval(arg, self.curframe.f_globals, \
200 self.curframe.f_locals)
201 except:
202 print '***', sys.exc_type + ':', `sys.exc_value`
203 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000204
Guido van Rossum23efba41992-01-27 16:58:47 +0000205 print `value`
206
Guido van Rossum921c8241992-01-10 14:54:42 +0000207 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000208 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000209 last = None
210 if arg:
211 try:
212 x = eval(arg, {}, {})
213 if type(x) == type(()):
214 first, last = x
215 first = int(first)
216 last = int(last)
217 if last < first:
218 # Assume it's a count
219 last = first + last
220 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000221 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000222 except:
223 print '*** Error in argument:', `arg`
224 return
225 elif self.lineno is None:
226 first = max(1, self.curframe.f_lineno - 5)
227 else:
228 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000229 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000230 last = first + 10
231 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000232 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000233 try:
234 for lineno in range(first, last+1):
235 line = linecache.getline(filename, lineno)
236 if not line:
237 print '[EOF]'
238 break
239 else:
240 s = string.rjust(`lineno`, 3)
241 if len(s) < 4: s = s + ' '
242 if lineno in breaklist: s = s + 'B'
243 else: s = s + ' '
244 if lineno == self.curframe.f_lineno:
245 s = s + '->'
246 print s + '\t' + line,
247 self.lineno = lineno
248 except KeyboardInterrupt:
249 pass
250 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000251
252 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000253 try:
254 value = eval(arg, self.curframe.f_globals, \
255 self.curframe.f_locals)
256 except:
257 print '***', sys.exc_type + ':', `sys.exc_value`
258 return
259 code = None
260 # Is it a function?
261 try: code = value.func_code
262 except: pass
263 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000265 return
266 # Is it an instance method?
267 try: code = value.im_func.func_code
268 except: pass
269 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000271 return
272 # None of the above...
273 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000274
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000275 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000276 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000277 # this is different from dbx and gdb, but consistent with
278 # the Python interpreter's stack trace.
279 # It is also consistent with the up/down commands (which are
280 # compatible with dbx and gdb: up moves towards 'main()'
281 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000282
Guido van Rossum23efba41992-01-27 16:58:47 +0000283 def print_stack_trace(self):
284 try:
285 for frame_lineno in self.stack:
286 self.print_stack_entry(frame_lineno)
287 except KeyboardInterrupt:
288 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000289
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000290 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000291 frame, lineno = frame_lineno
292 if frame is self.curframe:
293 print '>',
294 else:
295 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000296 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000297
298
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299 # Help methods (derived from pdb.doc)
300
301 def help_help(self):
302 self.help_h()
303
304 def help_h(self):
305 print """h(elp)
306 Without argument, print the list of available commands.
307 With a command name as argument, print help about that command
308 "help pdb" pipes the full documentation file to the $PAGER
309 "help exec" gives help on the ! command"""
310
311 def help_where(self):
312 self.help_w()
313
314 def help_w(self):
315 print """w(here)
316 Print a stack trace, with the most recent frame at the bottom.
317 An arrow indicates the "current frame", which determines the
318 context of most commands."""
319
320 def help_down(self):
321 self.help_d()
322
323 def help_d(self):
324 print """d(own)
325 Move the current frame one level down in the stack trace
326 (to an older frame)."""
327
328 def help_up(self):
329 self.help_u()
330
331 def help_u(self):
332 print """u(p)
333 Move the current frame one level up in the stack trace
334 (to a newer frame)."""
335
336 def help_break(self):
337 self.help_b()
338
339 def help_b(self):
340 print """b(reak) [lineno | function]
341 With a line number argument, set a break there in the current
342 file. With a function name, set a break at the entry of that
343 function. Without argument, list all breaks."""
344
345 def help_clear(self):
346 self.help_cl()
347
348 def help_cl(self):
349 print """cl(ear) [lineno]
350 With a line number argument, clear that break in the current file.
351 Without argument, clear all breaks (but first ask confirmation)."""
352
353 def help_step(self):
354 self.help_s()
355
356 def help_s(self):
357 print """s(tep)
358 Execute the current line, stop at the first possible occasion
359 (either in a function that is called or in the current function)."""
360
361 def help_next(self):
362 self.help_n()
363
364 def help_n(self):
365 print """n(ext)
366 Continue execution until the next line in the current function
367 is reached or it returns."""
368
369 def help_return(self):
370 self.help_r()
371
372 def help_r(self):
373 print """r(eturn)
374 Continue execution until the current function returns."""
375
376 def help_continue(self):
377 self.help_c()
378
379 def help_cont(self):
380 self.help_c()
381
382 def help_c(self):
383 print """c(ont(inue))
384 Continue execution, only stop when a breakpoint is encountered."""
385
386 def help_list(self):
387 self.help_l()
388
389 def help_l(self):
390 print """l(ist) [first [,last]]
391 List source code for the current file.
392 Without arguments, list 11 lines around the current line
393 or continue the previous listing.
394 With one argument, list 11 lines starting at that line.
395 With two arguments, list the given range;
396 if the second argument is less than the first, it is a count."""
397
398 def help_args(self):
399 self.help_a()
400
401 def help_a(self):
402 print """a(rgs)
403 Print the argument list of the current function."""
404
405 def help_p(self):
406 print """p expression
407 Print the value of the expression."""
408
409 def help_exec(self):
410 print """(!) statement
411 Execute the (one-line) statement in the context of
412 the current stack frame.
413 The exclamation point can be omitted unless the first word
414 of the statement resembles a debugger command.
415 To assign to a global variable you must always prefix the
416 command with a 'global' command, e.g.:
417 (Pdb) global list_options; list_options = ['-l']
418 (Pdb)"""
419
420 def help_quit(self):
421 self.help_q()
422
423 def help_q(self):
424 print """q(uit) Quit from the debugger.
425 The program being executed is aborted."""
426
427 def help_pdb(self):
428 help()
429
Guido van Rossum35771131992-09-08 11:59:04 +0000430# Simplified interface
431
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000432def run(statement):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000433 Pdb().run(statement)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000434
435def runctx(statement, globals, locals):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000436 Pdb().runctx(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000437
Guido van Rossum4e160981992-09-02 20:43:20 +0000438def runcall(*args):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000439 apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000440
Guido van Rossumb6775db1994-08-01 11:34:53 +0000441def set_trace():
442 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000443
444# Post-Mortem interface
445
446def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000447 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000448 p.reset()
449 while t.tb_next <> None: t = t.tb_next
450 p.interaction(t.tb_frame, t)
451
452def pm():
453 import sys
454 post_mortem(sys.last_traceback)
455
456
457# Main program for testing
458
Guido van Rossum23efba41992-01-27 16:58:47 +0000459TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000460
Guido van Rossum921c8241992-01-10 14:54:42 +0000461def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000462 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000463
464# print help
465def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000466 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000467 for dirname in sys.path:
468 fullname = os.path.join(dirname, 'pdb.doc')
469 if os.path.exists(fullname):
470 sts = os.system('${PAGER-more} '+fullname)
471 if sts: print '*** Pager exit status:', sts
472 break
473 else:
474 print 'Sorry, can\'t find the help file "pdb.doc"',
475 print 'along the Python search path'