Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 3 | """A Python debugger.""" |
Guido van Rossum | 92df0c6 | 1992-01-14 18:30:15 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 5 | # (See pdb.doc for documentation.) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 7 | import sys |
| 8 | import linecache |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 9 | import cmd |
| 10 | import bdb |
Brett Cannon | 2ee0e8e | 2008-05-23 05:03:59 +0000 | [diff] [blame] | 11 | from repr import Repr |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 12 | import os |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 13 | import re |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 14 | import pprint |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 15 | import traceback |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 16 | import signal |
Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class Restart(Exception): |
| 20 | """Causes a debugger to be restarted for the debugged python program.""" |
| 21 | pass |
| 22 | |
Guido van Rossum | ef1b41b | 2002-09-10 21:57:14 +0000 | [diff] [blame] | 23 | # Create a custom safe Repr instance and increase its maxstring. |
| 24 | # The default of 30 truncates error messages too easily. |
| 25 | _repr = Repr() |
| 26 | _repr.maxstring = 200 |
| 27 | _saferepr = _repr.repr |
| 28 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 29 | __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace", |
| 30 | "post_mortem", "help"] |
| 31 | |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 32 | def find_function(funcname, filename): |
Andrew M. Kuchling | e672825 | 2006-09-05 13:19:18 +0000 | [diff] [blame] | 33 | cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 34 | try: |
| 35 | fp = open(filename) |
| 36 | except IOError: |
| 37 | return None |
| 38 | # consumer of this info expects the first line to be 1 |
| 39 | lineno = 1 |
| 40 | answer = None |
| 41 | while 1: |
| 42 | line = fp.readline() |
| 43 | if line == '': |
| 44 | break |
| 45 | if cre.match(line): |
| 46 | answer = funcname, filename, lineno |
| 47 | break |
| 48 | lineno = lineno + 1 |
| 49 | fp.close() |
| 50 | return answer |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 51 | |
| 52 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 53 | # Interaction prompt line will separate file and call info from code |
| 54 | # text using value of line_prefix string. A newline and arrow may |
| 55 | # be to your liking. You can set it once pdb is imported using the |
| 56 | # command "pdb.line_prefix = '\n% '". |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 57 | # line_prefix = ': ' # Use this to get the old situation back |
| 58 | line_prefix = '\n-> ' # Probably a better default |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 60 | class Pdb(bdb.Bdb, cmd.Cmd): |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 62 | def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None): |
| 63 | bdb.Bdb.__init__(self, skip=skip) |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 64 | cmd.Cmd.__init__(self, completekey, stdin, stdout) |
| 65 | if stdout: |
| 66 | self.use_rawinput = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 67 | self.prompt = '(Pdb) ' |
| 68 | self.aliases = {} |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 69 | self.mainpyfile = '' |
| 70 | self._wait_for_mainpyfile = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 71 | # Try to load readline if it exists |
| 72 | try: |
| 73 | import readline |
| 74 | except ImportError: |
| 75 | pass |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 76 | self.allow_kbdint = False |
| 77 | signal.signal(signal.SIGINT, self.sigint_handler) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 78 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 79 | # Read $HOME/.pdbrc and ./.pdbrc |
| 80 | self.rcLines = [] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 81 | if 'HOME' in os.environ: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 82 | envHome = os.environ['HOME'] |
| 83 | try: |
| 84 | rcFile = open(os.path.join(envHome, ".pdbrc")) |
| 85 | except IOError: |
| 86 | pass |
| 87 | else: |
| 88 | for line in rcFile.readlines(): |
| 89 | self.rcLines.append(line) |
| 90 | rcFile.close() |
| 91 | try: |
| 92 | rcFile = open(".pdbrc") |
| 93 | except IOError: |
| 94 | pass |
| 95 | else: |
| 96 | for line in rcFile.readlines(): |
| 97 | self.rcLines.append(line) |
| 98 | rcFile.close() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 99 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 100 | self.commands = {} # associates a command list to breakpoint numbers |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 101 | self.commands_doprompt = {} # for each bp num, tells if the prompt |
| 102 | # must be disp. after execing the cmd list |
| 103 | self.commands_silent = {} # for each bp num, tells if the stack trace |
| 104 | # must be disp. after execing the cmd list |
| 105 | self.commands_defining = False # True while in the process of defining |
| 106 | # a command list |
| 107 | self.commands_bnum = None # The breakpoint number for which we are |
| 108 | # defining a list |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 109 | |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 110 | def sigint_handler(self, signum, frame): |
| 111 | if self.allow_kbdint: |
| 112 | raise KeyboardInterrupt() |
| 113 | print >>self.stdout, "\nProgram interrupted. (Use 'cont' to resume)." |
| 114 | self.set_step() |
| 115 | self.set_trace(frame) |
| 116 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 117 | def reset(self): |
| 118 | bdb.Bdb.reset(self) |
| 119 | self.forget() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 120 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 121 | def forget(self): |
| 122 | self.lineno = None |
| 123 | self.stack = [] |
| 124 | self.curindex = 0 |
| 125 | self.curframe = None |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 126 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 127 | def setup(self, f, t): |
| 128 | self.forget() |
| 129 | self.stack, self.curindex = self.get_stack(f, t) |
| 130 | self.curframe = self.stack[self.curindex][0] |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 131 | # The f_locals dictionary is updated from the actual frame |
| 132 | # locals whenever the .f_locals accessor is called, so we |
| 133 | # cache it here to ensure that modifications are not overwritten. |
| 134 | self.curframe_locals = self.curframe.f_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 135 | self.execRcLines() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 136 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 137 | # Can be executed earlier than 'setup' if desired |
| 138 | def execRcLines(self): |
| 139 | if self.rcLines: |
| 140 | # Make local copy because of recursion |
| 141 | rcLines = self.rcLines |
| 142 | # executed only once |
| 143 | self.rcLines = [] |
| 144 | for line in rcLines: |
| 145 | line = line[:-1] |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 146 | if len(line) > 0 and line[0] != '#': |
| 147 | self.onecmd(line) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 148 | |
Tim Peters | 280488b | 2002-08-23 18:19:30 +0000 | [diff] [blame] | 149 | # Override Bdb methods |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 150 | |
| 151 | def user_call(self, frame, argument_list): |
| 152 | """This method is called when there is the remote possibility |
| 153 | that we ever need to stop in this function.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 154 | if self._wait_for_mainpyfile: |
| 155 | return |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 156 | if self.stop_here(frame): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 157 | print >>self.stdout, '--Call--' |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 158 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 159 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 160 | def user_line(self, frame): |
| 161 | """This function is called when we stop or break at this line.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 162 | if self._wait_for_mainpyfile: |
| 163 | if (self.mainpyfile != self.canonic(frame.f_code.co_filename) |
| 164 | or frame.f_lineno<= 0): |
| 165 | return |
| 166 | self._wait_for_mainpyfile = 0 |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 167 | if self.bp_commands(frame): |
| 168 | self.interaction(frame, None) |
Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 169 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 170 | def bp_commands(self,frame): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 171 | """Call every command that was set for the current active breakpoint |
| 172 | (if there is one). |
| 173 | |
| 174 | Returns True if the normal interaction function must be called, |
| 175 | False otherwise.""" |
| 176 | # self.currentbp is set in bdb in Bdb.break_here if a breakpoint was hit |
| 177 | if getattr(self, "currentbp", False) and \ |
| 178 | self.currentbp in self.commands: |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 179 | currentbp = self.currentbp |
| 180 | self.currentbp = 0 |
| 181 | lastcmd_back = self.lastcmd |
| 182 | self.setup(frame, None) |
| 183 | for line in self.commands[currentbp]: |
| 184 | self.onecmd(line) |
| 185 | self.lastcmd = lastcmd_back |
| 186 | if not self.commands_silent[currentbp]: |
| 187 | self.print_stack_entry(self.stack[self.curindex]) |
| 188 | if self.commands_doprompt[currentbp]: |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 189 | self._cmdloop() |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 190 | self.forget() |
Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 191 | return |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 192 | return 1 |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 193 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 194 | def user_return(self, frame, return_value): |
| 195 | """This function is called when a return trap is set here.""" |
| 196 | frame.f_locals['__return__'] = return_value |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 197 | print >>self.stdout, '--Return--' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 198 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 199 | |
Brett Cannon | c6a30ec | 2008-08-01 01:36:47 +0000 | [diff] [blame] | 200 | def user_exception(self, frame, exc_info): |
| 201 | exc_type, exc_value, exc_traceback = exc_info |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 202 | """This function is called if an exception occurs, |
| 203 | but only if we are to stop at or just below this level.""" |
| 204 | frame.f_locals['__exception__'] = exc_type, exc_value |
| 205 | if type(exc_type) == type(''): |
| 206 | exc_type_name = exc_type |
| 207 | else: exc_type_name = exc_type.__name__ |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 208 | print >>self.stdout, exc_type_name + ':', _saferepr(exc_value) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 209 | self.interaction(frame, exc_traceback) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 210 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | # General interaction function |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 212 | def _cmdloop(self): |
| 213 | while 1: |
| 214 | try: |
| 215 | # keyboard interrupts allow for an easy way to interrupt |
| 216 | # the current command |
| 217 | self.allow_kbdint = True |
| 218 | self.cmdloop() |
| 219 | self.allow_kbdint = False |
| 220 | break |
| 221 | except KeyboardInterrupt: |
| 222 | print >>self.stdout, '--KeyboardInterrupt--' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 223 | |
| 224 | def interaction(self, frame, traceback): |
| 225 | self.setup(frame, traceback) |
| 226 | self.print_stack_entry(self.stack[self.curindex]) |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 227 | self._cmdloop() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 228 | self.forget() |
| 229 | |
Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 230 | def displayhook(self, obj): |
| 231 | """Custom displayhook for the exec in default(), which prevents |
| 232 | assignment of the _ variable in the builtins. |
| 233 | """ |
Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 234 | # reproduce the behavior of the standard displayhook, not printing None |
| 235 | if obj is not None: |
| 236 | print repr(obj) |
Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 237 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 238 | def default(self, line): |
| 239 | if line[:1] == '!': line = line[1:] |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 240 | locals = self.curframe_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 241 | globals = self.curframe.f_globals |
| 242 | try: |
| 243 | code = compile(line + '\n', '<stdin>', 'single') |
Amaury Forgeot d'Arc | ff0f267 | 2008-01-15 21:25:11 +0000 | [diff] [blame] | 244 | save_stdout = sys.stdout |
| 245 | save_stdin = sys.stdin |
Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 246 | save_displayhook = sys.displayhook |
Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 247 | try: |
| 248 | sys.stdin = self.stdin |
| 249 | sys.stdout = self.stdout |
Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 250 | sys.displayhook = self.displayhook |
Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 251 | exec code in globals, locals |
| 252 | finally: |
| 253 | sys.stdout = save_stdout |
| 254 | sys.stdin = save_stdin |
Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 255 | sys.displayhook = save_displayhook |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 256 | except: |
| 257 | t, v = sys.exc_info()[:2] |
| 258 | if type(t) == type(''): |
| 259 | exc_type_name = t |
| 260 | else: exc_type_name = t.__name__ |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 261 | print >>self.stdout, '***', exc_type_name + ':', v |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 262 | |
| 263 | def precmd(self, line): |
| 264 | """Handle alias expansion and ';;' separator.""" |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 265 | if not line.strip(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 266 | return line |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 267 | args = line.split() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 268 | while args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 269 | line = self.aliases[args[0]] |
| 270 | ii = 1 |
| 271 | for tmpArg in args[1:]: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 272 | line = line.replace("%" + str(ii), |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 273 | tmpArg) |
| 274 | ii = ii + 1 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 275 | line = line.replace("%*", ' '.join(args[1:])) |
| 276 | args = line.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 277 | # split into ';;' separated commands |
| 278 | # unless it's an alias command |
| 279 | if args[0] != 'alias': |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 280 | marker = line.find(';;') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 281 | if marker >= 0: |
| 282 | # queue up everything after marker |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 283 | next = line[marker+2:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 284 | self.cmdqueue.append(next) |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 285 | line = line[:marker].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 286 | return line |
| 287 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 288 | def onecmd(self, line): |
| 289 | """Interpret the argument as though it had been typed in response |
Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 290 | to the prompt. |
| 291 | |
Andrew M. Kuchling | 9aed98f | 2006-07-27 12:18:20 +0000 | [diff] [blame] | 292 | Checks whether this line is typed at the normal prompt or in |
Tim Peters | daea035 | 2006-07-27 15:11:00 +0000 | [diff] [blame] | 293 | a breakpoint command list definition. |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 294 | """ |
| 295 | if not self.commands_defining: |
| 296 | return cmd.Cmd.onecmd(self, line) |
| 297 | else: |
| 298 | return self.handle_command_def(line) |
| 299 | |
Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 300 | def handle_command_def(self,line): |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 301 | """ Handles one command line during command list definition. """ |
| 302 | cmd, arg, line = self.parseline(line) |
| 303 | if cmd == 'silent': |
| 304 | self.commands_silent[self.commands_bnum] = True |
| 305 | return # continue to handle other cmd def in the cmd list |
| 306 | elif cmd == 'end': |
| 307 | self.cmdqueue = [] |
| 308 | return 1 # end of cmd list |
| 309 | cmdlist = self.commands[self.commands_bnum] |
| 310 | if (arg): |
| 311 | cmdlist.append(cmd+' '+arg) |
| 312 | else: |
| 313 | cmdlist.append(cmd) |
| 314 | # Determine if we must stop |
| 315 | try: |
| 316 | func = getattr(self, 'do_' + cmd) |
| 317 | except AttributeError: |
| 318 | func = self.default |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 319 | # one of the resuming commands |
| 320 | if func.func_name in self.commands_resuming: |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 321 | self.commands_doprompt[self.commands_bnum] = False |
| 322 | self.cmdqueue = [] |
| 323 | return 1 |
Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 324 | return |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 325 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 326 | # Command definitions, called by cmdloop() |
| 327 | # The argument is the remaining string on the command line |
| 328 | # Return true to exit from the command loop |
| 329 | |
| 330 | do_h = cmd.Cmd.do_help |
| 331 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 332 | def do_commands(self, arg): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 333 | """Defines a list of commands associated to a breakpoint. |
| 334 | |
| 335 | Those commands will be executed whenever the breakpoint causes |
| 336 | the program to stop execution.""" |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 337 | if not arg: |
| 338 | bnum = len(bdb.Breakpoint.bpbynumber)-1 |
| 339 | else: |
| 340 | try: |
| 341 | bnum = int(arg) |
| 342 | except: |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 343 | print >>self.stdout, "Usage : commands [bnum]\n ..." \ |
| 344 | "\n end" |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 345 | return |
| 346 | self.commands_bnum = bnum |
| 347 | self.commands[bnum] = [] |
| 348 | self.commands_doprompt[bnum] = True |
| 349 | self.commands_silent[bnum] = False |
| 350 | prompt_back = self.prompt |
| 351 | self.prompt = '(com) ' |
| 352 | self.commands_defining = True |
Gregory P. Smith | 56fe656 | 2010-05-08 23:38:49 +0000 | [diff] [blame^] | 353 | try: |
| 354 | self.cmdloop() |
| 355 | except (KeyboardInterrupt, IOError): |
| 356 | # It appears that that when pdb is reading input from a pipe |
| 357 | # we may get IOErrors, rather than KeyboardInterrupt. |
| 358 | # Now discard all the commands entered so far (essentially undo |
| 359 | # any effect of this "commands" cmd) |
| 360 | self.commands.pop(bnum) |
| 361 | self.commands_doprompt.pop(bnum) |
| 362 | self.commands_silent.pop(bnum) |
| 363 | # this will get caught by the _cmdloop and pdb will reenter |
| 364 | # the main command loop |
| 365 | raise KeyboardInterrupt() |
| 366 | finally: |
| 367 | self.commands_defining = False |
| 368 | self.prompt = prompt_back |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 369 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 370 | def do_break(self, arg, temporary = 0): |
| 371 | # break [ ([filename:]lineno | function) [, "condition"] ] |
| 372 | if not arg: |
| 373 | if self.breaks: # There's at least one |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 374 | print >>self.stdout, "Num Type Disp Enb Where" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 375 | for bp in bdb.Breakpoint.bpbynumber: |
| 376 | if bp: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 377 | bp.bpprint(self.stdout) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 378 | return |
| 379 | # parse arguments; comma has lowest precedence |
| 380 | # and cannot occur in filename |
| 381 | filename = None |
| 382 | lineno = None |
| 383 | cond = None |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 384 | comma = arg.find(',') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 385 | if comma > 0: |
| 386 | # parse stuff after comma: "condition" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 387 | cond = arg[comma+1:].lstrip() |
| 388 | arg = arg[:comma].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 389 | # parse stuff before comma: [filename:]lineno | function |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 390 | colon = arg.rfind(':') |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 391 | funcname = None |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 392 | if colon >= 0: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 393 | filename = arg[:colon].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 394 | f = self.lookupmodule(filename) |
| 395 | if not f: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 396 | print >>self.stdout, '*** ', repr(filename), |
| 397 | print >>self.stdout, 'not found from sys.path' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 398 | return |
| 399 | else: |
| 400 | filename = f |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 401 | arg = arg[colon+1:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 402 | try: |
| 403 | lineno = int(arg) |
| 404 | except ValueError, msg: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 405 | print >>self.stdout, '*** Bad lineno:', arg |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 406 | return |
| 407 | else: |
| 408 | # no colon; can be lineno or function |
| 409 | try: |
| 410 | lineno = int(arg) |
| 411 | except ValueError: |
| 412 | try: |
| 413 | func = eval(arg, |
| 414 | self.curframe.f_globals, |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 415 | self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 416 | except: |
| 417 | func = arg |
| 418 | try: |
| 419 | if hasattr(func, 'im_func'): |
| 420 | func = func.im_func |
| 421 | code = func.func_code |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 422 | #use co_name to identify the bkpt (function names |
| 423 | #could be aliased, but co_name is invariant) |
| 424 | funcname = code.co_name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 425 | lineno = code.co_firstlineno |
| 426 | filename = code.co_filename |
| 427 | except: |
| 428 | # last thing to try |
| 429 | (ok, filename, ln) = self.lineinfo(arg) |
| 430 | if not ok: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 431 | print >>self.stdout, '*** The specified object', |
| 432 | print >>self.stdout, repr(arg), |
| 433 | print >>self.stdout, 'is not a function' |
| 434 | print >>self.stdout, 'or was not found along sys.path.' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 435 | return |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 436 | funcname = ok # ok contains a function name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 437 | lineno = int(ln) |
| 438 | if not filename: |
| 439 | filename = self.defaultFile() |
| 440 | # Check for reasonable breakpoint |
| 441 | line = self.checkline(filename, lineno) |
| 442 | if line: |
| 443 | # now set the break point |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 444 | err = self.set_break(filename, line, temporary, cond, funcname) |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 445 | if err: print >>self.stdout, '***', err |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 446 | else: |
| 447 | bp = self.get_breaks(filename, line)[-1] |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 448 | print >>self.stdout, "Breakpoint %d at %s:%d" % (bp.number, |
| 449 | bp.file, |
| 450 | bp.line) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 451 | |
| 452 | # To be overridden in derived debuggers |
| 453 | def defaultFile(self): |
| 454 | """Produce a reasonable default.""" |
| 455 | filename = self.curframe.f_code.co_filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 456 | if filename == '<string>' and self.mainpyfile: |
| 457 | filename = self.mainpyfile |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 458 | return filename |
| 459 | |
| 460 | do_b = do_break |
| 461 | |
| 462 | def do_tbreak(self, arg): |
| 463 | self.do_break(arg, 1) |
| 464 | |
| 465 | def lineinfo(self, identifier): |
| 466 | failed = (None, None, None) |
| 467 | # Input is identifier, may be in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 468 | idstring = identifier.split("'") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 469 | if len(idstring) == 1: |
| 470 | # not in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 471 | id = idstring[0].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 472 | elif len(idstring) == 3: |
| 473 | # quoted |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 474 | id = idstring[1].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 475 | else: |
| 476 | return failed |
| 477 | if id == '': return failed |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 478 | parts = id.split('.') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 479 | # Protection for derived debuggers |
| 480 | if parts[0] == 'self': |
| 481 | del parts[0] |
| 482 | if len(parts) == 0: |
| 483 | return failed |
| 484 | # Best first guess at file to look at |
| 485 | fname = self.defaultFile() |
| 486 | if len(parts) == 1: |
| 487 | item = parts[0] |
| 488 | else: |
| 489 | # More than one part. |
| 490 | # First is module, second is method/class |
| 491 | f = self.lookupmodule(parts[0]) |
| 492 | if f: |
| 493 | fname = f |
| 494 | item = parts[1] |
| 495 | answer = find_function(item, fname) |
| 496 | return answer or failed |
| 497 | |
| 498 | def checkline(self, filename, lineno): |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 499 | """Check whether specified line seems to be executable. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 500 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 501 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank |
| 502 | line or EOF). Warning: testing is not comprehensive. |
| 503 | """ |
Nick Coghlan | a205347 | 2008-12-14 10:54:50 +0000 | [diff] [blame] | 504 | line = linecache.getline(filename, lineno, self.curframe.f_globals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 505 | if not line: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 506 | print >>self.stdout, 'End of file' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 507 | return 0 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 508 | line = line.strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 509 | # Don't allow setting breakpoint at a blank line |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 510 | if (not line or (line[0] == '#') or |
| 511 | (line[:3] == '"""') or line[:3] == "'''"): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 512 | print >>self.stdout, '*** Blank or comment' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 513 | return 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 514 | return lineno |
| 515 | |
| 516 | def do_enable(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 517 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 518 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 519 | try: |
| 520 | i = int(i) |
| 521 | except ValueError: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 522 | print >>self.stdout, 'Breakpoint index %r is not a number' % i |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 523 | continue |
| 524 | |
| 525 | if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 526 | print >>self.stdout, 'No breakpoint numbered', i |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 527 | continue |
| 528 | |
| 529 | bp = bdb.Breakpoint.bpbynumber[i] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 530 | if bp: |
| 531 | bp.enable() |
| 532 | |
| 533 | def do_disable(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 534 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 535 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 536 | try: |
| 537 | i = int(i) |
| 538 | except ValueError: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 539 | print >>self.stdout, 'Breakpoint index %r is not a number' % i |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 540 | continue |
Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 541 | |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 542 | if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 543 | print >>self.stdout, 'No breakpoint numbered', i |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 544 | continue |
| 545 | |
| 546 | bp = bdb.Breakpoint.bpbynumber[i] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 547 | if bp: |
| 548 | bp.disable() |
| 549 | |
| 550 | def do_condition(self, arg): |
| 551 | # arg is breakpoint number and condition |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 552 | args = arg.split(' ', 1) |
Georg Brandl | e498083 | 2007-01-22 21:23:41 +0000 | [diff] [blame] | 553 | try: |
| 554 | bpnum = int(args[0].strip()) |
| 555 | except ValueError: |
| 556 | # something went wrong |
| 557 | print >>self.stdout, \ |
| 558 | 'Breakpoint index %r is not a number' % args[0] |
Georg Brandl | b278318 | 2007-03-11 08:28:46 +0000 | [diff] [blame] | 559 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 560 | try: |
| 561 | cond = args[1] |
| 562 | except: |
| 563 | cond = None |
Collin Winter | 2faa9e1 | 2007-03-11 16:00:20 +0000 | [diff] [blame] | 564 | try: |
| 565 | bp = bdb.Breakpoint.bpbynumber[bpnum] |
| 566 | except IndexError: |
| 567 | print >>self.stdout, 'Breakpoint index %r is not valid' % args[0] |
| 568 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 569 | if bp: |
| 570 | bp.cond = cond |
| 571 | if not cond: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 572 | print >>self.stdout, 'Breakpoint', bpnum, |
| 573 | print >>self.stdout, 'is now unconditional.' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 574 | |
| 575 | def do_ignore(self,arg): |
| 576 | """arg is bp number followed by ignore count.""" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 577 | args = arg.split() |
Georg Brandl | e498083 | 2007-01-22 21:23:41 +0000 | [diff] [blame] | 578 | try: |
| 579 | bpnum = int(args[0].strip()) |
| 580 | except ValueError: |
| 581 | # something went wrong |
| 582 | print >>self.stdout, \ |
| 583 | 'Breakpoint index %r is not a number' % args[0] |
Georg Brandl | b278318 | 2007-03-11 08:28:46 +0000 | [diff] [blame] | 584 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 585 | try: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 586 | count = int(args[1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 587 | except: |
| 588 | count = 0 |
Collin Winter | 2faa9e1 | 2007-03-11 16:00:20 +0000 | [diff] [blame] | 589 | try: |
| 590 | bp = bdb.Breakpoint.bpbynumber[bpnum] |
| 591 | except IndexError: |
| 592 | print >>self.stdout, 'Breakpoint index %r is not valid' % args[0] |
| 593 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 594 | if bp: |
| 595 | bp.ignore = count |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 596 | if count > 0: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 597 | reply = 'Will ignore next ' |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 598 | if count > 1: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 599 | reply = reply + '%d crossings' % count |
| 600 | else: |
| 601 | reply = reply + '1 crossing' |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 602 | print >>self.stdout, reply + ' of breakpoint %d.' % bpnum |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 603 | else: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 604 | print >>self.stdout, 'Will stop next time breakpoint', |
| 605 | print >>self.stdout, bpnum, 'is reached.' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 606 | |
| 607 | def do_clear(self, arg): |
| 608 | """Three possibilities, tried in this order: |
| 609 | clear -> clear all breaks, ask for confirmation |
| 610 | clear file:lineno -> clear all breaks at file:lineno |
| 611 | clear bpno bpno ... -> clear breakpoints by number""" |
| 612 | if not arg: |
| 613 | try: |
| 614 | reply = raw_input('Clear all breaks? ') |
| 615 | except EOFError: |
| 616 | reply = 'no' |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 617 | reply = reply.strip().lower() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 618 | if reply in ('y', 'yes'): |
| 619 | self.clear_all_breaks() |
| 620 | return |
| 621 | if ':' in arg: |
| 622 | # Make sure it works for "clear C:\foo\bar.py:12" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 623 | i = arg.rfind(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 624 | filename = arg[:i] |
| 625 | arg = arg[i+1:] |
| 626 | try: |
| 627 | lineno = int(arg) |
Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 628 | except ValueError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 629 | err = "Invalid line number (%s)" % arg |
| 630 | else: |
| 631 | err = self.clear_break(filename, lineno) |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 632 | if err: print >>self.stdout, '***', err |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 633 | return |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 634 | numberlist = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 635 | for i in numberlist: |
Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 636 | try: |
| 637 | i = int(i) |
| 638 | except ValueError: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 639 | print >>self.stdout, 'Breakpoint index %r is not a number' % i |
Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 640 | continue |
| 641 | |
Georg Brandl | 6d2b346 | 2005-08-24 07:36:17 +0000 | [diff] [blame] | 642 | if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 643 | print >>self.stdout, 'No breakpoint numbered', i |
Georg Brandl | 6d2b346 | 2005-08-24 07:36:17 +0000 | [diff] [blame] | 644 | continue |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 645 | err = self.clear_bpbynumber(i) |
| 646 | if err: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 647 | print >>self.stdout, '***', err |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 648 | else: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 649 | print >>self.stdout, 'Deleted breakpoint', i |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 650 | do_cl = do_clear # 'c' is already an abbreviation for 'continue' |
| 651 | |
| 652 | def do_where(self, arg): |
| 653 | self.print_stack_trace() |
| 654 | do_w = do_where |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 655 | do_bt = do_where |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 656 | |
| 657 | def do_up(self, arg): |
| 658 | if self.curindex == 0: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 659 | print >>self.stdout, '*** Oldest frame' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 660 | else: |
| 661 | self.curindex = self.curindex - 1 |
| 662 | self.curframe = self.stack[self.curindex][0] |
Georg Brandl | 569fc96 | 2009-04-02 02:00:01 +0000 | [diff] [blame] | 663 | self.curframe_locals = self.curframe.f_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 664 | self.print_stack_entry(self.stack[self.curindex]) |
| 665 | self.lineno = None |
| 666 | do_u = do_up |
| 667 | |
| 668 | def do_down(self, arg): |
| 669 | if self.curindex + 1 == len(self.stack): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 670 | print >>self.stdout, '*** Newest frame' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 671 | else: |
| 672 | self.curindex = self.curindex + 1 |
| 673 | self.curframe = self.stack[self.curindex][0] |
Georg Brandl | 569fc96 | 2009-04-02 02:00:01 +0000 | [diff] [blame] | 674 | self.curframe_locals = self.curframe.f_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 675 | self.print_stack_entry(self.stack[self.curindex]) |
| 676 | self.lineno = None |
| 677 | do_d = do_down |
| 678 | |
Benjamin Peterson | 9835394 | 2008-05-11 14:13:25 +0000 | [diff] [blame] | 679 | def do_until(self, arg): |
| 680 | self.set_until(self.curframe) |
| 681 | return 1 |
| 682 | do_unt = do_until |
| 683 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 684 | def do_step(self, arg): |
| 685 | self.set_step() |
| 686 | return 1 |
| 687 | do_s = do_step |
| 688 | |
| 689 | def do_next(self, arg): |
| 690 | self.set_next(self.curframe) |
| 691 | return 1 |
| 692 | do_n = do_next |
| 693 | |
Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 694 | def do_run(self, arg): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 695 | """Restart program by raising an exception to be caught in the main |
| 696 | debugger loop. If arguments were given, set them in sys.argv.""" |
Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 697 | if arg: |
| 698 | import shlex |
| 699 | argv0 = sys.argv[0:1] |
| 700 | sys.argv = shlex.split(arg) |
| 701 | sys.argv[:0] = argv0 |
| 702 | raise Restart |
| 703 | |
| 704 | do_restart = do_run |
| 705 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 706 | def do_return(self, arg): |
| 707 | self.set_return(self.curframe) |
| 708 | return 1 |
| 709 | do_r = do_return |
| 710 | |
| 711 | def do_continue(self, arg): |
| 712 | self.set_continue() |
| 713 | return 1 |
| 714 | do_c = do_cont = do_continue |
| 715 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 716 | def do_jump(self, arg): |
| 717 | if self.curindex + 1 != len(self.stack): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 718 | print >>self.stdout, "*** You can only jump within the bottom frame" |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 719 | return |
| 720 | try: |
| 721 | arg = int(arg) |
| 722 | except ValueError: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 723 | print >>self.stdout, "*** The 'jump' command requires a line number." |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 724 | else: |
| 725 | try: |
| 726 | # Do the jump, fix up our copy of the stack, and display the |
| 727 | # new position |
| 728 | self.curframe.f_lineno = arg |
| 729 | self.stack[self.curindex] = self.stack[self.curindex][0], arg |
| 730 | self.print_stack_entry(self.stack[self.curindex]) |
| 731 | except ValueError, e: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 732 | print >>self.stdout, '*** Jump failed:', e |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 733 | do_j = do_jump |
| 734 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 735 | def do_debug(self, arg): |
| 736 | sys.settrace(None) |
| 737 | globals = self.curframe.f_globals |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 738 | locals = self.curframe_locals |
Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 739 | p = Pdb(self.completekey, self.stdin, self.stdout) |
Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 740 | p.prompt = "(%s) " % self.prompt.strip() |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 741 | print >>self.stdout, "ENTERING RECURSIVE DEBUGGER" |
Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 742 | sys.call_tracing(p.run, (arg, globals, locals)) |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 743 | print >>self.stdout, "LEAVING RECURSIVE DEBUGGER" |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 744 | sys.settrace(self.trace_dispatch) |
| 745 | self.lastcmd = p.lastcmd |
| 746 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 747 | def do_quit(self, arg): |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 748 | self._user_requested_quit = 1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 749 | self.set_quit() |
| 750 | return 1 |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 751 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 752 | do_q = do_quit |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 753 | do_exit = do_quit |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 755 | def do_EOF(self, arg): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 756 | print >>self.stdout |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 757 | self._user_requested_quit = 1 |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 758 | self.set_quit() |
| 759 | return 1 |
| 760 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 761 | def do_args(self, arg): |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 762 | co = self.curframe.f_code |
| 763 | dict = self.curframe_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 764 | n = co.co_argcount |
| 765 | if co.co_flags & 4: n = n+1 |
| 766 | if co.co_flags & 8: n = n+1 |
| 767 | for i in range(n): |
| 768 | name = co.co_varnames[i] |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 769 | print >>self.stdout, name, '=', |
| 770 | if name in dict: print >>self.stdout, dict[name] |
| 771 | else: print >>self.stdout, "*** undefined ***" |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 772 | do_a = do_args |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 773 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 774 | def do_retval(self, arg): |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 775 | if '__return__' in self.curframe_locals: |
| 776 | print >>self.stdout, self.curframe_locals['__return__'] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 777 | else: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 778 | print >>self.stdout, '*** Not yet returned!' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 779 | do_rv = do_retval |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 780 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 781 | def _getval(self, arg): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 782 | try: |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 783 | return eval(arg, self.curframe.f_globals, |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 784 | self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 785 | except: |
| 786 | t, v = sys.exc_info()[:2] |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 787 | if isinstance(t, str): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 788 | exc_type_name = t |
| 789 | else: exc_type_name = t.__name__ |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 790 | print >>self.stdout, '***', exc_type_name + ':', repr(v) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 791 | raise |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 792 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 793 | def do_p(self, arg): |
| 794 | try: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 795 | print >>self.stdout, repr(self._getval(arg)) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 796 | except: |
| 797 | pass |
| 798 | |
| 799 | def do_pp(self, arg): |
| 800 | try: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 801 | pprint.pprint(self._getval(arg), self.stdout) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 802 | except: |
| 803 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 804 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 805 | def do_list(self, arg): |
| 806 | self.lastcmd = 'list' |
| 807 | last = None |
| 808 | if arg: |
| 809 | try: |
| 810 | x = eval(arg, {}, {}) |
| 811 | if type(x) == type(()): |
| 812 | first, last = x |
| 813 | first = int(first) |
| 814 | last = int(last) |
| 815 | if last < first: |
| 816 | # Assume it's a count |
| 817 | last = first + last |
| 818 | else: |
| 819 | first = max(1, int(x) - 5) |
| 820 | except: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 821 | print >>self.stdout, '*** Error in argument:', repr(arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 822 | return |
| 823 | elif self.lineno is None: |
| 824 | first = max(1, self.curframe.f_lineno - 5) |
| 825 | else: |
| 826 | first = self.lineno + 1 |
| 827 | if last is None: |
| 828 | last = first + 10 |
| 829 | filename = self.curframe.f_code.co_filename |
| 830 | breaklist = self.get_file_breaks(filename) |
| 831 | try: |
| 832 | for lineno in range(first, last+1): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 833 | line = linecache.getline(filename, lineno, |
| 834 | self.curframe.f_globals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 835 | if not line: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 836 | print >>self.stdout, '[EOF]' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 837 | break |
| 838 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 839 | s = repr(lineno).rjust(3) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 840 | if len(s) < 4: s = s + ' ' |
| 841 | if lineno in breaklist: s = s + 'B' |
| 842 | else: s = s + ' ' |
| 843 | if lineno == self.curframe.f_lineno: |
| 844 | s = s + '->' |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 845 | print >>self.stdout, s + '\t' + line, |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 846 | self.lineno = lineno |
| 847 | except KeyboardInterrupt: |
| 848 | pass |
| 849 | do_l = do_list |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 850 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 851 | def do_whatis(self, arg): |
| 852 | try: |
| 853 | value = eval(arg, self.curframe.f_globals, |
Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 854 | self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 855 | except: |
| 856 | t, v = sys.exc_info()[:2] |
| 857 | if type(t) == type(''): |
| 858 | exc_type_name = t |
| 859 | else: exc_type_name = t.__name__ |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 860 | print >>self.stdout, '***', exc_type_name + ':', repr(v) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 861 | return |
| 862 | code = None |
| 863 | # Is it a function? |
| 864 | try: code = value.func_code |
| 865 | except: pass |
| 866 | if code: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 867 | print >>self.stdout, 'Function', code.co_name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 868 | return |
| 869 | # Is it an instance method? |
| 870 | try: code = value.im_func.func_code |
| 871 | except: pass |
| 872 | if code: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 873 | print >>self.stdout, 'Method', code.co_name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 874 | return |
| 875 | # None of the above... |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 876 | print >>self.stdout, type(value) |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 877 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 878 | def do_alias(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 879 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 880 | if len(args) == 0: |
| 881 | keys = self.aliases.keys() |
| 882 | keys.sort() |
| 883 | for alias in keys: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 884 | print >>self.stdout, "%s = %s" % (alias, self.aliases[alias]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 885 | return |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 886 | if args[0] in self.aliases and len(args) == 1: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 887 | print >>self.stdout, "%s = %s" % (args[0], self.aliases[args[0]]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 888 | else: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 889 | self.aliases[args[0]] = ' '.join(args[1:]) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 890 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 891 | def do_unalias(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 892 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 893 | if len(args) == 0: return |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 894 | if args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 895 | del self.aliases[args[0]] |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 896 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 897 | #list of all the commands making the program resume execution. |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 898 | commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return', |
| 899 | 'do_quit', 'do_jump'] |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 900 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 901 | # Print a traceback starting at the top stack frame. |
| 902 | # The most recently entered frame is printed last; |
| 903 | # this is different from dbx and gdb, but consistent with |
| 904 | # the Python interpreter's stack trace. |
| 905 | # It is also consistent with the up/down commands (which are |
| 906 | # compatible with dbx and gdb: up moves towards 'main()' |
| 907 | # and down moves towards the most recent stack frame). |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 908 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 909 | def print_stack_trace(self): |
| 910 | try: |
| 911 | for frame_lineno in self.stack: |
| 912 | self.print_stack_entry(frame_lineno) |
| 913 | except KeyboardInterrupt: |
| 914 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 915 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 916 | def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): |
| 917 | frame, lineno = frame_lineno |
| 918 | if frame is self.curframe: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 919 | print >>self.stdout, '>', |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 920 | else: |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 921 | print >>self.stdout, ' ', |
| 922 | print >>self.stdout, self.format_stack_entry(frame_lineno, |
| 923 | prompt_prefix) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 924 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 925 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 926 | # Help methods (derived from pdb.doc) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 927 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 928 | def help_help(self): |
| 929 | self.help_h() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 930 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 931 | def help_h(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 932 | print >>self.stdout, """h(elp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 933 | Without argument, print the list of available commands. |
| 934 | With a command name as argument, print help about that command |
| 935 | "help pdb" pipes the full documentation file to the $PAGER |
| 936 | "help exec" gives help on the ! command""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 937 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 938 | def help_where(self): |
| 939 | self.help_w() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 940 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 941 | def help_w(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 942 | print >>self.stdout, """w(here) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 943 | Print a stack trace, with the most recent frame at the bottom. |
| 944 | An arrow indicates the "current frame", which determines the |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 945 | context of most commands. 'bt' is an alias for this command.""" |
| 946 | |
| 947 | help_bt = help_w |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 948 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 949 | def help_down(self): |
| 950 | self.help_d() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 951 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 952 | def help_d(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 953 | print >>self.stdout, """d(own) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 954 | Move the current frame one level down in the stack trace |
Johannes Gijsbers | 34c4120 | 2004-08-14 15:19:28 +0000 | [diff] [blame] | 955 | (to a newer frame).""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 956 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 957 | def help_up(self): |
| 958 | self.help_u() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 959 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 960 | def help_u(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 961 | print >>self.stdout, """u(p) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 962 | Move the current frame one level up in the stack trace |
Johannes Gijsbers | 34c4120 | 2004-08-14 15:19:28 +0000 | [diff] [blame] | 963 | (to an older frame).""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 964 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 965 | def help_break(self): |
| 966 | self.help_b() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 967 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 968 | def help_b(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 969 | print >>self.stdout, """b(reak) ([file:]lineno | function) [, condition] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 970 | With a line number argument, set a break there in the current |
| 971 | file. With a function name, set a break at first executable line |
| 972 | of that function. Without argument, list all breaks. If a second |
| 973 | argument is present, it is a string specifying an expression |
| 974 | which must evaluate to true before the breakpoint is honored. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 975 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 976 | The line number may be prefixed with a filename and a colon, |
| 977 | to specify a breakpoint in another file (probably one that |
| 978 | hasn't been loaded yet). The file is searched for on sys.path; |
| 979 | the .py suffix may be omitted.""" |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 980 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 981 | def help_clear(self): |
| 982 | self.help_cl() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 983 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 984 | def help_cl(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 985 | print >>self.stdout, "cl(ear) filename:lineno" |
| 986 | print >>self.stdout, """cl(ear) [bpnumber [bpnumber...]] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 987 | With a space separated list of breakpoint numbers, clear |
| 988 | those breakpoints. Without argument, clear all breaks (but |
| 989 | first ask confirmation). With a filename:lineno argument, |
| 990 | clear all breaks at that line in that file. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 991 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 992 | Note that the argument is different from previous versions of |
| 993 | the debugger (in python distributions 1.5.1 and before) where |
| 994 | a linenumber was used instead of either filename:lineno or |
| 995 | breakpoint numbers.""" |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 996 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 997 | def help_tbreak(self): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 998 | print >>self.stdout, """tbreak same arguments as break, but breakpoint |
| 999 | is removed when first hit.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1000 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1001 | def help_enable(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1002 | print >>self.stdout, """enable bpnumber [bpnumber ...] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1003 | Enables the breakpoints given as a space separated list of |
| 1004 | bp numbers.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1005 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1006 | def help_disable(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1007 | print >>self.stdout, """disable bpnumber [bpnumber ...] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1008 | Disables the breakpoints given as a space separated list of |
| 1009 | bp numbers.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1010 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1011 | def help_ignore(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1012 | print >>self.stdout, """ignore bpnumber count |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1013 | Sets the ignore count for the given breakpoint number. A breakpoint |
| 1014 | becomes active when the ignore count is zero. When non-zero, the |
| 1015 | count is decremented each time the breakpoint is reached and the |
| 1016 | breakpoint is not disabled and any associated condition evaluates |
| 1017 | to true.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1018 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1019 | def help_condition(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1020 | print >>self.stdout, """condition bpnumber str_condition |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1021 | str_condition is a string specifying an expression which |
| 1022 | must evaluate to true before the breakpoint is honored. |
| 1023 | If str_condition is absent, any existing condition is removed; |
| 1024 | i.e., the breakpoint is made unconditional.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1025 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1026 | def help_step(self): |
| 1027 | self.help_s() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1028 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1029 | def help_s(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1030 | print >>self.stdout, """s(tep) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1031 | Execute the current line, stop at the first possible occasion |
| 1032 | (either in a function that is called or in the current function).""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1033 | |
Benjamin Peterson | 9835394 | 2008-05-11 14:13:25 +0000 | [diff] [blame] | 1034 | def help_until(self): |
| 1035 | self.help_unt() |
| 1036 | |
| 1037 | def help_unt(self): |
| 1038 | print """unt(il) |
| 1039 | Continue execution until the line with a number greater than the current |
| 1040 | one is reached or until the current frame returns""" |
| 1041 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1042 | def help_next(self): |
| 1043 | self.help_n() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1044 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1045 | def help_n(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1046 | print >>self.stdout, """n(ext) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1047 | Continue execution until the next line in the current function |
| 1048 | is reached or it returns.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1049 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1050 | def help_return(self): |
| 1051 | self.help_r() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1052 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1053 | def help_r(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1054 | print >>self.stdout, """r(eturn) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1055 | Continue execution until the current function returns.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1056 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1057 | def help_continue(self): |
| 1058 | self.help_c() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1059 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1060 | def help_cont(self): |
| 1061 | self.help_c() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1062 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1063 | def help_c(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1064 | print >>self.stdout, """c(ont(inue)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1065 | Continue execution, only stop when a breakpoint is encountered.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1066 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1067 | def help_jump(self): |
| 1068 | self.help_j() |
| 1069 | |
| 1070 | def help_j(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1071 | print >>self.stdout, """j(ump) lineno |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1072 | Set the next line that will be executed.""" |
| 1073 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1074 | def help_debug(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1075 | print >>self.stdout, """debug code |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1076 | Enter a recursive debugger that steps through the code argument |
| 1077 | (which is an arbitrary expression or statement to be executed |
| 1078 | in the current environment).""" |
| 1079 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1080 | def help_list(self): |
| 1081 | self.help_l() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1082 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1083 | def help_l(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1084 | print >>self.stdout, """l(ist) [first [,last]] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1085 | List source code for the current file. |
| 1086 | Without arguments, list 11 lines around the current line |
| 1087 | or continue the previous listing. |
| 1088 | With one argument, list 11 lines starting at that line. |
| 1089 | With two arguments, list the given range; |
| 1090 | if the second argument is less than the first, it is a count.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1091 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1092 | def help_args(self): |
| 1093 | self.help_a() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1094 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1095 | def help_a(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1096 | print >>self.stdout, """a(rgs) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1097 | Print the arguments of the current function.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1098 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1099 | def help_p(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1100 | print >>self.stdout, """p expression |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1101 | Print the value of the expression.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1102 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1103 | def help_pp(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1104 | print >>self.stdout, """pp expression |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1105 | Pretty-print the value of the expression.""" |
| 1106 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1107 | def help_exec(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1108 | print >>self.stdout, """(!) statement |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1109 | Execute the (one-line) statement in the context of |
| 1110 | the current stack frame. |
| 1111 | The exclamation point can be omitted unless the first word |
| 1112 | of the statement resembles a debugger command. |
| 1113 | To assign to a global variable you must always prefix the |
| 1114 | command with a 'global' command, e.g.: |
| 1115 | (Pdb) global list_options; list_options = ['-l'] |
| 1116 | (Pdb)""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1117 | |
Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 1118 | def help_run(self): |
| 1119 | print """run [args...] |
| 1120 | Restart the debugged python program. If a string is supplied, it is |
| 1121 | splitted with "shlex" and the result is used as the new sys.argv. |
| 1122 | History, breakpoints, actions and debugger options are preserved. |
| 1123 | "restart" is an alias for "run".""" |
| 1124 | |
| 1125 | help_restart = help_run |
| 1126 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1127 | def help_quit(self): |
| 1128 | self.help_q() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1129 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1130 | def help_q(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1131 | print >>self.stdout, """q(uit) or exit - Quit from the debugger. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1132 | The program being executed is aborted.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1133 | |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 1134 | help_exit = help_q |
| 1135 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1136 | def help_whatis(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1137 | print >>self.stdout, """whatis arg |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1138 | Prints the type of the argument.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1139 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1140 | def help_EOF(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1141 | print >>self.stdout, """EOF |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1142 | Handles the receipt of EOF as a command.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1143 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1144 | def help_alias(self): |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1145 | print >>self.stdout, """alias [name [command [parameter parameter ...]]] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1146 | Creates an alias called 'name' the executes 'command'. The command |
| 1147 | must *not* be enclosed in quotes. Replaceable parameters are |
| 1148 | indicated by %1, %2, and so on, while %* is replaced by all the |
| 1149 | parameters. If no command is given, the current alias for name |
| 1150 | is shown. If no name is given, all aliases are listed. |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1151 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1152 | Aliases may be nested and can contain anything that can be |
| 1153 | legally typed at the pdb prompt. Note! You *can* override |
| 1154 | internal pdb commands with aliases! Those internal commands |
| 1155 | are then hidden until the alias is removed. Aliasing is recursively |
| 1156 | applied to the first word of the command line; all other words |
| 1157 | in the line are left alone. |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1158 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1159 | Some useful aliases (especially when placed in the .pdbrc file) are: |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1160 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1161 | #Print instance variables (usage "pi classInst") |
| 1162 | alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k] |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1163 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1164 | #Print instance variables in self |
| 1165 | alias ps pi self |
| 1166 | """ |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1167 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1168 | def help_unalias(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1169 | print >>self.stdout, """unalias name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1170 | Deletes the specified alias.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1171 | |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1172 | def help_commands(self): |
Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1173 | print >>self.stdout, """commands [bpnumber] |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1174 | (com) ... |
| 1175 | (com) end |
| 1176 | (Pdb) |
| 1177 | |
| 1178 | Specify a list of commands for breakpoint number bpnumber. The |
| 1179 | commands themselves appear on the following lines. Type a line |
| 1180 | containing just 'end' to terminate the commands. |
| 1181 | |
| 1182 | To remove all commands from a breakpoint, type commands and |
| 1183 | follow it immediately with end; that is, give no commands. |
| 1184 | |
| 1185 | With no bpnumber argument, commands refers to the last |
| 1186 | breakpoint set. |
| 1187 | |
| 1188 | You can use breakpoint commands to start your program up again. |
| 1189 | Simply use the continue command, or step, or any other |
| 1190 | command that resumes execution. |
| 1191 | |
| 1192 | Specifying any command resuming execution (currently continue, |
| 1193 | step, next, return, jump, quit and their abbreviations) terminates |
| 1194 | the command list (as if that command was immediately followed by end). |
| 1195 | This is because any time you resume execution |
Martin v. Löwis | f62eee1 | 2006-04-17 17:37:09 +0000 | [diff] [blame] | 1196 | (even with a simple next or step), you may encounter |
Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1197 | another breakpoint--which could have its own command list, leading to |
| 1198 | ambiguities about which list to execute. |
| 1199 | |
| 1200 | If you use the 'silent' command in the command list, the |
| 1201 | usual message about stopping at a breakpoint is not printed. This may |
| 1202 | be desirable for breakpoints that are to print a specific message and |
| 1203 | then continue. If none of the other commands print anything, you |
| 1204 | see no sign that the breakpoint was reached. |
| 1205 | """ |
| 1206 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1207 | def help_pdb(self): |
| 1208 | help() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1209 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1210 | def lookupmodule(self, filename): |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1211 | """Helper function for break/clear parsing -- may be overridden. |
| 1212 | |
| 1213 | lookupmodule() translates (possibly incomplete) file or module name |
| 1214 | into an absolute file name. |
| 1215 | """ |
| 1216 | if os.path.isabs(filename) and os.path.exists(filename): |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1217 | return filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1218 | f = os.path.join(sys.path[0], filename) |
| 1219 | if os.path.exists(f) and self.canonic(f) == self.mainpyfile: |
| 1220 | return f |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1221 | root, ext = os.path.splitext(filename) |
| 1222 | if ext == '': |
| 1223 | filename = filename + '.py' |
| 1224 | if os.path.isabs(filename): |
| 1225 | return filename |
| 1226 | for dirname in sys.path: |
| 1227 | while os.path.islink(dirname): |
| 1228 | dirname = os.readlink(dirname) |
| 1229 | fullname = os.path.join(dirname, filename) |
| 1230 | if os.path.exists(fullname): |
| 1231 | return fullname |
| 1232 | return None |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 1233 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1234 | def _runscript(self, filename): |
Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1235 | # The script has to run in __main__ namespace (or imports from |
| 1236 | # __main__ will break). |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1237 | # |
Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1238 | # So we clear up the __main__ and set several special variables |
| 1239 | # (this gets rid of pdb's globals and cleans old variables on restarts). |
| 1240 | import __main__ |
| 1241 | __main__.__dict__.clear() |
| 1242 | __main__.__dict__.update({"__name__" : "__main__", |
| 1243 | "__file__" : filename, |
| 1244 | "__builtins__": __builtins__, |
| 1245 | }) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1246 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1247 | # When bdb sets tracing, a number of call and line events happens |
| 1248 | # BEFORE debugger even reaches user's code (and the exact sequence of |
| 1249 | # events depends on python version). So we take special measures to |
| 1250 | # avoid stopping before we reach the main script (see user_line and |
| 1251 | # user_call for details). |
| 1252 | self._wait_for_mainpyfile = 1 |
| 1253 | self.mainpyfile = self.canonic(filename) |
| 1254 | self._user_requested_quit = 0 |
| 1255 | statement = 'execfile( "%s")' % filename |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1256 | self.run(statement) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1258 | # Simplified interface |
| 1259 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1260 | def run(statement, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1261 | Pdb().run(statement, globals, locals) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1262 | |
| 1263 | def runeval(expression, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1264 | return Pdb().runeval(expression, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1265 | |
| 1266 | def runctx(statement, globals, locals): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1267 | # B/W compatibility |
| 1268 | run(statement, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1269 | |
Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 1270 | def runcall(*args, **kwds): |
| 1271 | return Pdb().runcall(*args, **kwds) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 1272 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1273 | def set_trace(): |
Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 1274 | Pdb().set_trace(sys._getframe().f_back) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1275 | |
| 1276 | # Post-Mortem interface |
| 1277 | |
Facundo Batista | c54aec1 | 2008-03-08 16:50:27 +0000 | [diff] [blame] | 1278 | def post_mortem(t=None): |
| 1279 | # handling the default |
| 1280 | if t is None: |
| 1281 | # sys.exc_info() returns (type, value, traceback) if an exception is |
| 1282 | # being handled, otherwise it returns None |
| 1283 | t = sys.exc_info()[2] |
| 1284 | if t is None: |
| 1285 | raise ValueError("A valid traceback must be passed if no " |
| 1286 | "exception is being handled") |
| 1287 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1288 | p = Pdb() |
| 1289 | p.reset() |
Benjamin Peterson | c18574c | 2008-10-22 21:16:34 +0000 | [diff] [blame] | 1290 | p.interaction(None, t) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1291 | |
| 1292 | def pm(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1293 | post_mortem(sys.last_traceback) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1294 | |
| 1295 | |
| 1296 | # Main program for testing |
| 1297 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1298 | TESTCMD = 'import x; x.main()' |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1299 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1300 | def test(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1301 | run(TESTCMD) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 1302 | |
| 1303 | # print help |
| 1304 | def help(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1305 | for dirname in sys.path: |
| 1306 | fullname = os.path.join(dirname, 'pdb.doc') |
| 1307 | if os.path.exists(fullname): |
| 1308 | sts = os.system('${PAGER-more} '+fullname) |
| 1309 | if sts: print '*** Pager exit status:', sts |
| 1310 | break |
| 1311 | else: |
| 1312 | print 'Sorry, can\'t find the help file "pdb.doc"', |
| 1313 | print 'along the Python search path' |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1314 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1315 | def main(): |
Benjamin Peterson | 13be2cf | 2008-03-26 11:57:47 +0000 | [diff] [blame] | 1316 | if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1317 | print "usage: pdb.py scriptfile [arg] ..." |
| 1318 | sys.exit(2) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1319 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1320 | mainpyfile = sys.argv[1] # Get script filename |
| 1321 | if not os.path.exists(mainpyfile): |
| 1322 | print 'Error:', mainpyfile, 'does not exist' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1323 | sys.exit(1) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1324 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1325 | del sys.argv[0] # Hide "pdb.py" from argument list |
Guido van Rossum | ec577d5 | 1996-09-10 17:39:34 +0000 | [diff] [blame] | 1326 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1327 | # Replace pdb's dir with script's dir in front of module search path. |
| 1328 | sys.path[0] = os.path.dirname(mainpyfile) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1329 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1330 | # Note on saving/restoring sys.argv: it's a good idea when sys.argv was |
| 1331 | # modified by the script being debugged. It's a bad idea when it was |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1332 | # changed by the user from the command line. There is a "restart" command |
| 1333 | # which allows explicit specification of command line arguments. |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1334 | pdb = Pdb() |
| 1335 | while 1: |
| 1336 | try: |
| 1337 | pdb._runscript(mainpyfile) |
| 1338 | if pdb._user_requested_quit: |
| 1339 | break |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1340 | print "The program finished and will be restarted" |
Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 1341 | except Restart: |
| 1342 | print "Restarting", mainpyfile, "with arguments:" |
| 1343 | print "\t" + " ".join(sys.argv[1:]) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1344 | except SystemExit: |
| 1345 | # In most cases SystemExit does not warrant a post-mortem session. |
| 1346 | print "The program exited via sys.exit(). Exit status: ", |
| 1347 | print sys.exc_info()[1] |
| 1348 | except: |
| 1349 | traceback.print_exc() |
| 1350 | print "Uncaught exception. Entering post mortem debugging" |
| 1351 | print "Running 'cont' or 'step' will restart the program" |
| 1352 | t = sys.exc_info()[2] |
Benjamin Peterson | c18574c | 2008-10-22 21:16:34 +0000 | [diff] [blame] | 1353 | pdb.interaction(None, t) |
Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1354 | print "Post mortem debugger finished. The " + mainpyfile + \ |
| 1355 | " will be restarted" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1356 | |
| 1357 | |
| 1358 | # When invoked as main program, invoke the debugger on a script |
Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1359 | if __name__ == '__main__': |
| 1360 | import pdb |
| 1361 | pdb.main() |