Added a test for the ability to specify a class attribute in Formatter configuration...
[python.git] / Doc / lib / libpdb.tex
blob6301175d34cc19cfa98f6ea331e555f0a577ba36
1 \chapter{The Python Debugger \label{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 -m pdb myscript.py
43 \end{verbatim}
45 When invoked as a script, pdb will automatically enter post-mortem debugging
46 if the program being debugged exits abnormally. After post-mortem debugging
47 (or after normal exit of the program), pdb will restart the program.
48 Automatic restarting preserves pdb's state (such as breakpoints) and in most
49 cases is more useful than quitting the debugger upon program's exit.
50 \versionadded[Restarting post-mortem behavior added]{2.4}
52 Typical usage to inspect a crashed program is:
54 \begin{verbatim}
55 >>> import pdb
56 >>> import mymodule
57 >>> mymodule.test()
58 Traceback (most recent call last):
59 File "<stdin>", line 1, in ?
60 File "./mymodule.py", line 4, in test
61 test2()
62 File "./mymodule.py", line 3, in test2
63 print spam
64 NameError: spam
65 >>> pdb.pm()
66 > ./mymodule.py(3)test2()
67 -> print spam
68 (Pdb)
69 \end{verbatim}
71 The module defines the following functions; each enters the debugger
72 in a slightly different way:
74 \begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
75 Execute the \var{statement} (given as a string) under debugger
76 control. The debugger prompt appears before any code is executed; you
77 can set breakpoints and type \samp{continue}, or you can step through
78 the statement using \samp{step} or \samp{next} (all these commands are
79 explained below). The optional \var{globals} and \var{locals}
80 arguments specify the environment in which the code is executed; by
81 default the dictionary of the module \refmodule[main]{__main__} is
82 used. (See the explanation of the \keyword{exec} statement or the
83 \function{eval()} built-in function.)
84 \end{funcdesc}
86 \begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
87 Evaluate the \var{expression} (given as a string) under debugger
88 control. When \function{runeval()} returns, it returns the value of the
89 expression. Otherwise this function is similar to
90 \function{run()}.
91 \end{funcdesc}
93 \begin{funcdesc}{runcall}{function\optional{, argument, ...}}
94 Call the \var{function} (a function or method object, not a string)
95 with the given arguments. When \function{runcall()} returns, it returns
96 whatever the function call returned. The debugger prompt appears as
97 soon as the function is entered.
98 \end{funcdesc}
100 \begin{funcdesc}{set_trace}{}
101 Enter the debugger at the calling stack frame. This is useful to
102 hard-code a breakpoint at a given point in a program, even if the code
103 is not otherwise being debugged (e.g. when an assertion fails).
104 \end{funcdesc}
106 \begin{funcdesc}{post_mortem}{traceback}
107 Enter post-mortem debugging of the given \var{traceback} object.
108 \end{funcdesc}
110 \begin{funcdesc}{pm}{}
111 Enter post-mortem debugging of the traceback found in
112 \code{sys.last_traceback}.
113 \end{funcdesc}
116 \section{Debugger Commands \label{debugger-commands}}
118 The debugger recognizes the following commands. Most commands can be
119 abbreviated to one or two letters; e.g. \samp{h(elp)} means that
120 either \samp{h} or \samp{help} can be used to enter the help
121 command (but not \samp{he} or \samp{hel}, nor \samp{H} or
122 \samp{Help} or \samp{HELP}). Arguments to commands must be
123 separated by whitespace (spaces or tabs). Optional arguments are
124 enclosed in square brackets (\samp{[]}) in the command syntax; the
125 square brackets must not be typed. Alternatives in the command syntax
126 are separated by a vertical bar (\samp{|}).
128 Entering a blank line repeats the last command entered. Exception: if
129 the last command was a \samp{list} command, the next 11 lines are
130 listed.
132 Commands that the debugger doesn't recognize are assumed to be Python
133 statements and are executed in the context of the program being
134 debugged. Python statements can also be prefixed with an exclamation
135 point (\samp{!}). This is a powerful way to inspect the program
136 being debugged; it is even possible to change a variable or call a
137 function. When an
138 exception occurs in such a statement, the exception name is printed
139 but the debugger's state is not changed.
141 Multiple commands may be entered on a single line, separated by
142 \samp{;;}. (A single \samp{;} is not used as it is
143 the separator for multiple commands in a line that is passed to
144 the Python parser.)
145 No intelligence is applied to separating the commands;
146 the input is split at the first \samp{;;} pair, even if it is in
147 the middle of a quoted string.
149 The debugger supports aliases. Aliases can have parameters which
150 allows one a certain level of adaptability to the context under
151 examination.
153 If a file \file{.pdbrc}
154 \indexii{.pdbrc}{file}\indexiii{debugger}{configuration}{file}
155 exists in the user's home directory or in the current directory, it is
156 read in and executed as if it had been typed at the debugger prompt.
157 This is particularly useful for aliases. If both files exist, the one
158 in the home directory is read first and aliases defined there can be
159 overridden by the local file.
161 \begin{description}
163 \item[h(elp) \optional{\var{command}}]
165 Without argument, print the list of available commands. With a
166 \var{command} as argument, print help about that command. \samp{help
167 pdb} displays the full documentation file; if the environment variable
168 \envvar{PAGER} is defined, the file is piped through that command
169 instead. Since the \var{command} argument must be an identifier,
170 \samp{help exec} must be entered to get help on the \samp{!} command.
172 \item[w(here)]
174 Print a stack trace, with the most recent frame at the bottom. An
175 arrow indicates the current frame, which determines the context of
176 most commands.
178 \item[d(own)]
180 Move the current frame one level down in the stack trace
181 (to an newer frame).
183 \item[u(p)]
185 Move the current frame one level up in the stack trace
186 (to a older frame).
188 \item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
190 With a \var{lineno} argument, set a break there in the current
191 file. With a \var{function} argument, set a break at the first
192 executable statement within that function.
193 The line number may be prefixed with a filename and a colon,
194 to specify a breakpoint in another file (probably one that
195 hasn't been loaded yet). The file is searched on \code{sys.path}.
196 Note that each breakpoint is assigned a number to which all the other
197 breakpoint commands refer.
199 If a second argument is present, it is an expression which must
200 evaluate to true before the breakpoint is honored.
202 Without argument, list all breaks, including for each breakpoint,
203 the number of times that breakpoint has been hit, the current
204 ignore count, and the associated condition if any.
206 \item[tbreak \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
208 Temporary breakpoint, which is removed automatically when it is
209 first hit. The arguments are the same as break.
211 \item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
213 With a space separated list of breakpoint numbers, clear those
214 breakpoints. Without argument, clear all breaks (but first
215 ask confirmation).
217 \item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
219 Disables the breakpoints given as a space separated list of
220 breakpoint numbers. Disabling a breakpoint means it cannot cause
221 the program to stop execution, but unlike clearing a breakpoint, it
222 remains in the list of breakpoints and can be (re-)enabled.
224 \item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
226 Enables the breakpoints specified.
228 \item[ignore \var{bpnumber} \optional{\var{count}}]
230 Sets the ignore count for the given breakpoint number. If
231 count is omitted, the ignore count is set to 0. A breakpoint
232 becomes active when the ignore count is zero. When non-zero,
233 the count is decremented each time the breakpoint is reached
234 and the breakpoint is not disabled and any associated condition
235 evaluates to true.
237 \item[condition \var{bpnumber} \optional{\var{condition}}]
239 Condition is an expression which must evaluate to true before
240 the breakpoint is honored. If condition is absent, any existing
241 condition is removed; i.e., the breakpoint is made unconditional.
243 \item[s(tep)]
245 Execute the current line, stop at the first possible occasion
246 (either in a function that is called or on the next line in the
247 current function).
249 \item[n(ext)]
251 Continue execution until the next line in the current function
252 is reached or it returns. (The difference between \samp{next} and
253 \samp{step} is that \samp{step} stops inside a called function, while
254 \samp{next} executes called functions at (nearly) full speed, only
255 stopping at the next line in the current function.)
257 \item[r(eturn)]
259 Continue execution until the current function returns.
261 \item[c(ont(inue))]
263 Continue execution, only stop when a breakpoint is encountered.
265 \item[j(ump) \var{lineno}]
267 Set the next line that will be executed. Only available in the
268 bottom-most frame. This lets you jump back and execute code
269 again, or jump forward to skip code that you don't want to run.
271 It should be noted that not all jumps are allowed --- for instance it
272 is not possible to jump into the middle of a \keyword{for} loop or out
273 of a \keyword{finally} clause.
275 \item[l(ist) \optional{\var{first}\optional{, \var{last}}}]
277 List source code for the current file. Without arguments, list 11
278 lines around the current line or continue the previous listing. With
279 one argument, list 11 lines around at that line. With two arguments,
280 list the given range; if the second argument is less than the first,
281 it is interpreted as a count.
283 \item[a(rgs)]
285 Print the argument list of the current function.
287 \item[p \var{expression}]
289 Evaluate the \var{expression} in the current context and print its
290 value. \note{\samp{print} can also be used, but is not a debugger
291 command --- this executes the Python \keyword{print} statement.}
293 \item[pp \var{expression}]
295 Like the \samp{p} command, except the value of the expression is
296 pretty-printed using the \module{pprint} module.
298 \item[alias \optional{\var{name} \optional{command}}]
300 Creates an alias called \var{name} that executes \var{command}. The
301 command must \emph{not} be enclosed in quotes. Replaceable parameters
302 can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is
303 replaced by all the parameters. If no command is given, the current
304 alias for \var{name} is shown. If no arguments are given, all
305 aliases are listed.
307 Aliases may be nested and can contain anything that can be
308 legally typed at the pdb prompt. Note that internal pdb commands
309 \emph{can} be overridden by aliases. Such a command is
310 then hidden until the alias is removed. Aliasing is recursively
311 applied to the first word of the command line; all other words
312 in the line are left alone.
314 As an example, here are two useful aliases (especially when placed
315 in the \file{.pdbrc} file):
317 \begin{verbatim}
318 #Print instance variables (usage "pi classInst")
319 alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
320 #Print instance variables in self
321 alias ps pi self
322 \end{verbatim}
324 \item[unalias \var{name}]
326 Deletes the specified alias.
328 \item[\optional{!}\var{statement}]
330 Execute the (one-line) \var{statement} in the context of
331 the current stack frame.
332 The exclamation point can be omitted unless the first word
333 of the statement resembles a debugger command.
334 To set a global variable, you can prefix the assignment
335 command with a \samp{global} command on the same line, e.g.:
337 \begin{verbatim}
338 (Pdb) global list_options; list_options = ['-l']
339 (Pdb)
340 \end{verbatim}
342 \item[q(uit)]
344 Quit from the debugger.
345 The program being executed is aborted.
347 \end{description}
349 \section{How It Works \label{debugger-hooks}}
351 Some changes were made to the interpreter:
353 \begin{itemize}
354 \item \code{sys.settrace(\var{func})} sets the global trace function
355 \item there can also a local trace function (see later)
356 \end{itemize}
358 Trace functions have three arguments: \var{frame}, \var{event}, and
359 \var{arg}. \var{frame} is the current stack frame. \var{event} is a
360 string: \code{'call'}, \code{'line'}, \code{'return'}, \code{'exception'},
361 \code{'c_call'}, \code{'c_return'}, or \code{'c_exception'}. \var{arg}
362 depends on the event type.
364 The global trace function is invoked (with \var{event} set to
365 \code{'call'}) whenever a new local scope is entered; it should return
366 a reference to the local trace function to be used that scope, or
367 \code{None} if the scope shouldn't be traced.
369 The local trace function should return a reference to itself (or to
370 another function for further tracing in that scope), or \code{None} to
371 turn off tracing in that scope.
373 Instance methods are accepted (and very useful!) as trace functions.
375 The events have the following meaning:
377 \begin{description}
379 \item[\code{'call'}]
380 A function is called (or some other code block entered). The global
381 trace function is called; \var{arg} is \code{None};
382 the return value specifies the local trace function.
384 \item[\code{'line'}]
385 The interpreter is about to execute a new line of code (sometimes
386 multiple line events on one line exist). The local trace function is
387 called; \var{arg} is \code{None}; the return value specifies the new
388 local trace function.
390 \item[\code{'return'}]
391 A function (or other code block) is about to return. The local trace
392 function is called; \var{arg} is the value that will be returned. The
393 trace function's return value is ignored.
395 \item[\code{'exception'}]
396 An exception has occurred. The local trace function is called;
397 \var{arg} is a triple \code{(\var{exception}, \var{value},
398 \var{traceback})}; the return value specifies the new local trace
399 function.
401 \item[\code{'c_call'}]
402 A C function is about to be called. This may be an extension function
403 or a builtin. \var{arg} is the C function object.
405 \item[\code{'c_return'}]
406 A C function has returned. \var{arg} is \code{None}.
408 \item[\code{'c_exception'}]
409 A C function has thrown an exception. \var{arg} is \code{None}.
411 \end{description}
413 Note that as an exception is propagated down the chain of callers, an
414 \code{'exception'} event is generated at each level.
416 For more information on code and frame objects, refer to the
417 \citetitle[../ref/ref.html]{Python Reference Manual}.