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