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