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