blob: 64451d5f4e8c3179c84e15e579f4bbdbd79d220a [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 Rossum921c8241992-01-10 14:54:42 +000019
20 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000021 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000022 self.forget()
23
24 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000025 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000026 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000027 self.curindex = 0
28 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000029
30 def setup(self, f, t):
31 self.forget()
32 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000033 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000034
Guido van Rossum23efba41992-01-27 16:58:47 +000035 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000036
Guido van Rossum23efba41992-01-27 16:58:47 +000037 def user_line(self, frame):
38 # This function is called when we stop or break at this line
39 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000040
Guido van Rossum23efba41992-01-27 16:58:47 +000041 def user_return(self, frame, return_value):
42 # This function is called when a return trap is set here
43 frame.f_locals['__return__'] = return_value
44 print '--Return--'
45 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000046
Guido van Rossum23efba41992-01-27 16:58:47 +000047 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
48 # This function is called if an exception occurs,
49 # but only if we are to stop at or just below this level
50 frame.f_locals['__exception__'] = exc_type, exc_value
51 print exc_type + ':', repr.repr(exc_value)
52 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000053
Guido van Rossum23efba41992-01-27 16:58:47 +000054 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000055
Guido van Rossum23efba41992-01-27 16:58:47 +000056 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000057 self.setup(frame, traceback)
Guido van Rossum23efba41992-01-27 16:58:47 +000058 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000059 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000060 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000061
Guido van Rossum921c8241992-01-10 14:54:42 +000062 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000063 if line[:1] == '!': line = line[1:]
64 locals = self.curframe.f_locals
65 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000066 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000067 try:
68 exec(line + '\n', globals, locals)
69 except:
70 print '***', sys.exc_type + ':', sys.exc_value
71
72 # Command definitions, called by cmdloop()
73 # The argument is the remaining string on the command line
74 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000075
Guido van Rossum23efba41992-01-27 16:58:47 +000076 do_h = cmd.Cmd.do_help
Guido van Rossum921c8241992-01-10 14:54:42 +000077
78 def do_break(self, arg):
79 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000080 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000081 return
82 try:
83 lineno = int(eval(arg))
84 except:
85 print '*** Error in argument:', `arg`
86 return
87 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +000088 err = self.set_break(filename, lineno)
89 if err: print '***', err
Guido van Rossum921c8241992-01-10 14:54:42 +000090 do_b = do_break
91
92 def do_clear(self, arg):
93 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +000094 try:
95 reply = raw_input('Clear all breaks? ')
96 except EOFError:
97 reply = 'no'
98 reply = string.lower(string.strip(reply))
99 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000100 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000101 return
102 try:
103 lineno = int(eval(arg))
104 except:
105 print '*** Error in argument:', `arg`
106 return
107 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000108 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000109 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000110 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000111
112 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000113 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000114 do_w = do_where
115
116 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000117 if self.curindex == 0:
118 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000119 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000120 self.curindex = self.curindex - 1
121 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000122 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000123 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000124 do_u = do_up
125
126 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000127 if self.curindex + 1 == len(self.stack):
128 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000129 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000130 self.curindex = self.curindex + 1
131 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000132 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000133 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000134 do_d = do_down
135
136 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000137 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000138 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000139 do_s = do_step
140
141 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000142 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000143 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000144 do_n = do_next
145
Guido van Rossumb9142571992-01-12 23:32:55 +0000146 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000147 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000148 return 1
149 do_r = do_return
150
Guido van Rossum921c8241992-01-10 14:54:42 +0000151 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000152 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000153 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000154 do_c = do_cont = do_continue
155
156 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000157 self.set_quit()
158 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000159 do_q = do_quit
160
Guido van Rossum23efba41992-01-27 16:58:47 +0000161 def do_args(self, arg):
162 if self.curframe.f_locals.has_key('__return__'):
163 print `self.curframe.f_locals['__return__']`
164 else:
165 print '*** Not arguments?!'
166 do_a = do_args
167
168 def do_retval(self, arg):
169 if self.curframe.f_locals.has_key('__return__'):
170 print self.curframe.f_locals['__return__']
171 else:
172 print '*** Not yet returned!'
173 do_rv = do_retval
174
175 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000176 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000177 try:
178 value = eval(arg, self.curframe.f_globals, \
179 self.curframe.f_locals)
180 except:
181 print '***', sys.exc_type + ':', `sys.exc_value`
182 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000183
Guido van Rossum23efba41992-01-27 16:58:47 +0000184 print `value`
185
Guido van Rossum921c8241992-01-10 14:54:42 +0000186 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000187 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000188 last = None
189 if arg:
190 try:
191 x = eval(arg, {}, {})
192 if type(x) == type(()):
193 first, last = x
194 first = int(first)
195 last = int(last)
196 if last < first:
197 # Assume it's a count
198 last = first + last
199 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000200 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000201 except:
202 print '*** Error in argument:', `arg`
203 return
204 elif self.lineno is None:
205 first = max(1, self.curframe.f_lineno - 5)
206 else:
207 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000208 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000209 last = first + 10
210 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000211 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000212 try:
213 for lineno in range(first, last+1):
214 line = linecache.getline(filename, lineno)
215 if not line:
216 print '[EOF]'
217 break
218 else:
219 s = string.rjust(`lineno`, 3)
220 if len(s) < 4: s = s + ' '
221 if lineno in breaklist: s = s + 'B'
222 else: s = s + ' '
223 if lineno == self.curframe.f_lineno:
224 s = s + '->'
225 print s + '\t' + line,
226 self.lineno = lineno
227 except KeyboardInterrupt:
228 pass
229 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000230
231 def do_whatis(self, arg):
232 import codehack
233 try:
234 value = eval(arg, self.curframe.f_globals, \
235 self.curframe.f_locals)
236 except:
237 print '***', sys.exc_type + ':', `sys.exc_value`
238 return
239 code = None
240 # Is it a function?
241 try: code = value.func_code
242 except: pass
243 if code:
244 print 'Function', codehack.getcodename(code)
245 return
246 # Is it an instance method?
247 try: code = value.im_func.func_code
248 except: pass
249 if code:
250 print 'Method', codehack.getcodename(code)
251 return
252 # None of the above...
253 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000254
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000255 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000256 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000257 # this is different from dbx and gdb, but consistent with
258 # the Python interpreter's stack trace.
259 # It is also consistent with the up/down commands (which are
260 # compatible with dbx and gdb: up moves towards 'main()'
261 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000262
Guido van Rossum23efba41992-01-27 16:58:47 +0000263 def print_stack_trace(self):
264 try:
265 for frame_lineno in self.stack:
266 self.print_stack_entry(frame_lineno)
267 except KeyboardInterrupt:
268 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000269
Guido van Rossum23efba41992-01-27 16:58:47 +0000270 def print_stack_entry(self, frame_lineno):
271 frame, lineno = frame_lineno
272 if frame is self.curframe:
273 print '>',
274 else:
275 print ' ',
276 print self.format_stack_entry(frame_lineno)
Guido van Rossum921c8241992-01-10 14:54:42 +0000277
278
Guido van Rossum35771131992-09-08 11:59:04 +0000279# Simplified interface
280
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000281def run(statement):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000282 Pdb().run(statement)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000283
284def runctx(statement, globals, locals):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000285 Pdb().runctx(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000286
Guido van Rossum4e160981992-09-02 20:43:20 +0000287def runcall(*args):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000288 apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000289
Guido van Rossum35771131992-09-08 11:59:04 +0000290
291# Post-Mortem interface
292
293def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000294 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000295 p.reset()
296 while t.tb_next <> None: t = t.tb_next
297 p.interaction(t.tb_frame, t)
298
299def pm():
300 import sys
301 post_mortem(sys.last_traceback)
302
303
304# Main program for testing
305
Guido van Rossum23efba41992-01-27 16:58:47 +0000306TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000307
Guido van Rossum921c8241992-01-10 14:54:42 +0000308def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000309 import linecache
Guido van Rossum921c8241992-01-10 14:54:42 +0000310 linecache.checkcache()
Guido van Rossum23efba41992-01-27 16:58:47 +0000311 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000312
313# print help
314def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000315 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000316 for dirname in sys.path:
317 fullname = os.path.join(dirname, 'pdb.doc')
318 if os.path.exists(fullname):
319 sts = os.system('${PAGER-more} '+fullname)
320 if sts: print '*** Pager exit status:', sts
321 break
322 else:
323 print 'Sorry, can\'t find the help file "pdb.doc"',
324 print 'along the Python search path'