blob: 747e0929f0edd35ece3b97f4493b687b70ca8859 [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Debugger basics"""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00002
3import sys
Barry Warsaw148ffbc1999-09-09 23:24:33 +00004import os
Guido van Rossum4808dcb1996-10-15 14:40:21 +00005import types
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00006
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00007__all__ = ["BdbQuit","Bdb","Breakpoint"]
8
Neal Norwitz93cf79f2002-03-31 14:06:41 +00009class BdbQuit(Exception):
10 """Exception to give up completely"""
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000011
12
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000013class Bdb:
Tim Peters11cf6052001-01-14 21:54:20 +000014
Guido van Rossum4acc25b2000-02-02 15:10:15 +000015 """Generic Python debugger base class.
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000016
Guido van Rossum4acc25b2000-02-02 15:10:15 +000017 This class takes care of details of the trace facility;
18 a derived class should implement user interaction.
19 The standard debugger class (pdb.Pdb) is an example.
20 """
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000021
Guido van Rossum4acc25b2000-02-02 15:10:15 +000022 def __init__(self):
23 self.breaks = {}
24 self.fncache = {}
Barry Warsaw148ffbc1999-09-09 23:24:33 +000025
Guido van Rossum4acc25b2000-02-02 15:10:15 +000026 def canonic(self, filename):
Guido van Rossum42f53322001-11-29 02:50:15 +000027 if filename == "<" + filename[1:-1] + ">":
28 return filename
Guido van Rossum4acc25b2000-02-02 15:10:15 +000029 canonic = self.fncache.get(filename)
30 if not canonic:
31 canonic = os.path.abspath(filename)
Guido van Rossumbdba3202002-02-25 23:23:24 +000032 canonic = os.path.normcase(canonic)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000033 self.fncache[filename] = canonic
34 return canonic
Tim Peters11cf6052001-01-14 21:54:20 +000035
Guido van Rossum4acc25b2000-02-02 15:10:15 +000036 def reset(self):
37 import linecache
38 linecache.checkcache()
39 self.botframe = None
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000040 self._set_stopinfo(None, None)
Tim Peters11cf6052001-01-14 21:54:20 +000041
Guido van Rossum4acc25b2000-02-02 15:10:15 +000042 def trace_dispatch(self, frame, event, arg):
43 if self.quitting:
44 return # None
45 if event == 'line':
46 return self.dispatch_line(frame)
47 if event == 'call':
48 return self.dispatch_call(frame, arg)
49 if event == 'return':
50 return self.dispatch_return(frame, arg)
51 if event == 'exception':
52 return self.dispatch_exception(frame, arg)
Nicholas Bastinc69ebe82004-03-24 21:57:10 +000053 if event == 'c_call':
54 return self.trace_dispatch
55 if event == 'c_exception':
56 return self.trace_dispatch
57 if event == 'c_return':
58 return self.trace_dispatch
Guido van Rossumbe19ed72007-02-09 05:37:30 +000059 print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
Guido van Rossum4acc25b2000-02-02 15:10:15 +000060 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000061
Guido van Rossum4acc25b2000-02-02 15:10:15 +000062 def dispatch_line(self, frame):
63 if self.stop_here(frame) or self.break_here(frame):
64 self.user_line(frame)
65 if self.quitting: raise BdbQuit
66 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000067
Guido van Rossum4acc25b2000-02-02 15:10:15 +000068 def dispatch_call(self, frame, arg):
69 # XXX 'arg' is no longer used
70 if self.botframe is None:
71 # First call of dispatch since reset()
Christian Tismer313a7512002-05-28 08:04:00 +000072 self.botframe = frame.f_back # (CT) Note that this may also be None!
Guido van Rossum4acc25b2000-02-02 15:10:15 +000073 return self.trace_dispatch
74 if not (self.stop_here(frame) or self.break_anywhere(frame)):
75 # No need to trace this function
76 return # None
77 self.user_call(frame, arg)
78 if self.quitting: raise BdbQuit
79 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000080
Guido van Rossum4acc25b2000-02-02 15:10:15 +000081 def dispatch_return(self, frame, arg):
82 if self.stop_here(frame) or frame == self.returnframe:
83 self.user_return(frame, arg)
84 if self.quitting: raise BdbQuit
Just van Rossumae1f65f2001-06-25 18:01:24 +000085 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000086
Guido van Rossum4acc25b2000-02-02 15:10:15 +000087 def dispatch_exception(self, frame, arg):
88 if self.stop_here(frame):
89 self.user_exception(frame, arg)
90 if self.quitting: raise BdbQuit
91 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000092
Guido van Rossum4acc25b2000-02-02 15:10:15 +000093 # Normally derived classes don't override the following
94 # methods, but they may if they want to redefine the
95 # definition of stopping and breakpoints.
Tim Peters11cf6052001-01-14 21:54:20 +000096
Guido van Rossum4acc25b2000-02-02 15:10:15 +000097 def stop_here(self, frame):
Neal Norwitz72a2b4d2002-05-29 00:54:38 +000098 # (CT) stopframe may now also be None, see dispatch_call.
99 # (CT) the former test for None is therefore removed from here.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000100 if frame is self.stopframe:
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000101 return frame.f_lineno >= self.stoplineno
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000102 while frame is not None and frame is not self.stopframe:
103 if frame is self.botframe:
Tim Petersbc0e9102002-04-04 22:55:58 +0000104 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000105 frame = frame.f_back
Tim Petersbc0e9102002-04-04 22:55:58 +0000106 return False
Guido van Rossumd93643f1998-09-11 22:38:35 +0000107
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000108 def break_here(self, frame):
109 filename = self.canonic(frame.f_code.co_filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000110 if not filename in self.breaks:
Tim Petersbc0e9102002-04-04 22:55:58 +0000111 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000112 lineno = frame.f_lineno
113 if not lineno in self.breaks[filename]:
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000114 # The line itself has no breakpoint, but maybe the line is the
115 # first line of a function with breakpoint set by function name.
116 lineno = frame.f_code.co_firstlineno
117 if not lineno in self.breaks[filename]:
118 return False
119
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000120 # flag says ok to delete temp. bp
121 (bp, flag) = effective(filename, lineno, frame)
122 if bp:
123 self.currentbp = bp.number
124 if (flag and bp.temporary):
125 self.do_clear(str(bp.number))
Tim Petersbc0e9102002-04-04 22:55:58 +0000126 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000127 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000128 return False
Tim Peters11cf6052001-01-14 21:54:20 +0000129
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000130 def do_clear(self, arg):
Collin Winterce36ad82007-08-30 01:19:48 +0000131 raise NotImplementedError("subclass of bdb must implement do_clear()")
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000132
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000133 def break_anywhere(self, frame):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000134 return self.canonic(frame.f_code.co_filename) in self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000135
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000136 # Derived classes should override the user_* methods
137 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000138
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000139 def user_call(self, frame, argument_list):
140 """This method is called when there is the remote possibility
141 that we ever need to stop in this function."""
142 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000143
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000144 def user_line(self, frame):
145 """This method is called when we stop or break at this line."""
146 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000147
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000148 def user_return(self, frame, return_value):
149 """This method is called when a return trap is set here."""
150 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000151
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000152 def user_exception(self, frame, exc_info):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000153 """This method is called if an exception occurs,
154 but only if we are to stop at or just below this level."""
155 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000156
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000157 def _set_stopinfo(self, stopframe, returnframe, stoplineno=-1):
158 self.stopframe = stopframe
159 self.returnframe = returnframe
160 self.quitting = 0
161 self.stoplineno = stoplineno
162
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000163 # Derived classes and clients can call the following methods
164 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000165
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000166 def set_until(self, frame): #the name "until" is borrowed from gdb
167 """Stop when the line with the line no greater than the current one is
168 reached or when returning from current frame"""
169 self._set_stopinfo(frame, frame, frame.f_lineno+1)
170
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000171 def set_step(self):
172 """Stop after one line of code."""
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000173 self._set_stopinfo(None,None)
Tim Peters11cf6052001-01-14 21:54:20 +0000174
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000175 def set_next(self, frame):
176 """Stop on the next line in or below the given frame."""
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000177 self._set_stopinfo(frame, None)
Tim Peters11cf6052001-01-14 21:54:20 +0000178
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000179 def set_return(self, frame):
180 """Stop when returning from the given frame."""
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000181 self._set_stopinfo(frame.f_back, frame)
Tim Peters11cf6052001-01-14 21:54:20 +0000182
Johannes Gijsbers84a6c202004-11-07 11:35:30 +0000183 def set_trace(self, frame=None):
184 """Start debugging from `frame`.
185
186 If frame is not specified, debugging starts from caller's frame.
187 """
188 if frame is None:
189 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000190 self.reset()
191 while frame:
192 frame.f_trace = self.trace_dispatch
193 self.botframe = frame
194 frame = frame.f_back
195 self.set_step()
196 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000198 def set_continue(self):
199 # Don't stop except at breakpoints or when finished
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000200 self._set_stopinfo(self.botframe, None)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000201 if not self.breaks:
202 # no breakpoints; run without debugger overhead
203 sys.settrace(None)
Christian Tismer313a7512002-05-28 08:04:00 +0000204 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000205 while frame and frame is not self.botframe:
206 del frame.f_trace
207 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000208
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000209 def set_quit(self):
210 self.stopframe = self.botframe
211 self.returnframe = None
212 self.quitting = 1
213 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000214
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000215 # Derived classes and clients can call the following methods
216 # to manipulate breakpoints. These methods return an
217 # error message is something went wrong, None if all is well.
218 # Set_break prints out the breakpoint line and file:lineno.
219 # Call self.get_*break*() to see the breakpoints or better
220 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000221
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000222 def set_break(self, filename, lineno, temporary=0, cond = None,
223 funcname=None):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000224 filename = self.canonic(filename)
225 import linecache # Import as late as possible
226 line = linecache.getline(filename, lineno)
227 if not line:
228 return 'Line %s:%d does not exist' % (filename,
229 lineno)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000230 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000231 self.breaks[filename] = []
232 list = self.breaks[filename]
233 if not lineno in list:
234 list.append(lineno)
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000235 bp = Breakpoint(filename, lineno, temporary, cond, funcname)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000236
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000237 def clear_break(self, filename, lineno):
238 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000239 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000240 return 'There are no breakpoints in %s' % filename
241 if lineno not in self.breaks[filename]:
242 return 'There is no breakpoint at %s:%d' % (filename,
243 lineno)
244 # If there's only one bp in the list for that file,line
245 # pair, then remove the breaks entry
246 for bp in Breakpoint.bplist[filename, lineno][:]:
247 bp.deleteMe()
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000248 if (filename, lineno) not in Breakpoint.bplist:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000249 self.breaks[filename].remove(lineno)
250 if not self.breaks[filename]:
251 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000252
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000253 def clear_bpbynumber(self, arg):
254 try:
255 number = int(arg)
256 except:
257 return 'Non-numeric breakpoint number (%s)' % arg
258 try:
259 bp = Breakpoint.bpbynumber[number]
260 except IndexError:
261 return 'Breakpoint number (%d) out of range' % number
262 if not bp:
263 return 'Breakpoint (%d) already deleted' % number
264 self.clear_break(bp.file, bp.line)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000265
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000266 def clear_all_file_breaks(self, filename):
267 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000268 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000269 return 'There are no breakpoints in %s' % filename
270 for line in self.breaks[filename]:
271 blist = Breakpoint.bplist[filename, line]
272 for bp in blist:
273 bp.deleteMe()
274 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000275
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000276 def clear_all_breaks(self):
277 if not self.breaks:
278 return 'There are no breakpoints'
279 for bp in Breakpoint.bpbynumber:
280 if bp:
281 bp.deleteMe()
282 self.breaks = {}
Tim Peters11cf6052001-01-14 21:54:20 +0000283
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000284 def get_break(self, filename, lineno):
285 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000286 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000287 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000288
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000289 def get_breaks(self, filename, lineno):
290 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000291 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 lineno in self.breaks[filename] and \
293 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000294
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000295 def get_file_breaks(self, filename):
296 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000297 if filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000298 return self.breaks[filename]
299 else:
300 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000301
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000302 def get_all_breaks(self):
303 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000304
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000305 # Derived classes and clients can call the following method
306 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000307
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000308 def get_stack(self, f, t):
309 stack = []
310 if t and t.tb_frame is f:
311 t = t.tb_next
312 while f is not None:
313 stack.append((f, f.f_lineno))
314 if f is self.botframe:
315 break
316 f = f.f_back
317 stack.reverse()
318 i = max(0, len(stack) - 1)
319 while t is not None:
320 stack.append((t.tb_frame, t.tb_lineno))
321 t = t.tb_next
322 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000323
324 #
325
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000326 def format_stack_entry(self, frame_lineno, lprefix=': '):
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000327 import linecache, reprlib
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000328 frame, lineno = frame_lineno
329 filename = self.canonic(frame.f_code.co_filename)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000330 s = '%s(%r)' % (filename, lineno)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000331 if frame.f_code.co_name:
332 s = s + frame.f_code.co_name
333 else:
334 s = s + "<lambda>"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000335 if '__args__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000336 args = frame.f_locals['__args__']
337 else:
338 args = None
339 if args:
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000340 s = s + reprlib.repr(args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000341 else:
342 s = s + '()'
Raymond Hettinger54f02222002-06-01 14:18:47 +0000343 if '__return__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000344 rv = frame.f_locals['__return__']
345 s = s + '->'
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000346 s = s + reprlib.repr(rv)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000347 line = linecache.getline(filename, lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000348 if line: s = s + lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000349 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000350
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000351 # The following two methods can be called by clients to use
352 # a debugger to debug a statement, given as a string.
Tim Peters11cf6052001-01-14 21:54:20 +0000353
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000354 def run(self, cmd, globals=None, locals=None):
355 if globals is None:
356 import __main__
357 globals = __main__.__dict__
358 if locals is None:
359 locals = globals
360 self.reset()
361 sys.settrace(self.trace_dispatch)
362 if not isinstance(cmd, types.CodeType):
363 cmd = cmd+'\n'
364 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000365 exec(cmd, globals, locals)
366 except BdbQuit:
367 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000368 finally:
369 self.quitting = 1
370 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000371
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000372 def runeval(self, expr, globals=None, locals=None):
373 if globals is None:
374 import __main__
375 globals = __main__.__dict__
376 if locals is None:
377 locals = globals
378 self.reset()
379 sys.settrace(self.trace_dispatch)
380 if not isinstance(expr, types.CodeType):
381 expr = expr+'\n'
382 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000383 return eval(expr, globals, locals)
384 except BdbQuit:
385 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000386 finally:
387 self.quitting = 1
388 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000389
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000390 def runctx(self, cmd, globals, locals):
391 # B/W compatibility
392 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000393
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000394 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000395
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +0000396 def runcall(self, func, *args, **kwds):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000397 self.reset()
398 sys.settrace(self.trace_dispatch)
399 res = None
400 try:
Martin v. Löwisede8ffb2008-03-30 20:43:50 +0000401 res = func(*args, **kwds)
402 except BdbQuit:
403 pass
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 finally:
405 self.quitting = 1
406 sys.settrace(None)
407 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000408
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000409
Guido van Rossumb6775db1994-08-01 11:34:53 +0000410def set_trace():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000411 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000412
Guido van Rossumd93643f1998-09-11 22:38:35 +0000413
414class Breakpoint:
415
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000416 """Breakpoint class
Guido van Rossumd93643f1998-09-11 22:38:35 +0000417
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000418 Implements temporary breakpoints, ignore counts, disabling and
419 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000420
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000421 Breakpoints are indexed by number through bpbynumber and by
422 the file,line tuple using bplist. The former points to a
423 single instance of class Breakpoint. The latter points to a
424 list of such instances since there may be more than one
425 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000426
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000427 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000428
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000429 # XXX Keeping state in the class is a mistake -- this means
430 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000431
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000432 next = 1 # Next bp to be assigned
433 bplist = {} # indexed by (file, lineno) tuple
434 bpbynumber = [None] # Each entry is None or an instance of Bpt
435 # index 0 is unused, except for marking an
436 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000437
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000438 def __init__(self, file, line, temporary=0, cond=None, funcname=None):
439 self.funcname = funcname
440 # Needed if funcname is not None.
441 self.func_first_executable_line = None
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000442 self.file = file # This better be in canonical form!
443 self.line = line
444 self.temporary = temporary
445 self.cond = cond
446 self.enabled = 1
447 self.ignore = 0
448 self.hits = 0
449 self.number = Breakpoint.next
450 Breakpoint.next = Breakpoint.next + 1
451 # Build the two lists
452 self.bpbynumber.append(self)
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000453 if (file, line) in self.bplist:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000454 self.bplist[file, line].append(self)
455 else:
456 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000457
Tim Peters11cf6052001-01-14 21:54:20 +0000458
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000459 def deleteMe(self):
460 index = (self.file, self.line)
461 self.bpbynumber[self.number] = None # No longer in list
462 self.bplist[index].remove(self)
463 if not self.bplist[index]:
464 # No more bp for this f:l combo
465 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000466
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000467 def enable(self):
468 self.enabled = 1
Guido van Rossumd93643f1998-09-11 22:38:35 +0000469
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000470 def disable(self):
471 self.enabled = 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000472
Thomas Wouters477c8d52006-05-27 19:21:47 +0000473 def bpprint(self, out=None):
474 if out is None:
475 out = sys.stdout
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000476 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000477 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000478 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000479 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000480 if self.enabled:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 disp = disp + 'yes '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000482 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 disp = disp + 'no '
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000484 print('%-4dbreakpoint %s at %s:%d' % (self.number, disp,
485 self.file, self.line), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000486 if self.cond:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000487 print('\tstop only if %s' % (self.cond,), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000488 if self.ignore:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000489 print('\tignore next %d hits' % (self.ignore), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000490 if (self.hits):
491 if (self.hits > 1): ss = 's'
492 else: ss = ''
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000493 print(('\tbreakpoint already hit %d time%s' %
494 (self.hits, ss)), file=out)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000495
496# -----------end of Breakpoint class----------
497
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000498def checkfuncname(b, frame):
499 """Check whether we should break here because of `b.funcname`."""
500 if not b.funcname:
501 # Breakpoint was set via line number.
502 if b.line != frame.f_lineno:
503 # Breakpoint was set at a line with a def statement and the function
504 # defined is called: don't break.
505 return False
506 return True
507
508 # Breakpoint set via function name.
509
510 if frame.f_code.co_name != b.funcname:
511 # It's not a function call, but rather execution of def statement.
512 return False
513
514 # We are in the right frame.
515 if not b.func_first_executable_line:
516 # The function is entered for the 1st time.
517 b.func_first_executable_line = frame.f_lineno
518
519 if b.func_first_executable_line != frame.f_lineno:
520 # But we are not at the first line number: don't break.
521 return False
522 return True
523
Guido van Rossumd93643f1998-09-11 22:38:35 +0000524# Determines if there is an effective (active) breakpoint at this
525# line of code. Returns breakpoint number or 0 if none
526def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000527 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000528
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000529 Called only if we know there is a bpt at this
530 location. Returns breakpoint that was triggered and a flag
531 that indicates if it is ok to delete a temporary bp.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000532
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000533 """
534 possibles = Breakpoint.bplist[file,line]
535 for i in range(0, len(possibles)):
536 b = possibles[i]
537 if b.enabled == 0:
538 continue
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000539 if not checkfuncname(b, frame):
540 continue
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000541 # Count every hit when bp is enabled
542 b.hits = b.hits + 1
543 if not b.cond:
544 # If unconditional, and ignoring,
545 # go on to next, else break
546 if b.ignore > 0:
547 b.ignore = b.ignore -1
548 continue
549 else:
550 # breakpoint and marker that's ok
551 # to delete if temporary
552 return (b,1)
553 else:
554 # Conditional bp.
555 # Ignore count applies only to those bpt hits where the
556 # condition evaluates to true.
557 try:
558 val = eval(b.cond, frame.f_globals,
Tim Peters11cf6052001-01-14 21:54:20 +0000559 frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000560 if val:
561 if b.ignore > 0:
562 b.ignore = b.ignore -1
563 # continue
564 else:
565 return (b,1)
566 # else:
567 # continue
568 except:
569 # if eval fails, most conservative
570 # thing is to stop on breakpoint
Tim Peters11cf6052001-01-14 21:54:20 +0000571 # regardless of ignore count.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000572 # Don't delete temporary,
573 # as another hint to user.
574 return (b,0)
575 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000576
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000577# -------------------- testing --------------------
578
579class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000580 def user_call(self, frame, args):
581 name = frame.f_code.co_name
582 if not name: name = '???'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000583 print('+++ call', name, args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000584 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000585 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000586 name = frame.f_code.co_name
587 if not name: name = '???'
588 fn = self.canonic(frame.f_code.co_filename)
589 line = linecache.getline(fn, frame.f_lineno)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000590 print('+++', fn, frame.f_lineno, name, ':', line.strip())
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000591 def user_return(self, frame, retval):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000592 print('+++ return', retval)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000593 def user_exception(self, frame, exc_stuff):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000594 print('+++ exception', exc_stuff)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000595 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000596
597def foo(n):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000598 print('foo(', n, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000599 x = bar(n*10)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000600 print('bar returned', x)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000601
602def bar(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000603 print('bar(', a, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000604 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000605
606def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000607 t = Tdb()
608 t.run('import bdb; bdb.foo(10)')
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000609
610# end