blob: 06f5cf8c7b566ca3461b9234cc8673e8dd180bf4 [file] [log] [blame]
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001# pdb.py -- finally, a Python debugger!
Guido van Rossum92df0c61992-01-14 18:30:15 +00002
Guido van Rossum23efba41992-01-27 16:58:47 +00003# (See pdb.doc for documentation.)
Guido van Rossum921c8241992-01-10 14:54:42 +00004
5import string
6import sys
7import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +00008import cmd
9import bdb
10import repr
Guido van Rossum921c8241992-01-10 14:54:42 +000011
12
Guido van Rossum23efba41992-01-27 16:58:47 +000013class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum921c8241992-01-10 14:54:42 +000014
Guido van Rossum5ef74b81993-06-23 11:55:24 +000015 def __init__(self):
16 bdb.Bdb.__init__(self)
17 cmd.Cmd.__init__(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000018 self.prompt = '(Pdb) '
Guido van Rossum5ef74b81993-06-23 11:55:24 +000019
20 def init(self): # BW compat only
Guido van Rossum921c8241992-01-10 14:54:42 +000021 return self
22
23 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000024 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000025 self.forget()
26
27 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000028 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000029 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000030 self.curindex = 0
31 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000032
33 def setup(self, f, t):
34 self.forget()
35 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000036 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000037
Guido van Rossum23efba41992-01-27 16:58:47 +000038 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000039
Guido van Rossum23efba41992-01-27 16:58:47 +000040 def user_line(self, frame):
41 # This function is called when we stop or break at this line
42 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000043
Guido van Rossum23efba41992-01-27 16:58:47 +000044 def user_return(self, frame, return_value):
45 # This function is called when a return trap is set here
46 frame.f_locals['__return__'] = return_value
47 print '--Return--'
48 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000049
Guido van Rossum23efba41992-01-27 16:58:47 +000050 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
51 # This function is called if an exception occurs,
52 # but only if we are to stop at or just below this level
53 frame.f_locals['__exception__'] = exc_type, exc_value
54 print exc_type + ':', repr.repr(exc_value)
55 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000056
Guido van Rossum23efba41992-01-27 16:58:47 +000057 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000058
Guido van Rossum23efba41992-01-27 16:58:47 +000059 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000060 self.setup(frame, traceback)
Guido van Rossum23efba41992-01-27 16:58:47 +000061 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000062 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000063 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000064
Guido van Rossum921c8241992-01-10 14:54:42 +000065 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000066 if line[:1] == '!': line = line[1:]
67 locals = self.curframe.f_locals
68 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000069 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000070 try:
71 exec(line + '\n', globals, locals)
72 except:
73 print '***', sys.exc_type + ':', sys.exc_value
74
75 # Command definitions, called by cmdloop()
76 # The argument is the remaining string on the command line
77 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000078
Guido van Rossum23efba41992-01-27 16:58:47 +000079 do_h = cmd.Cmd.do_help
Guido van Rossum921c8241992-01-10 14:54:42 +000080
81 def do_break(self, arg):
82 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000083 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000084 return
85 try:
86 lineno = int(eval(arg))
87 except:
88 print '*** Error in argument:', `arg`
89 return
90 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +000091 err = self.set_break(filename, lineno)
92 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +000093 do_b = do_break
94
95 def do_clear(self, arg):
96 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +000097 try:
98 reply = raw_input('Clear all breaks? ')
99 except EOFError:
100 reply = 'no'
101 reply = string.lower(string.strip(reply))
102 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000103 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000104 return
105 try:
106 lineno = int(eval(arg))
107 except:
108 print '*** Error in argument:', `arg`
109 return
110 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000111 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000112 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000113 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000114
115 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000116 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000117 do_w = do_where
118
119 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000120 if self.curindex == 0:
121 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000122 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000123 self.curindex = self.curindex - 1
124 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000125 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000126 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000127 do_u = do_up
128
129 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000130 if self.curindex + 1 == len(self.stack):
131 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000132 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000133 self.curindex = self.curindex + 1
134 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000135 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000136 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000137 do_d = do_down
138
139 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000140 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000141 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000142 do_s = do_step
143
144 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000145 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000146 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000147 do_n = do_next
148
Guido van Rossumb9142571992-01-12 23:32:55 +0000149 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000150 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000151 return 1
152 do_r = do_return
153
Guido van Rossum921c8241992-01-10 14:54:42 +0000154 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000155 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000156 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000157 do_c = do_cont = do_continue
158
159 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000160 self.set_quit()
161 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000162 do_q = do_quit
163
Guido van Rossum23efba41992-01-27 16:58:47 +0000164 def do_args(self, arg):
165 if self.curframe.f_locals.has_key('__return__'):
166 print `self.curframe.f_locals['__return__']`
167 else:
168 print '*** Not arguments?!'
169 do_a = do_args
170
171 def do_retval(self, arg):
172 if self.curframe.f_locals.has_key('__return__'):
173 print self.curframe.f_locals['__return__']
174 else:
175 print '*** Not yet returned!'
176 do_rv = do_retval
177
178 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000179 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000180 try:
181 value = eval(arg, self.curframe.f_globals, \
182 self.curframe.f_locals)
183 except:
184 print '***', sys.exc_type + ':', `sys.exc_value`
185 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000186
Guido van Rossum23efba41992-01-27 16:58:47 +0000187 print `value`
188
Guido van Rossum921c8241992-01-10 14:54:42 +0000189 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000190 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000191 last = None
192 if arg:
193 try:
194 x = eval(arg, {}, {})
195 if type(x) == type(()):
196 first, last = x
197 first = int(first)
198 last = int(last)
199 if last < first:
200 # Assume it's a count
201 last = first + last
202 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000203 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000204 except:
205 print '*** Error in argument:', `arg`
206 return
207 elif self.lineno is None:
208 first = max(1, self.curframe.f_lineno - 5)
209 else:
210 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000211 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000212 last = first + 10
213 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000214 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000215 try:
216 for lineno in range(first, last+1):
217 line = linecache.getline(filename, lineno)
218 if not line:
219 print '[EOF]'
220 break
221 else:
222 s = string.rjust(`lineno`, 3)
223 if len(s) < 4: s = s + ' '
224 if lineno in breaklist: s = s + 'B'
225 else: s = s + ' '
226 if lineno == self.curframe.f_lineno:
227 s = s + '->'
228 print s + '\t' + line,
229 self.lineno = lineno
230 except KeyboardInterrupt:
231 pass
232 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000233
234 def do_whatis(self, arg):
235 import codehack
236 try:
237 value = eval(arg, self.curframe.f_globals, \
238 self.curframe.f_locals)
239 except:
240 print '***', sys.exc_type + ':', `sys.exc_value`
241 return
242 code = None
243 # Is it a function?
244 try: code = value.func_code
245 except: pass
246 if code:
247 print 'Function', codehack.getcodename(code)
248 return
249 # Is it an instance method?
250 try: code = value.im_func.func_code
251 except: pass
252 if code:
253 print 'Method', codehack.getcodename(code)
254 return
255 # None of the above...
256 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000257
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000258 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000259 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000260 # this is different from dbx and gdb, but consistent with
261 # the Python interpreter's stack trace.
262 # It is also consistent with the up/down commands (which are
263 # compatible with dbx and gdb: up moves towards 'main()'
264 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000265
Guido van Rossum23efba41992-01-27 16:58:47 +0000266 def print_stack_trace(self):
267 try:
268 for frame_lineno in self.stack:
269 self.print_stack_entry(frame_lineno)
270 except KeyboardInterrupt:
271 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000272
Guido van Rossum23efba41992-01-27 16:58:47 +0000273 def print_stack_entry(self, frame_lineno):
274 frame, lineno = frame_lineno
275 if frame is self.curframe:
276 print '>',
277 else:
278 print ' ',
279 print self.format_stack_entry(frame_lineno)
Guido van Rossum921c8241992-01-10 14:54:42 +0000280
281
Guido van Rossum35771131992-09-08 11:59:04 +0000282# Simplified interface
283
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000284def run(statement):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000285 Pdb().run(statement)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000286
287def runctx(statement, globals, locals):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000288 Pdb().runctx(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000289
Guido van Rossum4e160981992-09-02 20:43:20 +0000290def runcall(*args):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000291 apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000292
Guido van Rossum35771131992-09-08 11:59:04 +0000293
294# Post-Mortem interface
295
296def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000297 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000298 p.reset()
299 while t.tb_next <> None: t = t.tb_next
300 p.interaction(t.tb_frame, t)
301
302def pm():
303 import sys
304 post_mortem(sys.last_traceback)
305
306
307# Main program for testing
308
Guido van Rossum23efba41992-01-27 16:58:47 +0000309TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000310
Guido van Rossum921c8241992-01-10 14:54:42 +0000311def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000312 import linecache
Guido van Rossum921c8241992-01-10 14:54:42 +0000313 linecache.checkcache()
Guido van Rossum23efba41992-01-27 16:58:47 +0000314 run(TESTCMD)