blob: 5cfb5ef6c5d1e3500858a4f9cd8092f7a1d99372 [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
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00009BdbQuit = 'bdb.BdbQuit' # Exception to give up completely
10
11
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000012class Bdb:
Tim Peters11cf6052001-01-14 21:54:20 +000013
Guido van Rossum4acc25b2000-02-02 15:10:15 +000014 """Generic Python debugger base class.
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000015
Guido van Rossum4acc25b2000-02-02 15:10:15 +000016 This class takes care of details of the trace facility;
17 a derived class should implement user interaction.
18 The standard debugger class (pdb.Pdb) is an example.
19 """
Guido van Rossum6ea27cc1999-01-25 20:51:34 +000020
Guido van Rossum4acc25b2000-02-02 15:10:15 +000021 def __init__(self):
22 self.breaks = {}
23 self.fncache = {}
Barry Warsaw148ffbc1999-09-09 23:24:33 +000024
Guido van Rossum4acc25b2000-02-02 15:10:15 +000025 def canonic(self, filename):
26 canonic = self.fncache.get(filename)
27 if not canonic:
28 canonic = os.path.abspath(filename)
29 self.fncache[filename] = canonic
30 return canonic
Tim Peters11cf6052001-01-14 21:54:20 +000031
Guido van Rossum4acc25b2000-02-02 15:10:15 +000032 def reset(self):
33 import linecache
34 linecache.checkcache()
35 self.botframe = None
36 self.stopframe = None
37 self.returnframe = None
38 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +000039
Guido van Rossum4acc25b2000-02-02 15:10:15 +000040 def trace_dispatch(self, frame, event, arg):
41 if self.quitting:
42 return # None
43 if event == 'line':
44 return self.dispatch_line(frame)
45 if event == 'call':
46 return self.dispatch_call(frame, arg)
47 if event == 'return':
48 return self.dispatch_return(frame, arg)
49 if event == 'exception':
50 return self.dispatch_exception(frame, arg)
51 print 'bdb.Bdb.dispatch: unknown debugging event:', `event`
52 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000053
Guido van Rossum4acc25b2000-02-02 15:10:15 +000054 def dispatch_line(self, frame):
55 if self.stop_here(frame) or self.break_here(frame):
56 self.user_line(frame)
57 if self.quitting: raise BdbQuit
58 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000059
Guido van Rossum4acc25b2000-02-02 15:10:15 +000060 def dispatch_call(self, frame, arg):
61 # XXX 'arg' is no longer used
62 if self.botframe is None:
63 # First call of dispatch since reset()
64 self.botframe = frame
65 return self.trace_dispatch
66 if not (self.stop_here(frame) or self.break_anywhere(frame)):
67 # No need to trace this function
68 return # None
69 self.user_call(frame, arg)
70 if self.quitting: raise BdbQuit
71 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000072
Guido van Rossum4acc25b2000-02-02 15:10:15 +000073 def dispatch_return(self, frame, arg):
74 if self.stop_here(frame) or frame == self.returnframe:
75 self.user_return(frame, arg)
76 if self.quitting: raise BdbQuit
Tim Peters11cf6052001-01-14 21:54:20 +000077
Guido van Rossum4acc25b2000-02-02 15:10:15 +000078 def dispatch_exception(self, frame, arg):
79 if self.stop_here(frame):
80 self.user_exception(frame, arg)
81 if self.quitting: raise BdbQuit
82 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000083
Guido van Rossum4acc25b2000-02-02 15:10:15 +000084 # Normally derived classes don't override the following
85 # methods, but they may if they want to redefine the
86 # definition of stopping and breakpoints.
Tim Peters11cf6052001-01-14 21:54:20 +000087
Guido van Rossum4acc25b2000-02-02 15:10:15 +000088 def stop_here(self, frame):
89 if self.stopframe is None:
90 return 1
91 if frame is self.stopframe:
92 return 1
93 while frame is not None and frame is not self.stopframe:
94 if frame is self.botframe:
95 return 1
96 frame = frame.f_back
97 return 0
Guido van Rossumd93643f1998-09-11 22:38:35 +000098
Guido van Rossum4acc25b2000-02-02 15:10:15 +000099 def break_here(self, frame):
100 filename = self.canonic(frame.f_code.co_filename)
101 if not self.breaks.has_key(filename):
102 return 0
103 lineno = frame.f_lineno
104 if not lineno in self.breaks[filename]:
105 return 0
106 # flag says ok to delete temp. bp
107 (bp, flag) = effective(filename, lineno, frame)
108 if bp:
109 self.currentbp = bp.number
110 if (flag and bp.temporary):
111 self.do_clear(str(bp.number))
112 return 1
113 else:
114 return 0
Tim Peters11cf6052001-01-14 21:54:20 +0000115
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000116 def break_anywhere(self, frame):
117 return self.breaks.has_key(
118 self.canonic(frame.f_code.co_filename))
Tim Peters11cf6052001-01-14 21:54:20 +0000119
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000120 # Derived classes should override the user_* methods
121 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000122
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000123 def user_call(self, frame, argument_list):
124 """This method is called when there is the remote possibility
125 that we ever need to stop in this function."""
126 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000127
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000128 def user_line(self, frame):
129 """This method is called when we stop or break at this line."""
130 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000131
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000132 def user_return(self, frame, return_value):
133 """This method is called when a return trap is set here."""
134 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000135
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000136 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
137 """This method is called if an exception occurs,
138 but only if we are to stop at or just below this level."""
139 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000140
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000141 # Derived classes and clients can call the following methods
142 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000143
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000144 def set_step(self):
145 """Stop after one line of code."""
146 self.stopframe = None
147 self.returnframe = None
148 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000149
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000150 def set_next(self, frame):
151 """Stop on the next line in or below the given frame."""
152 self.stopframe = frame
153 self.returnframe = None
154 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000155
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000156 def set_return(self, frame):
157 """Stop when returning from the given frame."""
158 self.stopframe = frame.f_back
159 self.returnframe = frame
160 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000161
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000162 def set_trace(self):
163 """Start debugging from here."""
164 try:
165 1 + ''
166 except:
167 frame = sys.exc_info()[2].tb_frame.f_back
168 self.reset()
169 while frame:
170 frame.f_trace = self.trace_dispatch
171 self.botframe = frame
172 frame = frame.f_back
173 self.set_step()
174 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000176 def set_continue(self):
177 # Don't stop except at breakpoints or when finished
178 self.stopframe = self.botframe
179 self.returnframe = None
180 self.quitting = 0
181 if not self.breaks:
182 # no breakpoints; run without debugger overhead
183 sys.settrace(None)
184 try:
185 1 + '' # raise an exception
186 except:
187 frame = sys.exc_info()[2].tb_frame.f_back
188 while frame and frame is not self.botframe:
189 del frame.f_trace
190 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000191
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000192 def set_quit(self):
193 self.stopframe = self.botframe
194 self.returnframe = None
195 self.quitting = 1
196 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000197
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000198 # Derived classes and clients can call the following methods
199 # to manipulate breakpoints. These methods return an
200 # error message is something went wrong, None if all is well.
201 # Set_break prints out the breakpoint line and file:lineno.
202 # Call self.get_*break*() to see the breakpoints or better
203 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000204
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000205 def set_break(self, filename, lineno, temporary=0, cond = None):
206 filename = self.canonic(filename)
207 import linecache # Import as late as possible
208 line = linecache.getline(filename, lineno)
209 if not line:
210 return 'Line %s:%d does not exist' % (filename,
211 lineno)
212 if not self.breaks.has_key(filename):
213 self.breaks[filename] = []
214 list = self.breaks[filename]
215 if not lineno in list:
216 list.append(lineno)
217 bp = Breakpoint(filename, lineno, temporary, cond)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000218
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000219 def clear_break(self, filename, lineno):
220 filename = self.canonic(filename)
221 if not self.breaks.has_key(filename):
222 return 'There are no breakpoints in %s' % filename
223 if lineno not in self.breaks[filename]:
224 return 'There is no breakpoint at %s:%d' % (filename,
225 lineno)
226 # If there's only one bp in the list for that file,line
227 # pair, then remove the breaks entry
228 for bp in Breakpoint.bplist[filename, lineno][:]:
229 bp.deleteMe()
230 if not Breakpoint.bplist.has_key((filename, lineno)):
231 self.breaks[filename].remove(lineno)
232 if not self.breaks[filename]:
233 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000234
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000235 def clear_bpbynumber(self, arg):
236 try:
237 number = int(arg)
238 except:
239 return 'Non-numeric breakpoint number (%s)' % arg
240 try:
241 bp = Breakpoint.bpbynumber[number]
242 except IndexError:
243 return 'Breakpoint number (%d) out of range' % number
244 if not bp:
245 return 'Breakpoint (%d) already deleted' % number
246 self.clear_break(bp.file, bp.line)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000247
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000248 def clear_all_file_breaks(self, filename):
249 filename = self.canonic(filename)
250 if not self.breaks.has_key(filename):
251 return 'There are no breakpoints in %s' % filename
252 for line in self.breaks[filename]:
253 blist = Breakpoint.bplist[filename, line]
254 for bp in blist:
255 bp.deleteMe()
256 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000257
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000258 def clear_all_breaks(self):
259 if not self.breaks:
260 return 'There are no breakpoints'
261 for bp in Breakpoint.bpbynumber:
262 if bp:
263 bp.deleteMe()
264 self.breaks = {}
Tim Peters11cf6052001-01-14 21:54:20 +0000265
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000266 def get_break(self, filename, lineno):
267 filename = self.canonic(filename)
268 return self.breaks.has_key(filename) and \
269 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000270
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000271 def get_breaks(self, filename, lineno):
272 filename = self.canonic(filename)
273 return self.breaks.has_key(filename) and \
274 lineno in self.breaks[filename] and \
275 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000276
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000277 def get_file_breaks(self, filename):
278 filename = self.canonic(filename)
279 if self.breaks.has_key(filename):
280 return self.breaks[filename]
281 else:
282 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000283
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000284 def get_all_breaks(self):
285 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000286
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000287 # Derived classes and clients can call the following method
288 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000289
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000290 def get_stack(self, f, t):
291 stack = []
292 if t and t.tb_frame is f:
293 t = t.tb_next
294 while f is not None:
295 stack.append((f, f.f_lineno))
296 if f is self.botframe:
297 break
298 f = f.f_back
299 stack.reverse()
300 i = max(0, len(stack) - 1)
301 while t is not None:
302 stack.append((t.tb_frame, t.tb_lineno))
303 t = t.tb_next
304 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000305
306 #
307
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000308 def format_stack_entry(self, frame_lineno, lprefix=': '):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000309 import linecache, repr
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000310 frame, lineno = frame_lineno
311 filename = self.canonic(frame.f_code.co_filename)
312 s = filename + '(' + `lineno` + ')'
313 if frame.f_code.co_name:
314 s = s + frame.f_code.co_name
315 else:
316 s = s + "<lambda>"
317 if frame.f_locals.has_key('__args__'):
318 args = frame.f_locals['__args__']
319 else:
320 args = None
321 if args:
322 s = s + repr.repr(args)
323 else:
324 s = s + '()'
325 if frame.f_locals.has_key('__return__'):
326 rv = frame.f_locals['__return__']
327 s = s + '->'
328 s = s + repr.repr(rv)
329 line = linecache.getline(filename, lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000330 if line: s = s + lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000331 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000332
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000333 # The following two methods can be called by clients to use
334 # a debugger to debug a statement, given as a string.
Tim Peters11cf6052001-01-14 21:54:20 +0000335
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000336 def run(self, cmd, globals=None, locals=None):
337 if globals is None:
338 import __main__
339 globals = __main__.__dict__
340 if locals is None:
341 locals = globals
342 self.reset()
343 sys.settrace(self.trace_dispatch)
344 if not isinstance(cmd, types.CodeType):
345 cmd = cmd+'\n'
346 try:
347 try:
348 exec cmd in globals, locals
349 except BdbQuit:
350 pass
351 finally:
352 self.quitting = 1
353 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000354
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000355 def runeval(self, expr, globals=None, locals=None):
356 if globals is None:
357 import __main__
358 globals = __main__.__dict__
359 if locals is None:
360 locals = globals
361 self.reset()
362 sys.settrace(self.trace_dispatch)
363 if not isinstance(expr, types.CodeType):
364 expr = expr+'\n'
365 try:
366 try:
367 return eval(expr, globals, locals)
368 except BdbQuit:
369 pass
370 finally:
371 self.quitting = 1
372 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000373
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000374 def runctx(self, cmd, globals, locals):
375 # B/W compatibility
376 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000377
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000378 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000379
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000380 def runcall(self, func, *args):
381 self.reset()
382 sys.settrace(self.trace_dispatch)
383 res = None
384 try:
385 try:
386 res = apply(func, args)
387 except BdbQuit:
388 pass
389 finally:
390 self.quitting = 1
391 sys.settrace(None)
392 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000393
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000394
Guido van Rossumb6775db1994-08-01 11:34:53 +0000395def set_trace():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000396 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397
Guido van Rossumd93643f1998-09-11 22:38:35 +0000398
399class Breakpoint:
400
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000401 """Breakpoint class
Guido van Rossumd93643f1998-09-11 22:38:35 +0000402
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000403 Implements temporary breakpoints, ignore counts, disabling and
404 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000405
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000406 Breakpoints are indexed by number through bpbynumber and by
407 the file,line tuple using bplist. The former points to a
408 single instance of class Breakpoint. The latter points to a
409 list of such instances since there may be more than one
410 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000411
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000412 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000413
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000414 # XXX Keeping state in the class is a mistake -- this means
415 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000416
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000417 next = 1 # Next bp to be assigned
418 bplist = {} # indexed by (file, lineno) tuple
419 bpbynumber = [None] # Each entry is None or an instance of Bpt
420 # index 0 is unused, except for marking an
421 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000422
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000423 def __init__(self, file, line, temporary=0, cond = None):
424 self.file = file # This better be in canonical form!
425 self.line = line
426 self.temporary = temporary
427 self.cond = cond
428 self.enabled = 1
429 self.ignore = 0
430 self.hits = 0
431 self.number = Breakpoint.next
432 Breakpoint.next = Breakpoint.next + 1
433 # Build the two lists
434 self.bpbynumber.append(self)
435 if self.bplist.has_key((file, line)):
436 self.bplist[file, line].append(self)
437 else:
438 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000439
Tim Peters11cf6052001-01-14 21:54:20 +0000440
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000441 def deleteMe(self):
442 index = (self.file, self.line)
443 self.bpbynumber[self.number] = None # No longer in list
444 self.bplist[index].remove(self)
445 if not self.bplist[index]:
446 # No more bp for this f:l combo
447 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000448
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000449 def enable(self):
450 self.enabled = 1
Guido van Rossumd93643f1998-09-11 22:38:35 +0000451
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000452 def disable(self):
453 self.enabled = 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000454
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000455 def bpprint(self):
456 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000457 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000458 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000459 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000460 if self.enabled:
Tim Peters11cf6052001-01-14 21:54:20 +0000461 disp = disp + 'yes'
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000462 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000463 disp = disp + 'no '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000464 print '%-4dbreakpoint %s at %s:%d' % (self.number, disp,
465 self.file, self.line)
466 if self.cond:
467 print '\tstop only if %s' % (self.cond,)
468 if self.ignore:
469 print '\tignore next %d hits' % (self.ignore)
470 if (self.hits):
471 if (self.hits > 1): ss = 's'
472 else: ss = ''
473 print ('\tbreakpoint already hit %d time%s' %
474 (self.hits, ss))
Guido van Rossumd93643f1998-09-11 22:38:35 +0000475
476# -----------end of Breakpoint class----------
477
478# Determines if there is an effective (active) breakpoint at this
479# line of code. Returns breakpoint number or 0 if none
480def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000481 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000482
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000483 Called only if we know there is a bpt at this
484 location. Returns breakpoint that was triggered and a flag
485 that indicates if it is ok to delete a temporary bp.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000486
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000487 """
488 possibles = Breakpoint.bplist[file,line]
489 for i in range(0, len(possibles)):
490 b = possibles[i]
491 if b.enabled == 0:
492 continue
493 # Count every hit when bp is enabled
494 b.hits = b.hits + 1
495 if not b.cond:
496 # If unconditional, and ignoring,
497 # go on to next, else break
498 if b.ignore > 0:
499 b.ignore = b.ignore -1
500 continue
501 else:
502 # breakpoint and marker that's ok
503 # to delete if temporary
504 return (b,1)
505 else:
506 # Conditional bp.
507 # Ignore count applies only to those bpt hits where the
508 # condition evaluates to true.
509 try:
510 val = eval(b.cond, frame.f_globals,
Tim Peters11cf6052001-01-14 21:54:20 +0000511 frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000512 if val:
513 if b.ignore > 0:
514 b.ignore = b.ignore -1
515 # continue
516 else:
517 return (b,1)
518 # else:
519 # continue
520 except:
521 # if eval fails, most conservative
522 # thing is to stop on breakpoint
Tim Peters11cf6052001-01-14 21:54:20 +0000523 # regardless of ignore count.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000524 # Don't delete temporary,
525 # as another hint to user.
526 return (b,0)
527 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000528
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000529# -------------------- testing --------------------
530
531class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000532 def user_call(self, frame, args):
533 name = frame.f_code.co_name
534 if not name: name = '???'
535 print '+++ call', name, args
536 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000537 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000538 name = frame.f_code.co_name
539 if not name: name = '???'
540 fn = self.canonic(frame.f_code.co_filename)
541 line = linecache.getline(fn, frame.f_lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000542 print '+++', fn, frame.f_lineno, name, ':', line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000543 def user_return(self, frame, retval):
544 print '+++ return', retval
545 def user_exception(self, frame, exc_stuff):
546 print '+++ exception', exc_stuff
547 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000548
549def foo(n):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000550 print 'foo(', n, ')'
551 x = bar(n*10)
552 print 'bar returned', x
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000553
554def bar(a):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000555 print 'bar(', a, ')'
556 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000557
558def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000559 t = Tdb()
560 t.run('import bdb; bdb.foo(10)')
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000561
562# end