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