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