Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 1 | # A generic Python debugger base class. |
| 2 | # This class takes care of details of the trace facility; |
| 3 | # a derived class should implement user interaction. |
| 4 | # There are two debuggers based upon this: |
| 5 | # 'pdb', a text-oriented debugger not unlike dbx or gdb; |
| 6 | # and 'wdb', a window-oriented debugger. |
| 7 | # And of course... you can roll your own! |
| 8 | |
| 9 | import sys |
| 10 | |
| 11 | BdbQuit = 'bdb.BdbQuit' # Exception to give up completely |
| 12 | |
| 13 | |
| 14 | class Bdb: # Basic Debugger |
| 15 | |
| 16 | def init(self): |
| 17 | self.breaks = {} |
| 18 | return self |
| 19 | |
| 20 | def reset(self): |
| 21 | self.botframe = None |
| 22 | self.stopframe = None |
| 23 | self.returnframe = None |
| 24 | self.quitting = 0 |
| 25 | |
| 26 | def trace_dispatch(self, frame, event, arg): |
| 27 | if self.quitting: |
| 28 | return # None |
| 29 | if event == 'line': |
| 30 | return self.dispatch_line(frame) |
| 31 | if event == 'call': |
| 32 | return self.dispatch_call(frame, arg) |
| 33 | if event == 'return': |
| 34 | return self.dispatch_return(frame, arg) |
| 35 | if event == 'exception': |
| 36 | return self.dispatch_exception(frame, arg) |
| 37 | print 'bdb.Bdb.dispatch: unknown debugging event:', `event` |
| 38 | return self.trace_dispatch |
| 39 | |
| 40 | def dispatch_line(self, frame): |
| 41 | if self.stop_here(frame) or self.break_here(frame): |
| 42 | self.user_line(frame) |
| 43 | if self.quitting: raise BdbQuit |
| 44 | return self.trace_dispatch |
| 45 | |
| 46 | def dispatch_call(self, frame, arg): |
| 47 | frame.f_locals['__args__'] = arg |
| 48 | if self.botframe is None: |
| 49 | # First call of dispatch since reset() |
| 50 | self.botframe = frame |
| 51 | return self.trace_dispatch |
| 52 | if not (self.stop_here(frame) or self.break_anywhere(frame)): |
| 53 | # No need to trace this function |
| 54 | return # None |
| 55 | self.user_call(frame, arg) |
| 56 | if self.quitting: raise BdbQuit |
| 57 | return self.trace_dispatch |
| 58 | |
| 59 | def dispatch_return(self, frame, arg): |
| 60 | if self.stop_here(frame) or frame == self.returnframe: |
| 61 | self.user_return(frame, arg) |
| 62 | if self.quitting: raise BdbQuit |
| 63 | |
| 64 | def dispatch_exception(self, frame, arg): |
| 65 | if self.stop_here(frame): |
| 66 | self.user_exception(frame, arg) |
| 67 | if self.quitting: raise BdbQuit |
| 68 | return self.trace_dispatch |
| 69 | |
| 70 | # Normally derived classes don't override the following |
| 71 | # functions, but they may if they want to redefine the |
| 72 | # definition of stopping and breakpoints. |
| 73 | |
| 74 | def stop_here(self, frame): |
| 75 | if self.stopframe is None: |
| 76 | return 1 |
| 77 | if frame is self.stopframe: |
| 78 | return 1 |
| 79 | while frame is not None and frame is not self.stopframe: |
| 80 | if frame is self.botframe: |
| 81 | return 1 |
| 82 | frame = frame.f_back |
| 83 | return 0 |
| 84 | |
| 85 | def break_here(self, frame): |
| 86 | if not self.breaks.has_key(frame.f_code.co_filename): |
| 87 | return 0 |
| 88 | if not frame.f_lineno in \ |
| 89 | self.breaks[frame.f_code.co_filename]: |
| 90 | return 0 |
| 91 | return 1 |
| 92 | |
| 93 | def break_anywhere(self, frame): |
| 94 | return self.breaks.has_key(frame.f_code.co_filename) |
| 95 | |
| 96 | # Derived classes should override the user_* functions |
| 97 | # to gain control. |
| 98 | |
| 99 | def user_call(self, frame, argument_list): |
| 100 | # This function is called when there is the remote possibility |
| 101 | # that we ever need to stop in this function |
| 102 | pass |
| 103 | |
| 104 | def user_line(self, frame): |
| 105 | # This function is called when we stop or break at this line |
| 106 | pass |
| 107 | |
| 108 | def user_return(self, frame, return_value): |
| 109 | # This function is called when a return trap is set here |
| 110 | pass |
| 111 | |
| 112 | def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): |
| 113 | # This function is called if an exception occurs, |
| 114 | # but only if we are to stop at or just below this level |
| 115 | pass |
| 116 | |
| 117 | # Derived classes and clients can call the following functions |
| 118 | # to affect the stepping state. |
| 119 | |
| 120 | def set_step(self): |
| 121 | # Stop after one line of code |
| 122 | self.stopframe = None |
| 123 | self.returnframe = None |
| 124 | self.quitting = 0 |
| 125 | |
| 126 | def set_next(self, frame): |
| 127 | # Stop on the next line in or below the given frame |
| 128 | self.stopframe = frame |
| 129 | self.returnframe = None |
| 130 | self.quitting = 0 |
| 131 | |
| 132 | def set_return(self, frame): |
| 133 | # Stop when returning from the given frame |
| 134 | self.stopframe = frame.f_back |
| 135 | self.returnframe = frame |
| 136 | self.quitting = 0 |
| 137 | |
| 138 | def set_continue(self): |
| 139 | # Don't stop except at breakpoints or when finished |
| 140 | self.stopframe = self.botframe |
| 141 | self.returnframe = None |
| 142 | self.quitting = 0 |
| 143 | |
| 144 | def set_quit(self): |
| 145 | self.stopframe = self.botframe |
| 146 | self.returnframe = None |
| 147 | self.quitting = 1 |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 148 | sys.settrace(None) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 149 | |
| 150 | # Derived classes and clients can call the following functions |
| 151 | # to manipulate breakpoints. These functions return an |
| 152 | # error message is something went wrong, None if all is well. |
| 153 | # Call self.get_*break*() to see the breakpoints. |
| 154 | |
| 155 | def set_break(self, filename, lineno): |
| 156 | import linecache # Import as late as possible |
| 157 | line = linecache.getline(filename, lineno) |
| 158 | if not line: |
| 159 | return 'That line does not exist!' |
| 160 | if not self.breaks.has_key(filename): |
| 161 | self.breaks[filename] = [] |
| 162 | list = self.breaks[filename] |
| 163 | if lineno in list: |
| 164 | return 'There is already a breakpoint there!' |
| 165 | list.append(lineno) |
| 166 | |
| 167 | def clear_break(self, filename, lineno): |
| 168 | if not self.breaks.has_key(filename): |
| 169 | return 'There are no breakpoints in that file!' |
| 170 | if lineno not in self.breaks[filename]: |
| 171 | return 'There is no breakpoint there!' |
| 172 | self.breaks[filename].remove(lineno) |
| 173 | if not self.breaks[filename]: |
| 174 | del self.breaks[filename] |
| 175 | |
| 176 | def clear_all_file_breaks(self, filename): |
| 177 | if not self.breaks.has_key(filename): |
| 178 | return 'There are no breakpoints in that file!' |
| 179 | del self.breaks[filename] |
| 180 | |
| 181 | def clear_all_breaks(self, filename, lineno): |
| 182 | if not self.breaks: |
| 183 | return 'There are no breakpoints!' |
| 184 | self.breaks = {} |
| 185 | |
| 186 | def get_break(self, filename, lineno): |
| 187 | return self.breaks.has_key(filename) and \ |
| 188 | lineno in self.breaks[filename] |
| 189 | |
| 190 | def get_file_breaks(self, filename): |
| 191 | if self.breaks.has_key(filename): |
| 192 | return self.breaks[filename] |
| 193 | else: |
| 194 | return [] |
| 195 | |
| 196 | def get_all_breaks(self): |
| 197 | return self.breaks |
| 198 | |
| 199 | # Derived classes and clients can call the following function |
| 200 | # to get a data structure representing a stack trace. |
| 201 | |
| 202 | def get_stack(self, f, t): |
| 203 | stack = [] |
| 204 | if t and t.tb_frame is f: |
| 205 | t = t.tb_next |
| 206 | while f is not None: |
| 207 | stack.append((f, f.f_lineno)) |
| 208 | if f is self.botframe: |
| 209 | break |
| 210 | f = f.f_back |
| 211 | stack.reverse() |
| 212 | i = max(0, len(stack) - 1) |
| 213 | while t is not None: |
| 214 | stack.append((t.tb_frame, t.tb_lineno)) |
| 215 | t = t.tb_next |
| 216 | return stack, i |
| 217 | |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 218 | # |
| 219 | |
| 220 | def format_stack_entry(self, (frame, lineno)): |
| 221 | import codehack, linecache, repr, string |
| 222 | filename = frame.f_code.co_filename |
| 223 | s = filename + '(' + `lineno` + ')' |
| 224 | s = s + codehack.getcodename(frame.f_code) |
| 225 | if frame.f_locals.has_key('__args__'): |
| 226 | args = frame.f_locals['__args__'] |
| 227 | if args is not None: |
| 228 | s = s + repr.repr(args) |
| 229 | if frame.f_locals.has_key('__return__'): |
| 230 | rv = frame.f_locals['__return__'] |
| 231 | s = s + '->' |
| 232 | s = s + repr.repr(rv) |
| 233 | line = linecache.getline(filename, lineno) |
| 234 | if line: s = s + ': ' + string.strip(line) |
| 235 | return s |
| 236 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 237 | # The following two functions can be called by clients to use |
| 238 | # a debugger to debug a statement, given as a string. |
| 239 | |
| 240 | def run(self, cmd): |
Guido van Rossum | fac6da2 | 1992-01-27 16:59:34 +0000 | [diff] [blame] | 241 | import __main__ |
| 242 | dict = __main__.__dict__ |
| 243 | self.runctx(cmd, dict, dict) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 244 | |
| 245 | def runctx(self, cmd, globals, locals): |
| 246 | self.reset() |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 247 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 248 | try: |
Guido van Rossum | ceb3ba2 | 1992-04-03 16:32:37 +0000 | [diff] [blame^] | 249 | try: |
| 250 | exec(cmd + '\n', globals, locals) |
| 251 | except BdbQuit: |
| 252 | pass |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 253 | finally: |
| 254 | self.quitting = 1 |
Guido van Rossum | edbfcbd | 1992-03-27 15:09:46 +0000 | [diff] [blame] | 255 | sys.settrace(None) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 256 | # XXX What to do if the command finishes normally? |
| 257 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 258 | |
| 259 | # -------------------- testing -------------------- |
| 260 | |
| 261 | class Tdb(Bdb): |
| 262 | def user_call(self, frame, args): |
| 263 | import codehack |
| 264 | name = codehack.getcodename(frame.f_code) |
| 265 | if not name: name = '???' |
| 266 | print '+++ call', name, args |
| 267 | def user_line(self, frame): |
| 268 | import linecache, string, codehack |
| 269 | name = codehack.getcodename(frame.f_code) |
| 270 | if not name: name = '???' |
| 271 | fn = frame.f_code.co_filename |
| 272 | line = linecache.getline(fn, frame.f_lineno) |
| 273 | print '+++', fn, frame.f_lineno, name, ':', string.strip(line) |
| 274 | def user_return(self, frame, retval): |
| 275 | print '+++ return', retval |
| 276 | def user_exception(self, frame, exc_stuff): |
| 277 | print '+++ exception', exc_stuff |
| 278 | self.set_continue() |
| 279 | |
| 280 | def foo(n): |
| 281 | print 'foo(', n, ')' |
| 282 | x = bar(n*10) |
| 283 | print 'bar returned', x |
| 284 | |
| 285 | def bar(a): |
| 286 | print 'bar(', a, ')' |
| 287 | return a/2 |
| 288 | |
| 289 | def test(): |
| 290 | import linecache |
| 291 | linecache.checkcache() |
| 292 | t = Tdb().init() |
| 293 | t.run('import bdb; bdb.foo(10)') |