blob: 3d06b0a9fd41a376647ae2a46f1de021c1d52002 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumf17361d1996-07-30 16:28:13 +00002
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00003"""A Python debugger."""
Guido van Rossum92df0c61992-01-14 18:30:15 +00004
Guido van Rossum23efba41992-01-27 16:58:47 +00005# (See pdb.doc for documentation.)
Guido van Rossum921c8241992-01-10 14:54:42 +00006
Guido van Rossum921c8241992-01-10 14:54:42 +00007import sys
8import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +00009import cmd
10import bdb
Guido van Rossumef1b41b2002-09-10 21:57:14 +000011from repr import Repr
Guido van Rossumb5699c71998-07-20 23:13:54 +000012import os
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000013import re
Barry Warsaw210bd202002-11-05 22:40:20 +000014import pprint
Johannes Gijsbers25b38c82004-10-12 18:12:09 +000015import traceback
Guido van Rossumd8faa362007-04-27 19:54:29 +000016
17
18class Restart(Exception):
19 """Causes a debugger to be restarted for the debugged python program."""
20 pass
21
Guido van Rossumef1b41b2002-09-10 21:57:14 +000022# Create a custom safe Repr instance and increase its maxstring.
23# The default of 30 truncates error messages too easily.
24_repr = Repr()
25_repr.maxstring = 200
26_saferepr = _repr.repr
27
Skip Montanaro352674d2001-02-07 23:14:30 +000028__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
29 "post_mortem", "help"]
30
Neal Norwitzce96f692006-03-17 06:49:51 +000031def raw_input(prompt):
32 sys.stdout.write(prompt)
33 sys.stdout.flush()
34 return sys.stdin.readline()
35
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000036def find_function(funcname, filename):
Thomas Wouters89f507f2006-12-13 04:49:30 +000037 cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
Tim Peters2344fae2001-01-15 00:50:52 +000038 try:
39 fp = open(filename)
40 except IOError:
41 return None
42 # consumer of this info expects the first line to be 1
43 lineno = 1
44 answer = None
45 while 1:
46 line = fp.readline()
47 if line == '':
48 break
49 if cre.match(line):
50 answer = funcname, filename, lineno
51 break
52 lineno = lineno + 1
53 fp.close()
54 return answer
Guido van Rossum921c8241992-01-10 14:54:42 +000055
56
Guido van Rossuma558e371994-11-10 22:27:35 +000057# Interaction prompt line will separate file and call info from code
58# text using value of line_prefix string. A newline and arrow may
59# be to your liking. You can set it once pdb is imported using the
60# command "pdb.line_prefix = '\n% '".
Tim Peters2344fae2001-01-15 00:50:52 +000061# line_prefix = ': ' # Use this to get the old situation back
62line_prefix = '\n-> ' # Probably a better default
Guido van Rossuma558e371994-11-10 22:27:35 +000063
Guido van Rossum23efba41992-01-27 16:58:47 +000064class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum2424f851998-09-11 22:50:09 +000065
Thomas Wouters477c8d52006-05-27 19:21:47 +000066 def __init__(self, completekey='tab', stdin=None, stdout=None):
Tim Peters2344fae2001-01-15 00:50:52 +000067 bdb.Bdb.__init__(self)
Thomas Wouters477c8d52006-05-27 19:21:47 +000068 cmd.Cmd.__init__(self, completekey, stdin, stdout)
69 if stdout:
70 self.use_rawinput = 0
Tim Peters2344fae2001-01-15 00:50:52 +000071 self.prompt = '(Pdb) '
72 self.aliases = {}
Johannes Gijsbers25b38c82004-10-12 18:12:09 +000073 self.mainpyfile = ''
74 self._wait_for_mainpyfile = 0
Tim Peters2344fae2001-01-15 00:50:52 +000075 # Try to load readline if it exists
76 try:
77 import readline
78 except ImportError:
79 pass
Guido van Rossum2424f851998-09-11 22:50:09 +000080
Tim Peters2344fae2001-01-15 00:50:52 +000081 # Read $HOME/.pdbrc and ./.pdbrc
82 self.rcLines = []
Raymond Hettinger54f02222002-06-01 14:18:47 +000083 if 'HOME' in os.environ:
Tim Peters2344fae2001-01-15 00:50:52 +000084 envHome = os.environ['HOME']
85 try:
86 rcFile = open(os.path.join(envHome, ".pdbrc"))
87 except IOError:
88 pass
89 else:
90 for line in rcFile.readlines():
91 self.rcLines.append(line)
92 rcFile.close()
93 try:
94 rcFile = open(".pdbrc")
95 except IOError:
96 pass
97 else:
98 for line in rcFile.readlines():
99 self.rcLines.append(line)
100 rcFile.close()
Guido van Rossum23efba41992-01-27 16:58:47 +0000101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102 self.commands = {} # associates a command list to breakpoint numbers
103 self.commands_doprompt = {} # for each bp num, tells if the prompt must be disp. after execing the cmd list
104 self.commands_silent = {} # for each bp num, tells if the stack trace must be disp. after execing the cmd list
105 self.commands_defining = False # True while in the process of defining a command list
106 self.commands_bnum = None # The breakpoint number for which we are defining a list
107
Tim Peters2344fae2001-01-15 00:50:52 +0000108 def reset(self):
109 bdb.Bdb.reset(self)
110 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +0000111
Tim Peters2344fae2001-01-15 00:50:52 +0000112 def forget(self):
113 self.lineno = None
114 self.stack = []
115 self.curindex = 0
116 self.curframe = None
Guido van Rossum2424f851998-09-11 22:50:09 +0000117
Tim Peters2344fae2001-01-15 00:50:52 +0000118 def setup(self, f, t):
119 self.forget()
120 self.stack, self.curindex = self.get_stack(f, t)
121 self.curframe = self.stack[self.curindex][0]
122 self.execRcLines()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123
Tim Peters2344fae2001-01-15 00:50:52 +0000124 # Can be executed earlier than 'setup' if desired
125 def execRcLines(self):
126 if self.rcLines:
127 # Make local copy because of recursion
128 rcLines = self.rcLines
129 # executed only once
130 self.rcLines = []
131 for line in rcLines:
132 line = line[:-1]
Guido van Rossum08454592002-07-12 13:10:53 +0000133 if len(line) > 0 and line[0] != '#':
134 self.onecmd(line)
Guido van Rossum2424f851998-09-11 22:50:09 +0000135
Tim Peters280488b2002-08-23 18:19:30 +0000136 # Override Bdb methods
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000137
138 def user_call(self, frame, argument_list):
139 """This method is called when there is the remote possibility
140 that we ever need to stop in this function."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000141 if self._wait_for_mainpyfile:
142 return
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000143 if self.stop_here(frame):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000144 print('--Call--', file=self.stdout)
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000145 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000146
Tim Peters2344fae2001-01-15 00:50:52 +0000147 def user_line(self, frame):
148 """This function is called when we stop or break at this line."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000149 if self._wait_for_mainpyfile:
150 if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
151 or frame.f_lineno<= 0):
152 return
153 self._wait_for_mainpyfile = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 if self.bp_commands(frame):
155 self.interaction(frame, None)
156
157 def bp_commands(self,frame):
158 """ Call every command that was set for the current active breakpoint (if there is one)
159 Returns True if the normal interaction function must be called, False otherwise """
160 #self.currentbp is set in bdb.py in bdb.break_here if a breakpoint was hit
161 if getattr(self,"currentbp",False) and self.currentbp in self.commands:
162 currentbp = self.currentbp
163 self.currentbp = 0
164 lastcmd_back = self.lastcmd
165 self.setup(frame, None)
166 for line in self.commands[currentbp]:
167 self.onecmd(line)
168 self.lastcmd = lastcmd_back
169 if not self.commands_silent[currentbp]:
170 self.print_stack_entry(self.stack[self.curindex])
171 if self.commands_doprompt[currentbp]:
172 self.cmdloop()
173 self.forget()
174 return
175 return 1
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000176
Tim Peters2344fae2001-01-15 00:50:52 +0000177 def user_return(self, frame, return_value):
178 """This function is called when a return trap is set here."""
179 frame.f_locals['__return__'] = return_value
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000180 print('--Return--', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000181 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000182
Tim Peters2344fae2001-01-15 00:50:52 +0000183 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
184 """This function is called if an exception occurs,
185 but only if we are to stop at or just below this level."""
186 frame.f_locals['__exception__'] = exc_type, exc_value
187 if type(exc_type) == type(''):
188 exc_type_name = exc_type
189 else: exc_type_name = exc_type.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000190 print(exc_type_name + ':', _saferepr(exc_value), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000191 self.interaction(frame, exc_traceback)
Guido van Rossum2424f851998-09-11 22:50:09 +0000192
Tim Peters2344fae2001-01-15 00:50:52 +0000193 # General interaction function
194
195 def interaction(self, frame, traceback):
196 self.setup(frame, traceback)
197 self.print_stack_entry(self.stack[self.curindex])
198 self.cmdloop()
199 self.forget()
200
201 def default(self, line):
202 if line[:1] == '!': line = line[1:]
203 locals = self.curframe.f_locals
204 globals = self.curframe.f_globals
205 try:
206 code = compile(line + '\n', '<stdin>', 'single')
Georg Brandl7cae87c2006-09-06 06:51:57 +0000207 exec(code, globals, locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000208 except:
209 t, v = sys.exc_info()[:2]
210 if type(t) == type(''):
211 exc_type_name = t
212 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000213 print('***', exc_type_name + ':', v, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000214
215 def precmd(self, line):
216 """Handle alias expansion and ';;' separator."""
Guido van Rossum08454592002-07-12 13:10:53 +0000217 if not line.strip():
Tim Peters2344fae2001-01-15 00:50:52 +0000218 return line
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000219 args = line.split()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000220 while args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000221 line = self.aliases[args[0]]
222 ii = 1
223 for tmpArg in args[1:]:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000224 line = line.replace("%" + str(ii),
Tim Peters2344fae2001-01-15 00:50:52 +0000225 tmpArg)
226 ii = ii + 1
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000227 line = line.replace("%*", ' '.join(args[1:]))
228 args = line.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000229 # split into ';;' separated commands
230 # unless it's an alias command
231 if args[0] != 'alias':
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000232 marker = line.find(';;')
Tim Peters2344fae2001-01-15 00:50:52 +0000233 if marker >= 0:
234 # queue up everything after marker
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000235 next = line[marker+2:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000236 self.cmdqueue.append(next)
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000237 line = line[:marker].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000238 return line
239
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 def onecmd(self, line):
241 """Interpret the argument as though it had been typed in response
242 to the prompt.
243
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000244 Checks whether this line is typed at the normal prompt or in
245 a breakpoint command list definition.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 """
247 if not self.commands_defining:
248 return cmd.Cmd.onecmd(self, line)
249 else:
250 return self.handle_command_def(line)
251
252 def handle_command_def(self,line):
253 """ Handles one command line during command list definition. """
254 cmd, arg, line = self.parseline(line)
255 if cmd == 'silent':
256 self.commands_silent[self.commands_bnum] = True
257 return # continue to handle other cmd def in the cmd list
258 elif cmd == 'end':
259 self.cmdqueue = []
260 return 1 # end of cmd list
261 cmdlist = self.commands[self.commands_bnum]
262 if (arg):
263 cmdlist.append(cmd+' '+arg)
264 else:
265 cmdlist.append(cmd)
266 # Determine if we must stop
267 try:
268 func = getattr(self, 'do_' + cmd)
269 except AttributeError:
270 func = self.default
Neal Norwitz221085d2007-02-25 20:55:47 +0000271 if func.__name__ in self.commands_resuming : # one of the resuming commands.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000272 self.commands_doprompt[self.commands_bnum] = False
273 self.cmdqueue = []
274 return 1
275 return
276
Tim Peters2344fae2001-01-15 00:50:52 +0000277 # Command definitions, called by cmdloop()
278 # The argument is the remaining string on the command line
279 # Return true to exit from the command loop
280
281 do_h = cmd.Cmd.do_help
282
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283 def do_commands(self, arg):
284 """Defines a list of commands associated to a breakpoint
285 Those commands will be executed whenever the breakpoint causes the program to stop execution."""
286 if not arg:
287 bnum = len(bdb.Breakpoint.bpbynumber)-1
288 else:
289 try:
290 bnum = int(arg)
291 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000292 print("Usage : commands [bnum]\n ...\n end", file=self.stdout)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293 return
294 self.commands_bnum = bnum
295 self.commands[bnum] = []
296 self.commands_doprompt[bnum] = True
297 self.commands_silent[bnum] = False
298 prompt_back = self.prompt
299 self.prompt = '(com) '
300 self.commands_defining = True
301 self.cmdloop()
302 self.commands_defining = False
303 self.prompt = prompt_back
304
Tim Peters2344fae2001-01-15 00:50:52 +0000305 def do_break(self, arg, temporary = 0):
306 # break [ ([filename:]lineno | function) [, "condition"] ]
307 if not arg:
308 if self.breaks: # There's at least one
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000309 print("Num Type Disp Enb Where", file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000310 for bp in bdb.Breakpoint.bpbynumber:
311 if bp:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000312 bp.bpprint(self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000313 return
314 # parse arguments; comma has lowest precedence
315 # and cannot occur in filename
316 filename = None
317 lineno = None
318 cond = None
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000319 comma = arg.find(',')
Tim Peters2344fae2001-01-15 00:50:52 +0000320 if comma > 0:
321 # parse stuff after comma: "condition"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000322 cond = arg[comma+1:].lstrip()
323 arg = arg[:comma].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000324 # parse stuff before comma: [filename:]lineno | function
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000325 colon = arg.rfind(':')
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000326 funcname = None
Tim Peters2344fae2001-01-15 00:50:52 +0000327 if colon >= 0:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000328 filename = arg[:colon].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000329 f = self.lookupmodule(filename)
330 if not f:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000331 print('*** ', repr(filename), end=' ', file=self.stdout)
332 print('not found from sys.path', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000333 return
334 else:
335 filename = f
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000336 arg = arg[colon+1:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000337 try:
338 lineno = int(arg)
Guido van Rossumb940e112007-01-10 16:19:56 +0000339 except ValueError as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000340 print('*** Bad lineno:', arg, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000341 return
342 else:
343 # no colon; can be lineno or function
344 try:
345 lineno = int(arg)
346 except ValueError:
347 try:
348 func = eval(arg,
349 self.curframe.f_globals,
350 self.curframe.f_locals)
351 except:
352 func = arg
353 try:
354 if hasattr(func, 'im_func'):
355 func = func.im_func
Neal Norwitz221085d2007-02-25 20:55:47 +0000356 code = func.__code__
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000357 #use co_name to identify the bkpt (function names
358 #could be aliased, but co_name is invariant)
359 funcname = code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000360 lineno = code.co_firstlineno
361 filename = code.co_filename
362 except:
363 # last thing to try
364 (ok, filename, ln) = self.lineinfo(arg)
365 if not ok:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000366 print('*** The specified object', end=' ', file=self.stdout)
367 print(repr(arg), end=' ', file=self.stdout)
368 print('is not a function', file=self.stdout)
369 print('or was not found along sys.path.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000370 return
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000371 funcname = ok # ok contains a function name
Tim Peters2344fae2001-01-15 00:50:52 +0000372 lineno = int(ln)
373 if not filename:
374 filename = self.defaultFile()
375 # Check for reasonable breakpoint
376 line = self.checkline(filename, lineno)
377 if line:
378 # now set the break point
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000379 err = self.set_break(filename, line, temporary, cond, funcname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000380 if err: print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000381 else:
382 bp = self.get_breaks(filename, line)[-1]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000383 print("Breakpoint %d at %s:%d" % (bp.number,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000384 bp.file,
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000385 bp.line), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000386
387 # To be overridden in derived debuggers
388 def defaultFile(self):
389 """Produce a reasonable default."""
390 filename = self.curframe.f_code.co_filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000391 if filename == '<string>' and self.mainpyfile:
392 filename = self.mainpyfile
Tim Peters2344fae2001-01-15 00:50:52 +0000393 return filename
394
395 do_b = do_break
396
397 def do_tbreak(self, arg):
398 self.do_break(arg, 1)
399
400 def lineinfo(self, identifier):
401 failed = (None, None, None)
402 # Input is identifier, may be in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000403 idstring = identifier.split("'")
Tim Peters2344fae2001-01-15 00:50:52 +0000404 if len(idstring) == 1:
405 # not in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000406 id = idstring[0].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000407 elif len(idstring) == 3:
408 # quoted
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000409 id = idstring[1].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000410 else:
411 return failed
412 if id == '': return failed
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000413 parts = id.split('.')
Tim Peters2344fae2001-01-15 00:50:52 +0000414 # Protection for derived debuggers
415 if parts[0] == 'self':
416 del parts[0]
417 if len(parts) == 0:
418 return failed
419 # Best first guess at file to look at
420 fname = self.defaultFile()
421 if len(parts) == 1:
422 item = parts[0]
423 else:
424 # More than one part.
425 # First is module, second is method/class
426 f = self.lookupmodule(parts[0])
427 if f:
428 fname = f
429 item = parts[1]
430 answer = find_function(item, fname)
431 return answer or failed
432
433 def checkline(self, filename, lineno):
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000434 """Check whether specified line seems to be executable.
Tim Peters2344fae2001-01-15 00:50:52 +0000435
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000436 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
437 line or EOF). Warning: testing is not comprehensive.
438 """
Tim Peters2344fae2001-01-15 00:50:52 +0000439 line = linecache.getline(filename, lineno)
440 if not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000441 print('End of file', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000442 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000443 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000444 # Don't allow setting breakpoint at a blank line
Guido van Rossum08454592002-07-12 13:10:53 +0000445 if (not line or (line[0] == '#') or
446 (line[:3] == '"""') or line[:3] == "'''"):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000447 print('*** Blank or comment', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000448 return 0
Tim Peters2344fae2001-01-15 00:50:52 +0000449 return lineno
450
451 def do_enable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000452 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000453 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000454 try:
455 i = int(i)
456 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000457 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000458 continue
459
460 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000461 print('No breakpoint numbered', i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000462 continue
463
464 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000465 if bp:
466 bp.enable()
467
468 def do_disable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000469 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000470 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000471 try:
472 i = int(i)
473 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000474 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000475 continue
Tim Petersf545baa2003-06-15 23:26:30 +0000476
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000477 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000478 print('No breakpoint numbered', i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000479 continue
480
481 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000482 if bp:
483 bp.disable()
484
485 def do_condition(self, arg):
486 # arg is breakpoint number and condition
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000487 args = arg.split(' ', 1)
Thomas Woutersb2137042007-02-01 18:02:27 +0000488 try:
489 bpnum = int(args[0].strip())
490 except ValueError:
491 # something went wrong
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000492 print('Breakpoint index %r is not a number' % args[0], file=self.stdout)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000493 return
Tim Peters2344fae2001-01-15 00:50:52 +0000494 try:
495 cond = args[1]
496 except:
497 cond = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000498 try:
499 bp = bdb.Breakpoint.bpbynumber[bpnum]
500 except IndexError:
501 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
502 return
Tim Peters2344fae2001-01-15 00:50:52 +0000503 if bp:
504 bp.cond = cond
505 if not cond:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000506 print('Breakpoint', bpnum, end=' ', file=self.stdout)
507 print('is now unconditional.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000508
509 def do_ignore(self,arg):
510 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000511 args = arg.split()
Thomas Woutersb2137042007-02-01 18:02:27 +0000512 try:
513 bpnum = int(args[0].strip())
514 except ValueError:
515 # something went wrong
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000516 print('Breakpoint index %r is not a number' % args[0], file=self.stdout)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000517 return
Tim Peters2344fae2001-01-15 00:50:52 +0000518 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000519 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000520 except:
521 count = 0
Guido van Rossumd8faa362007-04-27 19:54:29 +0000522 try:
523 bp = bdb.Breakpoint.bpbynumber[bpnum]
524 except IndexError:
525 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
526 return
Tim Peters2344fae2001-01-15 00:50:52 +0000527 if bp:
528 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000529 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000530 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000531 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000532 reply = reply + '%d crossings' % count
533 else:
534 reply = reply + '1 crossing'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000535 print(reply + ' of breakpoint %d.' % bpnum, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000536 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000537 print('Will stop next time breakpoint', end=' ', file=self.stdout)
538 print(bpnum, 'is reached.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000539
540 def do_clear(self, arg):
541 """Three possibilities, tried in this order:
542 clear -> clear all breaks, ask for confirmation
543 clear file:lineno -> clear all breaks at file:lineno
544 clear bpno bpno ... -> clear breakpoints by number"""
545 if not arg:
546 try:
547 reply = raw_input('Clear all breaks? ')
548 except EOFError:
549 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000550 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000551 if reply in ('y', 'yes'):
552 self.clear_all_breaks()
553 return
554 if ':' in arg:
555 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000556 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000557 filename = arg[:i]
558 arg = arg[i+1:]
559 try:
560 lineno = int(arg)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 except ValueError:
Tim Peters2344fae2001-01-15 00:50:52 +0000562 err = "Invalid line number (%s)" % arg
563 else:
564 err = self.clear_break(filename, lineno)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000565 if err: print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000566 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000567 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000568 for i in numberlist:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000569 try:
570 i = int(i)
571 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000572 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000573 continue
574
Georg Brandl6d2b3462005-08-24 07:36:17 +0000575 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000576 print('No breakpoint numbered', i, file=self.stdout)
Georg Brandl6d2b3462005-08-24 07:36:17 +0000577 continue
Tim Peters2344fae2001-01-15 00:50:52 +0000578 err = self.clear_bpbynumber(i)
579 if err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000580 print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000581 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000582 print('Deleted breakpoint', i, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000583 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
584
585 def do_where(self, arg):
586 self.print_stack_trace()
587 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000588 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000589
590 def do_up(self, arg):
591 if self.curindex == 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000592 print('*** Oldest frame', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000593 else:
594 self.curindex = self.curindex - 1
595 self.curframe = self.stack[self.curindex][0]
596 self.print_stack_entry(self.stack[self.curindex])
597 self.lineno = None
598 do_u = do_up
599
600 def do_down(self, arg):
601 if self.curindex + 1 == len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000602 print('*** Newest frame', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000603 else:
604 self.curindex = self.curindex + 1
605 self.curframe = self.stack[self.curindex][0]
606 self.print_stack_entry(self.stack[self.curindex])
607 self.lineno = None
608 do_d = do_down
609
610 def do_step(self, arg):
611 self.set_step()
612 return 1
613 do_s = do_step
614
615 def do_next(self, arg):
616 self.set_next(self.curframe)
617 return 1
618 do_n = do_next
619
Guido van Rossumd8faa362007-04-27 19:54:29 +0000620 def do_run(self, arg):
621 """Restart program by raising an exception to be caught in the main debugger
622 loop. If arguments were given, set them in sys.argv."""
623 if arg:
624 import shlex
625 argv0 = sys.argv[0:1]
626 sys.argv = shlex.split(arg)
627 sys.argv[:0] = argv0
628 raise Restart
629
630 do_restart = do_run
631
Tim Peters2344fae2001-01-15 00:50:52 +0000632 def do_return(self, arg):
633 self.set_return(self.curframe)
634 return 1
635 do_r = do_return
636
637 def do_continue(self, arg):
638 self.set_continue()
639 return 1
640 do_c = do_cont = do_continue
641
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000642 def do_jump(self, arg):
643 if self.curindex + 1 != len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000644 print("*** You can only jump within the bottom frame", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000645 return
646 try:
647 arg = int(arg)
648 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000649 print("*** The 'jump' command requires a line number.", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000650 else:
651 try:
652 # Do the jump, fix up our copy of the stack, and display the
653 # new position
654 self.curframe.f_lineno = arg
655 self.stack[self.curindex] = self.stack[self.curindex][0], arg
656 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumb940e112007-01-10 16:19:56 +0000657 except ValueError as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000658 print('*** Jump failed:', e, file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000659 do_j = do_jump
660
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000661 def do_debug(self, arg):
662 sys.settrace(None)
663 globals = self.curframe.f_globals
664 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000665 p = Pdb()
666 p.prompt = "(%s) " % self.prompt.strip()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000667 print("ENTERING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossumed538d82003-04-09 19:36:34 +0000668 sys.call_tracing(p.run, (arg, globals, locals))
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000669 print("LEAVING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000670 sys.settrace(self.trace_dispatch)
671 self.lastcmd = p.lastcmd
672
Tim Peters2344fae2001-01-15 00:50:52 +0000673 def do_quit(self, arg):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000674 self._user_requested_quit = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000675 self.set_quit()
676 return 1
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000677
Tim Peters2344fae2001-01-15 00:50:52 +0000678 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000679 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000680
Guido van Rossumeef26072003-01-13 21:13:55 +0000681 def do_EOF(self, arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000682 print(file=self.stdout)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000683 self._user_requested_quit = 1
Guido van Rossumeef26072003-01-13 21:13:55 +0000684 self.set_quit()
685 return 1
686
Tim Peters2344fae2001-01-15 00:50:52 +0000687 def do_args(self, arg):
688 f = self.curframe
689 co = f.f_code
690 dict = f.f_locals
691 n = co.co_argcount
692 if co.co_flags & 4: n = n+1
693 if co.co_flags & 8: n = n+1
694 for i in range(n):
695 name = co.co_varnames[i]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000696 print(name, '=', end=' ', file=self.stdout)
697 if name in dict: print(dict[name], file=self.stdout)
698 else: print("*** undefined ***", file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000699 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000700
Tim Peters2344fae2001-01-15 00:50:52 +0000701 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000702 if '__return__' in self.curframe.f_locals:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000703 print(self.curframe.f_locals['__return__'], file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000704 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000705 print('*** Not yet returned!', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000706 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000707
Barry Warsaw210bd202002-11-05 22:40:20 +0000708 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000709 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000710 return eval(arg, self.curframe.f_globals,
711 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000712 except:
713 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000714 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000715 exc_type_name = t
716 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000717 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000718 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000719
Barry Warsaw210bd202002-11-05 22:40:20 +0000720 def do_p(self, arg):
721 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000722 print(repr(self._getval(arg)), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000723 except:
724 pass
725
726 def do_pp(self, arg):
727 try:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728 pprint.pprint(self._getval(arg), self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000729 except:
730 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000731
Tim Peters2344fae2001-01-15 00:50:52 +0000732 def do_list(self, arg):
733 self.lastcmd = 'list'
734 last = None
735 if arg:
736 try:
737 x = eval(arg, {}, {})
738 if type(x) == type(()):
739 first, last = x
740 first = int(first)
741 last = int(last)
742 if last < first:
743 # Assume it's a count
744 last = first + last
745 else:
746 first = max(1, int(x) - 5)
747 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000748 print('*** Error in argument:', repr(arg), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000749 return
750 elif self.lineno is None:
751 first = max(1, self.curframe.f_lineno - 5)
752 else:
753 first = self.lineno + 1
754 if last is None:
755 last = first + 10
756 filename = self.curframe.f_code.co_filename
757 breaklist = self.get_file_breaks(filename)
758 try:
759 for lineno in range(first, last+1):
760 line = linecache.getline(filename, lineno)
761 if not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000762 print('[EOF]', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000763 break
764 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000765 s = repr(lineno).rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000766 if len(s) < 4: s = s + ' '
767 if lineno in breaklist: s = s + 'B'
768 else: s = s + ' '
769 if lineno == self.curframe.f_lineno:
770 s = s + '->'
Guido van Rossumceae3752007-02-09 22:16:54 +0000771 print(s + '\t' + line, end='', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000772 self.lineno = lineno
773 except KeyboardInterrupt:
774 pass
775 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000776
Tim Peters2344fae2001-01-15 00:50:52 +0000777 def do_whatis(self, arg):
778 try:
779 value = eval(arg, self.curframe.f_globals,
780 self.curframe.f_locals)
781 except:
782 t, v = sys.exc_info()[:2]
783 if type(t) == type(''):
784 exc_type_name = t
785 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000786 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000787 return
788 code = None
789 # Is it a function?
Neal Norwitz221085d2007-02-25 20:55:47 +0000790 try: code = value.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000791 except: pass
792 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000793 print('Function', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000794 return
795 # Is it an instance method?
Neal Norwitz221085d2007-02-25 20:55:47 +0000796 try: code = value.im_func.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000797 except: pass
798 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000799 print('Method', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000800 return
801 # None of the above...
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000802 print(type(value), file=self.stdout)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000803
Tim Peters2344fae2001-01-15 00:50:52 +0000804 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000805 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000806 if len(args) == 0:
807 keys = self.aliases.keys()
808 keys.sort()
809 for alias in keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000810 print("%s = %s" % (alias, self.aliases[alias]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000811 return
Guido van Rossum08454592002-07-12 13:10:53 +0000812 if args[0] in self.aliases and len(args) == 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000813 print("%s = %s" % (args[0], self.aliases[args[0]]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000814 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000815 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000816
Tim Peters2344fae2001-01-15 00:50:52 +0000817 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000818 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000819 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000820 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000821 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000822
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823 #list of all the commands making the program resume execution.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824 commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return',
825 'do_quit', 'do_jump']
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000826
Tim Peters2344fae2001-01-15 00:50:52 +0000827 # Print a traceback starting at the top stack frame.
828 # The most recently entered frame is printed last;
829 # this is different from dbx and gdb, but consistent with
830 # the Python interpreter's stack trace.
831 # It is also consistent with the up/down commands (which are
832 # compatible with dbx and gdb: up moves towards 'main()'
833 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000834
Tim Peters2344fae2001-01-15 00:50:52 +0000835 def print_stack_trace(self):
836 try:
837 for frame_lineno in self.stack:
838 self.print_stack_entry(frame_lineno)
839 except KeyboardInterrupt:
840 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000841
Tim Peters2344fae2001-01-15 00:50:52 +0000842 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
843 frame, lineno = frame_lineno
844 if frame is self.curframe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000845 print('>', end=' ', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000846 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000847 print(' ', end=' ', file=self.stdout)
848 print(self.format_stack_entry(frame_lineno,
849 prompt_prefix), file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000850
Guido van Rossum921c8241992-01-10 14:54:42 +0000851
Tim Peters2344fae2001-01-15 00:50:52 +0000852 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000853
Tim Peters2344fae2001-01-15 00:50:52 +0000854 def help_help(self):
855 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000856
Tim Peters2344fae2001-01-15 00:50:52 +0000857 def help_h(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000858 print("""h(elp)
Tim Peters2344fae2001-01-15 00:50:52 +0000859Without argument, print the list of available commands.
860With a command name as argument, print help about that command
861"help pdb" pipes the full documentation file to the $PAGER
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000862"help exec" gives help on the ! command""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863
Tim Peters2344fae2001-01-15 00:50:52 +0000864 def help_where(self):
865 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866
Tim Peters2344fae2001-01-15 00:50:52 +0000867 def help_w(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000868 print("""w(here)
Tim Peters2344fae2001-01-15 00:50:52 +0000869Print a stack trace, with the most recent frame at the bottom.
870An arrow indicates the "current frame", which determines the
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000871context of most commands. 'bt' is an alias for this command.""", file=self.stdout)
Guido van Rossum6bd68352001-01-20 17:57:37 +0000872
873 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000874
Tim Peters2344fae2001-01-15 00:50:52 +0000875 def help_down(self):
876 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877
Tim Peters2344fae2001-01-15 00:50:52 +0000878 def help_d(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000879 print("""d(own)
Tim Peters2344fae2001-01-15 00:50:52 +0000880Move the current frame one level down in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000881(to a newer frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000882
Tim Peters2344fae2001-01-15 00:50:52 +0000883 def help_up(self):
884 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000885
Tim Peters2344fae2001-01-15 00:50:52 +0000886 def help_u(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000887 print("""u(p)
Tim Peters2344fae2001-01-15 00:50:52 +0000888Move the current frame one level up in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000889(to an older frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000890
Tim Peters2344fae2001-01-15 00:50:52 +0000891 def help_break(self):
892 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000893
Tim Peters2344fae2001-01-15 00:50:52 +0000894 def help_b(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000895 print("""b(reak) ([file:]lineno | function) [, condition]
Tim Peters2344fae2001-01-15 00:50:52 +0000896With a line number argument, set a break there in the current
897file. With a function name, set a break at first executable line
898of that function. Without argument, list all breaks. If a second
899argument is present, it is a string specifying an expression
900which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000901
Tim Peters2344fae2001-01-15 00:50:52 +0000902The line number may be prefixed with a filename and a colon,
903to specify a breakpoint in another file (probably one that
904hasn't been loaded yet). The file is searched for on sys.path;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000905the .py suffix may be omitted.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000906
Tim Peters2344fae2001-01-15 00:50:52 +0000907 def help_clear(self):
908 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000909
Tim Peters2344fae2001-01-15 00:50:52 +0000910 def help_cl(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000911 print("cl(ear) filename:lineno", file=self.stdout)
912 print("""cl(ear) [bpnumber [bpnumber...]]
Tim Peters2344fae2001-01-15 00:50:52 +0000913With a space separated list of breakpoint numbers, clear
914those breakpoints. Without argument, clear all breaks (but
915first ask confirmation). With a filename:lineno argument,
916clear all breaks at that line in that file.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000917
Tim Peters2344fae2001-01-15 00:50:52 +0000918Note that the argument is different from previous versions of
919the debugger (in python distributions 1.5.1 and before) where
920a linenumber was used instead of either filename:lineno or
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000921breakpoint numbers.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000922
Tim Peters2344fae2001-01-15 00:50:52 +0000923 def help_tbreak(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000924 print("""tbreak same arguments as break, but breakpoint is
925removed when first hit.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000926
Tim Peters2344fae2001-01-15 00:50:52 +0000927 def help_enable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000928 print("""enable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000929Enables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000930bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000931
Tim Peters2344fae2001-01-15 00:50:52 +0000932 def help_disable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000933 print("""disable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000934Disables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000935bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000936
Tim Peters2344fae2001-01-15 00:50:52 +0000937 def help_ignore(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000938 print("""ignore bpnumber count
Tim Peters2344fae2001-01-15 00:50:52 +0000939Sets the ignore count for the given breakpoint number. A breakpoint
940becomes active when the ignore count is zero. When non-zero, the
941count is decremented each time the breakpoint is reached and the
942breakpoint is not disabled and any associated condition evaluates
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000943to true.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000944
Tim Peters2344fae2001-01-15 00:50:52 +0000945 def help_condition(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000946 print("""condition bpnumber str_condition
Tim Peters2344fae2001-01-15 00:50:52 +0000947str_condition is a string specifying an expression which
948must evaluate to true before the breakpoint is honored.
949If str_condition is absent, any existing condition is removed;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000950i.e., the breakpoint is made unconditional.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000951
Tim Peters2344fae2001-01-15 00:50:52 +0000952 def help_step(self):
953 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000954
Tim Peters2344fae2001-01-15 00:50:52 +0000955 def help_s(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000956 print("""s(tep)
Tim Peters2344fae2001-01-15 00:50:52 +0000957Execute the current line, stop at the first possible occasion
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000958(either in a function that is called or in the current function).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000959
Tim Peters2344fae2001-01-15 00:50:52 +0000960 def help_next(self):
961 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000962
Tim Peters2344fae2001-01-15 00:50:52 +0000963 def help_n(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000964 print("""n(ext)
Tim Peters2344fae2001-01-15 00:50:52 +0000965Continue execution until the next line in the current function
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000966is reached or it returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000967
Tim Peters2344fae2001-01-15 00:50:52 +0000968 def help_return(self):
969 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000970
Tim Peters2344fae2001-01-15 00:50:52 +0000971 def help_r(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000972 print("""r(eturn)
973Continue execution until the current function returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000974
Tim Peters2344fae2001-01-15 00:50:52 +0000975 def help_continue(self):
976 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000977
Tim Peters2344fae2001-01-15 00:50:52 +0000978 def help_cont(self):
979 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000980
Tim Peters2344fae2001-01-15 00:50:52 +0000981 def help_c(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000982 print("""c(ont(inue))
983Continue execution, only stop when a breakpoint is encountered.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000984
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000985 def help_jump(self):
986 self.help_j()
987
988 def help_j(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000989 print("""j(ump) lineno
990Set the next line that will be executed.""", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000991
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000992 def help_debug(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000993 print("""debug code
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000994Enter a recursive debugger that steps through the code argument
995(which is an arbitrary expression or statement to be executed
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000996in the current environment).""", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000997
Tim Peters2344fae2001-01-15 00:50:52 +0000998 def help_list(self):
999 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001000
Tim Peters2344fae2001-01-15 00:50:52 +00001001 def help_l(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001002 print("""l(ist) [first [,last]]
Tim Peters2344fae2001-01-15 00:50:52 +00001003List source code for the current file.
1004Without arguments, list 11 lines around the current line
1005or continue the previous listing.
1006With one argument, list 11 lines starting at that line.
1007With two arguments, list the given range;
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001008if the second argument is less than the first, it is a count.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001009
Tim Peters2344fae2001-01-15 00:50:52 +00001010 def help_args(self):
1011 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001012
Tim Peters2344fae2001-01-15 00:50:52 +00001013 def help_a(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001014 print("""a(rgs)
1015Print the arguments of the current function.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001016
Tim Peters2344fae2001-01-15 00:50:52 +00001017 def help_p(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001018 print("""p expression
1019Print the value of the expression.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001020
Barry Warsaw210bd202002-11-05 22:40:20 +00001021 def help_pp(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001022 print("""pp expression
1023Pretty-print the value of the expression.""", file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +00001024
Tim Peters2344fae2001-01-15 00:50:52 +00001025 def help_exec(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001026 print("""(!) statement
Tim Peters2344fae2001-01-15 00:50:52 +00001027Execute the (one-line) statement in the context of
1028the current stack frame.
1029The exclamation point can be omitted unless the first word
1030of the statement resembles a debugger command.
1031To assign to a global variable you must always prefix the
1032command with a 'global' command, e.g.:
1033(Pdb) global list_options; list_options = ['-l']
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001034(Pdb)""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001035
Guido van Rossumd8faa362007-04-27 19:54:29 +00001036 def help_run(self):
1037 print("""run [args...]
1038Restart the debugged python program. If a string is supplied, it is
1039splitted with "shlex" and the result is used as the new sys.argv.
1040History, breakpoints, actions and debugger options are preserved.
1041"restart" is an alias for "run".""")
1042
1043 help_restart = help_run
1044
Tim Peters2344fae2001-01-15 00:50:52 +00001045 def help_quit(self):
1046 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001047
Tim Peters2344fae2001-01-15 00:50:52 +00001048 def help_q(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001049 print("""q(uit) or exit - Quit from the debugger.
1050The program being executed is aborted.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001051
Guido van Rossumd1c08f32002-04-15 00:48:24 +00001052 help_exit = help_q
1053
Tim Peters2344fae2001-01-15 00:50:52 +00001054 def help_whatis(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001055 print("""whatis arg
1056Prints the type of the argument.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001057
Tim Peters2344fae2001-01-15 00:50:52 +00001058 def help_EOF(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001059 print("""EOF
1060Handles the receipt of EOF as a command.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001061
Tim Peters2344fae2001-01-15 00:50:52 +00001062 def help_alias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001063 print("""alias [name [command [parameter parameter ...] ]]
Tim Peters2344fae2001-01-15 00:50:52 +00001064Creates an alias called 'name' the executes 'command'. The command
1065must *not* be enclosed in quotes. Replaceable parameters are
1066indicated by %1, %2, and so on, while %* is replaced by all the
1067parameters. If no command is given, the current alias for name
1068is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +00001069
Tim Peters2344fae2001-01-15 00:50:52 +00001070Aliases may be nested and can contain anything that can be
1071legally typed at the pdb prompt. Note! You *can* override
1072internal pdb commands with aliases! Those internal commands
1073are then hidden until the alias is removed. Aliasing is recursively
1074applied to the first word of the command line; all other words
1075in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +00001076
Tim Peters2344fae2001-01-15 00:50:52 +00001077Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +00001078
Tim Peters2344fae2001-01-15 00:50:52 +00001079#Print instance variables (usage "pi classInst")
1080alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +00001081
Tim Peters2344fae2001-01-15 00:50:52 +00001082#Print instance variables in self
1083alias ps pi self
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001084""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001085
Tim Peters2344fae2001-01-15 00:50:52 +00001086 def help_unalias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001087 print("""unalias name
1088Deletes the specified alias.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001089
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090 def help_commands(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001091 print("""commands [bpnumber]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092(com) ...
1093(com) end
1094(Pdb)
1095
1096Specify a list of commands for breakpoint number bpnumber. The
1097commands themselves appear on the following lines. Type a line
1098containing just 'end' to terminate the commands.
1099
1100To remove all commands from a breakpoint, type commands and
1101follow it immediately with end; that is, give no commands.
1102
1103With no bpnumber argument, commands refers to the last
1104breakpoint set.
1105
1106You can use breakpoint commands to start your program up again.
1107Simply use the continue command, or step, or any other
1108command that resumes execution.
1109
1110Specifying any command resuming execution (currently continue,
1111step, next, return, jump, quit and their abbreviations) terminates
1112the command list (as if that command was immediately followed by end).
1113This is because any time you resume execution
1114(even with a simple next or step), you may encounter
1115another breakpoint--which could have its own command list, leading to
1116ambiguities about which list to execute.
1117
1118 If you use the 'silent' command in the command list, the
1119usual message about stopping at a breakpoint is not printed. This may
1120be desirable for breakpoints that are to print a specific message and
1121then continue. If none of the other commands print anything, you
1122see no sign that the breakpoint was reached.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001123""", file=self.stdout)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124
Tim Peters2344fae2001-01-15 00:50:52 +00001125 def help_pdb(self):
1126 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001127
Tim Peters2344fae2001-01-15 00:50:52 +00001128 def lookupmodule(self, filename):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001129 """Helper function for break/clear parsing -- may be overridden.
1130
1131 lookupmodule() translates (possibly incomplete) file or module name
1132 into an absolute file name.
1133 """
1134 if os.path.isabs(filename) and os.path.exists(filename):
Tim Peterse718f612004-10-12 21:51:32 +00001135 return filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001136 f = os.path.join(sys.path[0], filename)
1137 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1138 return f
Tim Peters2344fae2001-01-15 00:50:52 +00001139 root, ext = os.path.splitext(filename)
1140 if ext == '':
1141 filename = filename + '.py'
1142 if os.path.isabs(filename):
1143 return filename
1144 for dirname in sys.path:
1145 while os.path.islink(dirname):
1146 dirname = os.readlink(dirname)
1147 fullname = os.path.join(dirname, filename)
1148 if os.path.exists(fullname):
1149 return fullname
1150 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +00001151
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001152 def _runscript(self, filename):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153 # The script has to run in __main__ namespace (or imports from
1154 # __main__ will break).
1155 #
1156 # So we clear up the __main__ and set several special variables
1157 # (this gets rid of pdb's globals and cleans old variables on restarts).
1158 import __main__
1159 __main__.__dict__.clear()
1160 __main__.__dict__.update({"__name__" : "__main__",
1161 "__file__" : filename,
1162 "__builtins__": __builtins__,
1163 })
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001164
1165 # When bdb sets tracing, a number of call and line events happens
1166 # BEFORE debugger even reaches user's code (and the exact sequence of
1167 # events depends on python version). So we take special measures to
1168 # avoid stopping before we reach the main script (see user_line and
1169 # user_call for details).
1170 self._wait_for_mainpyfile = 1
1171 self.mainpyfile = self.canonic(filename)
1172 self._user_requested_quit = 0
1173 statement = 'execfile( "%s")' % filename
Guido van Rossumd8faa362007-04-27 19:54:29 +00001174 self.run(statement)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001175
Guido van Rossum35771131992-09-08 11:59:04 +00001176# Simplified interface
1177
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001178def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001179 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001180
1181def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001182 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001183
1184def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +00001185 # B/W compatibility
1186 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001187
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +00001188def runcall(*args, **kwds):
1189 return Pdb().runcall(*args, **kwds)
Guido van Rossum4e160981992-09-02 20:43:20 +00001190
Guido van Rossumb6775db1994-08-01 11:34:53 +00001191def set_trace():
Johannes Gijsbers84a6c202004-11-07 11:35:30 +00001192 Pdb().set_trace(sys._getframe().f_back)
Guido van Rossum35771131992-09-08 11:59:04 +00001193
1194# Post-Mortem interface
1195
1196def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +00001197 p = Pdb()
1198 p.reset()
1199 while t.tb_next is not None:
1200 t = t.tb_next
1201 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +00001202
1203def pm():
Tim Peters2344fae2001-01-15 00:50:52 +00001204 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +00001205
1206
1207# Main program for testing
1208
Guido van Rossum23efba41992-01-27 16:58:47 +00001209TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001210
Guido van Rossum921c8241992-01-10 14:54:42 +00001211def test():
Tim Peters2344fae2001-01-15 00:50:52 +00001212 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +00001213
1214# print help
1215def help():
Tim Peters2344fae2001-01-15 00:50:52 +00001216 for dirname in sys.path:
1217 fullname = os.path.join(dirname, 'pdb.doc')
1218 if os.path.exists(fullname):
1219 sts = os.system('${PAGER-more} '+fullname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001220 if sts: print('*** Pager exit status:', sts)
Tim Peters2344fae2001-01-15 00:50:52 +00001221 break
1222 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001223 print('Sorry, can\'t find the help file "pdb.doc"', end=' ')
1224 print('along the Python search path')
Guido van Rossumf17361d1996-07-30 16:28:13 +00001225
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001226def main():
Tim Peters2344fae2001-01-15 00:50:52 +00001227 if not sys.argv[1:]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001228 print("usage: pdb.py scriptfile [arg] ...")
Tim Peters2344fae2001-01-15 00:50:52 +00001229 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001230
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001231 mainpyfile = sys.argv[1] # Get script filename
1232 if not os.path.exists(mainpyfile):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001233 print('Error:', mainpyfile, 'does not exist')
Tim Peters2344fae2001-01-15 00:50:52 +00001234 sys.exit(1)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001235
Tim Peters2344fae2001-01-15 00:50:52 +00001236 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001237
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001238 # Replace pdb's dir with script's dir in front of module search path.
1239 sys.path[0] = os.path.dirname(mainpyfile)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001240
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001241 # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1242 # modified by the script being debugged. It's a bad idea when it was
Guido van Rossumd8faa362007-04-27 19:54:29 +00001243 # changed by the user from the command line. There is a "restart" command which
1244 # allows explicit specification of command line arguments.
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001245 pdb = Pdb()
1246 while 1:
1247 try:
1248 pdb._runscript(mainpyfile)
1249 if pdb._user_requested_quit:
1250 break
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001251 print("The program finished and will be restarted")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001252 except Restart:
1253 print("Restarting", mainpyfile, "with arguments:")
1254 print("\t" + " ".join(sys.argv[1:]))
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001255 except SystemExit:
1256 # In most cases SystemExit does not warrant a post-mortem session.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001257 print("The program exited via sys.exit(). Exit status: ", end=' ')
1258 print(sys.exc_info()[1])
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001259 except:
1260 traceback.print_exc()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001261 print("Uncaught exception. Entering post mortem debugging")
1262 print("Running 'cont' or 'step' will restart the program")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001263 t = sys.exc_info()[2]
1264 while t.tb_next is not None:
1265 t = t.tb_next
1266 pdb.interaction(t.tb_frame,t)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001267 print("Post mortem debugger finished. The "+mainpyfile+" will be restarted")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001268
1269
1270# When invoked as main program, invoke the debugger on a script
Guido van Rossumd8faa362007-04-27 19:54:29 +00001271if __name__ == '__main__':
1272 import pdb
1273 pdb.main()