blob: b81a9fdc3450531fdc485d7a17aafe1d6f511d61 [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
40 self.stopframe = None
41 self.returnframe = None
42 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +000043
Guido van Rossum4acc25b2000-02-02 15:10:15 +000044 def trace_dispatch(self, frame, event, arg):
45 if self.quitting:
46 return # None
47 if event == 'line':
48 return self.dispatch_line(frame)
49 if event == 'call':
50 return self.dispatch_call(frame, arg)
51 if event == 'return':
52 return self.dispatch_return(frame, arg)
53 if event == 'exception':
54 return self.dispatch_exception(frame, arg)
55 print 'bdb.Bdb.dispatch: unknown debugging event:', `event`
56 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000057
Guido van Rossum4acc25b2000-02-02 15:10:15 +000058 def dispatch_line(self, frame):
59 if self.stop_here(frame) or self.break_here(frame):
60 self.user_line(frame)
61 if self.quitting: raise BdbQuit
62 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000063
Guido van Rossum4acc25b2000-02-02 15:10:15 +000064 def dispatch_call(self, frame, arg):
65 # XXX 'arg' is no longer used
66 if self.botframe is None:
67 # First call of dispatch since reset()
Christian Tismer313a7512002-05-28 08:04:00 +000068 self.botframe = frame.f_back # (CT) Note that this may also be None!
Guido van Rossum4acc25b2000-02-02 15:10:15 +000069 return self.trace_dispatch
70 if not (self.stop_here(frame) or self.break_anywhere(frame)):
71 # No need to trace this function
72 return # None
73 self.user_call(frame, arg)
74 if self.quitting: raise BdbQuit
75 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000076
Guido van Rossum4acc25b2000-02-02 15:10:15 +000077 def dispatch_return(self, frame, arg):
78 if self.stop_here(frame) or frame == self.returnframe:
79 self.user_return(frame, arg)
80 if self.quitting: raise BdbQuit
Just van Rossumae1f65f2001-06-25 18:01:24 +000081 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000082
Guido van Rossum4acc25b2000-02-02 15:10:15 +000083 def dispatch_exception(self, frame, arg):
84 if self.stop_here(frame):
85 self.user_exception(frame, arg)
86 if self.quitting: raise BdbQuit
87 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000088
Guido van Rossum4acc25b2000-02-02 15:10:15 +000089 # Normally derived classes don't override the following
90 # methods, but they may if they want to redefine the
91 # definition of stopping and breakpoints.
Tim Peters11cf6052001-01-14 21:54:20 +000092
Guido van Rossum4acc25b2000-02-02 15:10:15 +000093 def stop_here(self, frame):
Neal Norwitz72a2b4d2002-05-29 00:54:38 +000094 # (CT) stopframe may now also be None, see dispatch_call.
95 # (CT) the former test for None is therefore removed from here.
Guido van Rossum4acc25b2000-02-02 15:10:15 +000096 if frame is self.stopframe:
Tim Petersbc0e9102002-04-04 22:55:58 +000097 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +000098 while frame is not None and frame is not self.stopframe:
99 if frame is self.botframe:
Tim Petersbc0e9102002-04-04 22:55:58 +0000100 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000101 frame = frame.f_back
Tim Petersbc0e9102002-04-04 22:55:58 +0000102 return False
Guido van Rossumd93643f1998-09-11 22:38:35 +0000103
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000104 def break_here(self, frame):
105 filename = self.canonic(frame.f_code.co_filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000106 if not filename in self.breaks:
Tim Petersbc0e9102002-04-04 22:55:58 +0000107 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000108 lineno = frame.f_lineno
109 if not lineno in self.breaks[filename]:
Tim Petersbc0e9102002-04-04 22:55:58 +0000110 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000111 # flag says ok to delete temp. bp
112 (bp, flag) = effective(filename, lineno, frame)
113 if bp:
114 self.currentbp = bp.number
115 if (flag and bp.temporary):
116 self.do_clear(str(bp.number))
Tim Petersbc0e9102002-04-04 22:55:58 +0000117 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000118 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000119 return False
Tim Peters11cf6052001-01-14 21:54:20 +0000120
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000121 def do_clear(self, arg):
122 raise NotImplementedError, "subclass of bdb must implement do_clear()"
123
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000124 def break_anywhere(self, frame):
125 return self.breaks.has_key(
126 self.canonic(frame.f_code.co_filename))
Tim Peters11cf6052001-01-14 21:54:20 +0000127
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000128 # Derived classes should override the user_* methods
129 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000130
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000131 def user_call(self, frame, argument_list):
132 """This method is called when there is the remote possibility
133 that we ever need to stop in this function."""
134 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000135
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000136 def user_line(self, frame):
137 """This method is called when we stop or break at this line."""
138 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000139
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000140 def user_return(self, frame, return_value):
141 """This method is called when a return trap is set here."""
142 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000143
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000144 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
145 """This method is called if an exception occurs,
146 but only if we are to stop at or just below this level."""
147 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000148
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000149 # Derived classes and clients can call the following methods
150 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000151
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000152 def set_step(self):
153 """Stop after one line of code."""
154 self.stopframe = None
155 self.returnframe = None
156 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000157
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000158 def set_next(self, frame):
159 """Stop on the next line in or below the given frame."""
160 self.stopframe = frame
161 self.returnframe = None
162 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000163
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000164 def set_return(self, frame):
165 """Stop when returning from the given frame."""
166 self.stopframe = frame.f_back
167 self.returnframe = frame
168 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000169
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000170 def set_trace(self):
171 """Start debugging from here."""
Christian Tismer313a7512002-05-28 08:04:00 +0000172 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000173 self.reset()
174 while frame:
175 frame.f_trace = self.trace_dispatch
176 self.botframe = frame
177 frame = frame.f_back
178 self.set_step()
179 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000181 def set_continue(self):
182 # Don't stop except at breakpoints or when finished
183 self.stopframe = self.botframe
184 self.returnframe = None
185 self.quitting = 0
186 if not self.breaks:
187 # no breakpoints; run without debugger overhead
188 sys.settrace(None)
Christian Tismer313a7512002-05-28 08:04:00 +0000189 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000190 while frame and frame is not self.botframe:
191 del frame.f_trace
192 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000193
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000194 def set_quit(self):
195 self.stopframe = self.botframe
196 self.returnframe = None
197 self.quitting = 1
198 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000199
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000200 # Derived classes and clients can call the following methods
201 # to manipulate breakpoints. These methods return an
202 # error message is something went wrong, None if all is well.
203 # Set_break prints out the breakpoint line and file:lineno.
204 # Call self.get_*break*() to see the breakpoints or better
205 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000206
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000207 def set_break(self, filename, lineno, temporary=0, cond = None):
208 filename = self.canonic(filename)
209 import linecache # Import as late as possible
210 line = linecache.getline(filename, lineno)
211 if not line:
212 return 'Line %s:%d does not exist' % (filename,
213 lineno)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000214 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000215 self.breaks[filename] = []
216 list = self.breaks[filename]
217 if not lineno in list:
218 list.append(lineno)
219 bp = Breakpoint(filename, lineno, temporary, cond)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000220
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000221 def clear_break(self, filename, lineno):
222 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000223 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000224 return 'There are no breakpoints in %s' % filename
225 if lineno not in self.breaks[filename]:
226 return 'There is no breakpoint at %s:%d' % (filename,
227 lineno)
228 # If there's only one bp in the list for that file,line
229 # pair, then remove the breaks entry
230 for bp in Breakpoint.bplist[filename, lineno][:]:
231 bp.deleteMe()
232 if not Breakpoint.bplist.has_key((filename, lineno)):
233 self.breaks[filename].remove(lineno)
234 if not self.breaks[filename]:
235 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000236
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000237 def clear_bpbynumber(self, arg):
238 try:
239 number = int(arg)
240 except:
241 return 'Non-numeric breakpoint number (%s)' % arg
242 try:
243 bp = Breakpoint.bpbynumber[number]
244 except IndexError:
245 return 'Breakpoint number (%d) out of range' % number
246 if not bp:
247 return 'Breakpoint (%d) already deleted' % number
248 self.clear_break(bp.file, bp.line)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000249
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000250 def clear_all_file_breaks(self, filename):
251 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000252 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000253 return 'There are no breakpoints in %s' % filename
254 for line in self.breaks[filename]:
255 blist = Breakpoint.bplist[filename, line]
256 for bp in blist:
257 bp.deleteMe()
258 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000259
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000260 def clear_all_breaks(self):
261 if not self.breaks:
262 return 'There are no breakpoints'
263 for bp in Breakpoint.bpbynumber:
264 if bp:
265 bp.deleteMe()
266 self.breaks = {}
Tim Peters11cf6052001-01-14 21:54:20 +0000267
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000268 def get_break(self, filename, lineno):
269 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000270 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000271 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000272
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000273 def get_breaks(self, filename, lineno):
274 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000275 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000276 lineno in self.breaks[filename] and \
277 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000278
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000279 def get_file_breaks(self, filename):
280 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000281 if filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000282 return self.breaks[filename]
283 else:
284 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000285
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000286 def get_all_breaks(self):
287 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000288
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000289 # Derived classes and clients can call the following method
290 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000291
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 def get_stack(self, f, t):
293 stack = []
294 if t and t.tb_frame is f:
295 t = t.tb_next
296 while f is not None:
297 stack.append((f, f.f_lineno))
298 if f is self.botframe:
299 break
300 f = f.f_back
301 stack.reverse()
302 i = max(0, len(stack) - 1)
303 while t is not None:
304 stack.append((t.tb_frame, t.tb_lineno))
305 t = t.tb_next
306 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000307
308 #
309
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000310 def format_stack_entry(self, frame_lineno, lprefix=': '):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000311 import linecache, repr
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000312 frame, lineno = frame_lineno
313 filename = self.canonic(frame.f_code.co_filename)
314 s = filename + '(' + `lineno` + ')'
315 if frame.f_code.co_name:
316 s = s + frame.f_code.co_name
317 else:
318 s = s + "<lambda>"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000319 if '__args__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000320 args = frame.f_locals['__args__']
321 else:
322 args = None
323 if args:
324 s = s + repr.repr(args)
325 else:
326 s = s + '()'
Raymond Hettinger54f02222002-06-01 14:18:47 +0000327 if '__return__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000328 rv = frame.f_locals['__return__']
329 s = s + '->'
330 s = s + repr.repr(rv)
331 line = linecache.getline(filename, lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000332 if line: s = s + lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000333 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000334
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000335 # The following two methods can be called by clients to use
336 # a debugger to debug a statement, given as a string.
Tim Peters11cf6052001-01-14 21:54:20 +0000337
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000338 def run(self, cmd, globals=None, locals=None):
339 if globals is None:
340 import __main__
341 globals = __main__.__dict__
342 if locals is None:
343 locals = globals
344 self.reset()
345 sys.settrace(self.trace_dispatch)
346 if not isinstance(cmd, types.CodeType):
347 cmd = cmd+'\n'
348 try:
349 try:
350 exec cmd in globals, locals
351 except BdbQuit:
352 pass
353 finally:
354 self.quitting = 1
355 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000356
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000357 def runeval(self, expr, globals=None, locals=None):
358 if globals is None:
359 import __main__
360 globals = __main__.__dict__
361 if locals is None:
362 locals = globals
363 self.reset()
364 sys.settrace(self.trace_dispatch)
365 if not isinstance(expr, types.CodeType):
366 expr = expr+'\n'
367 try:
368 try:
369 return eval(expr, globals, locals)
370 except BdbQuit:
371 pass
372 finally:
373 self.quitting = 1
374 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000375
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000376 def runctx(self, cmd, globals, locals):
377 # B/W compatibility
378 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000379
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000380 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000381
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000382 def runcall(self, func, *args):
383 self.reset()
384 sys.settrace(self.trace_dispatch)
385 res = None
386 try:
387 try:
388 res = apply(func, args)
389 except BdbQuit:
390 pass
391 finally:
392 self.quitting = 1
393 sys.settrace(None)
394 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000395
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000396
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397def set_trace():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000398 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000399
Guido van Rossumd93643f1998-09-11 22:38:35 +0000400
401class Breakpoint:
402
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000403 """Breakpoint class
Guido van Rossumd93643f1998-09-11 22:38:35 +0000404
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000405 Implements temporary breakpoints, ignore counts, disabling and
406 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000407
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000408 Breakpoints are indexed by number through bpbynumber and by
409 the file,line tuple using bplist. The former points to a
410 single instance of class Breakpoint. The latter points to a
411 list of such instances since there may be more than one
412 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000413
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000414 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000415
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000416 # XXX Keeping state in the class is a mistake -- this means
417 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000418
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000419 next = 1 # Next bp to be assigned
420 bplist = {} # indexed by (file, lineno) tuple
421 bpbynumber = [None] # Each entry is None or an instance of Bpt
422 # index 0 is unused, except for marking an
423 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000424
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000425 def __init__(self, file, line, temporary=0, cond = None):
426 self.file = file # This better be in canonical form!
427 self.line = line
428 self.temporary = temporary
429 self.cond = cond
430 self.enabled = 1
431 self.ignore = 0
432 self.hits = 0
433 self.number = Breakpoint.next
434 Breakpoint.next = Breakpoint.next + 1
435 # Build the two lists
436 self.bpbynumber.append(self)
437 if self.bplist.has_key((file, line)):
438 self.bplist[file, line].append(self)
439 else:
440 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000441
Tim Peters11cf6052001-01-14 21:54:20 +0000442
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000443 def deleteMe(self):
444 index = (self.file, self.line)
445 self.bpbynumber[self.number] = None # No longer in list
446 self.bplist[index].remove(self)
447 if not self.bplist[index]:
448 # No more bp for this f:l combo
449 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000450
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000451 def enable(self):
452 self.enabled = 1
Guido van Rossumd93643f1998-09-11 22:38:35 +0000453
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000454 def disable(self):
455 self.enabled = 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000456
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000457 def bpprint(self):
458 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000459 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000460 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000461 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000462 if self.enabled:
Tim Peters11cf6052001-01-14 21:54:20 +0000463 disp = disp + 'yes'
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000464 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000465 disp = disp + 'no '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000466 print '%-4dbreakpoint %s at %s:%d' % (self.number, disp,
467 self.file, self.line)
468 if self.cond:
469 print '\tstop only if %s' % (self.cond,)
470 if self.ignore:
471 print '\tignore next %d hits' % (self.ignore)
472 if (self.hits):
473 if (self.hits > 1): ss = 's'
474 else: ss = ''
475 print ('\tbreakpoint already hit %d time%s' %
476 (self.hits, ss))
Guido van Rossumd93643f1998-09-11 22:38:35 +0000477
478# -----------end of Breakpoint class----------
479
480# Determines if there is an effective (active) breakpoint at this
481# line of code. Returns breakpoint number or 0 if none
482def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000483 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000484
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000485 Called only if we know there is a bpt at this
486 location. Returns breakpoint that was triggered and a flag
487 that indicates if it is ok to delete a temporary bp.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000488
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000489 """
490 possibles = Breakpoint.bplist[file,line]
491 for i in range(0, len(possibles)):
492 b = possibles[i]
493 if b.enabled == 0:
494 continue
495 # Count every hit when bp is enabled
496 b.hits = b.hits + 1
497 if not b.cond:
498 # If unconditional, and ignoring,
499 # go on to next, else break
500 if b.ignore > 0:
501 b.ignore = b.ignore -1
502 continue
503 else:
504 # breakpoint and marker that's ok
505 # to delete if temporary
506 return (b,1)
507 else:
508 # Conditional bp.
509 # Ignore count applies only to those bpt hits where the
510 # condition evaluates to true.
511 try:
512 val = eval(b.cond, frame.f_globals,
Tim Peters11cf6052001-01-14 21:54:20 +0000513 frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000514 if val:
515 if b.ignore > 0:
516 b.ignore = b.ignore -1
517 # continue
518 else:
519 return (b,1)
520 # else:
521 # continue
522 except:
523 # if eval fails, most conservative
524 # thing is to stop on breakpoint
Tim Peters11cf6052001-01-14 21:54:20 +0000525 # regardless of ignore count.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000526 # Don't delete temporary,
527 # as another hint to user.
528 return (b,0)
529 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000530
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000531# -------------------- testing --------------------
532
533class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000534 def user_call(self, frame, args):
535 name = frame.f_code.co_name
536 if not name: name = '???'
537 print '+++ call', name, args
538 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000539 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000540 name = frame.f_code.co_name
541 if not name: name = '???'
542 fn = self.canonic(frame.f_code.co_filename)
543 line = linecache.getline(fn, frame.f_lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000544 print '+++', fn, frame.f_lineno, name, ':', line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000545 def user_return(self, frame, retval):
546 print '+++ return', retval
547 def user_exception(self, frame, exc_stuff):
548 print '+++ exception', exc_stuff
549 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000550
551def foo(n):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000552 print 'foo(', n, ')'
553 x = bar(n*10)
554 print 'bar returned', x
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000555
556def bar(a):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000557 print 'bar(', a, ')'
558 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000559
560def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000561 t = Tdb()
562 t.run('import bdb; bdb.foo(10)')
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000563
564# end