Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 1 | # A generic Python debugger base class. |
| 2 | # This class takes care of details of the trace facility; |
| 3 | # a derived class should implement user interaction. |
| 4 | # There are two debuggers based upon this: |
| 5 | # 'pdb', a text-oriented debugger not unlike dbx or gdb; |
| 6 | # and 'wdb', a window-oriented debugger. |
| 7 | # And of course... you can roll your own! |
| 8 | |
| 9 | import sys |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 10 | import types |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 11 | |
| 12 | BdbQuit = 'bdb.BdbQuit' # Exception to give up completely |
| 13 | |
| 14 | |
| 15 | class Bdb: # Basic Debugger |
| 16 | |
Guido van Rossum | 5ef74b8 | 1993-06-23 11:55:24 +0000 | [diff] [blame] | 17 | def __init__(self): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 18 | self.breaks = {} |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 19 | |
| 20 | def reset(self): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 21 | import linecache |
| 22 | linecache.checkcache() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 23 | self.botframe = None |
| 24 | self.stopframe = None |
| 25 | self.returnframe = None |
| 26 | self.quitting = 0 |
| 27 | |
| 28 | def trace_dispatch(self, frame, event, arg): |
| 29 | if self.quitting: |
| 30 | return # None |
| 31 | if event == 'line': |
| 32 | return self.dispatch_line(frame) |
| 33 | if event == 'call': |
| 34 | return self.dispatch_call(frame, arg) |
| 35 | if event == 'return': |
| 36 | return self.dispatch_return(frame, arg) |
| 37 | if event == 'exception': |
| 38 | return self.dispatch_exception(frame, arg) |
| 39 | print 'bdb.Bdb.dispatch: unknown debugging event:', `event` |
| 40 | return self.trace_dispatch |
| 41 | |
| 42 | def dispatch_line(self, frame): |
| 43 | if self.stop_here(frame) or self.break_here(frame): |
| 44 | self.user_line(frame) |
| 45 | if self.quitting: raise BdbQuit |
| 46 | return self.trace_dispatch |
| 47 | |
| 48 | def dispatch_call(self, frame, arg): |
Guido van Rossum | 75bb54c | 1998-09-28 15:33:38 +0000 | [diff] [blame] | 49 | # XXX 'arg' is no longer used |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 50 | if self.botframe is None: |
| 51 | # First call of dispatch since reset() |
| 52 | self.botframe = frame |
| 53 | return self.trace_dispatch |
| 54 | if not (self.stop_here(frame) or self.break_anywhere(frame)): |
| 55 | # No need to trace this function |
| 56 | return # None |
| 57 | self.user_call(frame, arg) |
| 58 | if self.quitting: raise BdbQuit |
| 59 | return self.trace_dispatch |
| 60 | |
| 61 | def dispatch_return(self, frame, arg): |
| 62 | if self.stop_here(frame) or frame == self.returnframe: |
| 63 | self.user_return(frame, arg) |
| 64 | if self.quitting: raise BdbQuit |
| 65 | |
| 66 | def dispatch_exception(self, frame, arg): |
| 67 | if self.stop_here(frame): |
| 68 | self.user_exception(frame, arg) |
| 69 | if self.quitting: raise BdbQuit |
| 70 | return self.trace_dispatch |
| 71 | |
| 72 | # Normally derived classes don't override the following |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 73 | # methods, but they may if they want to redefine the |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 74 | # definition of stopping and breakpoints. |
| 75 | |
| 76 | def stop_here(self, frame): |
| 77 | if self.stopframe is None: |
| 78 | return 1 |
| 79 | if frame is self.stopframe: |
| 80 | return 1 |
| 81 | while frame is not None and frame is not self.stopframe: |
| 82 | if frame is self.botframe: |
| 83 | return 1 |
| 84 | frame = frame.f_back |
| 85 | return 0 |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 87 | def break_here(self, frame): |
Guido van Rossum | 6683617 | 1997-07-11 13:43:31 +0000 | [diff] [blame] | 88 | filename=frame.f_code.co_filename |
Guido van Rossum | ff02e1d | 1997-07-11 13:42:50 +0000 | [diff] [blame] | 89 | if not self.breaks.has_key(filename): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 90 | return 0 |
Guido van Rossum | ff02e1d | 1997-07-11 13:42:50 +0000 | [diff] [blame] | 91 | lineno=frame.f_lineno |
| 92 | if not lineno in self.breaks[filename]: |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 93 | return 0 |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 94 | # flag says ok to delete temp. bp |
| 95 | (bp, flag) = effective(filename, lineno, frame) |
| 96 | if bp: |
| 97 | self.currentbp = bp.number |
| 98 | if (flag and bp.temporary): |
| 99 | self.do_delete(str(bp.number)) |
| 100 | return 1 |
| 101 | else: |
| 102 | return 0 |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 103 | |
| 104 | def break_anywhere(self, frame): |
| 105 | return self.breaks.has_key(frame.f_code.co_filename) |
| 106 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 107 | # Derived classes should override the user_* methods |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 108 | # to gain control. |
| 109 | |
| 110 | def user_call(self, frame, argument_list): |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 111 | # This method is called when there is the remote possibility |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 112 | # that we ever need to stop in this function |
| 113 | pass |
| 114 | |
| 115 | def user_line(self, frame): |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 116 | # This method is called when we stop or break at this line |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 117 | pass |
| 118 | |
| 119 | def user_return(self, frame, return_value): |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 120 | # This method is called when a return trap is set here |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 121 | pass |
| 122 | |
| 123 | def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 124 | # This method is called if an exception occurs, |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 125 | # but only if we are to stop at or just below this level |
| 126 | pass |
| 127 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 128 | # Derived classes and clients can call the following methods |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 129 | # to affect the stepping state. |
| 130 | |
| 131 | def set_step(self): |
| 132 | # Stop after one line of code |
| 133 | self.stopframe = None |
| 134 | self.returnframe = None |
| 135 | self.quitting = 0 |
| 136 | |
| 137 | def set_next(self, frame): |
| 138 | # Stop on the next line in or below the given frame |
| 139 | self.stopframe = frame |
| 140 | self.returnframe = None |
| 141 | self.quitting = 0 |
| 142 | |
| 143 | def set_return(self, frame): |
| 144 | # Stop when returning from the given frame |
| 145 | self.stopframe = frame.f_back |
| 146 | self.returnframe = frame |
| 147 | self.quitting = 0 |
| 148 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 149 | def set_trace(self): |
| 150 | # Start debugging from here |
| 151 | try: |
| 152 | 1 + '' |
| 153 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 154 | frame = sys.exc_info()[2].tb_frame.f_back |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | self.reset() |
| 156 | while frame: |
| 157 | frame.f_trace = self.trace_dispatch |
| 158 | self.botframe = frame |
| 159 | frame = frame.f_back |
| 160 | self.set_step() |
| 161 | sys.settrace(self.trace_dispatch) |
| 162 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 163 | def set_continue(self): |
| 164 | # Don't stop except at breakpoints or when finished |
| 165 | self.stopframe = self.botframe |
| 166 | self.returnframe = None |
| 167 | self.quitting = 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 168 | if not self.breaks: |
| 169 | # no breakpoints; run without debugger overhead |
| 170 | sys.settrace(None) |
| 171 | try: |
| 172 | 1 + '' # raise an exception |
| 173 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame] | 174 | frame = sys.exc_info()[2].tb_frame.f_back |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | while frame and frame is not self.botframe: |
| 176 | del frame.f_trace |
| 177 | frame = frame.f_back |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 178 | |
| 179 | def set_quit(self): |
| 180 | self.stopframe = self.botframe |
| 181 | self.returnframe = None |
| 182 | self.quitting = 1 |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 183 | sys.settrace(None) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 185 | # Derived classes and clients can call the following methods |
| 186 | # to manipulate breakpoints. These methods return an |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 187 | # error message is something went wrong, None if all is well. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 188 | # Set_break prints out the breakpoint line and file:lineno. |
| 189 | # Call self.get_*break*() to see the breakpoints or better |
| 190 | # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint(). |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 192 | def set_break(self, filename, lineno, temporary=0, cond = None): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 193 | import linecache # Import as late as possible |
| 194 | line = linecache.getline(filename, lineno) |
| 195 | if not line: |
| 196 | return 'That line does not exist!' |
| 197 | if not self.breaks.has_key(filename): |
| 198 | self.breaks[filename] = [] |
| 199 | list = self.breaks[filename] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 200 | if not lineno in list: |
| 201 | list.append(lineno) |
| 202 | bp = Breakpoint(filename, lineno, temporary, cond) |
| 203 | print 'Breakpoint %d, at %s:%d.' %(bp.number, filename, lineno) |
| 204 | |
| 205 | def clear_break(self, arg): |
| 206 | try: |
| 207 | number = int(arg) |
| 208 | bp = Breakpoint.bpbynumber[int(arg)] |
| 209 | except: |
| 210 | return 'Invalid argument' |
| 211 | if not bp: |
| 212 | return 'Breakpoint already deleted' |
| 213 | filename = bp.file |
| 214 | lineno = bp.line |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 215 | if not self.breaks.has_key(filename): |
| 216 | return 'There are no breakpoints in that file!' |
| 217 | if lineno not in self.breaks[filename]: |
| 218 | return 'There is no breakpoint there!' |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 219 | # If there's only one bp in the list for that file,line |
| 220 | # pair, then remove the breaks entry |
| 221 | if len(Breakpoint.bplist[filename, lineno]) == 1: |
| 222 | self.breaks[filename].remove(lineno) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 223 | if not self.breaks[filename]: |
| 224 | del self.breaks[filename] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 225 | bp.deleteMe() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 226 | |
| 227 | def clear_all_file_breaks(self, filename): |
| 228 | if not self.breaks.has_key(filename): |
| 229 | return 'There are no breakpoints in that file!' |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 230 | for line in self.breaks[filename]: |
| 231 | blist = Breakpoint.bplist[filename, line] |
| 232 | for bp in blist: |
| 233 | bp.deleteMe() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 234 | del self.breaks[filename] |
| 235 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 236 | def clear_all_breaks(self): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 237 | if not self.breaks: |
| 238 | return 'There are no breakpoints!' |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 239 | for bp in Breakpoint.bpbynumber: |
| 240 | if bp: |
| 241 | bp.deleteMe() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 242 | self.breaks = {} |
| 243 | |
| 244 | def get_break(self, filename, lineno): |
| 245 | return self.breaks.has_key(filename) and \ |
| 246 | lineno in self.breaks[filename] |
| 247 | |
| 248 | def get_file_breaks(self, filename): |
| 249 | if self.breaks.has_key(filename): |
| 250 | return self.breaks[filename] |
| 251 | else: |
| 252 | return [] |
| 253 | |
| 254 | def get_all_breaks(self): |
| 255 | return self.breaks |
| 256 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 257 | # Derived classes and clients can call the following method |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 258 | # to get a data structure representing a stack trace. |
| 259 | |
| 260 | def get_stack(self, f, t): |
| 261 | stack = [] |
| 262 | if t and t.tb_frame is f: |
| 263 | t = t.tb_next |
| 264 | while f is not None: |
| 265 | stack.append((f, f.f_lineno)) |
| 266 | if f is self.botframe: |
| 267 | break |
| 268 | f = f.f_back |
| 269 | stack.reverse() |
| 270 | i = max(0, len(stack) - 1) |
| 271 | while t is not None: |
| 272 | stack.append((t.tb_frame, t.tb_lineno)) |
| 273 | t = t.tb_next |
| 274 | return stack, i |
| 275 | |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 276 | # |
| 277 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 278 | def format_stack_entry(self, frame_lineno, lprefix=': '): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 279 | import linecache, repr, string |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 280 | frame, lineno = frame_lineno |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 281 | filename = frame.f_code.co_filename |
| 282 | s = filename + '(' + `lineno` + ')' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 283 | if frame.f_code.co_name: |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 284 | s = s + frame.f_code.co_name |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | else: |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 286 | s = s + "<lambda>" |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 287 | if frame.f_locals.has_key('__args__'): |
| 288 | args = frame.f_locals['__args__'] |
Guido van Rossum | b6aa92e | 1995-02-03 12:50:04 +0000 | [diff] [blame] | 289 | else: |
| 290 | args = None |
| 291 | if args: |
| 292 | s = s + repr.repr(args) |
| 293 | else: |
| 294 | s = s + '()' |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 295 | if frame.f_locals.has_key('__return__'): |
| 296 | rv = frame.f_locals['__return__'] |
| 297 | s = s + '->' |
| 298 | s = s + repr.repr(rv) |
| 299 | line = linecache.getline(filename, lineno) |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 300 | if line: s = s + lprefix + string.strip(line) |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 301 | return s |
| 302 | |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 303 | # The following two methods can be called by clients to use |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 304 | # a debugger to debug a statement, given as a string. |
| 305 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 306 | def run(self, cmd, globals=None, locals=None): |
| 307 | if globals is None: |
| 308 | import __main__ |
| 309 | globals = __main__.__dict__ |
| 310 | if locals is None: |
| 311 | locals = globals |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 312 | self.reset() |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 313 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 314 | if type(cmd) <> types.CodeType: |
| 315 | cmd = cmd+'\n' |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 316 | try: |
Guido van Rossum | ceb3ba2 | 1992-04-03 16:32:37 +0000 | [diff] [blame] | 317 | try: |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 318 | exec cmd in globals, locals |
Guido van Rossum | ceb3ba2 | 1992-04-03 16:32:37 +0000 | [diff] [blame] | 319 | except BdbQuit: |
| 320 | pass |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 321 | finally: |
| 322 | self.quitting = 1 |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 323 | sys.settrace(None) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 324 | |
| 325 | def runeval(self, expr, globals=None, locals=None): |
| 326 | if globals is None: |
| 327 | import __main__ |
| 328 | globals = __main__.__dict__ |
| 329 | if locals is None: |
| 330 | locals = globals |
| 331 | self.reset() |
| 332 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 333 | if type(expr) <> types.CodeType: |
| 334 | expr = expr+'\n' |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 335 | try: |
| 336 | try: |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 337 | return eval(expr, globals, locals) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 338 | except BdbQuit: |
| 339 | pass |
| 340 | finally: |
| 341 | self.quitting = 1 |
| 342 | sys.settrace(None) |
| 343 | |
| 344 | def runctx(self, cmd, globals, locals): |
| 345 | # B/W compatibility |
| 346 | self.run(cmd, globals, locals) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 347 | |
| 348 | # This method is more useful to debug a single function call. |
| 349 | |
| 350 | def runcall(self, func, *args): |
| 351 | self.reset() |
| 352 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 353 | res = None |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 354 | try: |
| 355 | try: |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 356 | res = apply(func, args) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 357 | except BdbQuit: |
| 358 | pass |
| 359 | finally: |
| 360 | self.quitting = 1 |
| 361 | sys.settrace(None) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 362 | return res |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 365 | def set_trace(): |
| 366 | Bdb().set_trace() |
| 367 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 368 | |
| 369 | class Breakpoint: |
| 370 | |
| 371 | """Breakpoint class |
| 372 | |
| 373 | Implements temporary breakpoints, ignore counts, disabling and |
| 374 | (re)-enabling, and conditionals. |
| 375 | |
| 376 | Breakpoints are indexed by number through bpbynumber and by |
| 377 | the file,line tuple using bplist. The former points to a |
| 378 | single instance of class Breakpoint. The latter points to a |
| 379 | list of such instances since there may be more than one |
| 380 | breakpoint per line. |
| 381 | |
| 382 | """ |
| 383 | |
| 384 | |
| 385 | next = 1 # Next bp to be assigned |
| 386 | bplist = {} # indexed by (file, lineno) tuple |
| 387 | bpbynumber = [None] # Each entry is None or an instance of Bpt |
| 388 | # index 0 is unused, except for marking an |
| 389 | # effective break .... see effective() |
| 390 | |
| 391 | def __init__(self, file, line, temporary=0, cond = None): |
| 392 | self.file = file |
| 393 | self.line = line |
| 394 | self.temporary = temporary |
| 395 | self.cond = cond |
| 396 | self.enabled = 1 |
| 397 | self.ignore = 0 |
| 398 | self.hits = 0 |
| 399 | self.number = Breakpoint.next |
| 400 | Breakpoint.next = Breakpoint.next + 1 |
| 401 | # Build the two lists |
| 402 | self.bpbynumber.append(self) |
| 403 | if self.bplist.has_key((file, line)): |
| 404 | self.bplist[file, line].append(self) |
| 405 | else: |
| 406 | self.bplist[file, line] = [self] |
| 407 | |
| 408 | |
| 409 | def deleteMe(self): |
| 410 | index = (self.file, self.line) |
| 411 | self.bpbynumber[self.number] = None # No longer in list |
| 412 | self.bplist[index].remove(self) |
| 413 | if not self.bplist[index]: |
| 414 | # No more bp for this f:l combo |
| 415 | del self.bplist[index] |
| 416 | |
| 417 | def enable(self): |
| 418 | self.enabled = 1 |
| 419 | |
| 420 | def disable(self): |
| 421 | self.enabled = 0 |
| 422 | |
| 423 | def bpprint(self): |
| 424 | if self.temporary: |
| 425 | disp = 'del ' |
| 426 | else: |
| 427 | disp = 'keep ' |
| 428 | if self.enabled: |
| 429 | disp = disp + 'yes' |
| 430 | else: |
| 431 | disp = disp + 'no ' |
| 432 | print '%-4dbreakpoint %s at %s:%d' % (self.number, disp, |
| 433 | self.file, self.line) |
| 434 | if self.cond: |
| 435 | print '\tstop only if %s' % (self.cond,) |
| 436 | if self.ignore: |
| 437 | print '\tignore next %d hits' % (self.ignore) |
| 438 | if (self.hits): |
| 439 | if (self.hits > 1): ss = 's' |
| 440 | else: ss = '' |
| 441 | print ('\tbreakpoint already hit %d time%s' % |
| 442 | (self.hits, ss)) |
| 443 | |
| 444 | # -----------end of Breakpoint class---------- |
| 445 | |
| 446 | # Determines if there is an effective (active) breakpoint at this |
| 447 | # line of code. Returns breakpoint number or 0 if none |
| 448 | def effective(file, line, frame): |
| 449 | """Determine which breakpoint for this file:line is to be acted upon. |
| 450 | |
| 451 | Called only if we know there is a bpt at this |
| 452 | location. Returns breakpoint that was triggered and a flag |
| 453 | that indicates if it is ok to delete a temporary bp. |
| 454 | |
| 455 | """ |
| 456 | possibles = Breakpoint.bplist[file,line] |
| 457 | for i in range(0, len(possibles)): |
| 458 | b = possibles[i] |
| 459 | if b.enabled == 0: |
| 460 | continue |
| 461 | # Count every hit when bp is enabled |
| 462 | b.hits = b.hits + 1 |
| 463 | if not b.cond: |
| 464 | # If unconditional, and ignoring, |
| 465 | # go on to next, else break |
| 466 | if b.ignore > 0: |
| 467 | b.ignore = b.ignore -1 |
| 468 | continue |
| 469 | else: |
| 470 | # breakpoint and marker that's ok |
| 471 | # to delete if temporary |
| 472 | return (b,1) |
| 473 | else: |
| 474 | # Conditional bp. |
| 475 | # Ignore count applies only to those bpt hits where the |
| 476 | # condition evaluates to true. |
| 477 | try: |
| 478 | val = eval(b.cond, frame.f_globals, |
| 479 | frame.f_locals) |
| 480 | if val: |
| 481 | if b.ignore > 0: |
| 482 | b.ignore = b.ignore -1 |
| 483 | # continue |
| 484 | else: |
| 485 | return (b,1) |
| 486 | # else: |
| 487 | # continue |
| 488 | except: |
| 489 | # if eval fails, most conservative |
| 490 | # thing is to stop on breakpoint |
| 491 | # regardless of ignore count. |
| 492 | # Don't delete temporary, |
| 493 | # as another hint to user. |
| 494 | return (b,0) |
| 495 | return (None, None) |
| 496 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 497 | # -------------------- testing -------------------- |
| 498 | |
| 499 | class Tdb(Bdb): |
| 500 | def user_call(self, frame, args): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 501 | name = frame.f_code.co_name |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 502 | if not name: name = '???' |
| 503 | print '+++ call', name, args |
| 504 | def user_line(self, frame): |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 505 | import linecache, string |
| 506 | name = frame.f_code.co_name |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 507 | if not name: name = '???' |
| 508 | fn = frame.f_code.co_filename |
| 509 | line = linecache.getline(fn, frame.f_lineno) |
| 510 | print '+++', fn, frame.f_lineno, name, ':', string.strip(line) |
| 511 | def user_return(self, frame, retval): |
| 512 | print '+++ return', retval |
| 513 | def user_exception(self, frame, exc_stuff): |
| 514 | print '+++ exception', exc_stuff |
| 515 | self.set_continue() |
| 516 | |
| 517 | def foo(n): |
| 518 | print 'foo(', n, ')' |
| 519 | x = bar(n*10) |
| 520 | print 'bar returned', x |
| 521 | |
| 522 | def bar(a): |
| 523 | print 'bar(', a, ')' |
| 524 | return a/2 |
| 525 | |
| 526 | def test(): |
Guido van Rossum | 5ef74b8 | 1993-06-23 11:55:24 +0000 | [diff] [blame] | 527 | t = Tdb() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 528 | t.run('import bdb; bdb.foo(10)') |