blob: b70fdad52d5c57af1b4fc070a7fed79fb2b6d82d [file] [log] [blame]
Guido van Rossum6ea27cc1999-01-25 20:51:34 +00001# Debugger basics
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00002
3import sys
Guido van Rossum4808dcb1996-10-15 14:40:21 +00004import types
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00005
6BdbQuit = 'bdb.BdbQuit' # Exception to give up completely
7
8
Guido van Rossum6ea27cc1999-01-25 20:51:34 +00009class Bdb:
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000010
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000011 """Generic Python debugger base class.
12
13 This class takes care of details of the trace facility;
14 a derived class should implement user interaction.
15 The standard debugger class (pdb.Pdb) is an example.
16 """
17
Guido van Rossum5ef74b81993-06-23 11:55:24 +000018 def __init__(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000019 self.breaks = {}
Guido van Rossum170e1901999-01-29 22:03:51 +000020 # We want to have a method self.canonic() which
21 # canonicalizes filenames before comparing them
22 # but we want the default to be a very fast no-op.
23 # Solution: the built-in str function.
24 if not hasattr(self, "canonic"):
25 self.canonic = str
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000026
27 def reset(self):
Guido van Rossumb6775db1994-08-01 11:34:53 +000028 import linecache
29 linecache.checkcache()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000030 self.botframe = None
31 self.stopframe = None
32 self.returnframe = None
33 self.quitting = 0
34
35 def trace_dispatch(self, frame, event, arg):
36 if self.quitting:
37 return # None
38 if event == 'line':
39 return self.dispatch_line(frame)
40 if event == 'call':
41 return self.dispatch_call(frame, arg)
42 if event == 'return':
43 return self.dispatch_return(frame, arg)
44 if event == 'exception':
45 return self.dispatch_exception(frame, arg)
46 print 'bdb.Bdb.dispatch: unknown debugging event:', `event`
47 return self.trace_dispatch
48
49 def dispatch_line(self, frame):
50 if self.stop_here(frame) or self.break_here(frame):
51 self.user_line(frame)
52 if self.quitting: raise BdbQuit
53 return self.trace_dispatch
54
55 def dispatch_call(self, frame, arg):
Guido van Rossum75bb54c1998-09-28 15:33:38 +000056 # XXX 'arg' is no longer used
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000057 if self.botframe is None:
58 # First call of dispatch since reset()
59 self.botframe = frame
60 return self.trace_dispatch
61 if not (self.stop_here(frame) or self.break_anywhere(frame)):
62 # No need to trace this function
63 return # None
64 self.user_call(frame, arg)
65 if self.quitting: raise BdbQuit
66 return self.trace_dispatch
67
68 def dispatch_return(self, frame, arg):
69 if self.stop_here(frame) or frame == self.returnframe:
70 self.user_return(frame, arg)
71 if self.quitting: raise BdbQuit
72
73 def dispatch_exception(self, frame, arg):
74 if self.stop_here(frame):
75 self.user_exception(frame, arg)
76 if self.quitting: raise BdbQuit
77 return self.trace_dispatch
78
79 # Normally derived classes don't override the following
Guido van Rossum4e160981992-09-02 20:43:20 +000080 # methods, but they may if they want to redefine the
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000081 # definition of stopping and breakpoints.
82
83 def stop_here(self, frame):
84 if self.stopframe is None:
85 return 1
86 if frame is self.stopframe:
87 return 1
88 while frame is not None and frame is not self.stopframe:
89 if frame is self.botframe:
90 return 1
91 frame = frame.f_back
92 return 0
Guido van Rossumd93643f1998-09-11 22:38:35 +000093
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000094 def break_here(self, frame):
Guido van Rossum170e1901999-01-29 22:03:51 +000095 filename = self.canonic(frame.f_code.co_filename)
Guido van Rossumff02e1d1997-07-11 13:42:50 +000096 if not self.breaks.has_key(filename):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000097 return 0
Guido van Rossum170e1901999-01-29 22:03:51 +000098 lineno = frame.f_lineno
Guido van Rossumff02e1d1997-07-11 13:42:50 +000099 if not lineno in self.breaks[filename]:
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000100 return 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000101 # flag says ok to delete temp. bp
102 (bp, flag) = effective(filename, lineno, frame)
103 if bp:
104 self.currentbp = bp.number
105 if (flag and bp.temporary):
Guido van Rossum691d27a1998-11-18 15:56:06 +0000106 self.do_clear(str(bp.number))
Guido van Rossumd93643f1998-09-11 22:38:35 +0000107 return 1
108 else:
109 return 0
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000110
111 def break_anywhere(self, frame):
Guido van Rossum170e1901999-01-29 22:03:51 +0000112 return self.breaks.has_key(
113 self.canonic(frame.f_code.co_filename))
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000114
Guido van Rossum4e160981992-09-02 20:43:20 +0000115 # Derived classes should override the user_* methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000116 # to gain control.
117
118 def user_call(self, frame, argument_list):
Guido van Rossum4e160981992-09-02 20:43:20 +0000119 # This method is called when there is the remote possibility
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000120 # that we ever need to stop in this function
121 pass
122
123 def user_line(self, frame):
Guido van Rossum4e160981992-09-02 20:43:20 +0000124 # This method is called when we stop or break at this line
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000125 pass
126
127 def user_return(self, frame, return_value):
Guido van Rossum4e160981992-09-02 20:43:20 +0000128 # This method is called when a return trap is set here
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000129 pass
130
131 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
Guido van Rossum4e160981992-09-02 20:43:20 +0000132 # This method is called if an exception occurs,
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000133 # but only if we are to stop at or just below this level
134 pass
135
Guido van Rossum4e160981992-09-02 20:43:20 +0000136 # Derived classes and clients can call the following methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000137 # to affect the stepping state.
138
139 def set_step(self):
140 # Stop after one line of code
141 self.stopframe = None
142 self.returnframe = None
143 self.quitting = 0
144
145 def set_next(self, frame):
146 # Stop on the next line in or below the given frame
147 self.stopframe = frame
148 self.returnframe = None
149 self.quitting = 0
150
151 def set_return(self, frame):
152 # Stop when returning from the given frame
153 self.stopframe = frame.f_back
154 self.returnframe = frame
155 self.quitting = 0
156
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157 def set_trace(self):
158 # Start debugging from here
159 try:
160 1 + ''
161 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000162 frame = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163 self.reset()
164 while frame:
165 frame.f_trace = self.trace_dispatch
166 self.botframe = frame
167 frame = frame.f_back
168 self.set_step()
169 sys.settrace(self.trace_dispatch)
170
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000171 def set_continue(self):
172 # Don't stop except at breakpoints or when finished
173 self.stopframe = self.botframe
174 self.returnframe = None
175 self.quitting = 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 if not self.breaks:
177 # no breakpoints; run without debugger overhead
178 sys.settrace(None)
179 try:
180 1 + '' # raise an exception
181 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000182 frame = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 while frame and frame is not self.botframe:
184 del frame.f_trace
185 frame = frame.f_back
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000186
187 def set_quit(self):
188 self.stopframe = self.botframe
189 self.returnframe = None
190 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000191 sys.settrace(None)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000192
Guido van Rossum4e160981992-09-02 20:43:20 +0000193 # Derived classes and clients can call the following methods
194 # to manipulate breakpoints. These methods return an
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000195 # error message is something went wrong, None if all is well.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000196 # Set_break prints out the breakpoint line and file:lineno.
197 # Call self.get_*break*() to see the breakpoints or better
198 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000199
Guido van Rossumd93643f1998-09-11 22:38:35 +0000200 def set_break(self, filename, lineno, temporary=0, cond = None):
Guido van Rossum170e1901999-01-29 22:03:51 +0000201 filename = self.canonic(filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000202 import linecache # Import as late as possible
203 line = linecache.getline(filename, lineno)
204 if not line:
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000205 return 'Line %s:%d does not exist' % (filename,
206 lineno)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000207 if not self.breaks.has_key(filename):
208 self.breaks[filename] = []
209 list = self.breaks[filename]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000210 if not lineno in list:
211 list.append(lineno)
212 bp = Breakpoint(filename, lineno, temporary, cond)
Guido van Rossum170e1901999-01-29 22:03:51 +0000213 print "Breakpoint in", filename, "at", lineno
Guido van Rossumd93643f1998-09-11 22:38:35 +0000214
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000215 def clear_break(self, filename, lineno):
Guido van Rossum170e1901999-01-29 22:03:51 +0000216 filename = self.canonic(filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000217 if not self.breaks.has_key(filename):
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000218 return 'There are no breakpoints in %s' % filename
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000219 if lineno not in self.breaks[filename]:
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000220 return 'There is no breakpoint at %s:%d' % (filename,
221 lineno)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000222 # If there's only one bp in the list for that file,line
223 # pair, then remove the breaks entry
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000224 for bp in Breakpoint.bplist[filename, lineno][:]:
225 bp.deleteMe()
226 if not Breakpoint.bplist.has_key((filename, lineno)):
Guido van Rossumd93643f1998-09-11 22:38:35 +0000227 self.breaks[filename].remove(lineno)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000228 if not self.breaks[filename]:
229 del self.breaks[filename]
230
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000231 def clear_bpbynumber(self, arg):
232 try:
233 number = int(arg)
234 except:
235 return 'Non-numeric breakpoint number (%s)' % arg
236 try:
237 bp = Breakpoint.bpbynumber[number]
238 except IndexError:
239 return 'Breakpoint number (%d) out of range' % number
240 if not bp:
241 return 'Breakpoint (%d) already deleted' % number
242 self.clear_break(bp.file, bp.line)
243
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000244 def clear_all_file_breaks(self, filename):
Guido van Rossum170e1901999-01-29 22:03:51 +0000245 filename = self.canonic(filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000246 if not self.breaks.has_key(filename):
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000247 return 'There are no breakpoints in %s' % filename
Guido van Rossumd93643f1998-09-11 22:38:35 +0000248 for line in self.breaks[filename]:
249 blist = Breakpoint.bplist[filename, line]
250 for bp in blist:
251 bp.deleteMe()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000252 del self.breaks[filename]
253
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254 def clear_all_breaks(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000255 if not self.breaks:
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000256 return 'There are no breakpoints'
Guido van Rossumd93643f1998-09-11 22:38:35 +0000257 for bp in Breakpoint.bpbynumber:
258 if bp:
259 bp.deleteMe()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000260 self.breaks = {}
261
262 def get_break(self, filename, lineno):
Guido van Rossum170e1901999-01-29 22:03:51 +0000263 filename = self.canonic(filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000264 return self.breaks.has_key(filename) and \
265 lineno in self.breaks[filename]
266
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000267 def get_breaks(self, filename, lineno):
Guido van Rossum170e1901999-01-29 22:03:51 +0000268 filename = self.canonic(filename)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000269 return self.breaks.has_key(filename) and \
270 lineno in self.breaks[filename] and \
Guido van Rossum0189c011999-01-25 20:56:33 +0000271 Breakpoint.bplist[filename, lineno] or []
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000272
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000273 def get_file_breaks(self, filename):
Guido van Rossum170e1901999-01-29 22:03:51 +0000274 filename = self.canonic(filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000275 if self.breaks.has_key(filename):
276 return self.breaks[filename]
277 else:
278 return []
279
280 def get_all_breaks(self):
281 return self.breaks
282
Guido van Rossum4e160981992-09-02 20:43:20 +0000283 # Derived classes and clients can call the following method
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000284 # to get a data structure representing a stack trace.
285
286 def get_stack(self, f, t):
287 stack = []
288 if t and t.tb_frame is f:
289 t = t.tb_next
290 while f is not None:
291 stack.append((f, f.f_lineno))
292 if f is self.botframe:
293 break
294 f = f.f_back
295 stack.reverse()
296 i = max(0, len(stack) - 1)
297 while t is not None:
298 stack.append((t.tb_frame, t.tb_lineno))
299 t = t.tb_next
300 return stack, i
301
Guido van Rossumfac6da21992-01-27 16:59:34 +0000302 #
303
Guido van Rossuma558e371994-11-10 22:27:35 +0000304 def format_stack_entry(self, frame_lineno, lprefix=': '):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305 import linecache, repr, string
Guido van Rossum89a78691992-12-14 12:57:56 +0000306 frame, lineno = frame_lineno
Guido van Rossum170e1901999-01-29 22:03:51 +0000307 filename = self.canonic(frame.f_code.co_filename)
Guido van Rossumfac6da21992-01-27 16:59:34 +0000308 s = filename + '(' + `lineno` + ')'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309 if frame.f_code.co_name:
Guido van Rossumd93643f1998-09-11 22:38:35 +0000310 s = s + frame.f_code.co_name
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311 else:
Guido van Rossumd93643f1998-09-11 22:38:35 +0000312 s = s + "<lambda>"
Guido van Rossumfac6da21992-01-27 16:59:34 +0000313 if frame.f_locals.has_key('__args__'):
314 args = frame.f_locals['__args__']
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000315 else:
316 args = None
317 if args:
318 s = s + repr.repr(args)
319 else:
320 s = s + '()'
Guido van Rossumfac6da21992-01-27 16:59:34 +0000321 if frame.f_locals.has_key('__return__'):
322 rv = frame.f_locals['__return__']
323 s = s + '->'
324 s = s + repr.repr(rv)
325 line = linecache.getline(filename, lineno)
Guido van Rossuma558e371994-11-10 22:27:35 +0000326 if line: s = s + lprefix + string.strip(line)
Guido van Rossumfac6da21992-01-27 16:59:34 +0000327 return s
328
Guido van Rossum4e160981992-09-02 20:43:20 +0000329 # The following two methods can be called by clients to use
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000330 # a debugger to debug a statement, given as a string.
331
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000332 def run(self, cmd, globals=None, locals=None):
333 if globals is None:
334 import __main__
335 globals = __main__.__dict__
336 if locals is None:
337 locals = globals
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000338 self.reset()
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000339 sys.settrace(self.trace_dispatch)
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000340 if type(cmd) <> types.CodeType:
341 cmd = cmd+'\n'
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000342 try:
Guido van Rossumceb3ba21992-04-03 16:32:37 +0000343 try:
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000344 exec cmd in globals, locals
Guido van Rossumceb3ba21992-04-03 16:32:37 +0000345 except BdbQuit:
346 pass
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000347 finally:
348 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000349 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000350
351 def runeval(self, expr, globals=None, locals=None):
352 if globals is None:
353 import __main__
354 globals = __main__.__dict__
355 if locals is None:
356 locals = globals
357 self.reset()
358 sys.settrace(self.trace_dispatch)
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000359 if type(expr) <> types.CodeType:
360 expr = expr+'\n'
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000361 try:
362 try:
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000363 return eval(expr, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000364 except BdbQuit:
365 pass
366 finally:
367 self.quitting = 1
368 sys.settrace(None)
369
370 def runctx(self, cmd, globals, locals):
371 # B/W compatibility
372 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000373
374 # This method is more useful to debug a single function call.
375
376 def runcall(self, func, *args):
377 self.reset()
378 sys.settrace(self.trace_dispatch)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000379 res = None
Guido van Rossum4e160981992-09-02 20:43:20 +0000380 try:
381 try:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000382 res = apply(func, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000383 except BdbQuit:
384 pass
385 finally:
386 self.quitting = 1
387 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000388 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000389
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000390
Guido van Rossumb6775db1994-08-01 11:34:53 +0000391def set_trace():
392 Bdb().set_trace()
393
Guido van Rossumd93643f1998-09-11 22:38:35 +0000394
395class Breakpoint:
396
397 """Breakpoint class
398
399 Implements temporary breakpoints, ignore counts, disabling and
400 (re)-enabling, and conditionals.
401
402 Breakpoints are indexed by number through bpbynumber and by
403 the file,line tuple using bplist. The former points to a
404 single instance of class Breakpoint. The latter points to a
405 list of such instances since there may be more than one
406 breakpoint per line.
407
408 """
409
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000410 # XXX Keeping state in the class is a mistake -- this means
411 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000412
413 next = 1 # Next bp to be assigned
414 bplist = {} # indexed by (file, lineno) tuple
415 bpbynumber = [None] # Each entry is None or an instance of Bpt
416 # index 0 is unused, except for marking an
417 # effective break .... see effective()
418
419 def __init__(self, file, line, temporary=0, cond = None):
Guido van Rossum170e1901999-01-29 22:03:51 +0000420 self.file = file # This better be in canonical form!
Guido van Rossumd93643f1998-09-11 22:38:35 +0000421 self.line = line
422 self.temporary = temporary
423 self.cond = cond
424 self.enabled = 1
425 self.ignore = 0
426 self.hits = 0
427 self.number = Breakpoint.next
428 Breakpoint.next = Breakpoint.next + 1
429 # Build the two lists
430 self.bpbynumber.append(self)
431 if self.bplist.has_key((file, line)):
432 self.bplist[file, line].append(self)
433 else:
434 self.bplist[file, line] = [self]
435
436
437 def deleteMe(self):
438 index = (self.file, self.line)
439 self.bpbynumber[self.number] = None # No longer in list
440 self.bplist[index].remove(self)
441 if not self.bplist[index]:
442 # No more bp for this f:l combo
443 del self.bplist[index]
444
445 def enable(self):
446 self.enabled = 1
447
448 def disable(self):
449 self.enabled = 0
450
451 def bpprint(self):
452 if self.temporary:
453 disp = 'del '
454 else:
455 disp = 'keep '
456 if self.enabled:
457 disp = disp + 'yes'
458 else:
459 disp = disp + 'no '
460 print '%-4dbreakpoint %s at %s:%d' % (self.number, disp,
461 self.file, self.line)
462 if self.cond:
463 print '\tstop only if %s' % (self.cond,)
464 if self.ignore:
465 print '\tignore next %d hits' % (self.ignore)
466 if (self.hits):
467 if (self.hits > 1): ss = 's'
468 else: ss = ''
469 print ('\tbreakpoint already hit %d time%s' %
470 (self.hits, ss))
471
472# -----------end of Breakpoint class----------
473
474# Determines if there is an effective (active) breakpoint at this
475# line of code. Returns breakpoint number or 0 if none
476def effective(file, line, frame):
477 """Determine which breakpoint for this file:line is to be acted upon.
478
479 Called only if we know there is a bpt at this
480 location. Returns breakpoint that was triggered and a flag
481 that indicates if it is ok to delete a temporary bp.
482
483 """
484 possibles = Breakpoint.bplist[file,line]
485 for i in range(0, len(possibles)):
486 b = possibles[i]
487 if b.enabled == 0:
488 continue
489 # Count every hit when bp is enabled
490 b.hits = b.hits + 1
491 if not b.cond:
492 # If unconditional, and ignoring,
493 # go on to next, else break
494 if b.ignore > 0:
495 b.ignore = b.ignore -1
496 continue
497 else:
498 # breakpoint and marker that's ok
499 # to delete if temporary
500 return (b,1)
501 else:
502 # Conditional bp.
503 # Ignore count applies only to those bpt hits where the
504 # condition evaluates to true.
505 try:
506 val = eval(b.cond, frame.f_globals,
507 frame.f_locals)
508 if val:
509 if b.ignore > 0:
510 b.ignore = b.ignore -1
511 # continue
512 else:
513 return (b,1)
514 # else:
515 # continue
516 except:
517 # if eval fails, most conservative
518 # thing is to stop on breakpoint
519 # regardless of ignore count.
520 # Don't delete temporary,
521 # as another hint to user.
522 return (b,0)
523 return (None, None)
524
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000525# -------------------- testing --------------------
526
527class Tdb(Bdb):
528 def user_call(self, frame, args):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000529 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000530 if not name: name = '???'
531 print '+++ call', name, args
532 def user_line(self, frame):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000533 import linecache, string
534 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000535 if not name: name = '???'
Guido van Rossum170e1901999-01-29 22:03:51 +0000536 fn = self.canonic(frame.f_code.co_filename)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000537 line = linecache.getline(fn, frame.f_lineno)
538 print '+++', fn, frame.f_lineno, name, ':', string.strip(line)
539 def user_return(self, frame, retval):
540 print '+++ return', retval
541 def user_exception(self, frame, exc_stuff):
542 print '+++ exception', exc_stuff
543 self.set_continue()
544
545def foo(n):
546 print 'foo(', n, ')'
547 x = bar(n*10)
548 print 'bar returned', x
549
550def bar(a):
551 print 'bar(', a, ')'
552 return a/2
553
554def test():
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000555 t = Tdb()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000556 t.run('import bdb; bdb.foo(10)')