blob: d49e7e385d5ce15c465905baa8332a00cf9ec107 [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
Guido van Rossumbe19ed72007-02-09 05:37:30 +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]:
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000116 # The line itself has no breakpoint, but maybe the line is the
117 # first line of a function with breakpoint set by function name.
118 lineno = frame.f_code.co_firstlineno
119 if not lineno in self.breaks[filename]:
120 return False
121
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000122 # flag says ok to delete temp. bp
123 (bp, flag) = effective(filename, lineno, frame)
124 if bp:
125 self.currentbp = bp.number
126 if (flag and bp.temporary):
127 self.do_clear(str(bp.number))
Tim Petersbc0e9102002-04-04 22:55:58 +0000128 return True
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000129 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000130 return False
Tim Peters11cf6052001-01-14 21:54:20 +0000131
Guido van Rossum9cec8fb2001-04-08 15:05:16 +0000132 def do_clear(self, arg):
133 raise NotImplementedError, "subclass of bdb must implement do_clear()"
134
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000135 def break_anywhere(self, frame):
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000136 return self.canonic(frame.f_code.co_filename) in self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000137
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000138 # Derived classes should override the user_* methods
139 # to gain control.
Tim Peters11cf6052001-01-14 21:54:20 +0000140
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000141 def user_call(self, frame, argument_list):
142 """This method is called when there is the remote possibility
143 that we ever need to stop in this function."""
144 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000145
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000146 def user_line(self, frame):
147 """This method is called when we stop or break at this line."""
148 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000149
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000150 def user_return(self, frame, return_value):
151 """This method is called when a return trap is set here."""
152 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000153
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000154 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
155 """This method is called if an exception occurs,
156 but only if we are to stop at or just below this level."""
157 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000158
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000159 # Derived classes and clients can call the following methods
160 # to affect the stepping state.
Tim Peters11cf6052001-01-14 21:54:20 +0000161
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000162 def set_step(self):
163 """Stop after one line of code."""
164 self.stopframe = None
165 self.returnframe = None
166 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000167
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000168 def set_next(self, frame):
169 """Stop on the next line in or below the given frame."""
170 self.stopframe = frame
171 self.returnframe = None
172 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000173
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000174 def set_return(self, frame):
175 """Stop when returning from the given frame."""
176 self.stopframe = frame.f_back
177 self.returnframe = frame
178 self.quitting = 0
Tim Peters11cf6052001-01-14 21:54:20 +0000179
Johannes Gijsbers84a6c202004-11-07 11:35:30 +0000180 def set_trace(self, frame=None):
181 """Start debugging from `frame`.
182
183 If frame is not specified, debugging starts from caller's frame.
184 """
185 if frame is None:
186 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000187 self.reset()
188 while frame:
189 frame.f_trace = self.trace_dispatch
190 self.botframe = frame
191 frame = frame.f_back
192 self.set_step()
193 sys.settrace(self.trace_dispatch)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000195 def set_continue(self):
196 # Don't stop except at breakpoints or when finished
197 self.stopframe = self.botframe
198 self.returnframe = None
199 self.quitting = 0
200 if not self.breaks:
201 # no breakpoints; run without debugger overhead
202 sys.settrace(None)
Christian Tismer313a7512002-05-28 08:04:00 +0000203 frame = sys._getframe().f_back
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000204 while frame and frame is not self.botframe:
205 del frame.f_trace
206 frame = frame.f_back
Tim Peters11cf6052001-01-14 21:54:20 +0000207
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000208 def set_quit(self):
209 self.stopframe = self.botframe
210 self.returnframe = None
211 self.quitting = 1
212 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000213
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000214 # Derived classes and clients can call the following methods
215 # to manipulate breakpoints. These methods return an
216 # error message is something went wrong, None if all is well.
217 # Set_break prints out the breakpoint line and file:lineno.
218 # Call self.get_*break*() to see the breakpoints or better
219 # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint().
Tim Peters11cf6052001-01-14 21:54:20 +0000220
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000221 def set_break(self, filename, lineno, temporary=0, cond = None,
222 funcname=None):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000223 filename = self.canonic(filename)
224 import linecache # Import as late as possible
225 line = linecache.getline(filename, lineno)
226 if not line:
227 return 'Line %s:%d does not exist' % (filename,
228 lineno)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000229 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000230 self.breaks[filename] = []
231 list = self.breaks[filename]
232 if not lineno in list:
233 list.append(lineno)
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000234 bp = Breakpoint(filename, lineno, temporary, cond, funcname)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000235
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000236 def clear_break(self, filename, lineno):
237 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000238 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000239 return 'There are no breakpoints in %s' % filename
240 if lineno not in self.breaks[filename]:
241 return 'There is no breakpoint at %s:%d' % (filename,
242 lineno)
243 # If there's only one bp in the list for that file,line
244 # pair, then remove the breaks entry
245 for bp in Breakpoint.bplist[filename, lineno][:]:
246 bp.deleteMe()
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000247 if (filename, lineno) not in Breakpoint.bplist:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000248 self.breaks[filename].remove(lineno)
249 if not self.breaks[filename]:
250 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000251
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000252 def clear_bpbynumber(self, arg):
253 try:
254 number = int(arg)
255 except:
256 return 'Non-numeric breakpoint number (%s)' % arg
257 try:
258 bp = Breakpoint.bpbynumber[number]
259 except IndexError:
260 return 'Breakpoint number (%d) out of range' % number
261 if not bp:
262 return 'Breakpoint (%d) already deleted' % number
263 self.clear_break(bp.file, bp.line)
Guido van Rossum6ea27cc1999-01-25 20:51:34 +0000264
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000265 def clear_all_file_breaks(self, filename):
266 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000267 if not filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000268 return 'There are no breakpoints in %s' % filename
269 for line in self.breaks[filename]:
270 blist = Breakpoint.bplist[filename, line]
271 for bp in blist:
272 bp.deleteMe()
273 del self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000274
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000275 def clear_all_breaks(self):
276 if not self.breaks:
277 return 'There are no breakpoints'
278 for bp in Breakpoint.bpbynumber:
279 if bp:
280 bp.deleteMe()
281 self.breaks = {}
Tim Peters11cf6052001-01-14 21:54:20 +0000282
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000283 def get_break(self, filename, lineno):
284 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000285 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000286 lineno in self.breaks[filename]
Tim Peters11cf6052001-01-14 21:54:20 +0000287
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000288 def get_breaks(self, filename, lineno):
289 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000290 return filename in self.breaks and \
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000291 lineno in self.breaks[filename] and \
292 Breakpoint.bplist[filename, lineno] or []
Tim Peters11cf6052001-01-14 21:54:20 +0000293
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000294 def get_file_breaks(self, filename):
295 filename = self.canonic(filename)
Raymond Hettinger54f02222002-06-01 14:18:47 +0000296 if filename in self.breaks:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000297 return self.breaks[filename]
298 else:
299 return []
Tim Peters11cf6052001-01-14 21:54:20 +0000300
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000301 def get_all_breaks(self):
302 return self.breaks
Tim Peters11cf6052001-01-14 21:54:20 +0000303
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000304 # Derived classes and clients can call the following method
305 # to get a data structure representing a stack trace.
Tim Peters11cf6052001-01-14 21:54:20 +0000306
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000307 def get_stack(self, f, t):
308 stack = []
309 if t and t.tb_frame is f:
310 t = t.tb_next
311 while f is not None:
312 stack.append((f, f.f_lineno))
313 if f is self.botframe:
314 break
315 f = f.f_back
316 stack.reverse()
317 i = max(0, len(stack) - 1)
318 while t is not None:
319 stack.append((t.tb_frame, t.tb_lineno))
320 t = t.tb_next
321 return stack, i
Tim Peters11cf6052001-01-14 21:54:20 +0000322
323 #
324
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000325 def format_stack_entry(self, frame_lineno, lprefix=': '):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000326 import linecache, repr
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000327 frame, lineno = frame_lineno
328 filename = self.canonic(frame.f_code.co_filename)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000329 s = '%s(%r)' % (filename, lineno)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000330 if frame.f_code.co_name:
331 s = s + frame.f_code.co_name
332 else:
333 s = s + "<lambda>"
Raymond Hettinger54f02222002-06-01 14:18:47 +0000334 if '__args__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000335 args = frame.f_locals['__args__']
336 else:
337 args = None
338 if args:
339 s = s + repr.repr(args)
340 else:
341 s = s + '()'
Raymond Hettinger54f02222002-06-01 14:18:47 +0000342 if '__return__' in frame.f_locals:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000343 rv = frame.f_locals['__return__']
344 s = s + '->'
345 s = s + repr.repr(rv)
346 line = linecache.getline(filename, lineno)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000347 if line: s = s + lprefix + line.strip()
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000348 return s
Tim Peters11cf6052001-01-14 21:54:20 +0000349
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000350 # The following two methods can be called by clients to use
351 # a debugger to debug a statement, given as a string.
Tim Peters11cf6052001-01-14 21:54:20 +0000352
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000353 def run(self, cmd, globals=None, locals=None):
354 if globals is None:
355 import __main__
356 globals = __main__.__dict__
357 if locals is None:
358 locals = globals
359 self.reset()
360 sys.settrace(self.trace_dispatch)
361 if not isinstance(cmd, types.CodeType):
362 cmd = cmd+'\n'
363 try:
364 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000365 exec(cmd, globals, locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000366 except BdbQuit:
367 pass
368 finally:
369 self.quitting = 1
370 sys.settrace(None)
Tim Peters11cf6052001-01-14 21:54:20 +0000371
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000372 def runeval(self, expr, globals=None, locals=None):
373 if globals is None:
374 import __main__
375 globals = __main__.__dict__
376 if locals is None:
377 locals = globals
378 self.reset()
379 sys.settrace(self.trace_dispatch)
380 if not isinstance(expr, types.CodeType):
381 expr = expr+'\n'
382 try:
383 try:
384 return eval(expr, globals, locals)
385 except BdbQuit:
386 pass
387 finally:
388 self.quitting = 1
389 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000390
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000391 def runctx(self, cmd, globals, locals):
392 # B/W compatibility
393 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000394
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000395 # This method is more useful to debug a single function call.
Guido van Rossum4e160981992-09-02 20:43:20 +0000396
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +0000397 def runcall(self, func, *args, **kwds):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000398 self.reset()
399 sys.settrace(self.trace_dispatch)
400 res = None
401 try:
402 try:
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +0000403 res = func(*args, **kwds)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 except BdbQuit:
405 pass
406 finally:
407 self.quitting = 1
408 sys.settrace(None)
409 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000410
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000411
Guido van Rossumb6775db1994-08-01 11:34:53 +0000412def set_trace():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000413 Bdb().set_trace()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414
Guido van Rossumd93643f1998-09-11 22:38:35 +0000415
416class Breakpoint:
417
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000418 """Breakpoint class
Guido van Rossumd93643f1998-09-11 22:38:35 +0000419
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000420 Implements temporary breakpoints, ignore counts, disabling and
421 (re)-enabling, and conditionals.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000422
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000423 Breakpoints are indexed by number through bpbynumber and by
424 the file,line tuple using bplist. The former points to a
425 single instance of class Breakpoint. The latter points to a
426 list of such instances since there may be more than one
427 breakpoint per line.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000428
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000429 """
Guido van Rossumd93643f1998-09-11 22:38:35 +0000430
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000431 # XXX Keeping state in the class is a mistake -- this means
432 # you cannot have more than one active Bdb instance.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000433
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000434 next = 1 # Next bp to be assigned
435 bplist = {} # indexed by (file, lineno) tuple
436 bpbynumber = [None] # Each entry is None or an instance of Bpt
437 # index 0 is unused, except for marking an
438 # effective break .... see effective()
Guido van Rossumd93643f1998-09-11 22:38:35 +0000439
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000440 def __init__(self, file, line, temporary=0, cond=None, funcname=None):
441 self.funcname = funcname
442 # Needed if funcname is not None.
443 self.func_first_executable_line = None
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000444 self.file = file # This better be in canonical form!
445 self.line = line
446 self.temporary = temporary
447 self.cond = cond
448 self.enabled = 1
449 self.ignore = 0
450 self.hits = 0
451 self.number = Breakpoint.next
452 Breakpoint.next = Breakpoint.next + 1
453 # Build the two lists
454 self.bpbynumber.append(self)
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000455 if (file, line) in self.bplist:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000456 self.bplist[file, line].append(self)
457 else:
458 self.bplist[file, line] = [self]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000459
Tim Peters11cf6052001-01-14 21:54:20 +0000460
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000461 def deleteMe(self):
462 index = (self.file, self.line)
463 self.bpbynumber[self.number] = None # No longer in list
464 self.bplist[index].remove(self)
465 if not self.bplist[index]:
466 # No more bp for this f:l combo
467 del self.bplist[index]
Guido van Rossumd93643f1998-09-11 22:38:35 +0000468
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000469 def enable(self):
470 self.enabled = 1
Guido van Rossumd93643f1998-09-11 22:38:35 +0000471
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000472 def disable(self):
473 self.enabled = 0
Guido van Rossumd93643f1998-09-11 22:38:35 +0000474
Thomas Wouters477c8d52006-05-27 19:21:47 +0000475 def bpprint(self, out=None):
476 if out is None:
477 out = sys.stdout
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000478 if self.temporary:
Tim Peters11cf6052001-01-14 21:54:20 +0000479 disp = 'del '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000480 else:
Tim Peters11cf6052001-01-14 21:54:20 +0000481 disp = 'keep '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000482 if self.enabled:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000483 disp = disp + 'yes '
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000484 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 disp = disp + 'no '
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000486 print('%-4dbreakpoint %s at %s:%d' % (self.number, disp,
487 self.file, self.line), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000488 if self.cond:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000489 print('\tstop only if %s' % (self.cond,), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000490 if self.ignore:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000491 print('\tignore next %d hits' % (self.ignore), file=out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000492 if (self.hits):
493 if (self.hits > 1): ss = 's'
494 else: ss = ''
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000495 print(('\tbreakpoint already hit %d time%s' %
496 (self.hits, ss)), file=out)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000497
498# -----------end of Breakpoint class----------
499
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000500def checkfuncname(b, frame):
501 """Check whether we should break here because of `b.funcname`."""
502 if not b.funcname:
503 # Breakpoint was set via line number.
504 if b.line != frame.f_lineno:
505 # Breakpoint was set at a line with a def statement and the function
506 # defined is called: don't break.
507 return False
508 return True
509
510 # Breakpoint set via function name.
511
512 if frame.f_code.co_name != b.funcname:
513 # It's not a function call, but rather execution of def statement.
514 return False
515
516 # We are in the right frame.
517 if not b.func_first_executable_line:
518 # The function is entered for the 1st time.
519 b.func_first_executable_line = frame.f_lineno
520
521 if b.func_first_executable_line != frame.f_lineno:
522 # But we are not at the first line number: don't break.
523 return False
524 return True
525
Guido van Rossumd93643f1998-09-11 22:38:35 +0000526# Determines if there is an effective (active) breakpoint at this
527# line of code. Returns breakpoint number or 0 if none
528def effective(file, line, frame):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000529 """Determine which breakpoint for this file:line is to be acted upon.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000530
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000531 Called only if we know there is a bpt at this
532 location. Returns breakpoint that was triggered and a flag
533 that indicates if it is ok to delete a temporary bp.
Guido van Rossumd93643f1998-09-11 22:38:35 +0000534
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000535 """
536 possibles = Breakpoint.bplist[file,line]
537 for i in range(0, len(possibles)):
538 b = possibles[i]
539 if b.enabled == 0:
540 continue
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000541 if not checkfuncname(b, frame):
542 continue
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000543 # Count every hit when bp is enabled
544 b.hits = b.hits + 1
545 if not b.cond:
546 # If unconditional, and ignoring,
547 # go on to next, else break
548 if b.ignore > 0:
549 b.ignore = b.ignore -1
550 continue
551 else:
552 # breakpoint and marker that's ok
553 # to delete if temporary
554 return (b,1)
555 else:
556 # Conditional bp.
557 # Ignore count applies only to those bpt hits where the
558 # condition evaluates to true.
559 try:
560 val = eval(b.cond, frame.f_globals,
Tim Peters11cf6052001-01-14 21:54:20 +0000561 frame.f_locals)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000562 if val:
563 if b.ignore > 0:
564 b.ignore = b.ignore -1
565 # continue
566 else:
567 return (b,1)
568 # else:
569 # continue
570 except:
571 # if eval fails, most conservative
572 # thing is to stop on breakpoint
Tim Peters11cf6052001-01-14 21:54:20 +0000573 # regardless of ignore count.
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000574 # Don't delete temporary,
575 # as another hint to user.
576 return (b,0)
577 return (None, None)
Guido van Rossumd93643f1998-09-11 22:38:35 +0000578
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000579# -------------------- testing --------------------
580
581class Tdb(Bdb):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000582 def user_call(self, frame, args):
583 name = frame.f_code.co_name
584 if not name: name = '???'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000585 print('+++ call', name, args)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000586 def user_line(self, frame):
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000587 import linecache
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000588 name = frame.f_code.co_name
589 if not name: name = '???'
590 fn = self.canonic(frame.f_code.co_filename)
591 line = linecache.getline(fn, frame.f_lineno)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000592 print('+++', fn, frame.f_lineno, name, ':', line.strip())
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000593 def user_return(self, frame, retval):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000594 print('+++ return', retval)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000595 def user_exception(self, frame, exc_stuff):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000596 print('+++ exception', exc_stuff)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000597 self.set_continue()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000598
599def foo(n):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000600 print('foo(', n, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000601 x = bar(n*10)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000602 print('bar returned', x)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000603
604def bar(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000605 print('bar(', a, ')')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000606 return a/2
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000607
608def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000609 t = Tdb()
610 t.run('import bdb; bdb.foo(10)')
Eric S. Raymondb49f4a42001-02-09 05:07:04 +0000611
612# end