blob: 19a97227231a598da36e22eb59abdd8182b16437 [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)
Nicholas Bastinc69ebe82004-03-24 21:57:10 +000055 if event == 'c_call':
56 return self.trace_dispatch
57 if event == 'c_exception':
58 return self.trace_dispatch
59 if event == 'c_return':
60 return self.trace_dispatch
Walter Dörwald70a6b492004-02-12 17:35:32 +000061 print 'bdb.Bdb.dispatch: unknown debugging event:', repr(event)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000062 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000063
Guido van Rossum4acc25b2000-02-02 15:10:15 +000064 def dispatch_line(self, frame):
65 if self.stop_here(frame) or self.break_here(frame):
66 self.user_line(frame)
67 if self.quitting: raise BdbQuit
68 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000069
Guido van Rossum4acc25b2000-02-02 15:10:15 +000070 def dispatch_call(self, frame, arg):
71 # XXX 'arg' is no longer used
72 if self.botframe is None:
73 # First call of dispatch since reset()
Christian Tismer313a7512002-05-28 08:04:00 +000074 self.botframe = frame.f_back # (CT) Note that this may also be None!
Guido van Rossum4acc25b2000-02-02 15:10:15 +000075 return self.trace_dispatch
76 if not (self.stop_here(frame) or self.break_anywhere(frame)):
77 # No need to trace this function
78 return # None
79 self.user_call(frame, arg)
80 if self.quitting: raise BdbQuit
81 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000082
Guido van Rossum4acc25b2000-02-02 15:10:15 +000083 def dispatch_return(self, frame, arg):
84 if self.stop_here(frame) or frame == self.returnframe:
85 self.user_return(frame, arg)
86 if self.quitting: raise BdbQuit
Just van Rossumae1f65f2001-06-25 18:01:24 +000087 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000088
Guido van Rossum4acc25b2000-02-02 15:10:15 +000089 def dispatch_exception(self, frame, arg):
90 if self.stop_here(frame):
91 self.user_exception(frame, arg)
92 if self.quitting: raise BdbQuit
93 return self.trace_dispatch
Tim Peters11cf6052001-01-14 21:54:20 +000094
Guido van Rossum4acc25b2000-02-02 15:10:15 +000095 # Normally derived classes don't override the following
96 # methods, but they may if they want to redefine the
97 # definition of stopping and breakpoints.
Tim Peters11cf6052001-01-14 21:54:20 +000098
Guido van Rossum4acc25b2000-02-02 15:10:15 +000099 def stop_here(self, frame):
Neal Norwitz72a2b4d2002-05-29 00:54:38 +0000100 # (CT) stopframe may now also be None, see dispatch_call.
101 # (CT) the former test for None is therefore removed from here.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000102 if frame is self.stopframe:
Tim Petersbc0e9102002-04-04 22:55:58 +0000103 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000104 while frame is not None and frame is not self.stopframe:
105 if frame is self.botframe:
Tim Petersbc0e9102002-04-04 22:55:58 +0000106 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000107 frame = frame.f_back
Tim Petersbc0e9102002-04-04 22:55:58 +0000108 return False
Guido van Rossumd93643f1998-09-11 22:38:35 +0000109
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000110 def break_here(self, frame):
111 filename = self.canonic(frame.f_code.co_filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000112 if not filename in self.breaks:
Tim Petersbc0e9102002-04-04 22:55:58 +0000113 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000114 lineno = frame.f_lineno
115 if not lineno in self.breaks[filename]:
Tim Petersbc0e9102002-04-04 22:55:58 +0000116 return False
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000117 # flag says ok to delete temp. bp
118 (bp, flag) = effective(filename, lineno, frame)
119 if bp:
120 self.currentbp = bp.number
121 if (flag and bp.temporary):
122 self.do_clear(str(bp.number))
Tim Petersbc0e9102002-04-04 22:55:58 +0000123 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000124 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000125 return False
Tim Peters11cf6052001-01-14 21:54:20 +0000126
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000127 def do_clear(self, arg):
128 raise NotImplementedError, "subclass of bdb must implement do_clear()"
129
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000130 def break_anywhere(self, frame):
131 return self.breaks.has_key(
132 self.canonic(frame.f_code.co_filename))
Tim Peters11cf6052001-01-14 21:54:20 +0000133
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000134 # Derived classes should override the user_* methods
135 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000136
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000137 def user_call(self, frame, argument_list):
138 """This method is called when there is the remote possibility
139 that we ever need to stop in this function."""
140 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000141
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000142 def user_line(self, frame):
143 """This method is called when we stop or break at this line."""
144 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000145
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000146 def user_return(self, frame, return_value):
147 """This method is called when a return trap is set here."""
148 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000149
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000150 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
151 """This method is called if an exception occurs,
152 but only if we are to stop at or just below this level."""
153 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000154
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000155 # Derived classes and clients can call the following methods
156 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000157
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000158 def set_step(self):
159 """Stop after one line of code."""
160 self.stopframe = None
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_next(self, frame):
165 """Stop on the next line in or below the given frame."""
166 self.stopframe = frame
167 self.returnframe = None
168 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000169
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000170 def set_return(self, frame):
171 """Stop when returning from the given frame."""
172 self.stopframe = frame.f_back
173 self.returnframe = frame
174 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000175
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000176 def set_trace(self):
177 """Start debugging from here."""
Christian Tismer313a7512002-05-28 08:04:00 +0000178 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000179 self.reset()
180 while frame:
181 frame.f_trace = self.trace_dispatch
182 self.botframe = frame
183 frame = frame.f_back
184 self.set_step()
185 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000187 def set_continue(self):
188 # Don't stop except at breakpoints or when finished
189 self.stopframe = self.botframe
190 self.returnframe = None
191 self.quitting = 0
192 if not self.breaks:
193 # no breakpoints; run without debugger overhead
194 sys.settrace(None)
Christian Tismer313a7512002-05-28 08:04:00 +0000195 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000196 while frame and frame is not self.botframe:
197 del frame.f_trace
198 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000199
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000200 def set_quit(self):
201 self.stopframe = self.botframe
202 self.returnframe = None
203 self.quitting = 1
204 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000205
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000206 # Derived classes and clients can call the following methods
207 # to manipulate breakpoints. These methods return an
208 # error message is something went wrong, None if all is well.
209 # Set_break prints out the breakpoint line and file:lineno.
210 # Call self.get_*break*() to see the breakpoints or better
211 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000212
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000213 def set_break(self, filename, lineno, temporary=0, cond = None):
214 filename = self.canonic(filename)
215 import linecache # Import as late as possible
216 line = linecache.getline(filename, lineno)
217 if not line:
218 return 'Line %s:%d does not exist' % (filename,
219 lineno)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000220 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000221 self.breaks[filename] = []
222 list = self.breaks[filename]
223 if not lineno in list:
224 list.append(lineno)
225 bp = Breakpoint(filename, lineno, temporary, cond)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000226
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000227 def clear_break(self, filename, lineno):
228 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000229 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000230 return 'There are no breakpoints in %s' % filename
231 if lineno not in self.breaks[filename]:
232 return 'There is no breakpoint at %s:%d' % (filename,
233 lineno)
234 # If there's only one bp in the list for that file,line
235 # pair, then remove the breaks entry
236 for bp in Breakpoint.bplist[filename, lineno][:]:
237 bp.deleteMe()
238 if not Breakpoint.bplist.has_key((filename, lineno)):
239 self.breaks[filename].remove(lineno)
240 if not self.breaks[filename]:
241 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000242
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000243 def clear_bpbynumber(self, arg):
244 try:
245 number = int(arg)
246 except:
247 return 'Non-numeric breakpoint number (%s)' % arg
248 try:
249 bp = Breakpoint.bpbynumber[number]
250 except IndexError:
251 return 'Breakpoint number (%d) out of range' % number
252 if not bp:
253 return 'Breakpoint (%d) already deleted' % number
254 self.clear_break(bp.file, bp.line)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000255
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000256 def clear_all_file_breaks(self, filename):
257 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000258 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000259 return 'There are no breakpoints in %s' % filename
260 for line in self.breaks[filename]:
261 blist = Breakpoint.bplist[filename, line]
262 for bp in blist:
263 bp.deleteMe()
264 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000265
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000266 def clear_all_breaks(self):
267 if not self.breaks:
268 return 'There are no breakpoints'
269 for bp in Breakpoint.bpbynumber:
270 if bp:
271 bp.deleteMe()
272 self.breaks = {}
Tim Peters11cf6052001-01-14 21:54:20 +0000273
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000274 def get_break(self, filename, lineno):
275 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000276 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000277 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000278
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000279 def get_breaks(self, filename, lineno):
280 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000281 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000282 lineno in self.breaks[filename] and \
283 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000284
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000285 def get_file_breaks(self, filename):
286 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000287 if filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000288 return self.breaks[filename]
289 else:
290 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000291
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 def get_all_breaks(self):
293 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000294
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000295 # Derived classes and clients can call the following method
296 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000297
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000298 def get_stack(self, f, t):
299 stack = []
300 if t and t.tb_frame is f:
301 t = t.tb_next
302 while f is not None:
303 stack.append((f, f.f_lineno))
304 if f is self.botframe:
305 break
306 f = f.f_back
307 stack.reverse()
308 i = max(0, len(stack) - 1)
309 while t is not None:
310 stack.append((t.tb_frame, t.tb_lineno))
311 t = t.tb_next
312 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000313
314 #
315
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000316 def format_stack_entry(self, frame_lineno, lprefix=': '):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000317 import linecache, repr
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000318 frame, lineno = frame_lineno
319 filename = self.canonic(frame.f_code.co_filename)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000320 s = '%s(%r)' % (filename, lineno)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000321 if frame.f_code.co_name:
322 s = s + frame.f_code.co_name
323 else:
324 s = s + "<lambda>"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000325 if '__args__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000326 args = frame.f_locals['__args__']
327 else:
328 args = None
329 if args:
330 s = s + repr.repr(args)
331 else:
332 s = s + '()'
Raymond Hettinger54f02222002-06-01 14:18:47 +0000333 if '__return__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000334 rv = frame.f_locals['__return__']
335 s = s + '->'
336 s = s + repr.repr(rv)
337 line = linecache.getline(filename, lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000338 if line: s = s + lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000339 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000340
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000341 # The following two methods can be called by clients to use
342 # a debugger to debug a statement, given as a string.
Tim Peters11cf6052001-01-14 21:54:20 +0000343
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000344 def run(self, cmd, globals=None, locals=None):
345 if globals is None:
346 import __main__
347 globals = __main__.__dict__
348 if locals is None:
349 locals = globals
350 self.reset()
351 sys.settrace(self.trace_dispatch)
352 if not isinstance(cmd, types.CodeType):
353 cmd = cmd+'\n'
354 try:
355 try:
356 exec cmd in globals, locals
357 except BdbQuit:
358 pass
359 finally:
360 self.quitting = 1
361 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000362
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000363 def runeval(self, expr, globals=None, locals=None):
364 if globals is None:
365 import __main__
366 globals = __main__.__dict__
367 if locals is None:
368 locals = globals
369 self.reset()
370 sys.settrace(self.trace_dispatch)
371 if not isinstance(expr, types.CodeType):
372 expr = expr+'\n'
373 try:
374 try:
375 return eval(expr, globals, locals)
376 except BdbQuit:
377 pass
378 finally:
379 self.quitting = 1
380 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000381
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000382 def runctx(self, cmd, globals, locals):
383 # B/W compatibility
384 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000385
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000386 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000387
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000388 def runcall(self, func, *args):
389 self.reset()
390 sys.settrace(self.trace_dispatch)
391 res = None
392 try:
393 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000394 res = func(*args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000395 except BdbQuit:
396 pass
397 finally:
398 self.quitting = 1
399 sys.settrace(None)
400 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000401
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000402
Guido van Rossumb6775db1994-08-01 11:34:53 +0000403def set_trace():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000405
Guido van Rossumd93643f1998-09-11 22:38:35 +0000406
407class Breakpoint:
408
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000409 """Breakpoint class
Guido van Rossumd93643f1998-09-11 22:38:35 +0000410
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000411 Implements temporary breakpoints, ignore counts, disabling and
412 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000413
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000414 Breakpoints are indexed by number through bpbynumber and by
415 the file,line tuple using bplist. The former points to a
416 single instance of class Breakpoint. The latter points to a
417 list of such instances since there may be more than one
418 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000419
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000420 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000421
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000422 # XXX Keeping state in the class is a mistake -- this means
423 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000424
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000425 next = 1 # Next bp to be assigned
426 bplist = {} # indexed by (file, lineno) tuple
427 bpbynumber = [None] # Each entry is None or an instance of Bpt
428 # index 0 is unused, except for marking an
429 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000430
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000431 def __init__(self, file, line, temporary=0, cond = None):
432 self.file = file # This better be in canonical form!
433 self.line = line
434 self.temporary = temporary
435 self.cond = cond
436 self.enabled = 1
437 self.ignore = 0
438 self.hits = 0
439 self.number = Breakpoint.next
440 Breakpoint.next = Breakpoint.next + 1
441 # Build the two lists
442 self.bpbynumber.append(self)
443 if self.bplist.has_key((file, line)):
444 self.bplist[file, line].append(self)
445 else:
446 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000447
Tim Peters11cf6052001-01-14 21:54:20 +0000448
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000449 def deleteMe(self):
450 index = (self.file, self.line)
451 self.bpbynumber[self.number] = None # No longer in list
452 self.bplist[index].remove(self)
453 if not self.bplist[index]:
454 # No more bp for this f:l combo
455 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000456
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000457 def enable(self):
458 self.enabled = 1
Guido van Rossumd93643f1998-09-11 22:38:35 +0000459
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000460 def disable(self):
461 self.enabled = 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000462
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000463 def bpprint(self):
464 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000465 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000466 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000467 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000468 if self.enabled:
Tim Peters11cf6052001-01-14 21:54:20 +0000469 disp = disp + 'yes'
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000470 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000471 disp = disp + 'no '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000472 print '%-4dbreakpoint %s at %s:%d' % (self.number, disp,
473 self.file, self.line)
474 if self.cond:
475 print '\tstop only if %s' % (self.cond,)
476 if self.ignore:
477 print '\tignore next %d hits' % (self.ignore)
478 if (self.hits):
479 if (self.hits > 1): ss = 's'
480 else: ss = ''
481 print ('\tbreakpoint already hit %d time%s' %
482 (self.hits, ss))
Guido van Rossumd93643f1998-09-11 22:38:35 +0000483
484# -----------end of Breakpoint class----------
485
486# Determines if there is an effective (active) breakpoint at this
487# line of code. Returns breakpoint number or 0 if none
488def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000489 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000490
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000491 Called only if we know there is a bpt at this
492 location. Returns breakpoint that was triggered and a flag
493 that indicates if it is ok to delete a temporary bp.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000494
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000495 """
496 possibles = Breakpoint.bplist[file,line]
497 for i in range(0, len(possibles)):
498 b = possibles[i]
499 if b.enabled == 0:
500 continue
501 # Count every hit when bp is enabled
502 b.hits = b.hits + 1
503 if not b.cond:
504 # If unconditional, and ignoring,
505 # go on to next, else break
506 if b.ignore > 0:
507 b.ignore = b.ignore -1
508 continue
509 else:
510 # breakpoint and marker that's ok
511 # to delete if temporary
512 return (b,1)
513 else:
514 # Conditional bp.
515 # Ignore count applies only to those bpt hits where the
516 # condition evaluates to true.
517 try:
518 val = eval(b.cond, frame.f_globals,
Tim Peters11cf6052001-01-14 21:54:20 +0000519 frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000520 if val:
521 if b.ignore > 0:
522 b.ignore = b.ignore -1
523 # continue
524 else:
525 return (b,1)
526 # else:
527 # continue
528 except:
529 # if eval fails, most conservative
530 # thing is to stop on breakpoint
Tim Peters11cf6052001-01-14 21:54:20 +0000531 # regardless of ignore count.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000532 # Don't delete temporary,
533 # as another hint to user.
534 return (b,0)
535 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000536
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000537# -------------------- testing --------------------
538
539class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000540 def user_call(self, frame, args):
541 name = frame.f_code.co_name
542 if not name: name = '???'
543 print '+++ call', name, args
544 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000545 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000546 name = frame.f_code.co_name
547 if not name: name = '???'
548 fn = self.canonic(frame.f_code.co_filename)
549 line = linecache.getline(fn, frame.f_lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000550 print '+++', fn, frame.f_lineno, name, ':', line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000551 def user_return(self, frame, retval):
552 print '+++ return', retval
553 def user_exception(self, frame, exc_stuff):
554 print '+++ exception', exc_stuff
555 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000556
557def foo(n):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000558 print 'foo(', n, ')'
559 x = bar(n*10)
560 print 'bar returned', x
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000561
562def bar(a):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000563 print 'bar(', a, ')'
564 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000565
566def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000567 t = Tdb()
568 t.run('import bdb; bdb.foo(10)')
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000569
570# end