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