blob: a77dd29f89842fb80b2e17a2121acbab3ebdb3d4 [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 Rossum23efba41992-01-27 16:58:47 +000013class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum921c8241992-01-10 14:54:42 +000014
Guido van Rossum5ef74b81993-06-23 11:55:24 +000015 def __init__(self):
16 bdb.Bdb.__init__(self)
17 cmd.Cmd.__init__(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000018 self.prompt = '(Pdb) '
Guido van Rossum921c8241992-01-10 14:54:42 +000019
20 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000021 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000022 self.forget()
23
24 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000025 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000026 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000027 self.curindex = 0
28 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000029
30 def setup(self, f, t):
31 self.forget()
32 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000033 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000034
Guido van Rossum23efba41992-01-27 16:58:47 +000035 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000036
Guido van Rossum23efba41992-01-27 16:58:47 +000037 def user_line(self, frame):
38 # This function is called when we stop or break at this line
39 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000040
Guido van Rossum23efba41992-01-27 16:58:47 +000041 def user_return(self, frame, return_value):
42 # This function is called when a return trap is set here
43 frame.f_locals['__return__'] = return_value
44 print '--Return--'
45 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000046
Guido van Rossum23efba41992-01-27 16:58:47 +000047 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
48 # This function is called if an exception occurs,
49 # but only if we are to stop at or just below this level
50 frame.f_locals['__exception__'] = exc_type, exc_value
51 print exc_type + ':', repr.repr(exc_value)
52 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000053
Guido van Rossum23efba41992-01-27 16:58:47 +000054 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000055
Guido van Rossum23efba41992-01-27 16:58:47 +000056 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000057 self.setup(frame, traceback)
Guido van Rossum23efba41992-01-27 16:58:47 +000058 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000059 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000060 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000061
Guido van Rossum921c8241992-01-10 14:54:42 +000062 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000063 if line[:1] == '!': line = line[1:]
64 locals = self.curframe.f_locals
65 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000066 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000067 try:
68 exec(line + '\n', globals, locals)
69 except:
70 print '***', sys.exc_type + ':', sys.exc_value
71
72 # Command definitions, called by cmdloop()
73 # The argument is the remaining string on the command line
74 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000075
Guido van Rossum23efba41992-01-27 16:58:47 +000076 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Guido van Rossum921c8241992-01-10 14:54:42 +000078 def do_break(self, arg):
79 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000080 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000081 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000082 # Try line number as argument
83 try:
Guido van Rossum921c8241992-01-10 14:54:42 +000084 lineno = int(eval(arg))
Guido van Rossumb6775db1994-08-01 11:34:53 +000085 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +000086 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +000087 # Try function name as the argument
88 import codehack
89 try:
90 func = eval(arg, self.curframe.f_globals,
91 self.curframe.f_locals)
92 if hasattr(func, 'im_func'):
93 func = func.im_func
94 code = func.func_code
95 except:
96 print '*** Could not eval argument:', arg
97 return
98 lineno = codehack.getlineno(code)
99 filename = code.co_filename
100
101 # now set the break point
Guido van Rossum23efba41992-01-27 16:58:47 +0000102 err = self.set_break(filename, lineno)
103 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +0000104 do_b = do_break
105
106 def do_clear(self, arg):
107 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000108 try:
109 reply = raw_input('Clear all breaks? ')
110 except EOFError:
111 reply = 'no'
112 reply = string.lower(string.strip(reply))
113 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000114 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000115 return
116 try:
117 lineno = int(eval(arg))
118 except:
119 print '*** Error in argument:', `arg`
120 return
121 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000122 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000123 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000124 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000125
126 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000127 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000128 do_w = do_where
129
130 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000131 if self.curindex == 0:
132 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000133 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000134 self.curindex = self.curindex - 1
135 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000136 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000137 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000138 do_u = do_up
139
140 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000141 if self.curindex + 1 == len(self.stack):
142 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000143 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000144 self.curindex = self.curindex + 1
145 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000146 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000147 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000148 do_d = do_down
149
150 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000151 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000152 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000153 do_s = do_step
154
155 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000156 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000157 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000158 do_n = do_next
159
Guido van Rossumb9142571992-01-12 23:32:55 +0000160 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000161 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000162 return 1
163 do_r = do_return
164
Guido van Rossum921c8241992-01-10 14:54:42 +0000165 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000166 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000167 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000168 do_c = do_cont = do_continue
169
170 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000171 self.set_quit()
172 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000173 do_q = do_quit
174
Guido van Rossum23efba41992-01-27 16:58:47 +0000175 def do_args(self, arg):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 if self.curframe.f_locals.has_key('__args__'):
177 print `self.curframe.f_locals['__args__']`
Guido van Rossum23efba41992-01-27 16:58:47 +0000178 else:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179 print '*** No arguments?!'
Guido van Rossum23efba41992-01-27 16:58:47 +0000180 do_a = do_args
181
182 def do_retval(self, arg):
183 if self.curframe.f_locals.has_key('__return__'):
184 print self.curframe.f_locals['__return__']
185 else:
186 print '*** Not yet returned!'
187 do_rv = do_retval
188
189 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000190 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000191 try:
192 value = eval(arg, self.curframe.f_globals, \
193 self.curframe.f_locals)
194 except:
195 print '***', sys.exc_type + ':', `sys.exc_value`
196 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000197
Guido van Rossum23efba41992-01-27 16:58:47 +0000198 print `value`
199
Guido van Rossum921c8241992-01-10 14:54:42 +0000200 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000201 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000202 last = None
203 if arg:
204 try:
205 x = eval(arg, {}, {})
206 if type(x) == type(()):
207 first, last = x
208 first = int(first)
209 last = int(last)
210 if last < first:
211 # Assume it's a count
212 last = first + last
213 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000214 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000215 except:
216 print '*** Error in argument:', `arg`
217 return
218 elif self.lineno is None:
219 first = max(1, self.curframe.f_lineno - 5)
220 else:
221 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000222 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000223 last = first + 10
224 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000225 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000226 try:
227 for lineno in range(first, last+1):
228 line = linecache.getline(filename, lineno)
229 if not line:
230 print '[EOF]'
231 break
232 else:
233 s = string.rjust(`lineno`, 3)
234 if len(s) < 4: s = s + ' '
235 if lineno in breaklist: s = s + 'B'
236 else: s = s + ' '
237 if lineno == self.curframe.f_lineno:
238 s = s + '->'
239 print s + '\t' + line,
240 self.lineno = lineno
241 except KeyboardInterrupt:
242 pass
243 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000244
245 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000246 try:
247 value = eval(arg, self.curframe.f_globals, \
248 self.curframe.f_locals)
249 except:
250 print '***', sys.exc_type + ':', `sys.exc_value`
251 return
252 code = None
253 # Is it a function?
254 try: code = value.func_code
255 except: pass
256 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000258 return
259 # Is it an instance method?
260 try: code = value.im_func.func_code
261 except: pass
262 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000264 return
265 # None of the above...
266 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000267
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000268 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000269 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000270 # this is different from dbx and gdb, but consistent with
271 # the Python interpreter's stack trace.
272 # It is also consistent with the up/down commands (which are
273 # compatible with dbx and gdb: up moves towards 'main()'
274 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000275
Guido van Rossum23efba41992-01-27 16:58:47 +0000276 def print_stack_trace(self):
277 try:
278 for frame_lineno in self.stack:
279 self.print_stack_entry(frame_lineno)
280 except KeyboardInterrupt:
281 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000282
Guido van Rossum23efba41992-01-27 16:58:47 +0000283 def print_stack_entry(self, frame_lineno):
284 frame, lineno = frame_lineno
285 if frame is self.curframe:
286 print '>',
287 else:
288 print ' ',
289 print self.format_stack_entry(frame_lineno)
Guido van Rossum921c8241992-01-10 14:54:42 +0000290
291
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292 # Help methods (derived from pdb.doc)
293
294 def help_help(self):
295 self.help_h()
296
297 def help_h(self):
298 print """h(elp)
299 Without argument, print the list of available commands.
300 With a command name as argument, print help about that command
301 "help pdb" pipes the full documentation file to the $PAGER
302 "help exec" gives help on the ! command"""
303
304 def help_where(self):
305 self.help_w()
306
307 def help_w(self):
308 print """w(here)
309 Print a stack trace, with the most recent frame at the bottom.
310 An arrow indicates the "current frame", which determines the
311 context of most commands."""
312
313 def help_down(self):
314 self.help_d()
315
316 def help_d(self):
317 print """d(own)
318 Move the current frame one level down in the stack trace
319 (to an older frame)."""
320
321 def help_up(self):
322 self.help_u()
323
324 def help_u(self):
325 print """u(p)
326 Move the current frame one level up in the stack trace
327 (to a newer frame)."""
328
329 def help_break(self):
330 self.help_b()
331
332 def help_b(self):
333 print """b(reak) [lineno | function]
334 With a line number argument, set a break there in the current
335 file. With a function name, set a break at the entry of that
336 function. Without argument, list all breaks."""
337
338 def help_clear(self):
339 self.help_cl()
340
341 def help_cl(self):
342 print """cl(ear) [lineno]
343 With a line number argument, clear that break in the current file.
344 Without argument, clear all breaks (but first ask confirmation)."""
345
346 def help_step(self):
347 self.help_s()
348
349 def help_s(self):
350 print """s(tep)
351 Execute the current line, stop at the first possible occasion
352 (either in a function that is called or in the current function)."""
353
354 def help_next(self):
355 self.help_n()
356
357 def help_n(self):
358 print """n(ext)
359 Continue execution until the next line in the current function
360 is reached or it returns."""
361
362 def help_return(self):
363 self.help_r()
364
365 def help_r(self):
366 print """r(eturn)
367 Continue execution until the current function returns."""
368
369 def help_continue(self):
370 self.help_c()
371
372 def help_cont(self):
373 self.help_c()
374
375 def help_c(self):
376 print """c(ont(inue))
377 Continue execution, only stop when a breakpoint is encountered."""
378
379 def help_list(self):
380 self.help_l()
381
382 def help_l(self):
383 print """l(ist) [first [,last]]
384 List source code for the current file.
385 Without arguments, list 11 lines around the current line
386 or continue the previous listing.
387 With one argument, list 11 lines starting at that line.
388 With two arguments, list the given range;
389 if the second argument is less than the first, it is a count."""
390
391 def help_args(self):
392 self.help_a()
393
394 def help_a(self):
395 print """a(rgs)
396 Print the argument list of the current function."""
397
398 def help_p(self):
399 print """p expression
400 Print the value of the expression."""
401
402 def help_exec(self):
403 print """(!) statement
404 Execute the (one-line) statement in the context of
405 the current stack frame.
406 The exclamation point can be omitted unless the first word
407 of the statement resembles a debugger command.
408 To assign to a global variable you must always prefix the
409 command with a 'global' command, e.g.:
410 (Pdb) global list_options; list_options = ['-l']
411 (Pdb)"""
412
413 def help_quit(self):
414 self.help_q()
415
416 def help_q(self):
417 print """q(uit) Quit from the debugger.
418 The program being executed is aborted."""
419
420 def help_pdb(self):
421 help()
422
Guido van Rossum35771131992-09-08 11:59:04 +0000423# Simplified interface
424
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000425def run(statement):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000426 Pdb().run(statement)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000427
428def runctx(statement, globals, locals):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000429 Pdb().runctx(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000430
Guido van Rossum4e160981992-09-02 20:43:20 +0000431def runcall(*args):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000432 apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000433
Guido van Rossumb6775db1994-08-01 11:34:53 +0000434def set_trace():
435 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000436
437# Post-Mortem interface
438
439def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000440 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000441 p.reset()
442 while t.tb_next <> None: t = t.tb_next
443 p.interaction(t.tb_frame, t)
444
445def pm():
446 import sys
447 post_mortem(sys.last_traceback)
448
449
450# Main program for testing
451
Guido van Rossum23efba41992-01-27 16:58:47 +0000452TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000453
Guido van Rossum921c8241992-01-10 14:54:42 +0000454def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000455 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000456
457# print help
458def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000459 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000460 for dirname in sys.path:
461 fullname = os.path.join(dirname, 'pdb.doc')
462 if os.path.exists(fullname):
463 sts = os.system('${PAGER-more} '+fullname)
464 if sts: print '*** Pager exit status:', sts
465 break
466 else:
467 print 'Sorry, can\'t find the help file "pdb.doc"',
468 print 'along the Python search path'