blob: f44c7e08dda23834709b6b4d003537e5fb5a6fe5 [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 Rossumec8fd941995-08-07 20:16:05 +000077 code = compile(line + '\n', '<stdin>', 'single')
Guido van Rossum23efba41992-01-27 16:58:47 +000078 try:
Guido van Rossumec8fd941995-08-07 20:16:05 +000079 exec code in globals, locals
Guido van Rossum23efba41992-01-27 16:58:47 +000080 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000081 if type(sys.exc_type) == type(''):
82 exc_type_name = sys.exc_type
83 else: exc_type_name = sys.exc_type.__name__
84 print '***', exc_type_name + ':', sys.exc_value
Guido van Rossum23efba41992-01-27 16:58:47 +000085
86 # Command definitions, called by cmdloop()
87 # The argument is the remaining string on the command line
88 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000089
Guido van Rossum23efba41992-01-27 16:58:47 +000090 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000091
Guido van Rossum921c8241992-01-10 14:54:42 +000092 def do_break(self, arg):
93 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000094 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000095 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000096 # Try line number as argument
97 try:
Guido van Rossum921c8241992-01-10 14:54:42 +000098 lineno = int(eval(arg))
Guido van Rossumb6775db1994-08-01 11:34:53 +000099 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +0000100 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101 # Try function name as the argument
102 import codehack
103 try:
104 func = eval(arg, self.curframe.f_globals,
105 self.curframe.f_locals)
106 if hasattr(func, 'im_func'):
107 func = func.im_func
108 code = func.func_code
109 except:
110 print '*** Could not eval argument:', arg
111 return
112 lineno = codehack.getlineno(code)
113 filename = code.co_filename
114
115 # now set the break point
Guido van Rossum23efba41992-01-27 16:58:47 +0000116 err = self.set_break(filename, lineno)
117 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +0000118 do_b = do_break
119
120 def do_clear(self, arg):
121 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000122 try:
123 reply = raw_input('Clear all breaks? ')
124 except EOFError:
125 reply = 'no'
126 reply = string.lower(string.strip(reply))
127 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000128 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000129 return
130 try:
131 lineno = int(eval(arg))
132 except:
133 print '*** Error in argument:', `arg`
134 return
135 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000136 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000137 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000138 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000139
140 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000141 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000142 do_w = do_where
143
144 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000145 if self.curindex == 0:
146 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000147 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000148 self.curindex = self.curindex - 1
149 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000150 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000151 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000152 do_u = do_up
153
154 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000155 if self.curindex + 1 == len(self.stack):
156 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000157 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000158 self.curindex = self.curindex + 1
159 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000160 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000161 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000162 do_d = do_down
163
164 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000165 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000166 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000167 do_s = do_step
168
169 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000170 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000171 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000172 do_n = do_next
173
Guido van Rossumb9142571992-01-12 23:32:55 +0000174 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000175 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000176 return 1
177 do_r = do_return
178
Guido van Rossum921c8241992-01-10 14:54:42 +0000179 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000180 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000181 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000182 do_c = do_cont = do_continue
183
184 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000185 self.set_quit()
186 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000187 do_q = do_quit
188
Guido van Rossum23efba41992-01-27 16:58:47 +0000189 def do_args(self, arg):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190 if self.curframe.f_locals.has_key('__args__'):
191 print `self.curframe.f_locals['__args__']`
Guido van Rossum23efba41992-01-27 16:58:47 +0000192 else:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000193 print '*** No arguments?!'
Guido van Rossum23efba41992-01-27 16:58:47 +0000194 do_a = do_args
195
196 def do_retval(self, arg):
197 if self.curframe.f_locals.has_key('__return__'):
198 print self.curframe.f_locals['__return__']
199 else:
200 print '*** Not yet returned!'
201 do_rv = do_retval
202
203 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000204 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000205 try:
206 value = eval(arg, self.curframe.f_globals, \
207 self.curframe.f_locals)
208 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000209 if type(sys.exc_type) == type(''):
210 exc_type_name = sys.exc_type
211 else: exc_type_name = sys.exc_type.__name__
212 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum23efba41992-01-27 16:58:47 +0000213 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000214
Guido van Rossum23efba41992-01-27 16:58:47 +0000215 print `value`
216
Guido van Rossum921c8241992-01-10 14:54:42 +0000217 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000218 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000219 last = None
220 if arg:
221 try:
222 x = eval(arg, {}, {})
223 if type(x) == type(()):
224 first, last = x
225 first = int(first)
226 last = int(last)
227 if last < first:
228 # Assume it's a count
229 last = first + last
230 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000231 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000232 except:
233 print '*** Error in argument:', `arg`
234 return
235 elif self.lineno is None:
236 first = max(1, self.curframe.f_lineno - 5)
237 else:
238 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000239 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000240 last = first + 10
241 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000242 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000243 try:
244 for lineno in range(first, last+1):
245 line = linecache.getline(filename, lineno)
246 if not line:
247 print '[EOF]'
248 break
249 else:
250 s = string.rjust(`lineno`, 3)
251 if len(s) < 4: s = s + ' '
252 if lineno in breaklist: s = s + 'B'
253 else: s = s + ' '
254 if lineno == self.curframe.f_lineno:
255 s = s + '->'
256 print s + '\t' + line,
257 self.lineno = lineno
258 except KeyboardInterrupt:
259 pass
260 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000261
262 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000263 try:
264 value = eval(arg, self.curframe.f_globals, \
265 self.curframe.f_locals)
266 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000267 if type(sys.exc_type) == type(''):
268 exc_type_name = sys.exc_type
269 else: exc_type_name = sys.exc_type.__name__
270 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum00230781993-03-29 11:39:45 +0000271 return
272 code = None
273 # Is it a function?
274 try: code = value.func_code
275 except: pass
276 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000278 return
279 # Is it an instance method?
280 try: code = value.im_func.func_code
281 except: pass
282 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000284 return
285 # None of the above...
286 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000287
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000288 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000289 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000290 # this is different from dbx and gdb, but consistent with
291 # the Python interpreter's stack trace.
292 # It is also consistent with the up/down commands (which are
293 # compatible with dbx and gdb: up moves towards 'main()'
294 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000295
Guido van Rossum23efba41992-01-27 16:58:47 +0000296 def print_stack_trace(self):
297 try:
298 for frame_lineno in self.stack:
299 self.print_stack_entry(frame_lineno)
300 except KeyboardInterrupt:
301 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000302
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000303 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000304 frame, lineno = frame_lineno
305 if frame is self.curframe:
306 print '>',
307 else:
308 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000309 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000310
311
Guido van Rossumb6775db1994-08-01 11:34:53 +0000312 # Help methods (derived from pdb.doc)
313
314 def help_help(self):
315 self.help_h()
316
317 def help_h(self):
318 print """h(elp)
319 Without argument, print the list of available commands.
320 With a command name as argument, print help about that command
321 "help pdb" pipes the full documentation file to the $PAGER
322 "help exec" gives help on the ! command"""
323
324 def help_where(self):
325 self.help_w()
326
327 def help_w(self):
328 print """w(here)
329 Print a stack trace, with the most recent frame at the bottom.
330 An arrow indicates the "current frame", which determines the
331 context of most commands."""
332
333 def help_down(self):
334 self.help_d()
335
336 def help_d(self):
337 print """d(own)
338 Move the current frame one level down in the stack trace
339 (to an older frame)."""
340
341 def help_up(self):
342 self.help_u()
343
344 def help_u(self):
345 print """u(p)
346 Move the current frame one level up in the stack trace
347 (to a newer frame)."""
348
349 def help_break(self):
350 self.help_b()
351
352 def help_b(self):
353 print """b(reak) [lineno | function]
354 With a line number argument, set a break there in the current
355 file. With a function name, set a break at the entry of that
356 function. Without argument, list all breaks."""
357
358 def help_clear(self):
359 self.help_cl()
360
361 def help_cl(self):
362 print """cl(ear) [lineno]
363 With a line number argument, clear that break in the current file.
364 Without argument, clear all breaks (but first ask confirmation)."""
365
366 def help_step(self):
367 self.help_s()
368
369 def help_s(self):
370 print """s(tep)
371 Execute the current line, stop at the first possible occasion
372 (either in a function that is called or in the current function)."""
373
374 def help_next(self):
375 self.help_n()
376
377 def help_n(self):
378 print """n(ext)
379 Continue execution until the next line in the current function
380 is reached or it returns."""
381
382 def help_return(self):
383 self.help_r()
384
385 def help_r(self):
386 print """r(eturn)
387 Continue execution until the current function returns."""
388
389 def help_continue(self):
390 self.help_c()
391
392 def help_cont(self):
393 self.help_c()
394
395 def help_c(self):
396 print """c(ont(inue))
397 Continue execution, only stop when a breakpoint is encountered."""
398
399 def help_list(self):
400 self.help_l()
401
402 def help_l(self):
403 print """l(ist) [first [,last]]
404 List source code for the current file.
405 Without arguments, list 11 lines around the current line
406 or continue the previous listing.
407 With one argument, list 11 lines starting at that line.
408 With two arguments, list the given range;
409 if the second argument is less than the first, it is a count."""
410
411 def help_args(self):
412 self.help_a()
413
414 def help_a(self):
415 print """a(rgs)
416 Print the argument list of the current function."""
417
418 def help_p(self):
419 print """p expression
420 Print the value of the expression."""
421
422 def help_exec(self):
423 print """(!) statement
424 Execute the (one-line) statement in the context of
425 the current stack frame.
426 The exclamation point can be omitted unless the first word
427 of the statement resembles a debugger command.
428 To assign to a global variable you must always prefix the
429 command with a 'global' command, e.g.:
430 (Pdb) global list_options; list_options = ['-l']
431 (Pdb)"""
432
433 def help_quit(self):
434 self.help_q()
435
436 def help_q(self):
437 print """q(uit) Quit from the debugger.
438 The program being executed is aborted."""
439
440 def help_pdb(self):
441 help()
442
Guido van Rossum35771131992-09-08 11:59:04 +0000443# Simplified interface
444
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000445def run(statement, globals=None, locals=None):
446 Pdb().run(statement, globals, locals)
447
448def runeval(expression, globals=None, locals=None):
449 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000450
451def runctx(statement, globals, locals):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000452 # B/W compatibility
453 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000454
Guido van Rossum4e160981992-09-02 20:43:20 +0000455def runcall(*args):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000456 return apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000457
Guido van Rossumb6775db1994-08-01 11:34:53 +0000458def set_trace():
459 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000460
461# Post-Mortem interface
462
463def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000464 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000465 p.reset()
466 while t.tb_next <> None: t = t.tb_next
467 p.interaction(t.tb_frame, t)
468
469def pm():
470 import sys
471 post_mortem(sys.last_traceback)
472
473
474# Main program for testing
475
Guido van Rossum23efba41992-01-27 16:58:47 +0000476TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000477
Guido van Rossum921c8241992-01-10 14:54:42 +0000478def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000479 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000480
481# print help
482def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000483 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000484 for dirname in sys.path:
485 fullname = os.path.join(dirname, 'pdb.doc')
486 if os.path.exists(fullname):
487 sts = os.system('${PAGER-more} '+fullname)
488 if sts: print '*** Pager exit status:', sts
489 break
490 else:
491 print 'Sorry, can\'t find the help file "pdb.doc"',
492 print 'along the Python search path'