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