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