blob: eb2fa4fb3e97eea388018a069ca1625302d2ab76 [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Debugger basics"""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00002
Georg Brandl243ad662009-05-05 09:00:19 +00003import fnmatch
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00004import sys
Barry Warsaw148ffbc1999-09-09 23:24:33 +00005import os
Guido van Rossum8820c232013-11-21 11:30:06 -08006from inspect import CO_GENERATOR
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00007
Georg Brandl26607472010-11-29 20:12:24 +00008__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00009
csabella0774e792017-05-16 18:28:02 -040010
Neal Norwitz93cf79f2002-03-31 14:06:41 +000011class BdbQuit(Exception):
Georg Brandl26607472010-11-29 20:12:24 +000012 """Exception to give up completely."""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000013
14
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000015class Bdb:
Guido van Rossum4acc25b2000-02-02 15:10:15 +000016 """Generic Python debugger base class.
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000017
Guido van Rossum4acc25b2000-02-02 15:10:15 +000018 This class takes care of details of the trace facility;
19 a derived class should implement user interaction.
20 The standard debugger class (pdb.Pdb) is an example.
csabella0774e792017-05-16 18:28:02 -040021
22 The optional skip argument must be an iterable of glob-style
23 module name patterns. The debugger will not step into frames
24 that originate in a module that matches one of these patterns.
25 Whether a frame is considered to originate in a certain module
26 is determined by the __name__ in the frame globals.
Guido van Rossum4acc25b2000-02-02 15:10:15 +000027 """
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000028
Georg Brandl243ad662009-05-05 09:00:19 +000029 def __init__(self, skip=None):
30 self.skip = set(skip) if skip else None
Guido van Rossum4acc25b2000-02-02 15:10:15 +000031 self.breaks = {}
32 self.fncache = {}
Senthil Kumaran42d70812012-05-01 10:07:49 +080033 self.frame_returning = None
Barry Warsaw148ffbc1999-09-09 23:24:33 +000034
Guido van Rossum4acc25b2000-02-02 15:10:15 +000035 def canonic(self, filename):
csabella0774e792017-05-16 18:28:02 -040036 """Return canonical form of filename.
37
38 For real filenames, the canonical form is a case-normalized (on
39 case insenstive filesystems) absolute path. 'Filenames' with
40 angle brackets, such as "<stdin>", generated in interactive
41 mode, are returned unchanged.
42 """
Guido van Rossum42f53322001-11-29 02:50:15 +000043 if filename == "<" + filename[1:-1] + ">":
44 return filename
Guido van Rossum4acc25b2000-02-02 15:10:15 +000045 canonic = self.fncache.get(filename)
46 if not canonic:
47 canonic = os.path.abspath(filename)
Guido van Rossumbdba3202002-02-25 23:23:24 +000048 canonic = os.path.normcase(canonic)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000049 self.fncache[filename] = canonic
50 return canonic
Tim Peters11cf6052001-01-14 21:54:20 +000051
Guido van Rossum4acc25b2000-02-02 15:10:15 +000052 def reset(self):
csabella0774e792017-05-16 18:28:02 -040053 """Set values of attributes as ready to start debugging."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +000054 import linecache
55 linecache.checkcache()
56 self.botframe = None
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000057 self._set_stopinfo(None, None)
Tim Peters11cf6052001-01-14 21:54:20 +000058
Guido van Rossum4acc25b2000-02-02 15:10:15 +000059 def trace_dispatch(self, frame, event, arg):
csabella0774e792017-05-16 18:28:02 -040060 """Dispatch a trace function for debugged frames based on the event.
61
62 This function is installed as the trace function for debugged
63 frames. Its return value is the new trace function, which is
64 usually itself. The default implementation decides how to
65 dispatch a frame, depending on the type of event (passed in as a
66 string) that is about to be executed.
67
68 The event can be one of the following:
69 line: A new line of code is going to be executed.
70 call: A function is about to be called or another code block
71 is entered.
72 return: A function or other code block is about to return.
73 exception: An exception has occurred.
74 c_call: A C function is about to be called.
75 c_return: A C functon has returned.
76 c_exception: A C function has raised an exception.
77
78 For the Python events, specialized functions (see the dispatch_*()
79 methods) are called. For the C events, no action is taken.
80
81 The arg parameter depends on the previous event.
82 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +000083 if self.quitting:
84 return # None
85 if event == 'line':
86 return self.dispatch_line(frame)
87 if event == 'call':
88 return self.dispatch_call(frame, arg)
89 if event == 'return':
90 return self.dispatch_return(frame, arg)
91 if event == 'exception':
92 return self.dispatch_exception(frame, arg)
Nicholas Bastinc69ebe82004-03-24 21:57:10 +000093 if event == 'c_call':
94 return self.trace_dispatch
95 if event == 'c_exception':
96 return self.trace_dispatch
97 if event == 'c_return':
98 return self.trace_dispatch
Guido van Rossumbe19ed72007-02-09 05:37:30 +000099 print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000100 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +0000101
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000102 def dispatch_line(self, frame):
csabella0774e792017-05-16 18:28:02 -0400103 """Invoke user function and return trace function for line event.
104
105 If the debugger stops on the current line, invoke
106 self.user_line(). Raise BdbQuit if self.quitting is set.
107 Return self.trace_dispatch to continue tracing in this scope.
108 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000109 if self.stop_here(frame) or self.break_here(frame):
110 self.user_line(frame)
111 if self.quitting: raise BdbQuit
112 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +0000113
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000114 def dispatch_call(self, frame, arg):
csabella0774e792017-05-16 18:28:02 -0400115 """Invoke user function and return trace function for call event.
116
117 If the debugger stops on this function call, invoke
118 self.user_call(). Raise BbdQuit if self.quitting is set.
119 Return self.trace_dispatch to continue tracing in this scope.
120 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000121 # XXX 'arg' is no longer used
122 if self.botframe is None:
123 # First call of dispatch since reset()
Christian Tismer313a7512002-05-28 08:04:00 +0000124 self.botframe = frame.f_back # (CT) Note that this may also be None!
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000125 return self.trace_dispatch
126 if not (self.stop_here(frame) or self.break_anywhere(frame)):
127 # No need to trace this function
128 return # None
Guido van Rossum8820c232013-11-21 11:30:06 -0800129 # Ignore call events in generator except when stepping.
130 if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
131 return self.trace_dispatch
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000132 self.user_call(frame, arg)
133 if self.quitting: raise BdbQuit
134 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +0000135
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000136 def dispatch_return(self, frame, arg):
csabella0774e792017-05-16 18:28:02 -0400137 """Invoke user function and return trace function for return event.
138
139 If the debugger stops on this function return, invoke
140 self.user_return(). Raise BdbQuit if self.quitting is set.
141 Return self.trace_dispatch to continue tracing in this scope.
142 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000143 if self.stop_here(frame) or frame == self.returnframe:
Guido van Rossum8820c232013-11-21 11:30:06 -0800144 # Ignore return events in generator except when stepping.
145 if self.stopframe and frame.f_code.co_flags & CO_GENERATOR:
146 return self.trace_dispatch
Senthil Kumaran42d70812012-05-01 10:07:49 +0800147 try:
148 self.frame_returning = frame
149 self.user_return(frame, arg)
150 finally:
151 self.frame_returning = None
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000152 if self.quitting: raise BdbQuit
Guido van Rossum8820c232013-11-21 11:30:06 -0800153 # The user issued a 'next' or 'until' command.
154 if self.stopframe is frame and self.stoplineno != -1:
155 self._set_stopinfo(None, None)
Just van Rossumae1f65f2001-06-25 18:01:24 +0000156 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +0000157
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000158 def dispatch_exception(self, frame, arg):
csabella0774e792017-05-16 18:28:02 -0400159 """Invoke user function and return trace function for exception event.
160
161 If the debugger stops on this exception, invoke
162 self.user_exception(). Raise BdbQuit if self.quitting is set.
163 Return self.trace_dispatch to continue tracing in this scope.
164 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000165 if self.stop_here(frame):
Guido van Rossum8820c232013-11-21 11:30:06 -0800166 # When stepping with next/until/return in a generator frame, skip
167 # the internal StopIteration exception (with no traceback)
168 # triggered by a subiterator run with the 'yield from' statement.
169 if not (frame.f_code.co_flags & CO_GENERATOR
170 and arg[0] is StopIteration and arg[2] is None):
171 self.user_exception(frame, arg)
172 if self.quitting: raise BdbQuit
173 # Stop at the StopIteration or GeneratorExit exception when the user
174 # has set stopframe in a generator by issuing a return command, or a
175 # next/until command at the last statement in the generator before the
176 # exception.
177 elif (self.stopframe and frame is not self.stopframe
178 and self.stopframe.f_code.co_flags & CO_GENERATOR
179 and arg[0] in (StopIteration, GeneratorExit)):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000180 self.user_exception(frame, arg)
181 if self.quitting: raise BdbQuit
Guido van Rossum8820c232013-11-21 11:30:06 -0800182
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000183 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +0000184
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000185 # Normally derived classes don't override the following
186 # methods, but they may if they want to redefine the
187 # definition of stopping and breakpoints.
Tim Peters11cf6052001-01-14 21:54:20 +0000188
Georg Brandl243ad662009-05-05 09:00:19 +0000189 def is_skipped_module(self, module_name):
csabella0774e792017-05-16 18:28:02 -0400190 "Return True if module_name matches any skip pattern."
Georg Brandl243ad662009-05-05 09:00:19 +0000191 for pattern in self.skip:
192 if fnmatch.fnmatch(module_name, pattern):
193 return True
194 return False
195
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000196 def stop_here(self, frame):
csabella0774e792017-05-16 18:28:02 -0400197 "Return True if frame is below the starting frame in the stack."
Neal Norwitz72a2b4d2002-05-29 00:54:38 +0000198 # (CT) stopframe may now also be None, see dispatch_call.
199 # (CT) the former test for None is therefore removed from here.
Georg Brandl243ad662009-05-05 09:00:19 +0000200 if self.skip and \
201 self.is_skipped_module(frame.f_globals.get('__name__')):
202 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000203 if frame is self.stopframe:
Georg Brandl3f940892010-07-30 10:29:19 +0000204 if self.stoplineno == -1:
205 return False
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000206 return frame.f_lineno >= self.stoplineno
Guido van Rossum8820c232013-11-21 11:30:06 -0800207 if not self.stopframe:
208 return True
Tim Petersbc0e9102002-04-04 22:55:58 +0000209 return False
Guido van Rossumd93643f1998-09-11 22:38:35 +0000210
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000211 def break_here(self, frame):
csabella0774e792017-05-16 18:28:02 -0400212 """Return True if there is an effective breakpoint for this line.
213
214 Check for line or function breakpoint and if in effect.
215 Delete temporary breakpoints if effective() says to.
216 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000217 filename = self.canonic(frame.f_code.co_filename)
Georg Brandl26607472010-11-29 20:12:24 +0000218 if filename not in self.breaks:
Tim Petersbc0e9102002-04-04 22:55:58 +0000219 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000220 lineno = frame.f_lineno
Georg Brandl26607472010-11-29 20:12:24 +0000221 if lineno not in self.breaks[filename]:
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000222 # The line itself has no breakpoint, but maybe the line is the
223 # first line of a function with breakpoint set by function name.
224 lineno = frame.f_code.co_firstlineno
Georg Brandl26607472010-11-29 20:12:24 +0000225 if lineno not in self.breaks[filename]:
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000226 return False
227
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000228 # flag says ok to delete temp. bp
229 (bp, flag) = effective(filename, lineno, frame)
230 if bp:
231 self.currentbp = bp.number
232 if (flag and bp.temporary):
233 self.do_clear(str(bp.number))
Tim Petersbc0e9102002-04-04 22:55:58 +0000234 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000235 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000236 return False
Tim Peters11cf6052001-01-14 21:54:20 +0000237
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000238 def do_clear(self, arg):
csabella0774e792017-05-16 18:28:02 -0400239 """Remove temporary breakpoint.
240
241 Must implement in derived classes or get NotImplementedError.
242 """
Collin Winterce36ad82007-08-30 01:19:48 +0000243 raise NotImplementedError("subclass of bdb must implement do_clear()")
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000244
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000245 def break_anywhere(self, frame):
csabella0774e792017-05-16 18:28:02 -0400246 """Return True if there is any breakpoint for frame's filename.
247 """
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000248 return self.canonic(frame.f_code.co_filename) in self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000249
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000250 # Derived classes should override the user_* methods
251 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000252
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000253 def user_call(self, frame, argument_list):
csabella0774e792017-05-16 18:28:02 -0400254 """Called if we might stop in a function."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000255 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000256
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000257 def user_line(self, frame):
csabella0774e792017-05-16 18:28:02 -0400258 """Called when when we stop or break at a line."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000259 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000260
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000261 def user_return(self, frame, return_value):
csabella0774e792017-05-16 18:28:02 -0400262 """Called when a return trap is set here."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000263 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000264
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000265 def user_exception(self, frame, exc_info):
csabella0774e792017-05-16 18:28:02 -0400266 """Called when we stop on an exception."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000267 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000268
Georg Brandl3f940892010-07-30 10:29:19 +0000269 def _set_stopinfo(self, stopframe, returnframe, stoplineno=0):
csabella0774e792017-05-16 18:28:02 -0400270 """Set the attributes for stopping.
271
272 If stoplineno is greater than or equal to 0, then stop at line
273 greater than or equal to the stopline. If stoplineno is -1, then
274 don't stop at all.
275 """
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000276 self.stopframe = stopframe
277 self.returnframe = returnframe
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000278 self.quitting = False
Georg Brandl3f940892010-07-30 10:29:19 +0000279 # stoplineno >= 0 means: stop at line >= the stoplineno
280 # stoplineno -1 means: don't stop at all
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000281 self.stoplineno = stoplineno
282
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000283 # Derived classes and clients can call the following methods
284 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000285
Georg Brandl2dfec552010-07-30 08:43:32 +0000286 def set_until(self, frame, lineno=None):
csabella0774e792017-05-16 18:28:02 -0400287 """Stop when the line with the lineno greater than the current one is
288 reached or when returning from current frame."""
Georg Brandl2dfec552010-07-30 08:43:32 +0000289 # the name "until" is borrowed from gdb
290 if lineno is None:
291 lineno = frame.f_lineno + 1
292 self._set_stopinfo(frame, frame, lineno)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000293
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000294 def set_step(self):
295 """Stop after one line of code."""
Senthil Kumaran42d70812012-05-01 10:07:49 +0800296 # Issue #13183: pdb skips frames after hitting a breakpoint and running
297 # step commands.
298 # Restore the trace function in the caller (that may not have been set
299 # for performance reasons) when returning from the current frame.
300 if self.frame_returning:
301 caller_frame = self.frame_returning.f_back
302 if caller_frame and not caller_frame.f_trace:
303 caller_frame.f_trace = self.trace_dispatch
Georg Brandl3f940892010-07-30 10:29:19 +0000304 self._set_stopinfo(None, None)
Tim Peters11cf6052001-01-14 21:54:20 +0000305
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000306 def set_next(self, frame):
307 """Stop on the next line in or below the given frame."""
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000308 self._set_stopinfo(frame, None)
Tim Peters11cf6052001-01-14 21:54:20 +0000309
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000310 def set_return(self, frame):
311 """Stop when returning from the given frame."""
Guido van Rossum8820c232013-11-21 11:30:06 -0800312 if frame.f_code.co_flags & CO_GENERATOR:
313 self._set_stopinfo(frame, None, -1)
314 else:
315 self._set_stopinfo(frame.f_back, frame)
Tim Peters11cf6052001-01-14 21:54:20 +0000316
Johannes Gijsbers84a6c202004-11-07 11:35:30 +0000317 def set_trace(self, frame=None):
csabella0774e792017-05-16 18:28:02 -0400318 """Start debugging from frame.
Johannes Gijsbers84a6c202004-11-07 11:35:30 +0000319
320 If frame is not specified, debugging starts from caller's frame.
321 """
322 if frame is None:
323 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000324 self.reset()
325 while frame:
326 frame.f_trace = self.trace_dispatch
327 self.botframe = frame
328 frame = frame.f_back
329 self.set_step()
330 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000331
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000332 def set_continue(self):
csabella0774e792017-05-16 18:28:02 -0400333 """Stop only at breakpoints or when finished.
334
335 If there are no breakpoints, set the system trace function to None.
336 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000337 # Don't stop except at breakpoints or when finished
Georg Brandl3f940892010-07-30 10:29:19 +0000338 self._set_stopinfo(self.botframe, None, -1)
Georg Brandlf8b893e2010-12-04 16:22:44 +0000339 if not self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000340 # no breakpoints; run without debugger overhead
341 sys.settrace(None)
Christian Tismer313a7512002-05-28 08:04:00 +0000342 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000343 while frame and frame is not self.botframe:
344 del frame.f_trace
345 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000346
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000347 def set_quit(self):
csabella0774e792017-05-16 18:28:02 -0400348 """Set quitting attribute to True.
349
350 Raises BdbQuit exception in the next call to a dispatch_*() method.
351 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000352 self.stopframe = self.botframe
353 self.returnframe = None
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000354 self.quitting = True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000355 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000356
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000357 # Derived classes and clients can call the following methods
358 # to manipulate breakpoints. These methods return an
csabella0774e792017-05-16 18:28:02 -0400359 # error message if something went wrong, None if all is well.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000360 # Set_break prints out the breakpoint line and file:lineno.
361 # Call self.get_*break*() to see the breakpoints or better
362 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000363
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000364 def set_break(self, filename, lineno, temporary=False, cond=None,
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000365 funcname=None):
csabella0774e792017-05-16 18:28:02 -0400366 """Set a new breakpoint for filename:lineno.
367
368 If lineno doesn't exist for the filename, return an error message.
369 The filename should be in canonical form.
370 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000371 filename = self.canonic(filename)
372 import linecache # Import as late as possible
373 line = linecache.getline(filename, lineno)
374 if not line:
Georg Brandl26607472010-11-29 20:12:24 +0000375 return 'Line %s:%d does not exist' % (filename, lineno)
376 list = self.breaks.setdefault(filename, [])
377 if lineno not in list:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000378 list.append(lineno)
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000379 bp = Breakpoint(filename, lineno, temporary, cond, funcname)
csabella0774e792017-05-16 18:28:02 -0400380 return None
Guido van Rossumd93643f1998-09-11 22:38:35 +0000381
Senthil Kumaran6f107042010-11-29 11:54:17 +0000382 def _prune_breaks(self, filename, lineno):
csabella0774e792017-05-16 18:28:02 -0400383 """Prune breakpoints for filname:lineno.
384
385 A list of breakpoints is maintained in the Bdb instance and in
386 the Breakpoint class. If a breakpoint in the Bdb instance no
387 longer exists in the Breakpoint class, then it's removed from the
388 Bdb instance.
389 """
Senthil Kumaran6f107042010-11-29 11:54:17 +0000390 if (filename, lineno) not in Breakpoint.bplist:
391 self.breaks[filename].remove(lineno)
392 if not self.breaks[filename]:
393 del self.breaks[filename]
394
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000395 def clear_break(self, filename, lineno):
csabella0774e792017-05-16 18:28:02 -0400396 """Delete breakpoints for filename:lineno.
397
398 If no breakpoints were set, return an error message.
399 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000400 filename = self.canonic(filename)
Georg Brandl26607472010-11-29 20:12:24 +0000401 if filename not in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000402 return 'There are no breakpoints in %s' % filename
403 if lineno not in self.breaks[filename]:
Georg Brandl26607472010-11-29 20:12:24 +0000404 return 'There is no breakpoint at %s:%d' % (filename, lineno)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000405 # If there's only one bp in the list for that file,line
406 # pair, then remove the breaks entry
407 for bp in Breakpoint.bplist[filename, lineno][:]:
408 bp.deleteMe()
Senthil Kumaran6f107042010-11-29 11:54:17 +0000409 self._prune_breaks(filename, lineno)
csabella0774e792017-05-16 18:28:02 -0400410 return None
Tim Peters11cf6052001-01-14 21:54:20 +0000411
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000412 def clear_bpbynumber(self, arg):
csabella0774e792017-05-16 18:28:02 -0400413 """Delete a breakpoint by its index in Breakpoint.bpbynumber.
414
415 If arg is invalid, return an error message.
416 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000417 try:
Georg Brandl7410dd12010-07-30 12:01:20 +0000418 bp = self.get_bpbynumber(arg)
419 except ValueError as err:
420 return str(err)
Senthil Kumaran6f107042010-11-29 11:54:17 +0000421 bp.deleteMe()
422 self._prune_breaks(bp.file, bp.line)
csabella0774e792017-05-16 18:28:02 -0400423 return None
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000424
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000425 def clear_all_file_breaks(self, filename):
csabella0774e792017-05-16 18:28:02 -0400426 """Delete all breakpoints in filename.
427
428 If none were set, return an error message.
429 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000430 filename = self.canonic(filename)
Georg Brandl26607472010-11-29 20:12:24 +0000431 if filename not in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000432 return 'There are no breakpoints in %s' % filename
433 for line in self.breaks[filename]:
434 blist = Breakpoint.bplist[filename, line]
435 for bp in blist:
436 bp.deleteMe()
437 del self.breaks[filename]
csabella0774e792017-05-16 18:28:02 -0400438 return None
Tim Peters11cf6052001-01-14 21:54:20 +0000439
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000440 def clear_all_breaks(self):
csabella0774e792017-05-16 18:28:02 -0400441 """Delete all existing breakpoints.
442
443 If none were set, return an error message.
444 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000445 if not self.breaks:
446 return 'There are no breakpoints'
447 for bp in Breakpoint.bpbynumber:
448 if bp:
449 bp.deleteMe()
450 self.breaks = {}
csabella0774e792017-05-16 18:28:02 -0400451 return None
Tim Peters11cf6052001-01-14 21:54:20 +0000452
Georg Brandl7410dd12010-07-30 12:01:20 +0000453 def get_bpbynumber(self, arg):
csabella0774e792017-05-16 18:28:02 -0400454 """Return a breakpoint by its index in Breakpoint.bybpnumber.
455
456 For invalid arg values or if the breakpoint doesn't exist,
457 raise a ValueError.
458 """
Georg Brandl7410dd12010-07-30 12:01:20 +0000459 if not arg:
460 raise ValueError('Breakpoint number expected')
461 try:
462 number = int(arg)
463 except ValueError:
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300464 raise ValueError('Non-numeric breakpoint number %s' % arg) from None
Georg Brandl7410dd12010-07-30 12:01:20 +0000465 try:
466 bp = Breakpoint.bpbynumber[number]
467 except IndexError:
Serhiy Storchaka5affd232017-04-05 09:37:24 +0300468 raise ValueError('Breakpoint number %d out of range' % number) from None
Georg Brandl7410dd12010-07-30 12:01:20 +0000469 if bp is None:
470 raise ValueError('Breakpoint %d already deleted' % number)
471 return bp
472
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000473 def get_break(self, filename, lineno):
csabella0774e792017-05-16 18:28:02 -0400474 """Return True if there is a breakpoint for filename:lineno."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000475 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000476 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000477 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000478
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000479 def get_breaks(self, filename, lineno):
csabella0774e792017-05-16 18:28:02 -0400480 """Return all breakpoints for filename:lineno.
481
482 If no breakpoints are set, return an empty list.
483 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000484 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000485 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000486 lineno in self.breaks[filename] and \
487 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000488
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000489 def get_file_breaks(self, filename):
csabella0774e792017-05-16 18:28:02 -0400490 """Return all lines with breakpoints for filename.
491
492 If no breakpoints are set, return an empty list.
493 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000494 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000495 if filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000496 return self.breaks[filename]
497 else:
498 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000499
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000500 def get_all_breaks(self):
csabella0774e792017-05-16 18:28:02 -0400501 """Return all breakpoints that are set."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000502 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000503
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000504 # Derived classes and clients can call the following method
505 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000506
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000507 def get_stack(self, f, t):
csabella0774e792017-05-16 18:28:02 -0400508 """Return a list of (frame, lineno) in a stack trace and a size.
509
510 List starts with original calling frame, if there is one.
511 Size may be number of frames above or below f.
512 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000513 stack = []
514 if t and t.tb_frame is f:
515 t = t.tb_next
516 while f is not None:
517 stack.append((f, f.f_lineno))
518 if f is self.botframe:
519 break
520 f = f.f_back
521 stack.reverse()
522 i = max(0, len(stack) - 1)
523 while t is not None:
524 stack.append((t.tb_frame, t.tb_lineno))
525 t = t.tb_next
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000526 if f is None:
527 i = max(0, len(stack) - 1)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000528 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000529
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000530 def format_stack_entry(self, frame_lineno, lprefix=': '):
csabella0774e792017-05-16 18:28:02 -0400531 """Return a string with information about a stack entry.
532
533 The stack entry frame_lineno is a (frame, lineno) tuple. The
534 return string contains the canonical filename, the function name
535 or '<lambda>', the input arguments, the return value, and the
536 line of code (if it exists).
537
538 """
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000539 import linecache, reprlib
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000540 frame, lineno = frame_lineno
541 filename = self.canonic(frame.f_code.co_filename)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000542 s = '%s(%r)' % (filename, lineno)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000543 if frame.f_code.co_name:
Georg Brandl26607472010-11-29 20:12:24 +0000544 s += frame.f_code.co_name
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000545 else:
Georg Brandl26607472010-11-29 20:12:24 +0000546 s += "<lambda>"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000547 if '__args__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000548 args = frame.f_locals['__args__']
549 else:
550 args = None
551 if args:
Georg Brandl26607472010-11-29 20:12:24 +0000552 s += reprlib.repr(args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000553 else:
Georg Brandl26607472010-11-29 20:12:24 +0000554 s += '()'
Raymond Hettinger54f02222002-06-01 14:18:47 +0000555 if '__return__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000556 rv = frame.f_locals['__return__']
Georg Brandl26607472010-11-29 20:12:24 +0000557 s += '->'
558 s += reprlib.repr(rv)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000559 line = linecache.getline(filename, lineno, frame.f_globals)
Georg Brandl26607472010-11-29 20:12:24 +0000560 if line:
561 s += lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000562 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000563
Georg Brandl46b9afc2010-07-30 09:14:20 +0000564 # The following methods can be called by clients to use
565 # a debugger to debug a statement or an expression.
566 # Both can be given as a string, or a code object.
Tim Peters11cf6052001-01-14 21:54:20 +0000567
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000568 def run(self, cmd, globals=None, locals=None):
csabella0774e792017-05-16 18:28:02 -0400569 """Debug a statement executed via the exec() function.
570
571 globals defaults to __main__.dict; locals defaults to globals.
572 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000573 if globals is None:
574 import __main__
575 globals = __main__.__dict__
576 if locals is None:
577 locals = globals
578 self.reset()
Victor Stinner4bd81722011-01-06 00:49:38 +0000579 if isinstance(cmd, str):
580 cmd = compile(cmd, "<string>", "exec")
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000581 sys.settrace(self.trace_dispatch)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000582 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000583 exec(cmd, globals, locals)
584 except BdbQuit:
585 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000586 finally:
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000587 self.quitting = True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000588 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000589
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000590 def runeval(self, expr, globals=None, locals=None):
csabella0774e792017-05-16 18:28:02 -0400591 """Debug an expression executed via the eval() function.
592
593 globals defaults to __main__.dict; locals defaults to globals.
594 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000595 if globals is None:
596 import __main__
597 globals = __main__.__dict__
598 if locals is None:
599 locals = globals
600 self.reset()
601 sys.settrace(self.trace_dispatch)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000602 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000603 return eval(expr, globals, locals)
604 except BdbQuit:
605 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000606 finally:
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000607 self.quitting = True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000608 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000609
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000610 def runctx(self, cmd, globals, locals):
csabella0774e792017-05-16 18:28:02 -0400611 """For backwards-compatibility. Defers to run()."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000612 # B/W compatibility
613 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000614
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000615 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000616
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +0000617 def runcall(self, func, *args, **kwds):
csabella0774e792017-05-16 18:28:02 -0400618 """Debug a single function call.
619
620 Return the result of the function call.
621 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000622 self.reset()
623 sys.settrace(self.trace_dispatch)
624 res = None
625 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000626 res = func(*args, **kwds)
627 except BdbQuit:
628 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000629 finally:
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000630 self.quitting = True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000631 sys.settrace(None)
632 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000633
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000634
Guido van Rossumb6775db1994-08-01 11:34:53 +0000635def set_trace():
csabella0774e792017-05-16 18:28:02 -0400636 """Start debugging with a Bdb instance from the caller's frame."""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000637 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000638
Guido van Rossumd93643f1998-09-11 22:38:35 +0000639
640class Breakpoint:
Georg Brandl26607472010-11-29 20:12:24 +0000641 """Breakpoint class.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000642
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000643 Implements temporary breakpoints, ignore counts, disabling and
644 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000645
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000646 Breakpoints are indexed by number through bpbynumber and by
csabella0774e792017-05-16 18:28:02 -0400647 the (file, line) tuple using bplist. The former points to a
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000648 single instance of class Breakpoint. The latter points to a
649 list of such instances since there may be more than one
650 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000651
csabella0774e792017-05-16 18:28:02 -0400652 When creating a breakpoint, its associated filename should be
653 in canonical form. If funcname is defined, a breakpoint hit will be
654 counted when the first line of that function is executed. A
655 conditional breakpoint always counts a hit.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000656 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000657
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000658 # XXX Keeping state in the class is a mistake -- this means
659 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000660
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000661 next = 1 # Next bp to be assigned
662 bplist = {} # indexed by (file, lineno) tuple
663 bpbynumber = [None] # Each entry is None or an instance of Bpt
664 # index 0 is unused, except for marking an
665 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000666
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000667 def __init__(self, file, line, temporary=False, cond=None, funcname=None):
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000668 self.funcname = funcname
669 # Needed if funcname is not None.
670 self.func_first_executable_line = None
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000671 self.file = file # This better be in canonical form!
672 self.line = line
673 self.temporary = temporary
674 self.cond = cond
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000675 self.enabled = True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000676 self.ignore = 0
677 self.hits = 0
678 self.number = Breakpoint.next
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000679 Breakpoint.next += 1
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000680 # Build the two lists
681 self.bpbynumber.append(self)
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000682 if (file, line) in self.bplist:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000683 self.bplist[file, line].append(self)
684 else:
685 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000686
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000687 def deleteMe(self):
csabella0774e792017-05-16 18:28:02 -0400688 """Delete the breakpoint from the list associated to a file:line.
689
690 If it is the last breakpoint in that position, it also deletes
691 the entry for the file:line.
692 """
693
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000694 index = (self.file, self.line)
695 self.bpbynumber[self.number] = None # No longer in list
696 self.bplist[index].remove(self)
697 if not self.bplist[index]:
698 # No more bp for this f:l combo
699 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000700
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000701 def enable(self):
csabella0774e792017-05-16 18:28:02 -0400702 """Mark the breakpoint as enabled."""
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000703 self.enabled = True
Guido van Rossumd93643f1998-09-11 22:38:35 +0000704
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000705 def disable(self):
csabella0774e792017-05-16 18:28:02 -0400706 """Mark the breakpoint as disabled."""
Georg Brandlac9a2bb2010-11-29 20:19:15 +0000707 self.enabled = False
Guido van Rossumd93643f1998-09-11 22:38:35 +0000708
Thomas Wouters477c8d52006-05-27 19:21:47 +0000709 def bpprint(self, out=None):
csabella0774e792017-05-16 18:28:02 -0400710 """Print the output of bpformat().
711
712 The optional out argument directs where the output is sent
713 and defaults to standard output.
714 """
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715 if out is None:
716 out = sys.stdout
Georg Brandld2fd4ca2010-07-30 15:01:23 +0000717 print(self.bpformat(), file=out)
718
719 def bpformat(self):
csabella0774e792017-05-16 18:28:02 -0400720 """Return a string with information about the breakpoint.
721
722 The information includes the breakpoint number, temporary
723 status, file:line position, break condition, number of times to
724 ignore, and number of times hit.
725
726 """
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000727 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000728 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000729 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000730 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000731 if self.enabled:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732 disp = disp + 'yes '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000733 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734 disp = disp + 'no '
Georg Brandld2fd4ca2010-07-30 15:01:23 +0000735 ret = '%-4dbreakpoint %s at %s:%d' % (self.number, disp,
736 self.file, self.line)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000737 if self.cond:
Georg Brandld2fd4ca2010-07-30 15:01:23 +0000738 ret += '\n\tstop only if %s' % (self.cond,)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000739 if self.ignore:
Georg Brandld2fd4ca2010-07-30 15:01:23 +0000740 ret += '\n\tignore next %d hits' % (self.ignore,)
741 if self.hits:
742 if self.hits > 1:
743 ss = 's'
744 else:
745 ss = ''
746 ret += '\n\tbreakpoint already hit %d time%s' % (self.hits, ss)
747 return ret
Guido van Rossumd93643f1998-09-11 22:38:35 +0000748
Georg Brandl7410dd12010-07-30 12:01:20 +0000749 def __str__(self):
csabella0774e792017-05-16 18:28:02 -0400750 "Return a condensed description of the breakpoint."
Georg Brandl7410dd12010-07-30 12:01:20 +0000751 return 'breakpoint %s at %s:%s' % (self.number, self.file, self.line)
752
Guido van Rossumd93643f1998-09-11 22:38:35 +0000753# -----------end of Breakpoint class----------
754
csabella0774e792017-05-16 18:28:02 -0400755
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000756def checkfuncname(b, frame):
csabella0774e792017-05-16 18:28:02 -0400757 """Return True if break should happen here.
758
759 Whether a break should happen depends on the way that b (the breakpoint)
760 was set. If it was set via line number, check if b.line is the same as
761 the one in the frame. If it was set via function name, check if this is
762 the right function and if it is on the first executable line.
763 """
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000764 if not b.funcname:
765 # Breakpoint was set via line number.
766 if b.line != frame.f_lineno:
767 # Breakpoint was set at a line with a def statement and the function
768 # defined is called: don't break.
769 return False
770 return True
771
772 # Breakpoint set via function name.
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000773 if frame.f_code.co_name != b.funcname:
774 # It's not a function call, but rather execution of def statement.
775 return False
776
777 # We are in the right frame.
778 if not b.func_first_executable_line:
779 # The function is entered for the 1st time.
780 b.func_first_executable_line = frame.f_lineno
781
csabella0774e792017-05-16 18:28:02 -0400782 if b.func_first_executable_line != frame.f_lineno:
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000783 # But we are not at the first line number: don't break.
784 return False
785 return True
786
csabella0774e792017-05-16 18:28:02 -0400787
Guido van Rossumd93643f1998-09-11 22:38:35 +0000788# Determines if there is an effective (active) breakpoint at this
789# line of code. Returns breakpoint number or 0 if none
790def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000791 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000792
csabella0774e792017-05-16 18:28:02 -0400793 Called only if we know there is a breakpoint at this location. Return
794 the breakpoint that was triggered and a boolean that indicates if it is
795 ok to delete a temporary breakpoint. Return (None, None) if there is no
796 matching breakpoint.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000797 """
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000798 possibles = Breakpoint.bplist[file, line]
799 for b in possibles:
800 if not b.enabled:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000801 continue
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000802 if not checkfuncname(b, frame):
803 continue
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000804 # Count every hit when bp is enabled
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000805 b.hits += 1
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000806 if not b.cond:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000807 # If unconditional, and ignoring go on to next, else break
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000808 if b.ignore > 0:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000809 b.ignore -= 1
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000810 continue
811 else:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000812 # breakpoint and marker that it's ok to delete if temporary
813 return (b, True)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000814 else:
815 # Conditional bp.
816 # Ignore count applies only to those bpt hits where the
817 # condition evaluates to true.
818 try:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000819 val = eval(b.cond, frame.f_globals, frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000820 if val:
821 if b.ignore > 0:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000822 b.ignore -= 1
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000823 # continue
824 else:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000825 return (b, True)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000826 # else:
827 # continue
828 except:
Georg Brandl9aed6cc2010-11-26 12:05:27 +0000829 # if eval fails, most conservative thing is to stop on
830 # breakpoint regardless of ignore count. Don't delete
831 # temporary, as another hint to user.
832 return (b, False)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000833 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000834
Georg Brandl26607472010-11-29 20:12:24 +0000835
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000836# -------------------- testing --------------------
837
838class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000839 def user_call(self, frame, args):
840 name = frame.f_code.co_name
841 if not name: name = '???'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000842 print('+++ call', name, args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000843 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000844 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000845 name = frame.f_code.co_name
846 if not name: name = '???'
847 fn = self.canonic(frame.f_code.co_filename)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000848 line = linecache.getline(fn, frame.f_lineno, frame.f_globals)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000849 print('+++', fn, frame.f_lineno, name, ':', line.strip())
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000850 def user_return(self, frame, retval):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000851 print('+++ return', retval)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000852 def user_exception(self, frame, exc_stuff):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000853 print('+++ exception', exc_stuff)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000854 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000855
856def foo(n):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000857 print('foo(', n, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000858 x = bar(n*10)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000859 print('bar returned', x)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000860
861def bar(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000862 print('bar(', a, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000863 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000864
865def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000866 t = Tdb()
867 t.run('import bdb; bdb.foo(10)')