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 |
Guido van Rossum | ef1b41b | 2002-09-10 21:57:14 +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 |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | ef1b41b | 2002-09-10 21:57:14 +0000 | [diff] [blame] | 16 | # Create a custom safe Repr instance and increase its maxstring. |
| 17 | # The default of 30 truncates error messages too easily. |
| 18 | _repr = Repr() |
| 19 | _repr.maxstring = 200 |
| 20 | _saferepr = _repr.repr |
| 21 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 22 | __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace", |
| 23 | "post_mortem", "help"] |
| 24 | |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 25 | def find_function(funcname, filename): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 26 | cre = re.compile(r'def\s+%s\s*[(]' % funcname) |
| 27 | try: |
| 28 | fp = open(filename) |
| 29 | except IOError: |
| 30 | return None |
| 31 | # consumer of this info expects the first line to be 1 |
| 32 | lineno = 1 |
| 33 | answer = None |
| 34 | while 1: |
| 35 | line = fp.readline() |
| 36 | if line == '': |
| 37 | break |
| 38 | if cre.match(line): |
| 39 | answer = funcname, filename, lineno |
| 40 | break |
| 41 | lineno = lineno + 1 |
| 42 | fp.close() |
| 43 | return answer |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 44 | |
| 45 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 46 | # Interaction prompt line will separate file and call info from code |
| 47 | # text using value of line_prefix string. A newline and arrow may |
| 48 | # be to your liking. You can set it once pdb is imported using the |
| 49 | # command "pdb.line_prefix = '\n% '". |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 50 | # line_prefix = ': ' # Use this to get the old situation back |
| 51 | line_prefix = '\n-> ' # Probably a better default |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 53 | class Pdb(bdb.Bdb, cmd.Cmd): |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 54 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 55 | def __init__(self): |
| 56 | bdb.Bdb.__init__(self) |
| 57 | cmd.Cmd.__init__(self) |
| 58 | self.prompt = '(Pdb) ' |
| 59 | self.aliases = {} |
| 60 | # Try to load readline if it exists |
| 61 | try: |
| 62 | import readline |
| 63 | except ImportError: |
| 64 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 65 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 66 | # Read $HOME/.pdbrc and ./.pdbrc |
| 67 | self.rcLines = [] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 68 | if 'HOME' in os.environ: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 69 | envHome = os.environ['HOME'] |
| 70 | try: |
| 71 | rcFile = open(os.path.join(envHome, ".pdbrc")) |
| 72 | except IOError: |
| 73 | pass |
| 74 | else: |
| 75 | for line in rcFile.readlines(): |
| 76 | self.rcLines.append(line) |
| 77 | rcFile.close() |
| 78 | try: |
| 79 | rcFile = open(".pdbrc") |
| 80 | except IOError: |
| 81 | pass |
| 82 | else: |
| 83 | for line in rcFile.readlines(): |
| 84 | self.rcLines.append(line) |
| 85 | rcFile.close() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 86 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 87 | def reset(self): |
| 88 | bdb.Bdb.reset(self) |
| 89 | self.forget() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 90 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 91 | def forget(self): |
| 92 | self.lineno = None |
| 93 | self.stack = [] |
| 94 | self.curindex = 0 |
| 95 | self.curframe = None |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 96 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 97 | def setup(self, f, t): |
| 98 | self.forget() |
| 99 | self.stack, self.curindex = self.get_stack(f, t) |
| 100 | self.curframe = self.stack[self.curindex][0] |
| 101 | self.execRcLines() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 102 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 103 | # Can be executed earlier than 'setup' if desired |
| 104 | def execRcLines(self): |
| 105 | if self.rcLines: |
| 106 | # Make local copy because of recursion |
| 107 | rcLines = self.rcLines |
| 108 | # executed only once |
| 109 | self.rcLines = [] |
| 110 | for line in rcLines: |
| 111 | line = line[:-1] |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 112 | if len(line) > 0 and line[0] != '#': |
| 113 | self.onecmd(line) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 114 | |
Tim Peters | 280488b | 2002-08-23 18:19:30 +0000 | [diff] [blame] | 115 | # Override Bdb methods |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 116 | |
| 117 | def user_call(self, frame, argument_list): |
| 118 | """This method is called when there is the remote possibility |
| 119 | that we ever need to stop in this function.""" |
| 120 | print '--Call--' |
| 121 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 122 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 123 | def user_line(self, frame): |
| 124 | """This function is called when we stop or break at this line.""" |
| 125 | self.interaction(frame, None) |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 126 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 127 | def user_return(self, frame, return_value): |
| 128 | """This function is called when a return trap is set here.""" |
| 129 | frame.f_locals['__return__'] = return_value |
| 130 | print '--Return--' |
| 131 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 132 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 133 | def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): |
| 134 | """This function is called if an exception occurs, |
| 135 | but only if we are to stop at or just below this level.""" |
| 136 | frame.f_locals['__exception__'] = exc_type, exc_value |
| 137 | if type(exc_type) == type(''): |
| 138 | exc_type_name = exc_type |
| 139 | else: exc_type_name = exc_type.__name__ |
Tim Peters | 6f8ee59 | 2001-02-09 23:28:07 +0000 | [diff] [blame] | 140 | print exc_type_name + ':', _saferepr(exc_value) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 141 | self.interaction(frame, exc_traceback) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 142 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 143 | # General interaction function |
| 144 | |
| 145 | def interaction(self, frame, traceback): |
| 146 | self.setup(frame, traceback) |
| 147 | self.print_stack_entry(self.stack[self.curindex]) |
| 148 | self.cmdloop() |
| 149 | self.forget() |
| 150 | |
| 151 | def default(self, line): |
| 152 | if line[:1] == '!': line = line[1:] |
| 153 | locals = self.curframe.f_locals |
| 154 | globals = self.curframe.f_globals |
| 155 | try: |
| 156 | code = compile(line + '\n', '<stdin>', 'single') |
| 157 | exec code in globals, locals |
| 158 | except: |
| 159 | t, v = sys.exc_info()[:2] |
| 160 | if type(t) == type(''): |
| 161 | exc_type_name = t |
| 162 | else: exc_type_name = t.__name__ |
| 163 | print '***', exc_type_name + ':', v |
| 164 | |
| 165 | def precmd(self, line): |
| 166 | """Handle alias expansion and ';;' separator.""" |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 167 | if not line.strip(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 168 | return line |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 169 | args = line.split() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 170 | while args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 171 | line = self.aliases[args[0]] |
| 172 | ii = 1 |
| 173 | for tmpArg in args[1:]: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 174 | line = line.replace("%" + str(ii), |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 175 | tmpArg) |
| 176 | ii = ii + 1 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 177 | line = line.replace("%*", ' '.join(args[1:])) |
| 178 | args = line.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 179 | # split into ';;' separated commands |
| 180 | # unless it's an alias command |
| 181 | if args[0] != 'alias': |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 182 | marker = line.find(';;') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 183 | if marker >= 0: |
| 184 | # queue up everything after marker |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 185 | next = line[marker+2:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 186 | self.cmdqueue.append(next) |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 187 | line = line[:marker].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 188 | return line |
| 189 | |
| 190 | # Command definitions, called by cmdloop() |
| 191 | # The argument is the remaining string on the command line |
| 192 | # Return true to exit from the command loop |
| 193 | |
| 194 | do_h = cmd.Cmd.do_help |
| 195 | |
| 196 | def do_EOF(self, arg): |
| 197 | return 0 # Don't die on EOF |
| 198 | |
| 199 | def do_break(self, arg, temporary = 0): |
| 200 | # break [ ([filename:]lineno | function) [, "condition"] ] |
| 201 | if not arg: |
| 202 | if self.breaks: # There's at least one |
| 203 | print "Num Type Disp Enb Where" |
| 204 | for bp in bdb.Breakpoint.bpbynumber: |
| 205 | if bp: |
| 206 | bp.bpprint() |
| 207 | return |
| 208 | # parse arguments; comma has lowest precedence |
| 209 | # and cannot occur in filename |
| 210 | filename = None |
| 211 | lineno = None |
| 212 | cond = None |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 213 | comma = arg.find(',') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 214 | if comma > 0: |
| 215 | # parse stuff after comma: "condition" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 216 | cond = arg[comma+1:].lstrip() |
| 217 | arg = arg[:comma].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 218 | # parse stuff before comma: [filename:]lineno | function |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 219 | colon = arg.rfind(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 220 | if colon >= 0: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 221 | filename = arg[:colon].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 222 | f = self.lookupmodule(filename) |
| 223 | if not f: |
| 224 | print '*** ', `filename`, |
| 225 | print 'not found from sys.path' |
| 226 | return |
| 227 | else: |
| 228 | filename = f |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 229 | arg = arg[colon+1:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 230 | try: |
| 231 | lineno = int(arg) |
| 232 | except ValueError, msg: |
| 233 | print '*** Bad lineno:', arg |
| 234 | return |
| 235 | else: |
| 236 | # no colon; can be lineno or function |
| 237 | try: |
| 238 | lineno = int(arg) |
| 239 | except ValueError: |
| 240 | try: |
| 241 | func = eval(arg, |
| 242 | self.curframe.f_globals, |
| 243 | self.curframe.f_locals) |
| 244 | except: |
| 245 | func = arg |
| 246 | try: |
| 247 | if hasattr(func, 'im_func'): |
| 248 | func = func.im_func |
| 249 | code = func.func_code |
| 250 | lineno = code.co_firstlineno |
| 251 | filename = code.co_filename |
| 252 | except: |
| 253 | # last thing to try |
| 254 | (ok, filename, ln) = self.lineinfo(arg) |
| 255 | if not ok: |
| 256 | print '*** The specified object', |
| 257 | print `arg`, |
| 258 | print 'is not a function' |
| 259 | print ('or was not found ' |
| 260 | 'along sys.path.') |
| 261 | return |
| 262 | lineno = int(ln) |
| 263 | if not filename: |
| 264 | filename = self.defaultFile() |
| 265 | # Check for reasonable breakpoint |
| 266 | line = self.checkline(filename, lineno) |
| 267 | if line: |
| 268 | # now set the break point |
| 269 | err = self.set_break(filename, line, temporary, cond) |
| 270 | if err: print '***', err |
| 271 | else: |
| 272 | bp = self.get_breaks(filename, line)[-1] |
| 273 | print "Breakpoint %d at %s:%d" % (bp.number, |
| 274 | bp.file, |
| 275 | bp.line) |
| 276 | |
| 277 | # To be overridden in derived debuggers |
| 278 | def defaultFile(self): |
| 279 | """Produce a reasonable default.""" |
| 280 | filename = self.curframe.f_code.co_filename |
| 281 | if filename == '<string>' and mainpyfile: |
| 282 | filename = mainpyfile |
| 283 | return filename |
| 284 | |
| 285 | do_b = do_break |
| 286 | |
| 287 | def do_tbreak(self, arg): |
| 288 | self.do_break(arg, 1) |
| 289 | |
| 290 | def lineinfo(self, identifier): |
| 291 | failed = (None, None, None) |
| 292 | # Input is identifier, may be in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 293 | idstring = identifier.split("'") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 294 | if len(idstring) == 1: |
| 295 | # not in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 296 | id = idstring[0].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 297 | elif len(idstring) == 3: |
| 298 | # quoted |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 299 | id = idstring[1].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 300 | else: |
| 301 | return failed |
| 302 | if id == '': return failed |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 303 | parts = id.split('.') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 304 | # Protection for derived debuggers |
| 305 | if parts[0] == 'self': |
| 306 | del parts[0] |
| 307 | if len(parts) == 0: |
| 308 | return failed |
| 309 | # Best first guess at file to look at |
| 310 | fname = self.defaultFile() |
| 311 | if len(parts) == 1: |
| 312 | item = parts[0] |
| 313 | else: |
| 314 | # More than one part. |
| 315 | # First is module, second is method/class |
| 316 | f = self.lookupmodule(parts[0]) |
| 317 | if f: |
| 318 | fname = f |
| 319 | item = parts[1] |
| 320 | answer = find_function(item, fname) |
| 321 | return answer or failed |
| 322 | |
| 323 | def checkline(self, filename, lineno): |
| 324 | """Return line number of first line at or after input |
| 325 | argument such that if the input points to a 'def', the |
| 326 | returned line number is the first |
| 327 | non-blank/non-comment line to follow. If the input |
| 328 | points to a blank or comment line, return 0. At end |
| 329 | of file, also return 0.""" |
| 330 | |
| 331 | line = linecache.getline(filename, lineno) |
| 332 | if not line: |
| 333 | print 'End of file' |
| 334 | return 0 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 335 | line = line.strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 336 | # Don't allow setting breakpoint at a blank line |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 337 | if (not line or (line[0] == '#') or |
| 338 | (line[:3] == '"""') or line[:3] == "'''"): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 339 | print '*** Blank or comment' |
| 340 | return 0 |
| 341 | # When a file is read in and a breakpoint is at |
| 342 | # the 'def' statement, the system stops there at |
| 343 | # code parse time. We don't want that, so all breakpoints |
| 344 | # set at 'def' statements are moved one line onward |
| 345 | if line[:3] == 'def': |
| 346 | instr = '' |
| 347 | brackets = 0 |
| 348 | while 1: |
| 349 | skipone = 0 |
| 350 | for c in line: |
| 351 | if instr: |
| 352 | if skipone: |
| 353 | skipone = 0 |
| 354 | elif c == '\\': |
| 355 | skipone = 1 |
| 356 | elif c == instr: |
| 357 | instr = '' |
| 358 | elif c == '#': |
| 359 | break |
| 360 | elif c in ('"',"'"): |
| 361 | instr = c |
| 362 | elif c in ('(','{','['): |
| 363 | brackets = brackets + 1 |
| 364 | elif c in (')','}',']'): |
| 365 | brackets = brackets - 1 |
| 366 | lineno = lineno+1 |
| 367 | line = linecache.getline(filename, lineno) |
| 368 | if not line: |
| 369 | print 'end of file' |
| 370 | return 0 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 371 | line = line.strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 372 | if not line: continue # Blank line |
| 373 | if brackets <= 0 and line[0] not in ('#','"',"'"): |
| 374 | break |
| 375 | return lineno |
| 376 | |
| 377 | def do_enable(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 378 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 379 | for i in args: |
| 380 | bp = bdb.Breakpoint.bpbynumber[int(i)] |
| 381 | if bp: |
| 382 | bp.enable() |
| 383 | |
| 384 | def do_disable(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 385 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 386 | for i in args: |
| 387 | bp = bdb.Breakpoint.bpbynumber[int(i)] |
| 388 | if bp: |
| 389 | bp.disable() |
| 390 | |
| 391 | def do_condition(self, arg): |
| 392 | # arg is breakpoint number and condition |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 393 | args = arg.split(' ', 1) |
| 394 | bpnum = int(args[0].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 395 | try: |
| 396 | cond = args[1] |
| 397 | except: |
| 398 | cond = None |
| 399 | bp = bdb.Breakpoint.bpbynumber[bpnum] |
| 400 | if bp: |
| 401 | bp.cond = cond |
| 402 | if not cond: |
| 403 | print 'Breakpoint', bpnum, |
| 404 | print 'is now unconditional.' |
| 405 | |
| 406 | def do_ignore(self,arg): |
| 407 | """arg is bp number followed by ignore count.""" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 408 | args = arg.split() |
| 409 | bpnum = int(args[0].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 410 | try: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 411 | count = int(args[1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 412 | except: |
| 413 | count = 0 |
| 414 | bp = bdb.Breakpoint.bpbynumber[bpnum] |
| 415 | if bp: |
| 416 | bp.ignore = count |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 417 | if count > 0: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 418 | reply = 'Will ignore next ' |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 419 | if count > 1: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 420 | reply = reply + '%d crossings' % count |
| 421 | else: |
| 422 | reply = reply + '1 crossing' |
| 423 | print reply + ' of breakpoint %d.' % bpnum |
| 424 | else: |
| 425 | print 'Will stop next time breakpoint', |
| 426 | print bpnum, 'is reached.' |
| 427 | |
| 428 | def do_clear(self, arg): |
| 429 | """Three possibilities, tried in this order: |
| 430 | clear -> clear all breaks, ask for confirmation |
| 431 | clear file:lineno -> clear all breaks at file:lineno |
| 432 | clear bpno bpno ... -> clear breakpoints by number""" |
| 433 | if not arg: |
| 434 | try: |
| 435 | reply = raw_input('Clear all breaks? ') |
| 436 | except EOFError: |
| 437 | reply = 'no' |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 438 | reply = reply.strip().lower() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 439 | if reply in ('y', 'yes'): |
| 440 | self.clear_all_breaks() |
| 441 | return |
| 442 | if ':' in arg: |
| 443 | # Make sure it works for "clear C:\foo\bar.py:12" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 444 | i = arg.rfind(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 445 | filename = arg[:i] |
| 446 | arg = arg[i+1:] |
| 447 | try: |
| 448 | lineno = int(arg) |
| 449 | except: |
| 450 | err = "Invalid line number (%s)" % arg |
| 451 | else: |
| 452 | err = self.clear_break(filename, lineno) |
| 453 | if err: print '***', err |
| 454 | return |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 455 | numberlist = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 456 | for i in numberlist: |
| 457 | err = self.clear_bpbynumber(i) |
| 458 | if err: |
| 459 | print '***', err |
| 460 | else: |
| 461 | print 'Deleted breakpoint %s ' % (i,) |
| 462 | do_cl = do_clear # 'c' is already an abbreviation for 'continue' |
| 463 | |
| 464 | def do_where(self, arg): |
| 465 | self.print_stack_trace() |
| 466 | do_w = do_where |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 467 | do_bt = do_where |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 468 | |
| 469 | def do_up(self, arg): |
| 470 | if self.curindex == 0: |
| 471 | print '*** Oldest frame' |
| 472 | else: |
| 473 | self.curindex = self.curindex - 1 |
| 474 | self.curframe = self.stack[self.curindex][0] |
| 475 | self.print_stack_entry(self.stack[self.curindex]) |
| 476 | self.lineno = None |
| 477 | do_u = do_up |
| 478 | |
| 479 | def do_down(self, arg): |
| 480 | if self.curindex + 1 == len(self.stack): |
| 481 | print '*** Newest frame' |
| 482 | else: |
| 483 | self.curindex = self.curindex + 1 |
| 484 | self.curframe = self.stack[self.curindex][0] |
| 485 | self.print_stack_entry(self.stack[self.curindex]) |
| 486 | self.lineno = None |
| 487 | do_d = do_down |
| 488 | |
| 489 | def do_step(self, arg): |
| 490 | self.set_step() |
| 491 | return 1 |
| 492 | do_s = do_step |
| 493 | |
| 494 | def do_next(self, arg): |
| 495 | self.set_next(self.curframe) |
| 496 | return 1 |
| 497 | do_n = do_next |
| 498 | |
| 499 | def do_return(self, arg): |
| 500 | self.set_return(self.curframe) |
| 501 | return 1 |
| 502 | do_r = do_return |
| 503 | |
| 504 | def do_continue(self, arg): |
| 505 | self.set_continue() |
| 506 | return 1 |
| 507 | do_c = do_cont = do_continue |
| 508 | |
| 509 | def do_quit(self, arg): |
| 510 | self.set_quit() |
| 511 | return 1 |
| 512 | do_q = do_quit |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 513 | do_exit = do_quit |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 514 | |
| 515 | def do_args(self, arg): |
| 516 | f = self.curframe |
| 517 | co = f.f_code |
| 518 | dict = f.f_locals |
| 519 | n = co.co_argcount |
| 520 | if co.co_flags & 4: n = n+1 |
| 521 | if co.co_flags & 8: n = n+1 |
| 522 | for i in range(n): |
| 523 | name = co.co_varnames[i] |
| 524 | print name, '=', |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 525 | if name in dict: print dict[name] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 526 | else: print "*** undefined ***" |
| 527 | do_a = do_args |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 528 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 529 | def do_retval(self, arg): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 530 | if '__return__' in self.curframe.f_locals: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 531 | print self.curframe.f_locals['__return__'] |
| 532 | else: |
| 533 | print '*** Not yet returned!' |
| 534 | do_rv = do_retval |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 535 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 536 | def _getval(self, arg): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 537 | try: |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 538 | return eval(arg, self.curframe.f_globals, |
| 539 | self.curframe.f_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 540 | except: |
| 541 | t, v = sys.exc_info()[:2] |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 542 | if isinstance(t, str): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 543 | exc_type_name = t |
| 544 | else: exc_type_name = t.__name__ |
| 545 | print '***', exc_type_name + ':', `v` |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 546 | raise |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 547 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 548 | def do_p(self, arg): |
| 549 | try: |
| 550 | print repr(self._getval(arg)) |
| 551 | except: |
| 552 | pass |
| 553 | |
| 554 | def do_pp(self, arg): |
| 555 | try: |
| 556 | pprint.pprint(self._getval(arg)) |
| 557 | except: |
| 558 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 559 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 560 | def do_list(self, arg): |
| 561 | self.lastcmd = 'list' |
| 562 | last = None |
| 563 | if arg: |
| 564 | try: |
| 565 | x = eval(arg, {}, {}) |
| 566 | if type(x) == type(()): |
| 567 | first, last = x |
| 568 | first = int(first) |
| 569 | last = int(last) |
| 570 | if last < first: |
| 571 | # Assume it's a count |
| 572 | last = first + last |
| 573 | else: |
| 574 | first = max(1, int(x) - 5) |
| 575 | except: |
| 576 | print '*** Error in argument:', `arg` |
| 577 | return |
| 578 | elif self.lineno is None: |
| 579 | first = max(1, self.curframe.f_lineno - 5) |
| 580 | else: |
| 581 | first = self.lineno + 1 |
| 582 | if last is None: |
| 583 | last = first + 10 |
| 584 | filename = self.curframe.f_code.co_filename |
| 585 | breaklist = self.get_file_breaks(filename) |
| 586 | try: |
| 587 | for lineno in range(first, last+1): |
| 588 | line = linecache.getline(filename, lineno) |
| 589 | if not line: |
| 590 | print '[EOF]' |
| 591 | break |
| 592 | else: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 593 | s = `lineno`.rjust(3) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 594 | if len(s) < 4: s = s + ' ' |
| 595 | if lineno in breaklist: s = s + 'B' |
| 596 | else: s = s + ' ' |
| 597 | if lineno == self.curframe.f_lineno: |
| 598 | s = s + '->' |
| 599 | print s + '\t' + line, |
| 600 | self.lineno = lineno |
| 601 | except KeyboardInterrupt: |
| 602 | pass |
| 603 | do_l = do_list |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 604 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 605 | def do_whatis(self, arg): |
| 606 | try: |
| 607 | value = eval(arg, self.curframe.f_globals, |
| 608 | self.curframe.f_locals) |
| 609 | except: |
| 610 | t, v = sys.exc_info()[:2] |
| 611 | if type(t) == type(''): |
| 612 | exc_type_name = t |
| 613 | else: exc_type_name = t.__name__ |
| 614 | print '***', exc_type_name + ':', `v` |
| 615 | return |
| 616 | code = None |
| 617 | # Is it a function? |
| 618 | try: code = value.func_code |
| 619 | except: pass |
| 620 | if code: |
| 621 | print 'Function', code.co_name |
| 622 | return |
| 623 | # Is it an instance method? |
| 624 | try: code = value.im_func.func_code |
| 625 | except: pass |
| 626 | if code: |
| 627 | print 'Method', code.co_name |
| 628 | return |
| 629 | # None of the above... |
| 630 | print type(value) |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 631 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 632 | def do_alias(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 633 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 634 | if len(args) == 0: |
| 635 | keys = self.aliases.keys() |
| 636 | keys.sort() |
| 637 | for alias in keys: |
| 638 | print "%s = %s" % (alias, self.aliases[alias]) |
| 639 | return |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 640 | if args[0] in self.aliases and len(args) == 1: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 641 | print "%s = %s" % (args[0], self.aliases[args[0]]) |
| 642 | else: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 643 | self.aliases[args[0]] = ' '.join(args[1:]) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 644 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 645 | def do_unalias(self, arg): |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 646 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 647 | if len(args) == 0: return |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 648 | if args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 649 | del self.aliases[args[0]] |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 650 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 651 | # Print a traceback starting at the top stack frame. |
| 652 | # The most recently entered frame is printed last; |
| 653 | # this is different from dbx and gdb, but consistent with |
| 654 | # the Python interpreter's stack trace. |
| 655 | # It is also consistent with the up/down commands (which are |
| 656 | # compatible with dbx and gdb: up moves towards 'main()' |
| 657 | # and down moves towards the most recent stack frame). |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 658 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 659 | def print_stack_trace(self): |
| 660 | try: |
| 661 | for frame_lineno in self.stack: |
| 662 | self.print_stack_entry(frame_lineno) |
| 663 | except KeyboardInterrupt: |
| 664 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 665 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 666 | def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): |
| 667 | frame, lineno = frame_lineno |
| 668 | if frame is self.curframe: |
| 669 | print '>', |
| 670 | else: |
| 671 | print ' ', |
| 672 | print self.format_stack_entry(frame_lineno, prompt_prefix) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 673 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 674 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 675 | # Help methods (derived from pdb.doc) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 676 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 677 | def help_help(self): |
| 678 | self.help_h() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 679 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 680 | def help_h(self): |
| 681 | print """h(elp) |
| 682 | Without argument, print the list of available commands. |
| 683 | With a command name as argument, print help about that command |
| 684 | "help pdb" pipes the full documentation file to the $PAGER |
| 685 | "help exec" gives help on the ! command""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 686 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 687 | def help_where(self): |
| 688 | self.help_w() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 689 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 690 | def help_w(self): |
| 691 | print """w(here) |
| 692 | Print a stack trace, with the most recent frame at the bottom. |
| 693 | An arrow indicates the "current frame", which determines the |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 694 | context of most commands. 'bt' is an alias for this command.""" |
| 695 | |
| 696 | help_bt = help_w |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 697 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 698 | def help_down(self): |
| 699 | self.help_d() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 700 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 701 | def help_d(self): |
| 702 | print """d(own) |
| 703 | Move the current frame one level down in the stack trace |
| 704 | (to an older frame).""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 705 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 706 | def help_up(self): |
| 707 | self.help_u() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 708 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 709 | def help_u(self): |
| 710 | print """u(p) |
| 711 | Move the current frame one level up in the stack trace |
| 712 | (to a newer frame).""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 713 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 714 | def help_break(self): |
| 715 | self.help_b() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 716 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 717 | def help_b(self): |
| 718 | print """b(reak) ([file:]lineno | function) [, condition] |
| 719 | With a line number argument, set a break there in the current |
| 720 | file. With a function name, set a break at first executable line |
| 721 | of that function. Without argument, list all breaks. If a second |
| 722 | argument is present, it is a string specifying an expression |
| 723 | which must evaluate to true before the breakpoint is honored. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 724 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 725 | The line number may be prefixed with a filename and a colon, |
| 726 | to specify a breakpoint in another file (probably one that |
| 727 | hasn't been loaded yet). The file is searched for on sys.path; |
| 728 | the .py suffix may be omitted.""" |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 729 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 730 | def help_clear(self): |
| 731 | self.help_cl() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 732 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 733 | def help_cl(self): |
| 734 | print "cl(ear) filename:lineno" |
| 735 | print """cl(ear) [bpnumber [bpnumber...]] |
| 736 | With a space separated list of breakpoint numbers, clear |
| 737 | those breakpoints. Without argument, clear all breaks (but |
| 738 | first ask confirmation). With a filename:lineno argument, |
| 739 | clear all breaks at that line in that file. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 740 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 741 | Note that the argument is different from previous versions of |
| 742 | the debugger (in python distributions 1.5.1 and before) where |
| 743 | a linenumber was used instead of either filename:lineno or |
| 744 | breakpoint numbers.""" |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 745 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 746 | def help_tbreak(self): |
| 747 | print """tbreak same arguments as break, but breakpoint is |
| 748 | removed when first hit.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 749 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 750 | def help_enable(self): |
| 751 | print """enable bpnumber [bpnumber ...] |
| 752 | Enables the breakpoints given as a space separated list of |
| 753 | bp numbers.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 754 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 755 | def help_disable(self): |
| 756 | print """disable bpnumber [bpnumber ...] |
| 757 | Disables the breakpoints given as a space separated list of |
| 758 | bp numbers.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 759 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 760 | def help_ignore(self): |
| 761 | print """ignore bpnumber count |
| 762 | Sets the ignore count for the given breakpoint number. A breakpoint |
| 763 | becomes active when the ignore count is zero. When non-zero, the |
| 764 | count is decremented each time the breakpoint is reached and the |
| 765 | breakpoint is not disabled and any associated condition evaluates |
| 766 | to true.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 767 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 768 | def help_condition(self): |
| 769 | print """condition bpnumber str_condition |
| 770 | str_condition is a string specifying an expression which |
| 771 | must evaluate to true before the breakpoint is honored. |
| 772 | If str_condition is absent, any existing condition is removed; |
| 773 | i.e., the breakpoint is made unconditional.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 774 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 775 | def help_step(self): |
| 776 | self.help_s() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 777 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 778 | def help_s(self): |
| 779 | print """s(tep) |
| 780 | Execute the current line, stop at the first possible occasion |
| 781 | (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] | 782 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 783 | def help_next(self): |
| 784 | self.help_n() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 785 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 786 | def help_n(self): |
| 787 | print """n(ext) |
| 788 | Continue execution until the next line in the current function |
| 789 | is reached or it returns.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 790 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 791 | def help_return(self): |
| 792 | self.help_r() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 793 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 794 | def help_r(self): |
| 795 | print """r(eturn) |
| 796 | Continue execution until the current function returns.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 797 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 798 | def help_continue(self): |
| 799 | self.help_c() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 800 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 801 | def help_cont(self): |
| 802 | self.help_c() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 803 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 804 | def help_c(self): |
| 805 | print """c(ont(inue)) |
| 806 | Continue execution, only stop when a breakpoint is encountered.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 807 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 808 | def help_list(self): |
| 809 | self.help_l() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 810 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 811 | def help_l(self): |
| 812 | print """l(ist) [first [,last]] |
| 813 | List source code for the current file. |
| 814 | Without arguments, list 11 lines around the current line |
| 815 | or continue the previous listing. |
| 816 | With one argument, list 11 lines starting at that line. |
| 817 | With two arguments, list the given range; |
| 818 | 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] | 819 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 820 | def help_args(self): |
| 821 | self.help_a() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 822 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 823 | def help_a(self): |
| 824 | print """a(rgs) |
| 825 | Print the arguments of the current function.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 826 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 827 | def help_p(self): |
| 828 | print """p expression |
| 829 | Print the value of the expression.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 830 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 831 | def help_pp(self): |
| 832 | print """pp expression |
| 833 | Pretty-print the value of the expression.""" |
| 834 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 835 | def help_exec(self): |
| 836 | print """(!) statement |
| 837 | Execute the (one-line) statement in the context of |
| 838 | the current stack frame. |
| 839 | The exclamation point can be omitted unless the first word |
| 840 | of the statement resembles a debugger command. |
| 841 | To assign to a global variable you must always prefix the |
| 842 | command with a 'global' command, e.g.: |
| 843 | (Pdb) global list_options; list_options = ['-l'] |
| 844 | (Pdb)""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 845 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 846 | def help_quit(self): |
| 847 | self.help_q() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 848 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 849 | def help_q(self): |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 850 | print """q(uit) or exit - Quit from the debugger. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 851 | The program being executed is aborted.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 853 | help_exit = help_q |
| 854 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 855 | def help_whatis(self): |
| 856 | print """whatis arg |
| 857 | Prints the type of the argument.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 858 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 859 | def help_EOF(self): |
| 860 | print """EOF |
| 861 | Handles the receipt of EOF as a command.""" |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 862 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 863 | def help_alias(self): |
| 864 | print """alias [name [command [parameter parameter ...] ]] |
| 865 | Creates an alias called 'name' the executes 'command'. The command |
| 866 | must *not* be enclosed in quotes. Replaceable parameters are |
| 867 | indicated by %1, %2, and so on, while %* is replaced by all the |
| 868 | parameters. If no command is given, the current alias for name |
| 869 | is shown. If no name is given, all aliases are listed. |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 870 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 871 | Aliases may be nested and can contain anything that can be |
| 872 | legally typed at the pdb prompt. Note! You *can* override |
| 873 | internal pdb commands with aliases! Those internal commands |
| 874 | are then hidden until the alias is removed. Aliasing is recursively |
| 875 | applied to the first word of the command line; all other words |
| 876 | in the line are left alone. |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 877 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 878 | Some useful aliases (especially when placed in the .pdbrc file) are: |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 879 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 880 | #Print instance variables (usage "pi classInst") |
| 881 | 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] | 882 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 883 | #Print instance variables in self |
| 884 | alias ps pi self |
| 885 | """ |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 886 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 887 | def help_unalias(self): |
| 888 | print """unalias name |
| 889 | Deletes the specified alias.""" |
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 help_pdb(self): |
| 892 | help() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 893 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 894 | def lookupmodule(self, filename): |
| 895 | """Helper function for break/clear parsing -- may be overridden.""" |
| 896 | root, ext = os.path.splitext(filename) |
| 897 | if ext == '': |
| 898 | filename = filename + '.py' |
| 899 | if os.path.isabs(filename): |
| 900 | return filename |
| 901 | for dirname in sys.path: |
| 902 | while os.path.islink(dirname): |
| 903 | dirname = os.readlink(dirname) |
| 904 | fullname = os.path.join(dirname, filename) |
| 905 | if os.path.exists(fullname): |
| 906 | return fullname |
| 907 | return None |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 908 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 909 | # Simplified interface |
| 910 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 911 | def run(statement, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 912 | Pdb().run(statement, globals, locals) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 913 | |
| 914 | def runeval(expression, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 915 | return Pdb().runeval(expression, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 916 | |
| 917 | def runctx(statement, globals, locals): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 918 | # B/W compatibility |
| 919 | run(statement, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 920 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 921 | def runcall(*args): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 922 | return apply(Pdb().runcall, args) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 923 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 924 | def set_trace(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 925 | Pdb().set_trace() |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 926 | |
| 927 | # Post-Mortem interface |
| 928 | |
| 929 | def post_mortem(t): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 930 | p = Pdb() |
| 931 | p.reset() |
| 932 | while t.tb_next is not None: |
| 933 | t = t.tb_next |
| 934 | p.interaction(t.tb_frame, t) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 935 | |
| 936 | def pm(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 937 | post_mortem(sys.last_traceback) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 938 | |
| 939 | |
| 940 | # Main program for testing |
| 941 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 942 | TESTCMD = 'import x; x.main()' |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 944 | def test(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 945 | run(TESTCMD) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 946 | |
| 947 | # print help |
| 948 | def help(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 949 | for dirname in sys.path: |
| 950 | fullname = os.path.join(dirname, 'pdb.doc') |
| 951 | if os.path.exists(fullname): |
| 952 | sts = os.system('${PAGER-more} '+fullname) |
| 953 | if sts: print '*** Pager exit status:', sts |
| 954 | break |
| 955 | else: |
| 956 | print 'Sorry, can\'t find the help file "pdb.doc"', |
| 957 | print 'along the Python search path' |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 959 | mainmodule = '' |
| 960 | mainpyfile = '' |
| 961 | |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 962 | # When invoked as main program, invoke the debugger on a script |
| 963 | if __name__=='__main__': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 964 | if not sys.argv[1:]: |
| 965 | print "usage: pdb.py scriptfile [arg] ..." |
| 966 | sys.exit(2) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 967 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 968 | mainpyfile = filename = sys.argv[1] # Get script filename |
| 969 | if not os.path.exists(filename): |
| 970 | print 'Error:', `filename`, 'does not exist' |
| 971 | sys.exit(1) |
| 972 | mainmodule = os.path.basename(filename) |
| 973 | del sys.argv[0] # Hide "pdb.py" from argument list |
Guido van Rossum | ec577d5 | 1996-09-10 17:39:34 +0000 | [diff] [blame] | 974 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 975 | # Insert script directory in front of module search path |
| 976 | sys.path.insert(0, os.path.dirname(filename)) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 977 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 978 | run('execfile(' + `filename` + ')') |