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