blob: e4b6d39a29dcf13d41588b00a2849815b895dbda [file] [log] [blame]
Guido van Rossumbabe2bf1992-01-22 22:21:31 +00001# A generic Python debugger base class.
2# This class takes care of details of the trace facility;
3# a derived class should implement user interaction.
4# There are two debuggers based upon this:
5# 'pdb', a text-oriented debugger not unlike dbx or gdb;
6# and 'wdb', a window-oriented debugger.
7# And of course... you can roll your own!
8
9import sys
Guido van Rossum4808dcb1996-10-15 14:40:21 +000010import types
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000011
12BdbQuit = 'bdb.BdbQuit' # Exception to give up completely
13
14
15class Bdb: # Basic Debugger
16
Guido van Rossum5ef74b81993-06-23 11:55:24 +000017 def __init__(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000018 self.breaks = {}
Guido van Rossumff02e1d1997-07-11 13:42:50 +000019 self.cbreaks = {}
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000020
21 def reset(self):
Guido van Rossumb6775db1994-08-01 11:34:53 +000022 import linecache
23 linecache.checkcache()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000024 self.botframe = None
25 self.stopframe = None
26 self.returnframe = None
27 self.quitting = 0
28
29 def trace_dispatch(self, frame, event, arg):
30 if self.quitting:
31 return # None
32 if event == 'line':
33 return self.dispatch_line(frame)
34 if event == 'call':
35 return self.dispatch_call(frame, arg)
36 if event == 'return':
37 return self.dispatch_return(frame, arg)
38 if event == 'exception':
39 return self.dispatch_exception(frame, arg)
40 print 'bdb.Bdb.dispatch: unknown debugging event:', `event`
41 return self.trace_dispatch
42
43 def dispatch_line(self, frame):
44 if self.stop_here(frame) or self.break_here(frame):
45 self.user_line(frame)
46 if self.quitting: raise BdbQuit
47 return self.trace_dispatch
48
49 def dispatch_call(self, frame, arg):
50 frame.f_locals['__args__'] = arg
51 if self.botframe is None:
52 # First call of dispatch since reset()
53 self.botframe = frame
54 return self.trace_dispatch
55 if not (self.stop_here(frame) or self.break_anywhere(frame)):
56 # No need to trace this function
57 return # None
58 self.user_call(frame, arg)
59 if self.quitting: raise BdbQuit
60 return self.trace_dispatch
61
62 def dispatch_return(self, frame, arg):
63 if self.stop_here(frame) or frame == self.returnframe:
64 self.user_return(frame, arg)
65 if self.quitting: raise BdbQuit
66
67 def dispatch_exception(self, frame, arg):
68 if self.stop_here(frame):
69 self.user_exception(frame, arg)
70 if self.quitting: raise BdbQuit
71 return self.trace_dispatch
72
73 # Normally derived classes don't override the following
Guido van Rossum4e160981992-09-02 20:43:20 +000074 # methods, but they may if they want to redefine the
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000075 # definition of stopping and breakpoints.
76
77 def stop_here(self, frame):
78 if self.stopframe is None:
79 return 1
80 if frame is self.stopframe:
81 return 1
82 while frame is not None and frame is not self.stopframe:
83 if frame is self.botframe:
84 return 1
85 frame = frame.f_back
86 return 0
87
88 def break_here(self, frame):
Guido van Rossum66836171997-07-11 13:43:31 +000089 filename=frame.f_code.co_filename
Guido van Rossumff02e1d1997-07-11 13:42:50 +000090 if not self.breaks.has_key(filename):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000091 return 0
Guido van Rossumff02e1d1997-07-11 13:42:50 +000092 lineno=frame.f_lineno
93 if not lineno in self.breaks[filename]:
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000094 return 0
Guido van Rossumff02e1d1997-07-11 13:42:50 +000095 if self.cbreaks.has_key((filename, lineno)):
Guido van Rossum66836171997-07-11 13:43:31 +000096 cond=self.cbreaks[filename, lineno]
97 return eval(cond, frame.f_globals,
98 frame.f_locals)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000099 return 1
100
101 def break_anywhere(self, frame):
102 return self.breaks.has_key(frame.f_code.co_filename)
103
Guido van Rossum4e160981992-09-02 20:43:20 +0000104 # Derived classes should override the user_* methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000105 # to gain control.
106
107 def user_call(self, frame, argument_list):
Guido van Rossum4e160981992-09-02 20:43:20 +0000108 # This method is called when there is the remote possibility
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000109 # that we ever need to stop in this function
110 pass
111
112 def user_line(self, frame):
Guido van Rossum4e160981992-09-02 20:43:20 +0000113 # This method is called when we stop or break at this line
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000114 pass
115
116 def user_return(self, frame, return_value):
Guido van Rossum4e160981992-09-02 20:43:20 +0000117 # This method is called when a return trap is set here
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000118 pass
119
120 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
Guido van Rossum4e160981992-09-02 20:43:20 +0000121 # This method is called if an exception occurs,
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000122 # but only if we are to stop at or just below this level
123 pass
124
Guido van Rossum4e160981992-09-02 20:43:20 +0000125 # Derived classes and clients can call the following methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000126 # to affect the stepping state.
127
128 def set_step(self):
129 # Stop after one line of code
130 self.stopframe = None
131 self.returnframe = None
132 self.quitting = 0
133
134 def set_next(self, frame):
135 # Stop on the next line in or below the given frame
136 self.stopframe = frame
137 self.returnframe = None
138 self.quitting = 0
139
140 def set_return(self, frame):
141 # Stop when returning from the given frame
142 self.stopframe = frame.f_back
143 self.returnframe = frame
144 self.quitting = 0
145
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146 def set_trace(self):
147 # Start debugging from here
148 try:
149 1 + ''
150 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000151 frame = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152 self.reset()
153 while frame:
154 frame.f_trace = self.trace_dispatch
155 self.botframe = frame
156 frame = frame.f_back
157 self.set_step()
158 sys.settrace(self.trace_dispatch)
159
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000160 def set_continue(self):
161 # Don't stop except at breakpoints or when finished
162 self.stopframe = self.botframe
163 self.returnframe = None
164 self.quitting = 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165 if not self.breaks:
166 # no breakpoints; run without debugger overhead
167 sys.settrace(None)
168 try:
169 1 + '' # raise an exception
170 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000171 frame = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172 while frame and frame is not self.botframe:
173 del frame.f_trace
174 frame = frame.f_back
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000175
176 def set_quit(self):
177 self.stopframe = self.botframe
178 self.returnframe = None
179 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000180 sys.settrace(None)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000181
Guido van Rossum4e160981992-09-02 20:43:20 +0000182 # Derived classes and clients can call the following methods
183 # to manipulate breakpoints. These methods return an
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000184 # error message is something went wrong, None if all is well.
185 # Call self.get_*break*() to see the breakpoints.
186
Guido van Rossumff02e1d1997-07-11 13:42:50 +0000187 def set_break(self, filename, lineno, cond=None):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000188 import linecache # Import as late as possible
189 line = linecache.getline(filename, lineno)
190 if not line:
191 return 'That line does not exist!'
192 if not self.breaks.has_key(filename):
193 self.breaks[filename] = []
194 list = self.breaks[filename]
195 if lineno in list:
196 return 'There is already a breakpoint there!'
197 list.append(lineno)
Guido van Rossumff02e1d1997-07-11 13:42:50 +0000198 if cond is not None: self.cbreaks[filename, lineno]=cond
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000199
200 def clear_break(self, filename, lineno):
201 if not self.breaks.has_key(filename):
202 return 'There are no breakpoints in that file!'
203 if lineno not in self.breaks[filename]:
204 return 'There is no breakpoint there!'
205 self.breaks[filename].remove(lineno)
206 if not self.breaks[filename]:
207 del self.breaks[filename]
Guido van Rossumff02e1d1997-07-11 13:42:50 +0000208 try: del self.cbreaks[filename, lineno]
209 except: pass
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000210
211 def clear_all_file_breaks(self, filename):
212 if not self.breaks.has_key(filename):
213 return 'There are no breakpoints in that file!'
214 del self.breaks[filename]
Guido van Rossumff02e1d1997-07-11 13:42:50 +0000215 for f,l in self.cbreaks.keys():
216 if f==filename: del self.cbreaks[f,l]
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218 def clear_all_breaks(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000219 if not self.breaks:
220 return 'There are no breakpoints!'
221 self.breaks = {}
Guido van Rossumff02e1d1997-07-11 13:42:50 +0000222 self.cbreaks = {}
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000223
224 def get_break(self, filename, lineno):
225 return self.breaks.has_key(filename) and \
226 lineno in self.breaks[filename]
227
228 def get_file_breaks(self, filename):
229 if self.breaks.has_key(filename):
230 return self.breaks[filename]
231 else:
232 return []
233
234 def get_all_breaks(self):
235 return self.breaks
236
Guido van Rossum4e160981992-09-02 20:43:20 +0000237 # Derived classes and clients can call the following method
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000238 # to get a data structure representing a stack trace.
239
240 def get_stack(self, f, t):
241 stack = []
242 if t and t.tb_frame is f:
243 t = t.tb_next
244 while f is not None:
245 stack.append((f, f.f_lineno))
246 if f is self.botframe:
247 break
248 f = f.f_back
249 stack.reverse()
250 i = max(0, len(stack) - 1)
251 while t is not None:
252 stack.append((t.tb_frame, t.tb_lineno))
253 t = t.tb_next
254 return stack, i
255
Guido van Rossumfac6da21992-01-27 16:59:34 +0000256 #
257
Guido van Rossuma558e371994-11-10 22:27:35 +0000258 def format_stack_entry(self, frame_lineno, lprefix=': '):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259 import linecache, repr, string
Guido van Rossum89a78691992-12-14 12:57:56 +0000260 frame, lineno = frame_lineno
Guido van Rossumfac6da21992-01-27 16:59:34 +0000261 filename = frame.f_code.co_filename
262 s = filename + '(' + `lineno` + ')'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263 if frame.f_code.co_name:
264 s = s + frame.f_code.co_name
265 else:
266 s = s + "<lambda>"
Guido van Rossumfac6da21992-01-27 16:59:34 +0000267 if frame.f_locals.has_key('__args__'):
268 args = frame.f_locals['__args__']
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000269 else:
270 args = None
271 if args:
272 s = s + repr.repr(args)
273 else:
274 s = s + '()'
Guido van Rossumfac6da21992-01-27 16:59:34 +0000275 if frame.f_locals.has_key('__return__'):
276 rv = frame.f_locals['__return__']
277 s = s + '->'
278 s = s + repr.repr(rv)
279 line = linecache.getline(filename, lineno)
Guido van Rossuma558e371994-11-10 22:27:35 +0000280 if line: s = s + lprefix + string.strip(line)
Guido van Rossumfac6da21992-01-27 16:59:34 +0000281 return s
282
Guido van Rossum4e160981992-09-02 20:43:20 +0000283 # The following two methods can be called by clients to use
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000284 # a debugger to debug a statement, given as a string.
285
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000286 def run(self, cmd, globals=None, locals=None):
287 if globals is None:
288 import __main__
289 globals = __main__.__dict__
290 if locals is None:
291 locals = globals
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000292 self.reset()
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000293 sys.settrace(self.trace_dispatch)
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000294 if type(cmd) <> types.CodeType:
295 cmd = cmd+'\n'
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000296 try:
Guido van Rossumceb3ba21992-04-03 16:32:37 +0000297 try:
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000298 exec cmd in globals, locals
Guido van Rossumceb3ba21992-04-03 16:32:37 +0000299 except BdbQuit:
300 pass
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000301 finally:
302 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000303 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000304
305 def runeval(self, expr, globals=None, locals=None):
306 if globals is None:
307 import __main__
308 globals = __main__.__dict__
309 if locals is None:
310 locals = globals
311 self.reset()
312 sys.settrace(self.trace_dispatch)
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000313 if type(expr) <> types.CodeType:
314 expr = expr+'\n'
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000315 try:
316 try:
Guido van Rossum4808dcb1996-10-15 14:40:21 +0000317 return eval(expr, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000318 except BdbQuit:
319 pass
320 finally:
321 self.quitting = 1
322 sys.settrace(None)
323
324 def runctx(self, cmd, globals, locals):
325 # B/W compatibility
326 self.run(cmd, globals, locals)
Guido van Rossum4e160981992-09-02 20:43:20 +0000327
328 # This method is more useful to debug a single function call.
329
330 def runcall(self, func, *args):
331 self.reset()
332 sys.settrace(self.trace_dispatch)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000333 res = None
Guido van Rossum4e160981992-09-02 20:43:20 +0000334 try:
335 try:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000336 res = apply(func, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000337 except BdbQuit:
338 pass
339 finally:
340 self.quitting = 1
341 sys.settrace(None)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000342 return res
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000343
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000344
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345def set_trace():
346 Bdb().set_trace()
347
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000348# -------------------- testing --------------------
349
350class Tdb(Bdb):
351 def user_call(self, frame, args):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000352 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000353 if not name: name = '???'
354 print '+++ call', name, args
355 def user_line(self, frame):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356 import linecache, string
357 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000358 if not name: name = '???'
359 fn = frame.f_code.co_filename
360 line = linecache.getline(fn, frame.f_lineno)
361 print '+++', fn, frame.f_lineno, name, ':', string.strip(line)
362 def user_return(self, frame, retval):
363 print '+++ return', retval
364 def user_exception(self, frame, exc_stuff):
365 print '+++ exception', exc_stuff
366 self.set_continue()
367
368def foo(n):
369 print 'foo(', n, ')'
370 x = bar(n*10)
371 print 'bar returned', x
372
373def bar(a):
374 print 'bar(', a, ')'
375 return a/2
376
377def test():
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000378 t = Tdb()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000379 t.run('import bdb; bdb.foo(10)')