blob: 7a00b83379611ea914444dbceeca4d59d0d78e8f [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
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000058 if type(exc_type) == type(''):
59 exc_type_name = exc_type
60 else: exc_type_name = exc_type.__name__
61 print exc_type_name + ':', repr.repr(exc_value)
Guido van Rossum23efba41992-01-27 16:58:47 +000062 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000063
Guido van Rossum23efba41992-01-27 16:58:47 +000064 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000065
Guido van Rossum23efba41992-01-27 16:58:47 +000066 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000067 self.setup(frame, traceback)
Guido van Rossumb6aa92e1995-02-03 12:50:04 +000068 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000069 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000070 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000071
Guido van Rossum921c8241992-01-10 14:54:42 +000072 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000073 if line[:1] == '!': line = line[1:]
74 locals = self.curframe.f_locals
75 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000076 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000077 try:
78 exec(line + '\n', globals, locals)
79 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000080 if type(sys.exc_type) == type(''):
81 exc_type_name = sys.exc_type
82 else: exc_type_name = sys.exc_type.__name__
83 print '***', exc_type_name + ':', sys.exc_value
Guido van Rossum23efba41992-01-27 16:58:47 +000084
85 # Command definitions, called by cmdloop()
86 # The argument is the remaining string on the command line
87 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000088
Guido van Rossum23efba41992-01-27 16:58:47 +000089 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000090
Guido van Rossum921c8241992-01-10 14:54:42 +000091 def do_break(self, arg):
92 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000093 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000094 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000095 # Try line number as argument
96 try:
Guido van Rossum921c8241992-01-10 14:54:42 +000097 lineno = int(eval(arg))
Guido van Rossumb6775db1994-08-01 11:34:53 +000098 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +000099 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000100 # Try function name as the argument
101 import codehack
102 try:
103 func = eval(arg, self.curframe.f_globals,
104 self.curframe.f_locals)
105 if hasattr(func, 'im_func'):
106 func = func.im_func
107 code = func.func_code
108 except:
109 print '*** Could not eval argument:', arg
110 return
111 lineno = codehack.getlineno(code)
112 filename = code.co_filename
113
114 # now set the break point
Guido van Rossum23efba41992-01-27 16:58:47 +0000115 err = self.set_break(filename, lineno)
116 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +0000117 do_b = do_break
118
119 def do_clear(self, arg):
120 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000121 try:
122 reply = raw_input('Clear all breaks? ')
123 except EOFError:
124 reply = 'no'
125 reply = string.lower(string.strip(reply))
126 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000127 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000128 return
129 try:
130 lineno = int(eval(arg))
131 except:
132 print '*** Error in argument:', `arg`
133 return
134 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000135 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000136 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000137 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000138
139 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000140 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000141 do_w = do_where
142
143 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000144 if self.curindex == 0:
145 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000146 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000147 self.curindex = self.curindex - 1
148 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000149 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000150 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000151 do_u = do_up
152
153 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000154 if self.curindex + 1 == len(self.stack):
155 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000156 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000157 self.curindex = self.curindex + 1
158 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000159 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000160 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000161 do_d = do_down
162
163 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000164 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000165 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000166 do_s = do_step
167
168 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000169 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000170 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000171 do_n = do_next
172
Guido van Rossumb9142571992-01-12 23:32:55 +0000173 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000174 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000175 return 1
176 do_r = do_return
177
Guido van Rossum921c8241992-01-10 14:54:42 +0000178 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000179 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000180 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000181 do_c = do_cont = do_continue
182
183 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000184 self.set_quit()
185 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000186 do_q = do_quit
187
Guido van Rossum23efba41992-01-27 16:58:47 +0000188 def do_args(self, arg):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189 if self.curframe.f_locals.has_key('__args__'):
190 print `self.curframe.f_locals['__args__']`
Guido van Rossum23efba41992-01-27 16:58:47 +0000191 else:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192 print '*** No arguments?!'
Guido van Rossum23efba41992-01-27 16:58:47 +0000193 do_a = do_args
194
195 def do_retval(self, arg):
196 if self.curframe.f_locals.has_key('__return__'):
197 print self.curframe.f_locals['__return__']
198 else:
199 print '*** Not yet returned!'
200 do_rv = do_retval
201
202 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000203 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000204 try:
205 value = eval(arg, self.curframe.f_globals, \
206 self.curframe.f_locals)
207 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000208 if type(sys.exc_type) == type(''):
209 exc_type_name = sys.exc_type
210 else: exc_type_name = sys.exc_type.__name__
211 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum23efba41992-01-27 16:58:47 +0000212 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000213
Guido van Rossum23efba41992-01-27 16:58:47 +0000214 print `value`
215
Guido van Rossum921c8241992-01-10 14:54:42 +0000216 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000217 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000218 last = None
219 if arg:
220 try:
221 x = eval(arg, {}, {})
222 if type(x) == type(()):
223 first, last = x
224 first = int(first)
225 last = int(last)
226 if last < first:
227 # Assume it's a count
228 last = first + last
229 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000230 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000231 except:
232 print '*** Error in argument:', `arg`
233 return
234 elif self.lineno is None:
235 first = max(1, self.curframe.f_lineno - 5)
236 else:
237 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000238 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000239 last = first + 10
240 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000241 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000242 try:
243 for lineno in range(first, last+1):
244 line = linecache.getline(filename, lineno)
245 if not line:
246 print '[EOF]'
247 break
248 else:
249 s = string.rjust(`lineno`, 3)
250 if len(s) < 4: s = s + ' '
251 if lineno in breaklist: s = s + 'B'
252 else: s = s + ' '
253 if lineno == self.curframe.f_lineno:
254 s = s + '->'
255 print s + '\t' + line,
256 self.lineno = lineno
257 except KeyboardInterrupt:
258 pass
259 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000260
261 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000262 try:
263 value = eval(arg, self.curframe.f_globals, \
264 self.curframe.f_locals)
265 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000266 if type(sys.exc_type) == type(''):
267 exc_type_name = sys.exc_type
268 else: exc_type_name = sys.exc_type.__name__
269 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum00230781993-03-29 11:39:45 +0000270 return
271 code = None
272 # Is it a function?
273 try: code = value.func_code
274 except: pass
275 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000277 return
278 # Is it an instance method?
279 try: code = value.im_func.func_code
280 except: pass
281 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000283 return
284 # None of the above...
285 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000286
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000287 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000288 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000289 # this is different from dbx and gdb, but consistent with
290 # the Python interpreter's stack trace.
291 # It is also consistent with the up/down commands (which are
292 # compatible with dbx and gdb: up moves towards 'main()'
293 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000294
Guido van Rossum23efba41992-01-27 16:58:47 +0000295 def print_stack_trace(self):
296 try:
297 for frame_lineno in self.stack:
298 self.print_stack_entry(frame_lineno)
299 except KeyboardInterrupt:
300 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000301
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000302 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000303 frame, lineno = frame_lineno
304 if frame is self.curframe:
305 print '>',
306 else:
307 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000308 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000309
310
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311 # Help methods (derived from pdb.doc)
312
313 def help_help(self):
314 self.help_h()
315
316 def help_h(self):
317 print """h(elp)
318 Without argument, print the list of available commands.
319 With a command name as argument, print help about that command
320 "help pdb" pipes the full documentation file to the $PAGER
321 "help exec" gives help on the ! command"""
322
323 def help_where(self):
324 self.help_w()
325
326 def help_w(self):
327 print """w(here)
328 Print a stack trace, with the most recent frame at the bottom.
329 An arrow indicates the "current frame", which determines the
330 context of most commands."""
331
332 def help_down(self):
333 self.help_d()
334
335 def help_d(self):
336 print """d(own)
337 Move the current frame one level down in the stack trace
338 (to an older frame)."""
339
340 def help_up(self):
341 self.help_u()
342
343 def help_u(self):
344 print """u(p)
345 Move the current frame one level up in the stack trace
346 (to a newer frame)."""
347
348 def help_break(self):
349 self.help_b()
350
351 def help_b(self):
352 print """b(reak) [lineno | function]
353 With a line number argument, set a break there in the current
354 file. With a function name, set a break at the entry of that
355 function. Without argument, list all breaks."""
356
357 def help_clear(self):
358 self.help_cl()
359
360 def help_cl(self):
361 print """cl(ear) [lineno]
362 With a line number argument, clear that break in the current file.
363 Without argument, clear all breaks (but first ask confirmation)."""
364
365 def help_step(self):
366 self.help_s()
367
368 def help_s(self):
369 print """s(tep)
370 Execute the current line, stop at the first possible occasion
371 (either in a function that is called or in the current function)."""
372
373 def help_next(self):
374 self.help_n()
375
376 def help_n(self):
377 print """n(ext)
378 Continue execution until the next line in the current function
379 is reached or it returns."""
380
381 def help_return(self):
382 self.help_r()
383
384 def help_r(self):
385 print """r(eturn)
386 Continue execution until the current function returns."""
387
388 def help_continue(self):
389 self.help_c()
390
391 def help_cont(self):
392 self.help_c()
393
394 def help_c(self):
395 print """c(ont(inue))
396 Continue execution, only stop when a breakpoint is encountered."""
397
398 def help_list(self):
399 self.help_l()
400
401 def help_l(self):
402 print """l(ist) [first [,last]]
403 List source code for the current file.
404 Without arguments, list 11 lines around the current line
405 or continue the previous listing.
406 With one argument, list 11 lines starting at that line.
407 With two arguments, list the given range;
408 if the second argument is less than the first, it is a count."""
409
410 def help_args(self):
411 self.help_a()
412
413 def help_a(self):
414 print """a(rgs)
415 Print the argument list of the current function."""
416
417 def help_p(self):
418 print """p expression
419 Print the value of the expression."""
420
421 def help_exec(self):
422 print """(!) statement
423 Execute the (one-line) statement in the context of
424 the current stack frame.
425 The exclamation point can be omitted unless the first word
426 of the statement resembles a debugger command.
427 To assign to a global variable you must always prefix the
428 command with a 'global' command, e.g.:
429 (Pdb) global list_options; list_options = ['-l']
430 (Pdb)"""
431
432 def help_quit(self):
433 self.help_q()
434
435 def help_q(self):
436 print """q(uit) Quit from the debugger.
437 The program being executed is aborted."""
438
439 def help_pdb(self):
440 help()
441
Guido van Rossum35771131992-09-08 11:59:04 +0000442# Simplified interface
443
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000444def run(statement, globals=None, locals=None):
445 Pdb().run(statement, globals, locals)
446
447def runeval(expression, globals=None, locals=None):
448 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000449
450def runctx(statement, globals, locals):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000451 # B/W compatibility
452 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000453
Guido van Rossum4e160981992-09-02 20:43:20 +0000454def runcall(*args):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000455 return apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000456
Guido van Rossumb6775db1994-08-01 11:34:53 +0000457def set_trace():
458 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000459
460# Post-Mortem interface
461
462def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000463 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000464 p.reset()
465 while t.tb_next <> None: t = t.tb_next
466 p.interaction(t.tb_frame, t)
467
468def pm():
469 import sys
470 post_mortem(sys.last_traceback)
471
472
473# Main program for testing
474
Guido van Rossum23efba41992-01-27 16:58:47 +0000475TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000476
Guido van Rossum921c8241992-01-10 14:54:42 +0000477def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000478 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000479
480# print help
481def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000482 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000483 for dirname in sys.path:
484 fullname = os.path.join(dirname, 'pdb.doc')
485 if os.path.exists(fullname):
486 sts = os.system('${PAGER-more} '+fullname)
487 if sts: print '*** Pager exit status:', sts
488 break
489 else:
490 print 'Sorry, can\'t find the help file "pdb.doc"',
491 print 'along the Python search path'