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 | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 3 | # pdb.py -- finally, 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 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 14 | |
| 15 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 16 | # Interaction prompt line will separate file and call info from code |
| 17 | # text using value of line_prefix string. A newline and arrow may |
| 18 | # be to your liking. You can set it once pdb is imported using the |
| 19 | # command "pdb.line_prefix = '\n% '". |
| 20 | # line_prefix = ': ' # Use this to get the old situation back |
| 21 | line_prefix = '\n-> ' # Probably a better default |
| 22 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 23 | class Pdb(bdb.Bdb, cmd.Cmd): |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 5ef74b8 | 1993-06-23 11:55:24 +0000 | [diff] [blame] | 25 | def __init__(self): |
| 26 | bdb.Bdb.__init__(self) |
| 27 | cmd.Cmd.__init__(self) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 28 | self.prompt = '(Pdb) ' |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 29 | # Try to load readline if it exists |
| 30 | try: |
| 31 | import readline |
| 32 | except ImportError: |
| 33 | pass |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 34 | |
| 35 | def reset(self): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 36 | bdb.Bdb.reset(self) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 37 | self.forget() |
| 38 | |
| 39 | def forget(self): |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 40 | self.lineno = None |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 41 | self.stack = [] |
Guido van Rossum | 7ac1c81 | 1992-01-16 13:55:21 +0000 | [diff] [blame] | 42 | self.curindex = 0 |
| 43 | self.curframe = None |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 44 | |
| 45 | def setup(self, f, t): |
| 46 | self.forget() |
| 47 | self.stack, self.curindex = self.get_stack(f, t) |
Guido van Rossum | 7ac1c81 | 1992-01-16 13:55:21 +0000 | [diff] [blame] | 48 | self.curframe = self.stack[self.curindex][0] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 50 | # Override Bdb methods (except user_call, for now) |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 52 | def user_line(self, frame): |
| 53 | # This function is called when we stop or break at this line |
| 54 | self.interaction(frame, None) |
Guido van Rossum | 7ac1c81 | 1992-01-16 13:55:21 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 56 | def user_return(self, frame, return_value): |
| 57 | # This function is called when a return trap is set here |
| 58 | frame.f_locals['__return__'] = return_value |
| 59 | print '--Return--' |
| 60 | self.interaction(frame, None) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 62 | def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): |
| 63 | # This function is called if an exception occurs, |
| 64 | # but only if we are to stop at or just below this level |
| 65 | frame.f_locals['__exception__'] = exc_type, exc_value |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 66 | if type(exc_type) == type(''): |
| 67 | exc_type_name = exc_type |
| 68 | else: exc_type_name = exc_type.__name__ |
| 69 | print exc_type_name + ':', repr.repr(exc_value) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 70 | self.interaction(frame, exc_traceback) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 72 | # General interaction function |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 74 | def interaction(self, frame, traceback): |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 75 | self.setup(frame, traceback) |
Guido van Rossum | b6aa92e | 1995-02-03 12:50:04 +0000 | [diff] [blame] | 76 | self.print_stack_entry(self.stack[self.curindex]) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 77 | self.cmdloop() |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 78 | self.forget() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 80 | def default(self, line): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 81 | if line[:1] == '!': line = line[1:] |
| 82 | locals = self.curframe.f_locals |
| 83 | globals = self.curframe.f_globals |
| 84 | try: |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 85 | code = compile(line + '\n', '<stdin>', 'single') |
Guido van Rossum | ec8fd94 | 1995-08-07 20:16:05 +0000 | [diff] [blame] | 86 | exec code in globals, locals |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 87 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 88 | t, v = sys.exc_info()[:2] |
| 89 | if type(t) == type(''): |
| 90 | exc_type_name = t |
| 91 | else: exc_type_name = t.__name__ |
| 92 | print '***', exc_type_name + ':', v |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 93 | |
| 94 | # Command definitions, called by cmdloop() |
| 95 | # The argument is the remaining string on the command line |
| 96 | # Return true to exit from the command loop |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 98 | do_h = cmd.Cmd.do_help |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 100 | def do_break(self, arg): |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 101 | # break [ ([filename:]lineno | function) [, "condition"] ] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 102 | if not arg: |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 103 | print self.get_all_breaks() # XXX |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 104 | return |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 105 | # parse arguments; comma has lowest precendence |
| 106 | # and cannot occur in filename |
| 107 | filename = None |
| 108 | lineno = None |
| 109 | cond = None |
| 110 | comma = string.find(arg, ',') |
| 111 | if comma > 0: |
| 112 | # parse stuff after comma: "condition" |
| 113 | cond = string.lstrip(arg[comma+1:]) |
| 114 | arg = string.rstrip(arg[:comma]) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | try: |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 116 | cond = eval( |
| 117 | cond, |
| 118 | self.curframe.f_globals, |
| 119 | self.curframe.f_locals) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | except: |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 121 | print '*** Could not eval condition:', cond |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 122 | return |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 123 | # parse stuff before comma: [filename:]lineno | function |
| 124 | colon = string.rfind(arg, ':') |
| 125 | if colon >= 0: |
| 126 | filename = string.rstrip(arg[:colon]) |
| 127 | filename = self.lookupmodule(filename) |
| 128 | arg = string.lstrip(arg[colon+1:]) |
| 129 | try: |
| 130 | lineno = int(arg) |
| 131 | except ValueError, msg: |
| 132 | print '*** Bad lineno:', arg |
| 133 | return |
| 134 | else: |
| 135 | # no colon; can be lineno or function |
| 136 | try: |
| 137 | lineno = int(arg) |
| 138 | except ValueError: |
| 139 | try: |
| 140 | func = eval(arg, |
| 141 | self.curframe.f_globals, |
| 142 | self.curframe.f_locals) |
| 143 | except: |
| 144 | print '*** Could not eval argument:', |
| 145 | print arg |
| 146 | return |
| 147 | try: |
| 148 | if hasattr(func, 'im_func'): |
| 149 | func = func.im_func |
| 150 | code = func.func_code |
| 151 | except: |
| 152 | print '*** The specified object', |
| 153 | print 'is not a function', arg |
| 154 | return |
| 155 | lineno = code.co_firstlineno |
| 156 | if not filename: |
| 157 | filename = code.co_filename |
| 158 | # supply default filename if necessary |
| 159 | if not filename: |
| 160 | filename = self.curframe.f_code.co_filename |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 161 | # now set the break point |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 162 | err = self.set_break(filename, lineno, cond) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 163 | if err: print '***', err |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 165 | do_b = do_break |
| 166 | |
| 167 | def do_clear(self, arg): |
| 168 | if not arg: |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 169 | try: |
| 170 | reply = raw_input('Clear all breaks? ') |
| 171 | except EOFError: |
| 172 | reply = 'no' |
| 173 | reply = string.lower(string.strip(reply)) |
| 174 | if reply in ('y', 'yes'): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 175 | self.clear_all_breaks() |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 176 | return |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 177 | filename = None |
| 178 | colon = string.rfind(arg, ':') |
| 179 | if colon >= 0: |
| 180 | filename = string.rstrip(arg[:colon]) |
| 181 | filename = self.lookupmodule(filename) |
| 182 | arg = string.lstrip(arg[colon+1:]) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 183 | try: |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 184 | lineno = int(arg) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 185 | except: |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 186 | print '*** Bad lineno:', `arg` |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 187 | return |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 188 | if not filename: |
| 189 | filename = self.curframe.f_code.co_filename |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 190 | err = self.clear_break(filename, lineno) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 191 | if err: print '***', err |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 192 | do_cl = do_clear # 'c' is already an abbreviation for 'continue' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 193 | |
| 194 | def do_where(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 195 | self.print_stack_trace() |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 196 | do_w = do_where |
| 197 | |
| 198 | def do_up(self, arg): |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 199 | if self.curindex == 0: |
| 200 | print '*** Oldest frame' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 201 | else: |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 202 | self.curindex = self.curindex - 1 |
| 203 | self.curframe = self.stack[self.curindex][0] |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 204 | self.print_stack_entry(self.stack[self.curindex]) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 205 | self.lineno = None |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 206 | do_u = do_up |
| 207 | |
| 208 | def do_down(self, arg): |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 209 | if self.curindex + 1 == len(self.stack): |
| 210 | print '*** Newest frame' |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 211 | else: |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 212 | self.curindex = self.curindex + 1 |
| 213 | self.curframe = self.stack[self.curindex][0] |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 214 | self.print_stack_entry(self.stack[self.curindex]) |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 215 | self.lineno = None |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 216 | do_d = do_down |
| 217 | |
| 218 | def do_step(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 219 | self.set_step() |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 220 | return 1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 221 | do_s = do_step |
| 222 | |
| 223 | def do_next(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 224 | self.set_next(self.curframe) |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 225 | return 1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 226 | do_n = do_next |
| 227 | |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 228 | def do_return(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 229 | self.set_return(self.curframe) |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 230 | return 1 |
| 231 | do_r = do_return |
| 232 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 233 | def do_continue(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 234 | self.set_continue() |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 235 | return 1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 236 | do_c = do_cont = do_continue |
| 237 | |
| 238 | def do_quit(self, arg): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 239 | self.set_quit() |
| 240 | return 1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 241 | do_q = do_quit |
| 242 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 243 | def do_args(self, arg): |
Guido van Rossum | 46c86bb | 1998-02-25 20:50:32 +0000 | [diff] [blame] | 244 | f = self.curframe |
| 245 | co = f.f_code |
| 246 | dict = f.f_locals |
| 247 | n = co.co_argcount |
| 248 | if co.co_flags & 4: n = n+1 |
| 249 | if co.co_flags & 8: n = n+1 |
| 250 | for i in range(n): |
| 251 | name = co.co_varnames[i] |
| 252 | print name, '=', |
| 253 | if dict.has_key(name): print dict[name] |
| 254 | else: print "*** undefined ***" |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 255 | do_a = do_args |
| 256 | |
| 257 | def do_retval(self, arg): |
| 258 | if self.curframe.f_locals.has_key('__return__'): |
| 259 | print self.curframe.f_locals['__return__'] |
| 260 | else: |
| 261 | print '*** Not yet returned!' |
| 262 | do_rv = do_retval |
| 263 | |
| 264 | def do_p(self, arg): |
| 265 | try: |
| 266 | value = eval(arg, self.curframe.f_globals, \ |
| 267 | self.curframe.f_locals) |
| 268 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 269 | t, v = sys.exc_info()[:2] |
| 270 | if type(t) == type(''): |
| 271 | exc_type_name = t |
| 272 | else: exc_type_name = t.__name__ |
| 273 | print '***', exc_type_name + ':', `v` |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 274 | return |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 276 | print `value` |
| 277 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 278 | def do_list(self, arg): |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 279 | self.lastcmd = 'list' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 280 | last = None |
| 281 | if arg: |
| 282 | try: |
| 283 | x = eval(arg, {}, {}) |
| 284 | if type(x) == type(()): |
| 285 | first, last = x |
| 286 | first = int(first) |
| 287 | last = int(last) |
| 288 | if last < first: |
| 289 | # Assume it's a count |
| 290 | last = first + last |
| 291 | else: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 292 | first = max(1, int(x) - 5) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 293 | except: |
| 294 | print '*** Error in argument:', `arg` |
| 295 | return |
| 296 | elif self.lineno is None: |
| 297 | first = max(1, self.curframe.f_lineno - 5) |
| 298 | else: |
| 299 | first = self.lineno + 1 |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 300 | if last == None: |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 301 | last = first + 10 |
| 302 | filename = self.curframe.f_code.co_filename |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 303 | breaklist = self.get_file_breaks(filename) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 304 | try: |
| 305 | for lineno in range(first, last+1): |
| 306 | line = linecache.getline(filename, lineno) |
| 307 | if not line: |
| 308 | print '[EOF]' |
| 309 | break |
| 310 | else: |
| 311 | s = string.rjust(`lineno`, 3) |
| 312 | if len(s) < 4: s = s + ' ' |
| 313 | if lineno in breaklist: s = s + 'B' |
| 314 | else: s = s + ' ' |
| 315 | if lineno == self.curframe.f_lineno: |
| 316 | s = s + '->' |
| 317 | print s + '\t' + line, |
| 318 | self.lineno = lineno |
| 319 | except KeyboardInterrupt: |
| 320 | pass |
| 321 | do_l = do_list |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 322 | |
| 323 | def do_whatis(self, arg): |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 324 | try: |
| 325 | value = eval(arg, self.curframe.f_globals, \ |
| 326 | self.curframe.f_locals) |
| 327 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 328 | t, v = sys.exc_info()[:2] |
| 329 | if type(t) == type(''): |
| 330 | exc_type_name = t |
| 331 | else: exc_type_name = t.__name__ |
| 332 | print '***', exc_type_name + ':', `v` |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 333 | return |
| 334 | code = None |
| 335 | # Is it a function? |
| 336 | try: code = value.func_code |
| 337 | except: pass |
| 338 | if code: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 339 | print 'Function', code.co_name |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 340 | return |
| 341 | # Is it an instance method? |
| 342 | try: code = value.im_func.func_code |
| 343 | except: pass |
| 344 | if code: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 345 | print 'Method', code.co_name |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 346 | return |
| 347 | # None of the above... |
| 348 | print type(value) |
Guido van Rossum | b914257 | 1992-01-12 23:32:55 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 350 | # Print a traceback starting at the top stack frame. |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 351 | # The most recently entered frame is printed last; |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 352 | # this is different from dbx and gdb, but consistent with |
| 353 | # the Python interpreter's stack trace. |
| 354 | # It is also consistent with the up/down commands (which are |
| 355 | # compatible with dbx and gdb: up moves towards 'main()' |
| 356 | # and down moves towards the most recent stack frame). |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 358 | def print_stack_trace(self): |
| 359 | try: |
| 360 | for frame_lineno in self.stack: |
| 361 | self.print_stack_entry(frame_lineno) |
| 362 | except KeyboardInterrupt: |
| 363 | pass |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | b6aa92e | 1995-02-03 12:50:04 +0000 | [diff] [blame] | 365 | def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 366 | frame, lineno = frame_lineno |
| 367 | if frame is self.curframe: |
| 368 | print '>', |
| 369 | else: |
| 370 | print ' ', |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 371 | print self.format_stack_entry(frame_lineno, prompt_prefix) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 372 | |
| 373 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 374 | # Help methods (derived from pdb.doc) |
| 375 | |
| 376 | def help_help(self): |
| 377 | self.help_h() |
| 378 | |
| 379 | def help_h(self): |
| 380 | print """h(elp) |
| 381 | Without argument, print the list of available commands. |
| 382 | With a command name as argument, print help about that command |
| 383 | "help pdb" pipes the full documentation file to the $PAGER |
| 384 | "help exec" gives help on the ! command""" |
| 385 | |
| 386 | def help_where(self): |
| 387 | self.help_w() |
| 388 | |
| 389 | def help_w(self): |
| 390 | print """w(here) |
| 391 | Print a stack trace, with the most recent frame at the bottom. |
| 392 | An arrow indicates the "current frame", which determines the |
| 393 | context of most commands.""" |
| 394 | |
| 395 | def help_down(self): |
| 396 | self.help_d() |
| 397 | |
| 398 | def help_d(self): |
| 399 | print """d(own) |
| 400 | Move the current frame one level down in the stack trace |
| 401 | (to an older frame).""" |
| 402 | |
| 403 | def help_up(self): |
| 404 | self.help_u() |
| 405 | |
| 406 | def help_u(self): |
| 407 | print """u(p) |
| 408 | Move the current frame one level up in the stack trace |
| 409 | (to a newer frame).""" |
| 410 | |
| 411 | def help_break(self): |
| 412 | self.help_b() |
| 413 | |
| 414 | def help_b(self): |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 415 | print """b(reak) ([file:]lineno | function) [, "condition"] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 416 | With a line number argument, set a break there in the current |
| 417 | file. With a function name, set a break at the entry of that |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 418 | function. Without argument, list all breaks. If a second |
| 419 | argument is present, it is a string specifying an expression |
| 420 | which must evaluate to true before the breakpoint is honored. |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 421 | |
| 422 | The line number may be prefixed with a filename and a colon, |
| 423 | to specify a breakpoint in another file (probably one that |
Guido van Rossum | 1f00eed | 1998-07-22 13:35:21 +0000 | [diff] [blame] | 424 | hasn't been loaded yet). The file is searched on sys.path; |
| 425 | the .py suffix may be omitted.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 426 | |
| 427 | def help_clear(self): |
| 428 | self.help_cl() |
| 429 | |
| 430 | def help_cl(self): |
Guido van Rossum | 1f00eed | 1998-07-22 13:35:21 +0000 | [diff] [blame] | 431 | print """cl(ear) [file:][lineno] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 432 | With a line number argument, clear that break in the current file. |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 433 | Without argument, clear all breaks (but first ask confirmation). |
| 434 | |
| 435 | The line number may be prefixed with a filename and a colon, |
| 436 | to specify a breakpoint in another file (probably one that |
Guido van Rossum | 1f00eed | 1998-07-22 13:35:21 +0000 | [diff] [blame] | 437 | hasn't been loaded yet). The file is searched on sys.path; |
| 438 | the .py suffix may be omitted.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 439 | |
| 440 | def help_step(self): |
| 441 | self.help_s() |
| 442 | |
| 443 | def help_s(self): |
| 444 | print """s(tep) |
| 445 | Execute the current line, stop at the first possible occasion |
| 446 | (either in a function that is called or in the current function).""" |
| 447 | |
| 448 | def help_next(self): |
| 449 | self.help_n() |
| 450 | |
| 451 | def help_n(self): |
| 452 | print """n(ext) |
| 453 | Continue execution until the next line in the current function |
| 454 | is reached or it returns.""" |
| 455 | |
| 456 | def help_return(self): |
| 457 | self.help_r() |
| 458 | |
| 459 | def help_r(self): |
| 460 | print """r(eturn) |
| 461 | Continue execution until the current function returns.""" |
| 462 | |
| 463 | def help_continue(self): |
| 464 | self.help_c() |
| 465 | |
| 466 | def help_cont(self): |
| 467 | self.help_c() |
| 468 | |
| 469 | def help_c(self): |
| 470 | print """c(ont(inue)) |
| 471 | Continue execution, only stop when a breakpoint is encountered.""" |
| 472 | |
| 473 | def help_list(self): |
| 474 | self.help_l() |
| 475 | |
| 476 | def help_l(self): |
| 477 | print """l(ist) [first [,last]] |
| 478 | List source code for the current file. |
| 479 | Without arguments, list 11 lines around the current line |
| 480 | or continue the previous listing. |
| 481 | With one argument, list 11 lines starting at that line. |
| 482 | With two arguments, list the given range; |
| 483 | if the second argument is less than the first, it is a count.""" |
| 484 | |
| 485 | def help_args(self): |
| 486 | self.help_a() |
| 487 | |
| 488 | def help_a(self): |
| 489 | print """a(rgs) |
Guido van Rossum | 46c86bb | 1998-02-25 20:50:32 +0000 | [diff] [blame] | 490 | Print the arguments of the current function.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 491 | |
| 492 | def help_p(self): |
| 493 | print """p expression |
| 494 | Print the value of the expression.""" |
| 495 | |
| 496 | def help_exec(self): |
| 497 | print """(!) statement |
| 498 | Execute the (one-line) statement in the context of |
| 499 | the current stack frame. |
| 500 | The exclamation point can be omitted unless the first word |
| 501 | of the statement resembles a debugger command. |
| 502 | To assign to a global variable you must always prefix the |
| 503 | command with a 'global' command, e.g.: |
| 504 | (Pdb) global list_options; list_options = ['-l'] |
| 505 | (Pdb)""" |
| 506 | |
| 507 | def help_quit(self): |
| 508 | self.help_q() |
| 509 | |
| 510 | def help_q(self): |
| 511 | print """q(uit) Quit from the debugger. |
| 512 | The program being executed is aborted.""" |
| 513 | |
| 514 | def help_pdb(self): |
| 515 | help() |
| 516 | |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 517 | # Helper function for break/clear parsing -- may be overridden |
| 518 | |
| 519 | def lookupmodule(self, filename): |
| 520 | if filename == mainmodule: |
| 521 | return mainpyfile |
Guido van Rossum | 1f00eed | 1998-07-22 13:35:21 +0000 | [diff] [blame] | 522 | root, ext = os.path.splitext(filename) |
| 523 | if ext == '': |
| 524 | filename = filename + '.py' |
| 525 | if os.path.isabs(filename): |
| 526 | return filename |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 527 | for dirname in sys.path: |
| 528 | fullname = os.path.join(dirname, filename) |
| 529 | if os.path.exists(fullname): |
| 530 | return fullname |
| 531 | print 'Warning:', `filename`, 'not found from sys.path' |
| 532 | return filename |
| 533 | |
| 534 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 535 | # Simplified interface |
| 536 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 537 | def run(statement, globals=None, locals=None): |
| 538 | Pdb().run(statement, globals, locals) |
| 539 | |
| 540 | def runeval(expression, globals=None, locals=None): |
| 541 | return Pdb().runeval(expression, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 542 | |
| 543 | def runctx(statement, globals, locals): |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 544 | # B/W compatibility |
| 545 | run(statement, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 547 | def runcall(*args): |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 548 | return apply(Pdb().runcall, args) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 550 | def set_trace(): |
| 551 | Pdb().set_trace() |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 552 | |
| 553 | # Post-Mortem interface |
| 554 | |
| 555 | def post_mortem(t): |
Guido van Rossum | 5ef74b8 | 1993-06-23 11:55:24 +0000 | [diff] [blame] | 556 | p = Pdb() |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 557 | p.reset() |
| 558 | while t.tb_next <> None: t = t.tb_next |
| 559 | p.interaction(t.tb_frame, t) |
| 560 | |
| 561 | def pm(): |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 562 | post_mortem(sys.last_traceback) |
| 563 | |
| 564 | |
| 565 | # Main program for testing |
| 566 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 567 | TESTCMD = 'import x; x.main()' |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 568 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 569 | def test(): |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 570 | run(TESTCMD) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 571 | |
| 572 | # print help |
| 573 | def help(): |
| 574 | for dirname in sys.path: |
| 575 | fullname = os.path.join(dirname, 'pdb.doc') |
| 576 | if os.path.exists(fullname): |
| 577 | sts = os.system('${PAGER-more} '+fullname) |
| 578 | if sts: print '*** Pager exit status:', sts |
| 579 | break |
| 580 | else: |
| 581 | print 'Sorry, can\'t find the help file "pdb.doc"', |
| 582 | print 'along the Python search path' |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 583 | |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 584 | mainmodule = '' |
| 585 | mainpyfile = '' |
| 586 | |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 587 | # When invoked as main program, invoke the debugger on a script |
| 588 | if __name__=='__main__': |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 589 | global mainmodule, mainpyfile |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 590 | if not sys.argv[1:]: |
| 591 | print "usage: pdb.py scriptfile [arg] ..." |
| 592 | sys.exit(2) |
| 593 | |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 594 | mainpyfile = filename = sys.argv[1] # Get script filename |
| 595 | if not os.path.exists(filename): |
| 596 | print 'Error:', `filename`, 'does not exist' |
| 597 | sys.exit(1) |
| 598 | mainmodule = os.path.basename(filename) |
Guido van Rossum | ec577d5 | 1996-09-10 17:39:34 +0000 | [diff] [blame] | 599 | del sys.argv[0] # Hide "pdb.py" from argument list |
| 600 | |
| 601 | # Insert script directory in front of module search path |
| 602 | sys.path.insert(0, os.path.dirname(filename)) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 603 | |
| 604 | run('execfile(' + `filename` + ')', {'__name__': '__main__'}) |