blob: 7642ed0cd97874f1a118bf67025365f0fb131d5a [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
10
11BdbQuit = 'bdb.BdbQuit' # Exception to give up completely
12
13
14class Bdb: # Basic Debugger
15
Guido van Rossum5ef74b81993-06-23 11:55:24 +000016 def __init__(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000017 self.breaks = {}
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000018
19 def reset(self):
Guido van Rossumb6775db1994-08-01 11:34:53 +000020 import linecache
21 linecache.checkcache()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000022 self.botframe = None
23 self.stopframe = None
24 self.returnframe = None
25 self.quitting = 0
26
27 def trace_dispatch(self, frame, event, arg):
28 if self.quitting:
29 return # None
30 if event == 'line':
31 return self.dispatch_line(frame)
32 if event == 'call':
33 return self.dispatch_call(frame, arg)
34 if event == 'return':
35 return self.dispatch_return(frame, arg)
36 if event == 'exception':
37 return self.dispatch_exception(frame, arg)
38 print 'bdb.Bdb.dispatch: unknown debugging event:', `event`
39 return self.trace_dispatch
40
41 def dispatch_line(self, frame):
42 if self.stop_here(frame) or self.break_here(frame):
43 self.user_line(frame)
44 if self.quitting: raise BdbQuit
45 return self.trace_dispatch
46
47 def dispatch_call(self, frame, arg):
48 frame.f_locals['__args__'] = arg
49 if self.botframe is None:
50 # First call of dispatch since reset()
51 self.botframe = frame
52 return self.trace_dispatch
53 if not (self.stop_here(frame) or self.break_anywhere(frame)):
54 # No need to trace this function
55 return # None
56 self.user_call(frame, arg)
57 if self.quitting: raise BdbQuit
58 return self.trace_dispatch
59
60 def dispatch_return(self, frame, arg):
61 if self.stop_here(frame) or frame == self.returnframe:
62 self.user_return(frame, arg)
63 if self.quitting: raise BdbQuit
64
65 def dispatch_exception(self, frame, arg):
66 if self.stop_here(frame):
67 self.user_exception(frame, arg)
68 if self.quitting: raise BdbQuit
69 return self.trace_dispatch
70
71 # Normally derived classes don't override the following
Guido van Rossum4e160981992-09-02 20:43:20 +000072 # methods, but they may if they want to redefine the
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000073 # definition of stopping and breakpoints.
74
75 def stop_here(self, frame):
76 if self.stopframe is None:
77 return 1
78 if frame is self.stopframe:
79 return 1
80 while frame is not None and frame is not self.stopframe:
81 if frame is self.botframe:
82 return 1
83 frame = frame.f_back
84 return 0
85
86 def break_here(self, frame):
87 if not self.breaks.has_key(frame.f_code.co_filename):
88 return 0
89 if not frame.f_lineno in \
90 self.breaks[frame.f_code.co_filename]:
91 return 0
92 return 1
93
94 def break_anywhere(self, frame):
95 return self.breaks.has_key(frame.f_code.co_filename)
96
Guido van Rossum4e160981992-09-02 20:43:20 +000097 # Derived classes should override the user_* methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +000098 # to gain control.
99
100 def user_call(self, frame, argument_list):
Guido van Rossum4e160981992-09-02 20:43:20 +0000101 # This method is called when there is the remote possibility
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000102 # that we ever need to stop in this function
103 pass
104
105 def user_line(self, frame):
Guido van Rossum4e160981992-09-02 20:43:20 +0000106 # This method is called when we stop or break at this line
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000107 pass
108
109 def user_return(self, frame, return_value):
Guido van Rossum4e160981992-09-02 20:43:20 +0000110 # This method is called when a return trap is set here
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000111 pass
112
113 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
Guido van Rossum4e160981992-09-02 20:43:20 +0000114 # This method is called if an exception occurs,
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000115 # but only if we are to stop at or just below this level
116 pass
117
Guido van Rossum4e160981992-09-02 20:43:20 +0000118 # Derived classes and clients can call the following methods
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000119 # to affect the stepping state.
120
121 def set_step(self):
122 # Stop after one line of code
123 self.stopframe = None
124 self.returnframe = None
125 self.quitting = 0
126
127 def set_next(self, frame):
128 # Stop on the next line in or below the given frame
129 self.stopframe = frame
130 self.returnframe = None
131 self.quitting = 0
132
133 def set_return(self, frame):
134 # Stop when returning from the given frame
135 self.stopframe = frame.f_back
136 self.returnframe = frame
137 self.quitting = 0
138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139 def set_trace(self):
140 # Start debugging from here
141 try:
142 1 + ''
143 except:
144 frame = sys.exc_traceback.tb_frame.f_back
145 self.reset()
146 while frame:
147 frame.f_trace = self.trace_dispatch
148 self.botframe = frame
149 frame = frame.f_back
150 self.set_step()
151 sys.settrace(self.trace_dispatch)
152
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000153 def set_continue(self):
154 # Don't stop except at breakpoints or when finished
155 self.stopframe = self.botframe
156 self.returnframe = None
157 self.quitting = 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158 if not self.breaks:
159 # no breakpoints; run without debugger overhead
160 sys.settrace(None)
161 try:
162 1 + '' # raise an exception
163 except:
164 frame = sys.exc_traceback.tb_frame.f_back
165 while frame and frame is not self.botframe:
166 del frame.f_trace
167 frame = frame.f_back
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000168
169 def set_quit(self):
170 self.stopframe = self.botframe
171 self.returnframe = None
172 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000173 sys.settrace(None)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000174
Guido van Rossum4e160981992-09-02 20:43:20 +0000175 # Derived classes and clients can call the following methods
176 # to manipulate breakpoints. These methods return an
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000177 # error message is something went wrong, None if all is well.
178 # Call self.get_*break*() to see the breakpoints.
179
180 def set_break(self, filename, lineno):
181 import linecache # Import as late as possible
182 line = linecache.getline(filename, lineno)
183 if not line:
184 return 'That line does not exist!'
185 if not self.breaks.has_key(filename):
186 self.breaks[filename] = []
187 list = self.breaks[filename]
188 if lineno in list:
189 return 'There is already a breakpoint there!'
190 list.append(lineno)
191
192 def clear_break(self, filename, lineno):
193 if not self.breaks.has_key(filename):
194 return 'There are no breakpoints in that file!'
195 if lineno not in self.breaks[filename]:
196 return 'There is no breakpoint there!'
197 self.breaks[filename].remove(lineno)
198 if not self.breaks[filename]:
199 del self.breaks[filename]
200
201 def clear_all_file_breaks(self, filename):
202 if not self.breaks.has_key(filename):
203 return 'There are no breakpoints in that file!'
204 del self.breaks[filename]
205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206 def clear_all_breaks(self):
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000207 if not self.breaks:
208 return 'There are no breakpoints!'
209 self.breaks = {}
210
211 def get_break(self, filename, lineno):
212 return self.breaks.has_key(filename) and \
213 lineno in self.breaks[filename]
214
215 def get_file_breaks(self, filename):
216 if self.breaks.has_key(filename):
217 return self.breaks[filename]
218 else:
219 return []
220
221 def get_all_breaks(self):
222 return self.breaks
223
Guido van Rossum4e160981992-09-02 20:43:20 +0000224 # Derived classes and clients can call the following method
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000225 # to get a data structure representing a stack trace.
226
227 def get_stack(self, f, t):
228 stack = []
229 if t and t.tb_frame is f:
230 t = t.tb_next
231 while f is not None:
232 stack.append((f, f.f_lineno))
233 if f is self.botframe:
234 break
235 f = f.f_back
236 stack.reverse()
237 i = max(0, len(stack) - 1)
238 while t is not None:
239 stack.append((t.tb_frame, t.tb_lineno))
240 t = t.tb_next
241 return stack, i
242
Guido van Rossumfac6da21992-01-27 16:59:34 +0000243 #
244
Guido van Rossuma558e371994-11-10 22:27:35 +0000245 def format_stack_entry(self, frame_lineno, lprefix=': '):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246 import linecache, repr, string
Guido van Rossum89a78691992-12-14 12:57:56 +0000247 frame, lineno = frame_lineno
Guido van Rossumfac6da21992-01-27 16:59:34 +0000248 filename = frame.f_code.co_filename
249 s = filename + '(' + `lineno` + ')'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250 if frame.f_code.co_name:
251 s = s + frame.f_code.co_name
252 else:
253 s = s + "<lambda>"
Guido van Rossumfac6da21992-01-27 16:59:34 +0000254 if frame.f_locals.has_key('__args__'):
255 args = frame.f_locals['__args__']
256 if args is not None:
257 s = s + repr.repr(args)
258 if frame.f_locals.has_key('__return__'):
259 rv = frame.f_locals['__return__']
260 s = s + '->'
261 s = s + repr.repr(rv)
262 line = linecache.getline(filename, lineno)
Guido van Rossuma558e371994-11-10 22:27:35 +0000263 if line: s = s + lprefix + string.strip(line)
Guido van Rossumfac6da21992-01-27 16:59:34 +0000264 return s
265
Guido van Rossum4e160981992-09-02 20:43:20 +0000266 # The following two methods can be called by clients to use
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000267 # a debugger to debug a statement, given as a string.
268
269 def run(self, cmd):
Guido van Rossumfac6da21992-01-27 16:59:34 +0000270 import __main__
271 dict = __main__.__dict__
272 self.runctx(cmd, dict, dict)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000273
274 def runctx(self, cmd, globals, locals):
275 self.reset()
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000276 sys.settrace(self.trace_dispatch)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000277 try:
Guido van Rossumceb3ba21992-04-03 16:32:37 +0000278 try:
279 exec(cmd + '\n', globals, locals)
280 except BdbQuit:
281 pass
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000282 finally:
283 self.quitting = 1
Guido van Rossumedbfcbd1992-03-27 15:09:46 +0000284 sys.settrace(None)
Guido van Rossum4e160981992-09-02 20:43:20 +0000285
286 # This method is more useful to debug a single function call.
287
288 def runcall(self, func, *args):
289 self.reset()
290 sys.settrace(self.trace_dispatch)
291 try:
292 try:
293 apply(func, args)
294 except BdbQuit:
295 pass
296 finally:
297 self.quitting = 1
298 sys.settrace(None)
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000299
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000300
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301def set_trace():
302 Bdb().set_trace()
303
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000304# -------------------- testing --------------------
305
306class Tdb(Bdb):
307 def user_call(self, frame, args):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000308 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000309 if not name: name = '???'
310 print '+++ call', name, args
311 def user_line(self, frame):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000312 import linecache, string
313 name = frame.f_code.co_name
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000314 if not name: name = '???'
315 fn = frame.f_code.co_filename
316 line = linecache.getline(fn, frame.f_lineno)
317 print '+++', fn, frame.f_lineno, name, ':', string.strip(line)
318 def user_return(self, frame, retval):
319 print '+++ return', retval
320 def user_exception(self, frame, exc_stuff):
321 print '+++ exception', exc_stuff
322 self.set_continue()
323
324def foo(n):
325 print 'foo(', n, ')'
326 x = bar(n*10)
327 print 'bar returned', x
328
329def bar(a):
330 print 'bar(', a, ')'
331 return a/2
332
333def test():
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000334 t = Tdb()
Guido van Rossumbabe2bf1992-01-22 22:21:31 +0000335 t.run('import bdb; bdb.foo(10)')