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