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