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 |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 6 | from inspect import CO_GENERATOR, CO_COROUTINE, CO_ASYNC_GENERATOR |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 7 | |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 8 | __all__ = ["BdbQuit", "Bdb", "Breakpoint"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 9 | |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 10 | GENERATOR_AND_COROUTINE_FLAGS = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR |
| 11 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 12 | |
Neal Norwitz | 93cf79f | 2002-03-31 14:06:41 +0000 | [diff] [blame] | 13 | class BdbQuit(Exception): |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 14 | """Exception to give up completely.""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 15 | |
| 16 | |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 17 | class Bdb: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 18 | """Generic Python debugger base class. |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 20 | This class takes care of details of the trace facility; |
| 21 | a derived class should implement user interaction. |
| 22 | The standard debugger class (pdb.Pdb) is an example. |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 23 | |
| 24 | The optional skip argument must be an iterable of glob-style |
| 25 | module name patterns. The debugger will not step into frames |
| 26 | that originate in a module that matches one of these patterns. |
| 27 | Whether a frame is considered to originate in a certain module |
| 28 | is determined by the __name__ in the frame globals. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 29 | """ |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 31 | def __init__(self, skip=None): |
| 32 | self.skip = set(skip) if skip else None |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 33 | self.breaks = {} |
| 34 | self.fncache = {} |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 35 | self.frame_returning = None |
Barry Warsaw | 148ffbc | 1999-09-09 23:24:33 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 37 | def canonic(self, filename): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 38 | """Return canonical form of filename. |
| 39 | |
| 40 | For real filenames, the canonical form is a case-normalized (on |
| 41 | case insenstive filesystems) absolute path. 'Filenames' with |
| 42 | angle brackets, such as "<stdin>", generated in interactive |
| 43 | mode, are returned unchanged. |
| 44 | """ |
Guido van Rossum | 42f5332 | 2001-11-29 02:50:15 +0000 | [diff] [blame] | 45 | if filename == "<" + filename[1:-1] + ">": |
| 46 | return filename |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 47 | canonic = self.fncache.get(filename) |
| 48 | if not canonic: |
| 49 | canonic = os.path.abspath(filename) |
Guido van Rossum | bdba320 | 2002-02-25 23:23:24 +0000 | [diff] [blame] | 50 | canonic = os.path.normcase(canonic) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 51 | self.fncache[filename] = canonic |
| 52 | return canonic |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 54 | def reset(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 55 | """Set values of attributes as ready to start debugging.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 56 | import linecache |
| 57 | linecache.checkcache() |
| 58 | self.botframe = None |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 59 | self._set_stopinfo(None, None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 61 | def trace_dispatch(self, frame, event, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 62 | """Dispatch a trace function for debugged frames based on the event. |
| 63 | |
| 64 | This function is installed as the trace function for debugged |
| 65 | frames. Its return value is the new trace function, which is |
| 66 | usually itself. The default implementation decides how to |
| 67 | dispatch a frame, depending on the type of event (passed in as a |
| 68 | string) that is about to be executed. |
| 69 | |
| 70 | The event can be one of the following: |
| 71 | line: A new line of code is going to be executed. |
| 72 | call: A function is about to be called or another code block |
| 73 | is entered. |
| 74 | return: A function or other code block is about to return. |
| 75 | exception: An exception has occurred. |
| 76 | c_call: A C function is about to be called. |
| 77 | c_return: A C functon has returned. |
| 78 | c_exception: A C function has raised an exception. |
| 79 | |
| 80 | For the Python events, specialized functions (see the dispatch_*() |
| 81 | methods) are called. For the C events, no action is taken. |
| 82 | |
| 83 | The arg parameter depends on the previous event. |
| 84 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 85 | if self.quitting: |
| 86 | return # None |
| 87 | if event == 'line': |
| 88 | return self.dispatch_line(frame) |
| 89 | if event == 'call': |
| 90 | return self.dispatch_call(frame, arg) |
| 91 | if event == 'return': |
| 92 | return self.dispatch_return(frame, arg) |
| 93 | if event == 'exception': |
| 94 | return self.dispatch_exception(frame, arg) |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 95 | if event == 'c_call': |
| 96 | return self.trace_dispatch |
| 97 | if event == 'c_exception': |
| 98 | return self.trace_dispatch |
| 99 | if event == 'c_return': |
| 100 | return self.trace_dispatch |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 101 | print('bdb.Bdb.dispatch: unknown debugging event:', repr(event)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 102 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 104 | def dispatch_line(self, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 105 | """Invoke user function and return trace function for line event. |
| 106 | |
| 107 | If the debugger stops on the current line, invoke |
| 108 | self.user_line(). Raise BdbQuit if self.quitting is set. |
| 109 | Return self.trace_dispatch to continue tracing in this scope. |
| 110 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 111 | if self.stop_here(frame) or self.break_here(frame): |
| 112 | self.user_line(frame) |
| 113 | if self.quitting: raise BdbQuit |
| 114 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 116 | def dispatch_call(self, frame, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 117 | """Invoke user function and return trace function for call event. |
| 118 | |
| 119 | If the debugger stops on this function call, invoke |
| 120 | self.user_call(). Raise BbdQuit if self.quitting is set. |
| 121 | Return self.trace_dispatch to continue tracing in this scope. |
| 122 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 123 | # XXX 'arg' is no longer used |
| 124 | if self.botframe is None: |
| 125 | # First call of dispatch since reset() |
Christian Tismer | 313a751 | 2002-05-28 08:04:00 +0000 | [diff] [blame] | 126 | 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] | 127 | return self.trace_dispatch |
| 128 | if not (self.stop_here(frame) or self.break_anywhere(frame)): |
| 129 | # No need to trace this function |
| 130 | return # None |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 131 | # Ignore call events in generator except when stepping. |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 132 | if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 133 | return self.trace_dispatch |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 134 | self.user_call(frame, arg) |
| 135 | if self.quitting: raise BdbQuit |
| 136 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 137 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 138 | def dispatch_return(self, frame, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 139 | """Invoke user function and return trace function for return event. |
| 140 | |
| 141 | If the debugger stops on this function return, invoke |
| 142 | self.user_return(). Raise BdbQuit if self.quitting is set. |
| 143 | Return self.trace_dispatch to continue tracing in this scope. |
| 144 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 145 | if self.stop_here(frame) or frame == self.returnframe: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 146 | # Ignore return events in generator except when stepping. |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 147 | if self.stopframe and frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 148 | return self.trace_dispatch |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 149 | try: |
| 150 | self.frame_returning = frame |
| 151 | self.user_return(frame, arg) |
| 152 | finally: |
| 153 | self.frame_returning = None |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 154 | if self.quitting: raise BdbQuit |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 155 | # The user issued a 'next' or 'until' command. |
| 156 | if self.stopframe is frame and self.stoplineno != -1: |
| 157 | self._set_stopinfo(None, None) |
Just van Rossum | ae1f65f | 2001-06-25 18:01:24 +0000 | [diff] [blame] | 158 | return self.trace_dispatch |
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 dispatch_exception(self, frame, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 161 | """Invoke user function and return trace function for exception event. |
| 162 | |
| 163 | If the debugger stops on this exception, invoke |
| 164 | self.user_exception(). Raise BdbQuit if self.quitting is set. |
| 165 | Return self.trace_dispatch to continue tracing in this scope. |
| 166 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 167 | if self.stop_here(frame): |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 168 | # When stepping with next/until/return in a generator frame, skip |
| 169 | # the internal StopIteration exception (with no traceback) |
| 170 | # triggered by a subiterator run with the 'yield from' statement. |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 171 | if not (frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 172 | and arg[0] is StopIteration and arg[2] is None): |
| 173 | self.user_exception(frame, arg) |
| 174 | if self.quitting: raise BdbQuit |
| 175 | # Stop at the StopIteration or GeneratorExit exception when the user |
| 176 | # has set stopframe in a generator by issuing a return command, or a |
| 177 | # next/until command at the last statement in the generator before the |
| 178 | # exception. |
| 179 | elif (self.stopframe and frame is not self.stopframe |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 180 | and self.stopframe.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 181 | and arg[0] in (StopIteration, GeneratorExit)): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 182 | self.user_exception(frame, arg) |
| 183 | if self.quitting: raise BdbQuit |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 184 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 185 | return self.trace_dispatch |
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 | # Normally derived classes don't override the following |
| 188 | # methods, but they may if they want to redefine the |
| 189 | # definition of stopping and breakpoints. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 190 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 191 | def is_skipped_module(self, module_name): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 192 | "Return True if module_name matches any skip pattern." |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 193 | for pattern in self.skip: |
| 194 | if fnmatch.fnmatch(module_name, pattern): |
| 195 | return True |
| 196 | return False |
| 197 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 198 | def stop_here(self, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 199 | "Return True if frame is below the starting frame in the stack." |
Neal Norwitz | 72a2b4d | 2002-05-29 00:54:38 +0000 | [diff] [blame] | 200 | # (CT) stopframe may now also be None, see dispatch_call. |
| 201 | # (CT) the former test for None is therefore removed from here. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 202 | if self.skip and \ |
| 203 | self.is_skipped_module(frame.f_globals.get('__name__')): |
| 204 | return False |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 205 | if frame is self.stopframe: |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 206 | if self.stoplineno == -1: |
| 207 | return False |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 208 | return frame.f_lineno >= self.stoplineno |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 209 | if not self.stopframe: |
| 210 | return True |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 211 | return False |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 213 | def break_here(self, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 214 | """Return True if there is an effective breakpoint for this line. |
| 215 | |
| 216 | Check for line or function breakpoint and if in effect. |
| 217 | Delete temporary breakpoints if effective() says to. |
| 218 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 219 | filename = self.canonic(frame.f_code.co_filename) |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 220 | if filename not in self.breaks: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 221 | return False |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 222 | lineno = frame.f_lineno |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 223 | if lineno not in self.breaks[filename]: |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 224 | # The line itself has no breakpoint, but maybe the line is the |
| 225 | # first line of a function with breakpoint set by function name. |
| 226 | lineno = frame.f_code.co_firstlineno |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 227 | if lineno not in self.breaks[filename]: |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 228 | return False |
| 229 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 230 | # flag says ok to delete temp. bp |
| 231 | (bp, flag) = effective(filename, lineno, frame) |
| 232 | if bp: |
| 233 | self.currentbp = bp.number |
| 234 | if (flag and bp.temporary): |
| 235 | self.do_clear(str(bp.number)) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 236 | return True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 237 | else: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 238 | return False |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 9cec8fb | 2001-04-08 15:05:16 +0000 | [diff] [blame] | 240 | def do_clear(self, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 241 | """Remove temporary breakpoint. |
| 242 | |
| 243 | Must implement in derived classes or get NotImplementedError. |
| 244 | """ |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 245 | raise NotImplementedError("subclass of bdb must implement do_clear()") |
Guido van Rossum | 9cec8fb | 2001-04-08 15:05:16 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 247 | def break_anywhere(self, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 248 | """Return True if there is any breakpoint for frame's filename. |
| 249 | """ |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 250 | return self.canonic(frame.f_code.co_filename) in self.breaks |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 252 | # Derived classes should override the user_* methods |
| 253 | # to gain control. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 255 | def user_call(self, frame, argument_list): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 256 | """Called if we might stop in a function.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 257 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 259 | def user_line(self, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 260 | """Called when when we stop or break at a line.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 261 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 263 | def user_return(self, frame, return_value): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 264 | """Called when a return trap is set here.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 265 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 267 | def user_exception(self, frame, exc_info): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 268 | """Called when we stop on an exception.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 269 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 270 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 271 | def _set_stopinfo(self, stopframe, returnframe, stoplineno=0): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 272 | """Set the attributes for stopping. |
| 273 | |
| 274 | If stoplineno is greater than or equal to 0, then stop at line |
| 275 | greater than or equal to the stopline. If stoplineno is -1, then |
| 276 | don't stop at all. |
| 277 | """ |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 278 | self.stopframe = stopframe |
| 279 | self.returnframe = returnframe |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 280 | self.quitting = False |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 281 | # stoplineno >= 0 means: stop at line >= the stoplineno |
| 282 | # stoplineno -1 means: don't stop at all |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 283 | self.stoplineno = stoplineno |
| 284 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 285 | # Derived classes and clients can call the following methods |
| 286 | # to affect the stepping state. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 287 | |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 288 | def set_until(self, frame, lineno=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 289 | """Stop when the line with the lineno greater than the current one is |
| 290 | reached or when returning from current frame.""" |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 291 | # the name "until" is borrowed from gdb |
| 292 | if lineno is None: |
| 293 | lineno = frame.f_lineno + 1 |
| 294 | self._set_stopinfo(frame, frame, lineno) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 296 | def set_step(self): |
| 297 | """Stop after one line of code.""" |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 298 | # Issue #13183: pdb skips frames after hitting a breakpoint and running |
| 299 | # step commands. |
| 300 | # Restore the trace function in the caller (that may not have been set |
| 301 | # for performance reasons) when returning from the current frame. |
| 302 | if self.frame_returning: |
| 303 | caller_frame = self.frame_returning.f_back |
| 304 | if caller_frame and not caller_frame.f_trace: |
| 305 | caller_frame.f_trace = self.trace_dispatch |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 306 | self._set_stopinfo(None, None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 308 | def set_next(self, frame): |
| 309 | """Stop on the next line in or below the given frame.""" |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 310 | self._set_stopinfo(frame, None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 312 | def set_return(self, frame): |
| 313 | """Stop when returning from the given frame.""" |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 314 | if frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 315 | self._set_stopinfo(frame, None, -1) |
| 316 | else: |
| 317 | self._set_stopinfo(frame.f_back, frame) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 318 | |
Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 319 | def set_trace(self, frame=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 320 | """Start debugging from frame. |
Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 321 | |
| 322 | If frame is not specified, debugging starts from caller's frame. |
| 323 | """ |
| 324 | if frame is None: |
| 325 | frame = sys._getframe().f_back |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 326 | self.reset() |
| 327 | while frame: |
| 328 | frame.f_trace = self.trace_dispatch |
| 329 | self.botframe = frame |
| 330 | frame = frame.f_back |
| 331 | self.set_step() |
| 332 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 333 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 334 | def set_continue(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 335 | """Stop only at breakpoints or when finished. |
| 336 | |
| 337 | If there are no breakpoints, set the system trace function to None. |
| 338 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 339 | # Don't stop except at breakpoints or when finished |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 340 | self._set_stopinfo(self.botframe, None, -1) |
Georg Brandl | f8b893e | 2010-12-04 16:22:44 +0000 | [diff] [blame] | 341 | if not self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 342 | # no breakpoints; run without debugger overhead |
| 343 | sys.settrace(None) |
Christian Tismer | 313a751 | 2002-05-28 08:04:00 +0000 | [diff] [blame] | 344 | frame = sys._getframe().f_back |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 345 | while frame and frame is not self.botframe: |
| 346 | del frame.f_trace |
| 347 | frame = frame.f_back |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 349 | def set_quit(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 350 | """Set quitting attribute to True. |
| 351 | |
| 352 | Raises BdbQuit exception in the next call to a dispatch_*() method. |
| 353 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 354 | self.stopframe = self.botframe |
| 355 | self.returnframe = None |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 356 | self.quitting = True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 357 | sys.settrace(None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 359 | # Derived classes and clients can call the following methods |
| 360 | # to manipulate breakpoints. These methods return an |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 361 | # error message if something went wrong, None if all is well. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 362 | # Set_break prints out the breakpoint line and file:lineno. |
| 363 | # Call self.get_*break*() to see the breakpoints or better |
| 364 | # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint(). |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 365 | |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 366 | def set_break(self, filename, lineno, temporary=False, cond=None, |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 367 | funcname=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 368 | """Set a new breakpoint for filename:lineno. |
| 369 | |
| 370 | If lineno doesn't exist for the filename, return an error message. |
| 371 | The filename should be in canonical form. |
| 372 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 373 | filename = self.canonic(filename) |
| 374 | import linecache # Import as late as possible |
| 375 | line = linecache.getline(filename, lineno) |
| 376 | if not line: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 377 | return 'Line %s:%d does not exist' % (filename, lineno) |
| 378 | list = self.breaks.setdefault(filename, []) |
| 379 | if lineno not in list: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 380 | list.append(lineno) |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 381 | bp = Breakpoint(filename, lineno, temporary, cond, funcname) |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 382 | return None |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 383 | |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 384 | def _prune_breaks(self, filename, lineno): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 385 | """Prune breakpoints for filname:lineno. |
| 386 | |
| 387 | A list of breakpoints is maintained in the Bdb instance and in |
| 388 | the Breakpoint class. If a breakpoint in the Bdb instance no |
| 389 | longer exists in the Breakpoint class, then it's removed from the |
| 390 | Bdb instance. |
| 391 | """ |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 392 | if (filename, lineno) not in Breakpoint.bplist: |
| 393 | self.breaks[filename].remove(lineno) |
| 394 | if not self.breaks[filename]: |
| 395 | del self.breaks[filename] |
| 396 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 397 | def clear_break(self, filename, lineno): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 398 | """Delete breakpoints for filename:lineno. |
| 399 | |
| 400 | If no breakpoints were set, return an error message. |
| 401 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 402 | filename = self.canonic(filename) |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 403 | if filename not in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 404 | return 'There are no breakpoints in %s' % filename |
| 405 | if lineno not in self.breaks[filename]: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 406 | return 'There is no breakpoint at %s:%d' % (filename, lineno) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 407 | # If there's only one bp in the list for that file,line |
| 408 | # pair, then remove the breaks entry |
| 409 | for bp in Breakpoint.bplist[filename, lineno][:]: |
| 410 | bp.deleteMe() |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 411 | self._prune_breaks(filename, lineno) |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 412 | return None |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 414 | def clear_bpbynumber(self, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 415 | """Delete a breakpoint by its index in Breakpoint.bpbynumber. |
| 416 | |
| 417 | If arg is invalid, return an error message. |
| 418 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 419 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 420 | bp = self.get_bpbynumber(arg) |
| 421 | except ValueError as err: |
| 422 | return str(err) |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 423 | bp.deleteMe() |
| 424 | self._prune_breaks(bp.file, bp.line) |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 425 | return None |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 427 | def clear_all_file_breaks(self, filename): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 428 | """Delete all breakpoints in filename. |
| 429 | |
| 430 | If none were set, return an error message. |
| 431 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 432 | filename = self.canonic(filename) |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 433 | if filename not in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 434 | return 'There are no breakpoints in %s' % filename |
| 435 | for line in self.breaks[filename]: |
| 436 | blist = Breakpoint.bplist[filename, line] |
| 437 | for bp in blist: |
| 438 | bp.deleteMe() |
| 439 | del self.breaks[filename] |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 440 | return None |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 442 | def clear_all_breaks(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 443 | """Delete all existing breakpoints. |
| 444 | |
| 445 | If none were set, return an error message. |
| 446 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 447 | if not self.breaks: |
| 448 | return 'There are no breakpoints' |
| 449 | for bp in Breakpoint.bpbynumber: |
| 450 | if bp: |
| 451 | bp.deleteMe() |
| 452 | self.breaks = {} |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 453 | return None |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 454 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 455 | def get_bpbynumber(self, arg): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 456 | """Return a breakpoint by its index in Breakpoint.bybpnumber. |
| 457 | |
| 458 | For invalid arg values or if the breakpoint doesn't exist, |
| 459 | raise a ValueError. |
| 460 | """ |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 461 | if not arg: |
| 462 | raise ValueError('Breakpoint number expected') |
| 463 | try: |
| 464 | number = int(arg) |
| 465 | except ValueError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 466 | raise ValueError('Non-numeric breakpoint number %s' % arg) from None |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 467 | try: |
| 468 | bp = Breakpoint.bpbynumber[number] |
| 469 | except IndexError: |
Serhiy Storchaka | 5affd23 | 2017-04-05 09:37:24 +0300 | [diff] [blame] | 470 | raise ValueError('Breakpoint number %d out of range' % number) from None |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 471 | if bp is None: |
| 472 | raise ValueError('Breakpoint %d already deleted' % number) |
| 473 | return bp |
| 474 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 475 | def get_break(self, filename, lineno): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 476 | """Return True if there is a breakpoint for filename:lineno.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 477 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 478 | return filename in self.breaks and \ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 479 | lineno in self.breaks[filename] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 481 | def get_breaks(self, filename, lineno): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 482 | """Return all breakpoints for filename:lineno. |
| 483 | |
| 484 | If no breakpoints are set, return an empty list. |
| 485 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 486 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 487 | return filename in self.breaks and \ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 488 | lineno in self.breaks[filename] and \ |
| 489 | Breakpoint.bplist[filename, lineno] or [] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 490 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 491 | def get_file_breaks(self, filename): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 492 | """Return all lines with breakpoints for filename. |
| 493 | |
| 494 | If no breakpoints are set, return an empty list. |
| 495 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 496 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 497 | if filename in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 498 | return self.breaks[filename] |
| 499 | else: |
| 500 | return [] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 502 | def get_all_breaks(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 503 | """Return all breakpoints that are set.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 504 | return self.breaks |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 506 | # Derived classes and clients can call the following method |
| 507 | # to get a data structure representing a stack trace. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 509 | def get_stack(self, f, t): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 510 | """Return a list of (frame, lineno) in a stack trace and a size. |
| 511 | |
| 512 | List starts with original calling frame, if there is one. |
| 513 | Size may be number of frames above or below f. |
| 514 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 515 | stack = [] |
| 516 | if t and t.tb_frame is f: |
| 517 | t = t.tb_next |
| 518 | while f is not None: |
| 519 | stack.append((f, f.f_lineno)) |
| 520 | if f is self.botframe: |
| 521 | break |
| 522 | f = f.f_back |
| 523 | stack.reverse() |
| 524 | i = max(0, len(stack) - 1) |
| 525 | while t is not None: |
| 526 | stack.append((t.tb_frame, t.tb_lineno)) |
| 527 | t = t.tb_next |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 528 | if f is None: |
| 529 | i = max(0, len(stack) - 1) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 530 | return stack, i |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 532 | def format_stack_entry(self, frame_lineno, lprefix=': '): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 533 | """Return a string with information about a stack entry. |
| 534 | |
| 535 | The stack entry frame_lineno is a (frame, lineno) tuple. The |
| 536 | return string contains the canonical filename, the function name |
| 537 | or '<lambda>', the input arguments, the return value, and the |
| 538 | line of code (if it exists). |
| 539 | |
| 540 | """ |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 541 | import linecache, reprlib |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 542 | frame, lineno = frame_lineno |
| 543 | filename = self.canonic(frame.f_code.co_filename) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 544 | s = '%s(%r)' % (filename, lineno) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 545 | if frame.f_code.co_name: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 546 | s += frame.f_code.co_name |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 547 | else: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 548 | s += "<lambda>" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 549 | if '__args__' in frame.f_locals: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 550 | args = frame.f_locals['__args__'] |
| 551 | else: |
| 552 | args = None |
| 553 | if args: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 554 | s += reprlib.repr(args) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 555 | else: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 556 | s += '()' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 557 | if '__return__' in frame.f_locals: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 558 | rv = frame.f_locals['__return__'] |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 559 | s += '->' |
| 560 | s += reprlib.repr(rv) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 561 | line = linecache.getline(filename, lineno, frame.f_globals) |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 562 | if line: |
| 563 | s += lprefix + line.strip() |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 564 | return s |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 565 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 566 | # The following methods can be called by clients to use |
| 567 | # a debugger to debug a statement or an expression. |
| 568 | # Both can be given as a string, or a code object. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 570 | def run(self, cmd, globals=None, locals=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 571 | """Debug a statement executed via the exec() function. |
| 572 | |
| 573 | globals defaults to __main__.dict; locals defaults to globals. |
| 574 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 575 | if globals is None: |
| 576 | import __main__ |
| 577 | globals = __main__.__dict__ |
| 578 | if locals is None: |
| 579 | locals = globals |
| 580 | self.reset() |
Victor Stinner | 4bd8172 | 2011-01-06 00:49:38 +0000 | [diff] [blame] | 581 | if isinstance(cmd, str): |
| 582 | cmd = compile(cmd, "<string>", "exec") |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 583 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 584 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 585 | exec(cmd, globals, locals) |
| 586 | except BdbQuit: |
| 587 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 588 | finally: |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 589 | self.quitting = True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 590 | sys.settrace(None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 592 | def runeval(self, expr, globals=None, locals=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 593 | """Debug an expression executed via the eval() function. |
| 594 | |
| 595 | globals defaults to __main__.dict; locals defaults to globals. |
| 596 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 597 | if globals is None: |
| 598 | import __main__ |
| 599 | globals = __main__.__dict__ |
| 600 | if locals is None: |
| 601 | locals = globals |
| 602 | self.reset() |
| 603 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 604 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 605 | return eval(expr, globals, locals) |
| 606 | except BdbQuit: |
| 607 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 608 | finally: |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 609 | self.quitting = True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 610 | sys.settrace(None) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 612 | def runctx(self, cmd, globals, locals): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 613 | """For backwards-compatibility. Defers to run().""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 614 | # B/W compatibility |
| 615 | self.run(cmd, globals, locals) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 617 | # This method is more useful to debug a single function call. |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 618 | |
Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 619 | def runcall(self, func, *args, **kwds): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 620 | """Debug a single function call. |
| 621 | |
| 622 | Return the result of the function call. |
| 623 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 624 | self.reset() |
| 625 | sys.settrace(self.trace_dispatch) |
| 626 | res = None |
| 627 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 628 | res = func(*args, **kwds) |
| 629 | except BdbQuit: |
| 630 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 631 | finally: |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 632 | self.quitting = True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 633 | sys.settrace(None) |
| 634 | return res |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 636 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 637 | def set_trace(): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 638 | """Start debugging with a Bdb instance from the caller's frame.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 639 | Bdb().set_trace() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 640 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 641 | |
| 642 | class Breakpoint: |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 643 | """Breakpoint class. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 645 | Implements temporary breakpoints, ignore counts, disabling and |
| 646 | (re)-enabling, and conditionals. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 647 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 648 | Breakpoints are indexed by number through bpbynumber and by |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 649 | the (file, line) tuple using bplist. The former points to a |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 650 | single instance of class Breakpoint. The latter points to a |
| 651 | list of such instances since there may be more than one |
| 652 | breakpoint per line. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 653 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 654 | When creating a breakpoint, its associated filename should be |
| 655 | in canonical form. If funcname is defined, a breakpoint hit will be |
| 656 | counted when the first line of that function is executed. A |
| 657 | conditional breakpoint always counts a hit. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 658 | """ |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 659 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 660 | # XXX Keeping state in the class is a mistake -- this means |
| 661 | # you cannot have more than one active Bdb instance. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 662 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 663 | next = 1 # Next bp to be assigned |
| 664 | bplist = {} # indexed by (file, lineno) tuple |
| 665 | bpbynumber = [None] # Each entry is None or an instance of Bpt |
| 666 | # index 0 is unused, except for marking an |
| 667 | # effective break .... see effective() |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 668 | |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 669 | def __init__(self, file, line, temporary=False, cond=None, funcname=None): |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 670 | self.funcname = funcname |
| 671 | # Needed if funcname is not None. |
| 672 | self.func_first_executable_line = None |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 673 | self.file = file # This better be in canonical form! |
| 674 | self.line = line |
| 675 | self.temporary = temporary |
| 676 | self.cond = cond |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 677 | self.enabled = True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 678 | self.ignore = 0 |
| 679 | self.hits = 0 |
| 680 | self.number = Breakpoint.next |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 681 | Breakpoint.next += 1 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 682 | # Build the two lists |
| 683 | self.bpbynumber.append(self) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 684 | if (file, line) in self.bplist: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 685 | self.bplist[file, line].append(self) |
| 686 | else: |
| 687 | self.bplist[file, line] = [self] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 689 | def deleteMe(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 690 | """Delete the breakpoint from the list associated to a file:line. |
| 691 | |
| 692 | If it is the last breakpoint in that position, it also deletes |
| 693 | the entry for the file:line. |
| 694 | """ |
| 695 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 696 | index = (self.file, self.line) |
| 697 | self.bpbynumber[self.number] = None # No longer in list |
| 698 | self.bplist[index].remove(self) |
| 699 | if not self.bplist[index]: |
| 700 | # No more bp for this f:l combo |
| 701 | del self.bplist[index] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 702 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 703 | def enable(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 704 | """Mark the breakpoint as enabled.""" |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 705 | self.enabled = True |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 706 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 707 | def disable(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 708 | """Mark the breakpoint as disabled.""" |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 709 | self.enabled = False |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 710 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 711 | def bpprint(self, out=None): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 712 | """Print the output of bpformat(). |
| 713 | |
| 714 | The optional out argument directs where the output is sent |
| 715 | and defaults to standard output. |
| 716 | """ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 717 | if out is None: |
| 718 | out = sys.stdout |
Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 719 | print(self.bpformat(), file=out) |
| 720 | |
| 721 | def bpformat(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 722 | """Return a string with information about the breakpoint. |
| 723 | |
| 724 | The information includes the breakpoint number, temporary |
| 725 | status, file:line position, break condition, number of times to |
| 726 | ignore, and number of times hit. |
| 727 | |
| 728 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 729 | if self.temporary: |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 730 | disp = 'del ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 731 | else: |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 732 | disp = 'keep ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 733 | if self.enabled: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 734 | disp = disp + 'yes ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 735 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 736 | disp = disp + 'no ' |
Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 737 | ret = '%-4dbreakpoint %s at %s:%d' % (self.number, disp, |
| 738 | self.file, self.line) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 739 | if self.cond: |
Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 740 | ret += '\n\tstop only if %s' % (self.cond,) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 741 | if self.ignore: |
Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 742 | ret += '\n\tignore next %d hits' % (self.ignore,) |
| 743 | if self.hits: |
| 744 | if self.hits > 1: |
| 745 | ss = 's' |
| 746 | else: |
| 747 | ss = '' |
| 748 | ret += '\n\tbreakpoint already hit %d time%s' % (self.hits, ss) |
| 749 | return ret |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 750 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 751 | def __str__(self): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 752 | "Return a condensed description of the breakpoint." |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 753 | return 'breakpoint %s at %s:%s' % (self.number, self.file, self.line) |
| 754 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 755 | # -----------end of Breakpoint class---------- |
| 756 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 757 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 758 | def checkfuncname(b, frame): |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 759 | """Return True if break should happen here. |
| 760 | |
| 761 | Whether a break should happen depends on the way that b (the breakpoint) |
| 762 | was set. If it was set via line number, check if b.line is the same as |
| 763 | the one in the frame. If it was set via function name, check if this is |
| 764 | the right function and if it is on the first executable line. |
| 765 | """ |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 766 | if not b.funcname: |
| 767 | # Breakpoint was set via line number. |
| 768 | if b.line != frame.f_lineno: |
| 769 | # Breakpoint was set at a line with a def statement and the function |
| 770 | # defined is called: don't break. |
| 771 | return False |
| 772 | return True |
| 773 | |
| 774 | # Breakpoint set via function name. |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 775 | if frame.f_code.co_name != b.funcname: |
| 776 | # It's not a function call, but rather execution of def statement. |
| 777 | return False |
| 778 | |
| 779 | # We are in the right frame. |
| 780 | if not b.func_first_executable_line: |
| 781 | # The function is entered for the 1st time. |
| 782 | b.func_first_executable_line = frame.f_lineno |
| 783 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 784 | if b.func_first_executable_line != frame.f_lineno: |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 785 | # But we are not at the first line number: don't break. |
| 786 | return False |
| 787 | return True |
| 788 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 789 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 790 | # Determines if there is an effective (active) breakpoint at this |
| 791 | # line of code. Returns breakpoint number or 0 if none |
| 792 | def effective(file, line, frame): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 793 | """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] | 794 | |
csabella | 0774e79 | 2017-05-16 18:28:02 -0400 | [diff] [blame] | 795 | Called only if we know there is a breakpoint at this location. Return |
| 796 | the breakpoint that was triggered and a boolean that indicates if it is |
| 797 | ok to delete a temporary breakpoint. Return (None, None) if there is no |
| 798 | matching breakpoint. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 799 | """ |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 800 | possibles = Breakpoint.bplist[file, line] |
| 801 | for b in possibles: |
| 802 | if not b.enabled: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 803 | continue |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 804 | if not checkfuncname(b, frame): |
| 805 | continue |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 806 | # Count every hit when bp is enabled |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 807 | b.hits += 1 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 808 | if not b.cond: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 809 | # If unconditional, and ignoring go on to next, else break |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 810 | if b.ignore > 0: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 811 | b.ignore -= 1 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 812 | continue |
| 813 | else: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 814 | # breakpoint and marker that it's ok to delete if temporary |
| 815 | return (b, True) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 816 | else: |
| 817 | # Conditional bp. |
| 818 | # Ignore count applies only to those bpt hits where the |
| 819 | # condition evaluates to true. |
| 820 | try: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 821 | val = eval(b.cond, frame.f_globals, frame.f_locals) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 822 | if val: |
| 823 | if b.ignore > 0: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 824 | b.ignore -= 1 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 825 | # continue |
| 826 | else: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 827 | return (b, True) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 828 | # else: |
| 829 | # continue |
| 830 | except: |
Georg Brandl | 9aed6cc | 2010-11-26 12:05:27 +0000 | [diff] [blame] | 831 | # if eval fails, most conservative thing is to stop on |
| 832 | # breakpoint regardless of ignore count. Don't delete |
| 833 | # temporary, as another hint to user. |
| 834 | return (b, False) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 835 | return (None, None) |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 836 | |
Georg Brandl | 2660747 | 2010-11-29 20:12:24 +0000 | [diff] [blame] | 837 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 838 | # -------------------- testing -------------------- |
| 839 | |
| 840 | class Tdb(Bdb): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 841 | def user_call(self, frame, args): |
| 842 | name = frame.f_code.co_name |
| 843 | if not name: name = '???' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 844 | print('+++ call', name, args) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 845 | def user_line(self, frame): |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 846 | import linecache |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 847 | name = frame.f_code.co_name |
| 848 | if not name: name = '???' |
| 849 | fn = self.canonic(frame.f_code.co_filename) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 850 | line = linecache.getline(fn, frame.f_lineno, frame.f_globals) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 851 | print('+++', fn, frame.f_lineno, name, ':', line.strip()) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 852 | def user_return(self, frame, retval): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 853 | print('+++ return', retval) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 854 | def user_exception(self, frame, exc_stuff): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 855 | print('+++ exception', exc_stuff) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 856 | self.set_continue() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 857 | |
| 858 | def foo(n): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 859 | print('foo(', n, ')') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 860 | x = bar(n*10) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 861 | print('bar returned', x) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 862 | |
| 863 | def bar(a): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 864 | print('bar(', a, ')') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 865 | return a/2 |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 866 | |
| 867 | def test(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 868 | t = Tdb() |
| 869 | t.run('import bdb; bdb.foo(10)') |