3 """A Python debugger."""
5 # (See pdb.doc for documentation.)
16 # Create a custom safe Repr instance and increase its maxstring.
17 # The default of 30 truncates error messages too easily.
20 _saferepr = _repr.repr
22 __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
23 "post_mortem", "help"]
25 def find_function(funcname, filename):
26 cre = re.compile(r'def\s+%s\s*[(]' % funcname)
31 # consumer of this info expects the first line to be 1
39 answer = funcname, filename, lineno
46 # Interaction prompt line will separate file and call info from code
47 # text using value of line_prefix string. A newline and arrow may
48 # be to your liking. You can set it once pdb is imported using the
49 # command "pdb.line_prefix = '\n% '".
50 # line_prefix = ': ' # Use this to get the old situation back
51 line_prefix = '\n-> ' # Probably a better default
53 class Pdb(bdb.Bdb, cmd.Cmd):
55 def __init__(self, completekey='tab', stdin=None, stdout=None):
56 bdb.Bdb.__init__(self)
57 cmd.Cmd.__init__(self, completekey, stdin, stdout)
60 self.prompt = '(Pdb) '
63 self._wait_for_mainpyfile = 0
64 # Try to load readline if it exists
70 # Read $HOME/.pdbrc and ./.pdbrc
72 if 'HOME' in os.environ:
73 envHome = os.environ['HOME']
75 rcFile = open(os.path.join(envHome, ".pdbrc"))
79 for line in rcFile.readlines():
80 self.rcLines.append(line)
83 rcFile = open(".pdbrc")
87 for line in rcFile.readlines():
88 self.rcLines.append(line)
91 self.commands = {} # associates a command list to breakpoint numbers
92 self.commands_doprompt = {} # for each bp num, tells if the prompt must be disp. after execing the cmd list
93 self.commands_silent = {} # for each bp num, tells if the stack trace must be disp. after execing the cmd list
94 self.commands_defining = False # True while in the process of defining a command list
95 self.commands_bnum = None # The breakpoint number for which we are defining a list
107 def setup(self, f, t):
109 self.stack, self.curindex = self.get_stack(f, t)
110 self.curframe = self.stack[self.curindex][0]
113 # Can be executed earlier than 'setup' if desired
114 def execRcLines(self):
116 # Make local copy because of recursion
117 rcLines = self.rcLines
122 if len(line) > 0 and line[0] != '#':
125 # Override Bdb methods
127 def user_call(self, frame, argument_list):
128 """This method is called when there is the remote possibility
129 that we ever need to stop in this function."""
130 if self._wait_for_mainpyfile:
132 if self.stop_here(frame):
133 print >>self.stdout, '--Call--'
134 self.interaction(frame, None)
136 def user_line(self, frame):
137 """This function is called when we stop or break at this line."""
138 if self._wait_for_mainpyfile:
139 if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
140 or frame.f_lineno<= 0):
142 self._wait_for_mainpyfile = 0
143 if self.bp_commands(frame):
144 self.interaction(frame, None)
146 def bp_commands(self,frame):
147 """ Call every command that was set for the current active breakpoint (if there is one)
148 Returns True if the normal interaction function must be called, False otherwise """
149 #self.currentbp is set in bdb.py in bdb.break_here if a breakpoint was hit
150 if getattr(self,"currentbp",False) and self.currentbp in self.commands:
151 currentbp = self.currentbp
153 lastcmd_back = self.lastcmd
154 self.setup(frame, None)
155 for line in self.commands[currentbp]:
157 self.lastcmd = lastcmd_back
158 if not self.commands_silent[currentbp]:
159 self.print_stack_entry(self.stack[self.curindex])
160 if self.commands_doprompt[currentbp]:
166 def user_return(self, frame, return_value):
167 """This function is called when a return trap is set here."""
168 frame.f_locals['__return__'] = return_value
169 print >>self.stdout, '--Return--'
170 self.interaction(frame, None)
172 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
173 """This function is called if an exception occurs,
174 but only if we are to stop at or just below this level."""
175 frame.f_locals['__exception__'] = exc_type, exc_value
176 if type(exc_type) == type(''):
177 exc_type_name = exc_type
178 else: exc_type_name = exc_type.__name__
179 print >>self.stdout, exc_type_name + ':', _saferepr(exc_value)
180 self.interaction(frame, exc_traceback)
182 # General interaction function
184 def interaction(self, frame, traceback):
185 self.setup(frame, traceback)
186 self.print_stack_entry(self.stack[self.curindex])
190 def default(self, line):
191 if line[:1] == '!': line = line[1:]
192 locals = self.curframe.f_locals
193 globals = self.curframe.f_globals
195 code = compile(line + '\n', '<stdin>', 'single')
196 exec code in globals, locals
198 t, v = sys.exc_info()[:2]
199 if type(t) == type(''):
201 else: exc_type_name = t.__name__
202 print >>self.stdout, '***', exc_type_name + ':', v
204 def precmd(self, line):
205 """Handle alias expansion and ';;' separator."""
209 while args[0] in self.aliases:
210 line = self.aliases[args[0]]
212 for tmpArg in args[1:]:
213 line = line.replace("%" + str(ii),
216 line = line.replace("%*", ' '.join(args[1:]))
218 # split into ';;' separated commands
219 # unless it's an alias command
220 if args[0] != 'alias':
221 marker = line.find(';;')
223 # queue up everything after marker
224 next = line[marker+2:].lstrip()
225 self.cmdqueue.append(next)
226 line = line[:marker].rstrip()
229 def onecmd(self, line):
230 """Interpret the argument as though it had been typed in response
233 Checks whether this line is typed at the normal prompt or in
234 a breakpoint command list definition.
236 if not self.commands_defining:
237 return cmd.Cmd.onecmd(self, line)
239 return self.handle_command_def(line)
241 def handle_command_def(self,line):
242 """ Handles one command line during command list definition. """
243 cmd, arg, line = self.parseline(line)
245 self.commands_silent[self.commands_bnum] = True
246 return # continue to handle other cmd def in the cmd list
249 return 1 # end of cmd list
250 cmdlist = self.commands[self.commands_bnum]
252 cmdlist.append(cmd+' '+arg)
255 # Determine if we must stop
257 func = getattr(self, 'do_' + cmd)
258 except AttributeError:
260 if func.func_name in self.commands_resuming : # one of the resuming commands.
261 self.commands_doprompt[self.commands_bnum] = False
266 # Command definitions, called by cmdloop()
267 # The argument is the remaining string on the command line
268 # Return true to exit from the command loop
270 do_h = cmd.Cmd.do_help
272 def do_commands(self, arg):
273 """Defines a list of commands associated to a breakpoint
274 Those commands will be executed whenever the breakpoint causes the program to stop execution."""
276 bnum = len(bdb.Breakpoint.bpbynumber)-1
281 print >>self.stdout, "Usage : commands [bnum]\n ...\n end"
283 self.commands_bnum = bnum
284 self.commands[bnum] = []
285 self.commands_doprompt[bnum] = True
286 self.commands_silent[bnum] = False
287 prompt_back = self.prompt
288 self.prompt = '(com) '
289 self.commands_defining = True
291 self.commands_defining = False
292 self.prompt = prompt_back
294 def do_break(self, arg, temporary = 0):
295 # break [ ([filename:]lineno | function) [, "condition"] ]
297 if self.breaks: # There's at least one
298 print >>self.stdout, "Num Type Disp Enb Where"
299 for bp in bdb.Breakpoint.bpbynumber:
301 bp.bpprint(self.stdout)
303 # parse arguments; comma has lowest precedence
304 # and cannot occur in filename
308 comma = arg.find(',')
310 # parse stuff after comma: "condition"
311 cond = arg[comma+1:].lstrip()
312 arg = arg[:comma].rstrip()
313 # parse stuff before comma: [filename:]lineno | function
314 colon = arg.rfind(':')
317 filename = arg[:colon].rstrip()
318 f = self.lookupmodule(filename)
320 print >>self.stdout, '*** ', repr(filename),
321 print >>self.stdout, 'not found from sys.path'
325 arg = arg[colon+1:].lstrip()
328 except ValueError, msg:
329 print >>self.stdout, '*** Bad lineno:', arg
332 # no colon; can be lineno or function
338 self.curframe.f_globals,
339 self.curframe.f_locals)
343 if hasattr(func, 'im_func'):
345 code = func.func_code
346 #use co_name to identify the bkpt (function names
347 #could be aliased, but co_name is invariant)
348 funcname = code.co_name
349 lineno = code.co_firstlineno
350 filename = code.co_filename
353 (ok, filename, ln) = self.lineinfo(arg)
355 print >>self.stdout, '*** The specified object',
356 print >>self.stdout, repr(arg),
357 print >>self.stdout, 'is not a function'
358 print >>self.stdout, 'or was not found along sys.path.'
360 funcname = ok # ok contains a function name
363 filename = self.defaultFile()
364 # Check for reasonable breakpoint
365 line = self.checkline(filename, lineno)
367 # now set the break point
368 err = self.set_break(filename, line, temporary, cond, funcname)
369 if err: print >>self.stdout, '***', err
371 bp = self.get_breaks(filename, line)[-1]
372 print >>self.stdout, "Breakpoint %d at %s:%d" % (bp.number,
376 # To be overridden in derived debuggers
377 def defaultFile(self):
378 """Produce a reasonable default."""
379 filename = self.curframe.f_code.co_filename
380 if filename == '<string>' and self.mainpyfile:
381 filename = self.mainpyfile
386 def do_tbreak(self, arg):
387 self.do_break(arg, 1)
389 def lineinfo(self, identifier):
390 failed = (None, None, None)
391 # Input is identifier, may be in single quotes
392 idstring = identifier.split("'")
393 if len(idstring) == 1:
394 # not in single quotes
395 id = idstring[0].strip()
396 elif len(idstring) == 3:
398 id = idstring[1].strip()
401 if id == '': return failed
402 parts = id.split('.')
403 # Protection for derived debuggers
404 if parts[0] == 'self':
408 # Best first guess at file to look at
409 fname = self.defaultFile()
413 # More than one part.
414 # First is module, second is method/class
415 f = self.lookupmodule(parts[0])
419 answer = find_function(item, fname)
420 return answer or failed
422 def checkline(self, filename, lineno):
423 """Check whether specified line seems to be executable.
425 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
426 line or EOF). Warning: testing is not comprehensive.
428 line = linecache.getline(filename, lineno)
430 print >>self.stdout, 'End of file'
433 # Don't allow setting breakpoint at a blank line
434 if (not line or (line[0] == '#') or
435 (line[:3] == '"""') or line[:3] == "'''"):
436 print >>self.stdout, '*** Blank or comment'
440 def do_enable(self, arg):
446 print >>self.stdout, 'Breakpoint index %r is not a number' % i
449 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
450 print >>self.stdout, 'No breakpoint numbered', i
453 bp = bdb.Breakpoint.bpbynumber[i]
457 def do_disable(self, arg):
463 print >>self.stdout, 'Breakpoint index %r is not a number' % i
466 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
467 print >>self.stdout, 'No breakpoint numbered', i
470 bp = bdb.Breakpoint.bpbynumber[i]
474 def do_condition(self, arg):
475 # arg is breakpoint number and condition
476 args = arg.split(' ', 1)
478 bpnum = int(args[0].strip())
480 # something went wrong
481 print >>self.stdout, \
482 'Breakpoint index %r is not a number' % args[0]
489 bp = bdb.Breakpoint.bpbynumber[bpnum]
491 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
496 print >>self.stdout, 'Breakpoint', bpnum,
497 print >>self.stdout, 'is now unconditional.'
499 def do_ignore(self,arg):
500 """arg is bp number followed by ignore count."""
503 bpnum = int(args[0].strip())
505 # something went wrong
506 print >>self.stdout, \
507 'Breakpoint index %r is not a number' % args[0]
510 count = int(args[1].strip())
514 bp = bdb.Breakpoint.bpbynumber[bpnum]
516 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
521 reply = 'Will ignore next '
523 reply = reply + '%d crossings' % count
525 reply = reply + '1 crossing'
526 print >>self.stdout, reply + ' of breakpoint %d.' % bpnum
528 print >>self.stdout, 'Will stop next time breakpoint',
529 print >>self.stdout, bpnum, 'is reached.'
531 def do_clear(self, arg):
532 """Three possibilities, tried in this order:
533 clear -> clear all breaks, ask for confirmation
534 clear file:lineno -> clear all breaks at file:lineno
535 clear bpno bpno ... -> clear breakpoints by number"""
538 reply = raw_input('Clear all breaks? ')
541 reply = reply.strip().lower()
542 if reply in ('y', 'yes'):
543 self.clear_all_breaks()
546 # Make sure it works for "clear C:\foo\bar.py:12"
553 err = "Invalid line number (%s)" % arg
555 err = self.clear_break(filename, lineno)
556 if err: print >>self.stdout, '***', err
558 numberlist = arg.split()
563 print >>self.stdout, 'Breakpoint index %r is not a number' % i
566 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
567 print >>self.stdout, 'No breakpoint numbered', i
569 err = self.clear_bpbynumber(i)
571 print >>self.stdout, '***', err
573 print >>self.stdout, 'Deleted breakpoint', i
574 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
576 def do_where(self, arg):
577 self.print_stack_trace()
581 def do_up(self, arg):
582 if self.curindex == 0:
583 print >>self.stdout, '*** Oldest frame'
585 self.curindex = self.curindex - 1
586 self.curframe = self.stack[self.curindex][0]
587 self.print_stack_entry(self.stack[self.curindex])
591 def do_down(self, arg):
592 if self.curindex + 1 == len(self.stack):
593 print >>self.stdout, '*** Newest frame'
595 self.curindex = self.curindex + 1
596 self.curframe = self.stack[self.curindex][0]
597 self.print_stack_entry(self.stack[self.curindex])
601 def do_step(self, arg):
606 def do_next(self, arg):
607 self.set_next(self.curframe)
611 def do_return(self, arg):
612 self.set_return(self.curframe)
616 def do_continue(self, arg):
619 do_c = do_cont = do_continue
621 def do_jump(self, arg):
622 if self.curindex + 1 != len(self.stack):
623 print >>self.stdout, "*** You can only jump within the bottom frame"
628 print >>self.stdout, "*** The 'jump' command requires a line number."
631 # Do the jump, fix up our copy of the stack, and display the
633 self.curframe.f_lineno = arg
634 self.stack[self.curindex] = self.stack[self.curindex][0], arg
635 self.print_stack_entry(self.stack[self.curindex])
636 except ValueError, e:
637 print >>self.stdout, '*** Jump failed:', e
640 def do_debug(self, arg):
642 globals = self.curframe.f_globals
643 locals = self.curframe.f_locals
645 p.prompt = "(%s) " % self.prompt.strip()
646 print >>self.stdout, "ENTERING RECURSIVE DEBUGGER"
647 sys.call_tracing(p.run, (arg, globals, locals))
648 print >>self.stdout, "LEAVING RECURSIVE DEBUGGER"
649 sys.settrace(self.trace_dispatch)
650 self.lastcmd = p.lastcmd
652 def do_quit(self, arg):
653 self._user_requested_quit = 1
660 def do_EOF(self, arg):
662 self._user_requested_quit = 1
666 def do_args(self, arg):
671 if co.co_flags & 4: n = n+1
672 if co.co_flags & 8: n = n+1
674 name = co.co_varnames[i]
675 print >>self.stdout, name, '=',
676 if name in dict: print >>self.stdout, dict[name]
677 else: print >>self.stdout, "*** undefined ***"
680 def do_retval(self, arg):
681 if '__return__' in self.curframe.f_locals:
682 print >>self.stdout, self.curframe.f_locals['__return__']
684 print >>self.stdout, '*** Not yet returned!'
687 def _getval(self, arg):
689 return eval(arg, self.curframe.f_globals,
690 self.curframe.f_locals)
692 t, v = sys.exc_info()[:2]
693 if isinstance(t, str):
695 else: exc_type_name = t.__name__
696 print >>self.stdout, '***', exc_type_name + ':', repr(v)
701 print >>self.stdout, repr(self._getval(arg))
705 def do_pp(self, arg):
707 pprint.pprint(self._getval(arg), self.stdout)
711 def do_list(self, arg):
712 self.lastcmd = 'list'
716 x = eval(arg, {}, {})
717 if type(x) == type(()):
722 # Assume it's a count
725 first = max(1, int(x) - 5)
727 print >>self.stdout, '*** Error in argument:', repr(arg)
729 elif self.lineno is None:
730 first = max(1, self.curframe.f_lineno - 5)
732 first = self.lineno + 1
735 filename = self.curframe.f_code.co_filename
736 breaklist = self.get_file_breaks(filename)
738 for lineno in range(first, last+1):
739 line = linecache.getline(filename, lineno)
741 print >>self.stdout, '[EOF]'
744 s = repr(lineno).rjust(3)
745 if len(s) < 4: s = s + ' '
746 if lineno in breaklist: s = s + 'B'
748 if lineno == self.curframe.f_lineno:
750 print >>self.stdout, s + '\t' + line,
752 except KeyboardInterrupt:
756 def do_whatis(self, arg):
758 value = eval(arg, self.curframe.f_globals,
759 self.curframe.f_locals)
761 t, v = sys.exc_info()[:2]
762 if type(t) == type(''):
764 else: exc_type_name = t.__name__
765 print >>self.stdout, '***', exc_type_name + ':', repr(v)
769 try: code = value.func_code
772 print >>self.stdout, 'Function', code.co_name
774 # Is it an instance method?
775 try: code = value.im_func.func_code
778 print >>self.stdout, 'Method', code.co_name
780 # None of the above...
781 print >>self.stdout, type(value)
783 def do_alias(self, arg):
786 keys = self.aliases.keys()
789 print >>self.stdout, "%s = %s" % (alias, self.aliases[alias])
791 if args[0] in self.aliases and len(args) == 1:
792 print >>self.stdout, "%s = %s" % (args[0], self.aliases[args[0]])
794 self.aliases[args[0]] = ' '.join(args[1:])
796 def do_unalias(self, arg):
798 if len(args) == 0: return
799 if args[0] in self.aliases:
800 del self.aliases[args[0]]
802 #list of all the commands making the program resume execution.
803 commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return',
804 'do_quit', 'do_jump']
806 # Print a traceback starting at the top stack frame.
807 # The most recently entered frame is printed last;
808 # this is different from dbx and gdb, but consistent with
809 # the Python interpreter's stack trace.
810 # It is also consistent with the up/down commands (which are
811 # compatible with dbx and gdb: up moves towards 'main()'
812 # and down moves towards the most recent stack frame).
814 def print_stack_trace(self):
816 for frame_lineno in self.stack:
817 self.print_stack_entry(frame_lineno)
818 except KeyboardInterrupt:
821 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
822 frame, lineno = frame_lineno
823 if frame is self.curframe:
824 print >>self.stdout, '>',
826 print >>self.stdout, ' ',
827 print >>self.stdout, self.format_stack_entry(frame_lineno,
831 # Help methods (derived from pdb.doc)
837 print >>self.stdout, """h(elp)
838 Without argument, print the list of available commands.
839 With a command name as argument, print help about that command
840 "help pdb" pipes the full documentation file to the $PAGER
841 "help exec" gives help on the ! command"""
843 def help_where(self):
847 print >>self.stdout, """w(here)
848 Print a stack trace, with the most recent frame at the bottom.
849 An arrow indicates the "current frame", which determines the
850 context of most commands. 'bt' is an alias for this command."""
858 print >>self.stdout, """d(own)
859 Move the current frame one level down in the stack trace
860 (to a newer frame)."""
866 print >>self.stdout, """u(p)
867 Move the current frame one level up in the stack trace
868 (to an older frame)."""
870 def help_break(self):
874 print >>self.stdout, """b(reak) ([file:]lineno | function) [, condition]
875 With a line number argument, set a break there in the current
876 file. With a function name, set a break at first executable line
877 of that function. Without argument, list all breaks. If a second
878 argument is present, it is a string specifying an expression
879 which must evaluate to true before the breakpoint is honored.
881 The line number may be prefixed with a filename and a colon,
882 to specify a breakpoint in another file (probably one that
883 hasn't been loaded yet). The file is searched for on sys.path;
884 the .py suffix may be omitted."""
886 def help_clear(self):
890 print >>self.stdout, "cl(ear) filename:lineno"
891 print >>self.stdout, """cl(ear) [bpnumber [bpnumber...]]
892 With a space separated list of breakpoint numbers, clear
893 those breakpoints. Without argument, clear all breaks (but
894 first ask confirmation). With a filename:lineno argument,
895 clear all breaks at that line in that file.
897 Note that the argument is different from previous versions of
898 the debugger (in python distributions 1.5.1 and before) where
899 a linenumber was used instead of either filename:lineno or
900 breakpoint numbers."""
902 def help_tbreak(self):
903 print >>self.stdout, """tbreak same arguments as break, but breakpoint is
904 removed when first hit."""
906 def help_enable(self):
907 print >>self.stdout, """enable bpnumber [bpnumber ...]
908 Enables the breakpoints given as a space separated list of
911 def help_disable(self):
912 print >>self.stdout, """disable bpnumber [bpnumber ...]
913 Disables the breakpoints given as a space separated list of
916 def help_ignore(self):
917 print >>self.stdout, """ignore bpnumber count
918 Sets the ignore count for the given breakpoint number. A breakpoint
919 becomes active when the ignore count is zero. When non-zero, the
920 count is decremented each time the breakpoint is reached and the
921 breakpoint is not disabled and any associated condition evaluates
924 def help_condition(self):
925 print >>self.stdout, """condition bpnumber str_condition
926 str_condition is a string specifying an expression which
927 must evaluate to true before the breakpoint is honored.
928 If str_condition is absent, any existing condition is removed;
929 i.e., the breakpoint is made unconditional."""
935 print >>self.stdout, """s(tep)
936 Execute the current line, stop at the first possible occasion
937 (either in a function that is called or in the current function)."""
943 print >>self.stdout, """n(ext)
944 Continue execution until the next line in the current function
945 is reached or it returns."""
947 def help_return(self):
951 print >>self.stdout, """r(eturn)
952 Continue execution until the current function returns."""
954 def help_continue(self):
961 print >>self.stdout, """c(ont(inue))
962 Continue execution, only stop when a breakpoint is encountered."""
968 print >>self.stdout, """j(ump) lineno
969 Set the next line that will be executed."""
971 def help_debug(self):
972 print >>self.stdout, """debug code
973 Enter a recursive debugger that steps through the code argument
974 (which is an arbitrary expression or statement to be executed
975 in the current environment)."""
981 print >>self.stdout, """l(ist) [first [,last]]
982 List source code for the current file.
983 Without arguments, list 11 lines around the current line
984 or continue the previous listing.
985 With one argument, list 11 lines starting at that line.
986 With two arguments, list the given range;
987 if the second argument is less than the first, it is a count."""
993 print >>self.stdout, """a(rgs)
994 Print the arguments of the current function."""
997 print >>self.stdout, """p expression
998 Print the value of the expression."""
1001 print >>self.stdout, """pp expression
1002 Pretty-print the value of the expression."""
1004 def help_exec(self):
1005 print >>self.stdout, """(!) statement
1006 Execute the (one-line) statement in the context of
1007 the current stack frame.
1008 The exclamation point can be omitted unless the first word
1009 of the statement resembles a debugger command.
1010 To assign to a global variable you must always prefix the
1011 command with a 'global' command, e.g.:
1012 (Pdb) global list_options; list_options = ['-l']
1015 def help_quit(self):
1019 print >>self.stdout, """q(uit) or exit - Quit from the debugger.
1020 The program being executed is aborted."""
1024 def help_whatis(self):
1025 print >>self.stdout, """whatis arg
1026 Prints the type of the argument."""
1029 print >>self.stdout, """EOF
1030 Handles the receipt of EOF as a command."""
1032 def help_alias(self):
1033 print >>self.stdout, """alias [name [command [parameter parameter ...] ]]
1034 Creates an alias called 'name' the executes 'command'. The command
1035 must *not* be enclosed in quotes. Replaceable parameters are
1036 indicated by %1, %2, and so on, while %* is replaced by all the
1037 parameters. If no command is given, the current alias for name
1038 is shown. If no name is given, all aliases are listed.
1040 Aliases may be nested and can contain anything that can be
1041 legally typed at the pdb prompt. Note! You *can* override
1042 internal pdb commands with aliases! Those internal commands
1043 are then hidden until the alias is removed. Aliasing is recursively
1044 applied to the first word of the command line; all other words
1045 in the line are left alone.
1047 Some useful aliases (especially when placed in the .pdbrc file) are:
1049 #Print instance variables (usage "pi classInst")
1050 alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
1052 #Print instance variables in self
1056 def help_unalias(self):
1057 print >>self.stdout, """unalias name
1058 Deletes the specified alias."""
1060 def help_commands(self):
1061 print >>self.stdout, """commands [bpnumber]
1066 Specify a list of commands for breakpoint number bpnumber. The
1067 commands themselves appear on the following lines. Type a line
1068 containing just 'end' to terminate the commands.
1070 To remove all commands from a breakpoint, type commands and
1071 follow it immediately with end; that is, give no commands.
1073 With no bpnumber argument, commands refers to the last
1076 You can use breakpoint commands to start your program up again.
1077 Simply use the continue command, or step, or any other
1078 command that resumes execution.
1080 Specifying any command resuming execution (currently continue,
1081 step, next, return, jump, quit and their abbreviations) terminates
1082 the command list (as if that command was immediately followed by end).
1083 This is because any time you resume execution
1084 (even with a simple next or step), you may encounter
1085 another breakpoint--which could have its own command list, leading to
1086 ambiguities about which list to execute.
1088 If you use the 'silent' command in the command list, the
1089 usual message about stopping at a breakpoint is not printed. This may
1090 be desirable for breakpoints that are to print a specific message and
1091 then continue. If none of the other commands print anything, you
1092 see no sign that the breakpoint was reached.
1098 def lookupmodule(self, filename):
1099 """Helper function for break/clear parsing -- may be overridden.
1101 lookupmodule() translates (possibly incomplete) file or module name
1102 into an absolute file name.
1104 if os.path.isabs(filename) and os.path.exists(filename):
1106 f = os.path.join(sys.path[0], filename)
1107 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1109 root, ext = os.path.splitext(filename)
1111 filename = filename + '.py'
1112 if os.path.isabs(filename):
1114 for dirname in sys.path:
1115 while os.path.islink(dirname):
1116 dirname = os.readlink(dirname)
1117 fullname = os.path.join(dirname, filename)
1118 if os.path.exists(fullname):
1122 def _runscript(self, filename):
1123 # Start with fresh empty copy of globals and locals and tell the script
1124 # that it's being run as __main__ to avoid scripts being able to access
1125 # the pdb.py namespace.
1126 globals_ = {"__name__" : "__main__"}
1129 # When bdb sets tracing, a number of call and line events happens
1130 # BEFORE debugger even reaches user's code (and the exact sequence of
1131 # events depends on python version). So we take special measures to
1132 # avoid stopping before we reach the main script (see user_line and
1133 # user_call for details).
1134 self._wait_for_mainpyfile = 1
1135 self.mainpyfile = self.canonic(filename)
1136 self._user_requested_quit = 0
1137 statement = 'execfile( "%s")' % filename
1138 self.run(statement, globals=globals_, locals=locals_)
1140 # Simplified interface
1142 def run(statement, globals=None, locals=None):
1143 Pdb().run(statement, globals, locals)
1145 def runeval(expression, globals=None, locals=None):
1146 return Pdb().runeval(expression, globals, locals)
1148 def runctx(statement, globals, locals):
1150 run(statement, globals, locals)
1152 def runcall(*args, **kwds):
1153 return Pdb().runcall(*args, **kwds)
1156 Pdb().set_trace(sys._getframe().f_back)
1158 # Post-Mortem interface
1163 while t.tb_next is not None:
1165 p.interaction(t.tb_frame, t)
1168 post_mortem(sys.last_traceback)
1171 # Main program for testing
1173 TESTCMD = 'import x; x.main()'
1180 for dirname in sys.path:
1181 fullname = os.path.join(dirname, 'pdb.doc')
1182 if os.path.exists(fullname):
1183 sts = os.system('${PAGER-more} '+fullname)
1184 if sts: print '*** Pager exit status:', sts
1187 print 'Sorry, can\'t find the help file "pdb.doc"',
1188 print 'along the Python search path'
1191 if not sys.argv[1:]:
1192 print "usage: pdb.py scriptfile [arg] ..."
1195 mainpyfile = sys.argv[1] # Get script filename
1196 if not os.path.exists(mainpyfile):
1197 print 'Error:', mainpyfile, 'does not exist'
1200 del sys.argv[0] # Hide "pdb.py" from argument list
1202 # Replace pdb's dir with script's dir in front of module search path.
1203 sys.path[0] = os.path.dirname(mainpyfile)
1205 # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1206 # modified by the script being debugged. It's a bad idea when it was
1207 # changed by the user from the command line. The best approach would be to
1208 # have a "restart" command which would allow explicit specification of
1209 # command line arguments.
1213 pdb._runscript(mainpyfile)
1214 if pdb._user_requested_quit:
1216 print "The program finished and will be restarted"
1218 # In most cases SystemExit does not warrant a post-mortem session.
1219 print "The program exited via sys.exit(). Exit status: ",
1220 print sys.exc_info()[1]
1222 traceback.print_exc()
1223 print "Uncaught exception. Entering post mortem debugging"
1224 print "Running 'cont' or 'step' will restart the program"
1225 t = sys.exc_info()[2]
1226 while t.tb_next is not None:
1228 pdb.interaction(t.tb_frame,t)
1229 print "Post mortem debugger finished. The "+mainpyfile+" will be restarted"
1232 # When invoked as main program, invoke the debugger on a script
1233 if __name__=='__main__':