Files for 2.1b1 distribution.
[python/dscho.git] / Doc / lib / libpdb.tex
blob93d39c0c447a8473cc336f67ff9ce60a4be5a262
1 \chapter{The Python Debugger}
3 \declaremodule{standard}{pdb}
4 \modulesynopsis{The Python debugger for interactive interpreters.}
7 The module \module{pdb} defines an interactive source code
8 debugger\index{debugging} for Python programs. It supports setting
9 (conditional) breakpoints and single stepping at the source line
10 level, inspection of stack frames, source code listing, and evaluation
11 of arbitrary Python code in the context of any stack frame. It also
12 supports post-mortem debugging and can be called under program
13 control.
15 The debugger is extensible --- it is actually defined as the class
16 \class{Pdb}\withsubitem{(class in pdb)}{\ttindex{Pdb}}.
17 This is currently undocumented but easily understood by reading the
18 source. The extension interface uses the modules
19 \module{bdb}\refstmodindex{bdb} (undocumented) and
20 \refmodule{cmd}\refstmodindex{cmd}.
22 The debugger's prompt is \samp{(Pdb) }.
23 Typical usage to run a program under control of the debugger is:
25 \begin{verbatim}
26 >>> import pdb
27 >>> import mymodule
28 >>> pdb.run('mymodule.test()')
29 > <string>(0)?()
30 (Pdb) continue
31 > <string>(1)?()
32 (Pdb) continue
33 NameError: 'spam'
34 > <string>(1)?()
35 (Pdb)
36 \end{verbatim}
38 \file{pdb.py} can also be invoked as
39 a script to debug other scripts. For example:
41 \begin{verbatim}
42 python /usr/local/lib/python1.5/pdb.py myscript.py
43 \end{verbatim}
45 Typical usage to inspect a crashed program is:
47 \begin{verbatim}
48 >>> import pdb
49 >>> import mymodule
50 >>> mymodule.test()
51 Traceback (most recent call last):
52 File "<stdin>", line 1, in ?
53 File "./mymodule.py", line 4, in test
54 test2()
55 File "./mymodule.py", line 3, in test2
56 print spam
57 NameError: spam
58 >>> pdb.pm()
59 > ./mymodule.py(3)test2()
60 -> print spam
61 (Pdb)
62 \end{verbatim}
64 The module defines the following functions; each enters the debugger
65 in a slightly different way:
67 \begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
68 Execute the \var{statement} (given as a string) under debugger
69 control. The debugger prompt appears before any code is executed; you
70 can set breakpoints and type \samp{continue}, or you can step through
71 the statement using \samp{step} or \samp{next} (all these commands are
72 explained below). The optional \var{globals} and \var{locals}
73 arguments specify the environment in which the code is executed; by
74 default the dictionary of the module \refmodule[main]{__main__} is
75 used. (See the explanation of the \keyword{exec} statement or the
76 \function{eval()} built-in function.)
77 \end{funcdesc}
79 \begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
80 Evaluate the \var{expression} (given as a a string) under debugger
81 control. When \function{runeval()} returns, it returns the value of the
82 expression. Otherwise this function is similar to
83 \function{run()}.
84 \end{funcdesc}
86 \begin{funcdesc}{runcall}{function\optional{, argument, ...}}
87 Call the \var{function} (a function or method object, not a string)
88 with the given arguments. When \function{runcall()} returns, it returns
89 whatever the function call returned. The debugger prompt appears as
90 soon as the function is entered.
91 \end{funcdesc}
93 \begin{funcdesc}{set_trace}{}
94 Enter the debugger at the calling stack frame. This is useful to
95 hard-code a breakpoint at a given point in a program, even if the code
96 is not otherwise being debugged (e.g. when an assertion fails).
97 \end{funcdesc}
99 \begin{funcdesc}{post_mortem}{traceback}
100 Enter post-mortem debugging of the given \var{traceback} object.
101 \end{funcdesc}
103 \begin{funcdesc}{pm}{}
104 Enter post-mortem debugging of the traceback found in
105 \code{sys.last_traceback}.
106 \end{funcdesc}
109 \section{Debugger Commands \label{debugger-commands}}
111 The debugger recognizes the following commands. Most commands can be
112 abbreviated to one or two letters; e.g. \samp{h(elp)} means that
113 either \samp{h} or \samp{help} can be used to enter the help
114 command (but not \samp{he} or \samp{hel}, nor \samp{H} or
115 \samp{Help} or \samp{HELP}). Arguments to commands must be
116 separated by whitespace (spaces or tabs). Optional arguments are
117 enclosed in square brackets (\samp{[]}) in the command syntax; the
118 square brackets must not be typed. Alternatives in the command syntax
119 are separated by a vertical bar (\samp{|}).
121 Entering a blank line repeats the last command entered. Exception: if
122 the last command was a \samp{list} command, the next 11 lines are
123 listed.
125 Commands that the debugger doesn't recognize are assumed to be Python
126 statements and are executed in the context of the program being
127 debugged. Python statements can also be prefixed with an exclamation
128 point (\samp{!}). This is a powerful way to inspect the program
129 being debugged; it is even possible to change a variable or call a
130 function. When an
131 exception occurs in such a statement, the exception name is printed
132 but the debugger's state is not changed.
134 Multiple commands may be entered on a single line, separated by
135 \samp{;;}. (A single \samp{;} is not used as it is
136 the separator for multiple commands in a line that is passed to
137 the Python parser.)
138 No intelligence is applied to separating the commands;
139 the input is split at the first \samp{;;} pair, even if it is in
140 the middle of a quoted string.
142 The debugger supports aliases. Aliases can have parameters which
143 allows one a certain level of adaptability to the context under
144 examination.
146 If a file \file{.pdbrc}
147 \indexii{.pdbrc}{file}\indexiii{debugger}{configuration}{file}
148 exists in the user's home directory or in the current directory, it is
149 read in and executed as if it had been typed at the debugger prompt.
150 This is particularly useful for aliases. If both files exist, the one
151 in the home directory is read first and aliases defined there can be
152 overridden by the local file.
154 \begin{description}
156 \item[h(elp) \optional{\var{command}}]
158 Without argument, print the list of available commands. With a
159 \var{command} as argument, print help about that command. \samp{help
160 pdb} displays the full documentation file; if the environment variable
161 \envvar{PAGER} is defined, the file is piped through that command
162 instead. Since the \var{command} argument must be an identifier,
163 \samp{help exec} must be entered to get help on the \samp{!} command.
165 \item[w(here)]
167 Print a stack trace, with the most recent frame at the bottom. An
168 arrow indicates the current frame, which determines the context of
169 most commands.
171 \item[d(own)]
173 Move the current frame one level down in the stack trace
174 (to an newer frame).
176 \item[u(p)]
178 Move the current frame one level up in the stack trace
179 (to a older frame).
181 \item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
183 With a \var{lineno} argument, set a break there in the current
184 file. With a \var{function} argument, set a break at the first
185 executable statement within that function.
186 The line number may be prefixed with a filename and a colon,
187 to specify a breakpoint in another file (probably one that
188 hasn't been loaded yet). The file is searched on \code{sys.path}.
189 Note that each breakpoint is assigned a number to which all the other
190 breakpoint commands refer.
192 If a second argument is present, it is an expression which must
193 evaluate to true before the breakpoint is honored.
195 Without argument, list all breaks, including for each breakpoint,
196 the number of times that breakpoint has been hit, the current
197 ignore count, and the associated condition if any.
199 \item[tbreak \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
201 Temporary breakpoint, which is removed automatically when it is
202 first hit. The arguments are the same as break.
204 \item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
206 With a space separated list of breakpoint numbers, clear those
207 breakpoints. Without argument, clear all breaks (but first
208 ask confirmation).
210 \item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
212 Disables the breakpoints given as a space separated list of
213 breakpoint numbers. Disabling a breakpoint means it cannot cause
214 the program to stop execution, but unlike clearing a breakpoint, it
215 remains in the list of breakpoints and can be (re-)enabled.
217 \item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
219 Enables the breakpoints specified.
221 \item[ignore \var{bpnumber} \optional{\var{count}}]
223 Sets the ignore count for the given breakpoint number. If
224 count is omitted, the ignore count is set to 0. A breakpoint
225 becomes active when the ignore count is zero. When non-zero,
226 the count is decremented each time the breakpoint is reached
227 and the breakpoint is not disabled and any associated condition
228 evaluates to true.
230 \item[condition \var{bpnumber} \optional{\var{condition}}]
232 Condition is an expression which must evaluate to true before
233 the breakpoint is honored. If condition is absent, any existing
234 condition is removed; i.e., the breakpoint is made unconditional.
236 \item[s(tep)]
238 Execute the current line, stop at the first possible occasion
239 (either in a function that is called or on the next line in the
240 current function).
242 \item[n(ext)]
244 Continue execution until the next line in the current function
245 is reached or it returns. (The difference between \samp{next} and
246 \samp{step} is that \samp{step} stops inside a called function, while
247 \samp{next} executes called functions at (nearly) full speed, only
248 stopping at the next line in the current function.)
250 \item[r(eturn)]
252 Continue execution until the current function returns.
254 \item[c(ont(inue))]
256 Continue execution, only stop when a breakpoint is encountered.
258 \item[l(ist) \optional{\var{first\optional{, last}}}]
260 List source code for the current file. Without arguments, list 11
261 lines around the current line or continue the previous listing. With
262 one argument, list 11 lines around at that line. With two arguments,
263 list the given range; if the second argument is less than the first,
264 it is interpreted as a count.
266 \item[a(rgs)]
268 Print the argument list of the current function.
270 \item[p \var{expression}]
272 Evaluate the \var{expression} in the current context and print its
273 value. (Note: \samp{print} can also be used, but is not a debugger
274 command --- this executes the Python \keyword{print} statement.)
276 \item[alias \optional{\var{name} \optional{command}}]
278 Creates an alias called \var{name} that executes \var{command}. The
279 command must \emph{not} be enclosed in quotes. Replaceable parameters
280 can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is
281 replaced by all the parameters. If no command is given, the current
282 alias for \var{name} is shown. If no arguments are given, all
283 aliases are listed.
285 Aliases may be nested and can contain anything that can be
286 legally typed at the pdb prompt. Note that internal pdb commands
287 \emph{can} be overridden by aliases. Such a command is
288 then hidden until the alias is removed. Aliasing is recursively
289 applied to the first word of the command line; all other words
290 in the line are left alone.
292 As an example, here are two useful aliases (especially when placed
293 in the \file{.pdbrc} file):
295 \begin{verbatim}
296 #Print instance variables (usage "pi classInst")
297 alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
298 #Print instance variables in self
299 alias ps pi self
300 \end{verbatim}
302 \item[unalias \var{name}]
304 Deletes the specified alias.
306 \item[\optional{!}\var{statement}]
308 Execute the (one-line) \var{statement} in the context of
309 the current stack frame.
310 The exclamation point can be omitted unless the first word
311 of the statement resembles a debugger command.
312 To set a global variable, you can prefix the assignment
313 command with a \samp{global} command on the same line, e.g.:
315 \begin{verbatim}
316 (Pdb) global list_options; list_options = ['-l']
317 (Pdb)
318 \end{verbatim}
320 \item[q(uit)]
322 Quit from the debugger.
323 The program being executed is aborted.
325 \end{description}
327 \section{How It Works}
329 Some changes were made to the interpreter:
331 \begin{itemize}
332 \item \code{sys.settrace(\var{func})} sets the global trace function
333 \item there can also a local trace function (see later)
334 \end{itemize}
336 Trace functions have three arguments: \var{frame}, \var{event}, and
337 \var{arg}. \var{frame} is the current stack frame. \var{event} is a
338 string: \code{'call'}, \code{'line'}, \code{'return'} or
339 \code{'exception'}. \var{arg} depends on the event type.
341 The global trace function is invoked (with \var{event} set to
342 \code{'call'}) whenever a new local scope is entered; it should return
343 a reference to the local trace function to be used that scope, or
344 \code{None} if the scope shouldn't be traced.
346 The local trace function should return a reference to itself (or to
347 another function for further tracing in that scope), or \code{None} to
348 turn off tracing in that scope.
350 Instance methods are accepted (and very useful!) as trace functions.
352 The events have the following meaning:
354 \begin{description}
356 \item[\code{'call'}]
357 A function is called (or some other code block entered). The global
358 trace function is called; arg is the argument list to the function;
359 the return value specifies the local trace function.
361 \item[\code{'line'}]
362 The interpreter is about to execute a new line of code (sometimes
363 multiple line events on one line exist). The local trace function is
364 called; arg in None; the return value specifies the new local trace
365 function.
367 \item[\code{'return'}]
368 A function (or other code block) is about to return. The local trace
369 function is called; arg is the value that will be returned. The trace
370 function's return value is ignored.
372 \item[\code{'exception'}]
373 An exception has occurred. The local trace function is called; arg is
374 a triple (exception, value, traceback); the return value specifies the
375 new local trace function
377 \end{description}
379 Note that as an exception is propagated down the chain of callers, an
380 \code{'exception'} event is generated at each level.
382 For more information on code and frame objects, refer to the
383 \citetitle[../ref/ref.html]{Python Reference Manual}.