blob: f31db4836b149f0fb60f40a8a8683fb9e1635188 [file] [log] [blame]
Guido van Rossumf4aac481995-03-02 12:37:55 +00001\chapter{The Python Debugger}
Fred Drakec8993aa1999-04-22 16:50:40 +00002
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{pdb}
Fred Drakec8993aa1999-04-22 16:50:40 +00004\modulesynopsis{The Python debugger for interactive interpreters.}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +00005
Guido van Rossumf4aac481995-03-02 12:37:55 +00006
Fred Drakec8993aa1999-04-22 16:50:40 +00007The module \module{pdb} defines an interactive source code
8debugger\index{debugging} for Python programs. It supports setting
9(conditional) breakpoints and single stepping at the source line
10level, inspection of stack frames, source code listing, and evaluation
11of arbitrary Python code in the context of any stack frame. It also
12supports post-mortem debugging and can be called under program
13control.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000014
Fred Drakec8993aa1999-04-22 16:50:40 +000015The debugger is extensible --- it is actually defined as the class
16\class{Pdb}\withsubitem{(class in pdb)}{\ttindex{Pdb}}.
Fred Drake82d493f1998-04-07 19:14:17 +000017This is currently undocumented but easily understood by reading the
Fred Drakec8993aa1999-04-22 16:50:40 +000018source. The extension interface uses the modules
19\module{bdb}\refstmodindex{bdb} (undocumented) and
20\refmodule{cmd}\refstmodindex{cmd}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000021
22A primitive windowing version of the debugger also exists --- this is
Fred Drakec8993aa1999-04-22 16:50:40 +000023module \module{wdb}\refstmodindex{wdb}, which requires
24\module{stdwin}\refbimodindex{stdwin}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000025
Fred Drake82d493f1998-04-07 19:14:17 +000026The debugger's prompt is \samp{(Pdb) }.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000027Typical usage to run a program under control of the debugger is:
28
Fred Drake19479911998-02-13 06:58:54 +000029\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000030>>> import pdb
31>>> import mymodule
32>>> pdb.run('mymodule.test()')
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000033> <string>(0)?()
34(Pdb) continue
35> <string>(1)?()
36(Pdb) continue
37NameError: 'spam'
38> <string>(1)?()
39(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000040\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000041
42\file{pdb.py} can also be invoked as
Guido van Rossum809408e1997-06-02 17:28:16 +000043a script to debug other scripts. For example:
Fred Drake82d493f1998-04-07 19:14:17 +000044
45\begin{verbatim}
46python /usr/local/lib/python1.5/pdb.py myscript.py
47\end{verbatim}
Guido van Rossum809408e1997-06-02 17:28:16 +000048
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000049Typical usage to inspect a crashed program is:
50
Fred Drake19479911998-02-13 06:58:54 +000051\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000052>>> import pdb
53>>> import mymodule
54>>> mymodule.test()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000055Traceback (innermost last):
56 File "<stdin>", line 1, in ?
57 File "./mymodule.py", line 4, in test
58 test2()
59 File "./mymodule.py", line 3, in test2
60 print spam
61NameError: spam
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000062>>> pdb.pm()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000063> ./mymodule.py(3)test2()
64-> print spam
65(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000066\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000067
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000068The module defines the following functions; each enters the debugger
69in a slightly different way:
70
Fred Drakecce10901998-03-17 06:33:25 +000071\begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000072Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000073control. The debugger prompt appears before any code is executed; you
Fred Drakec8993aa1999-04-22 16:50:40 +000074can set breakpoints and type \samp{continue}, or you can step through
75the statement using \samp{step} or \samp{next} (all these commands are
Guido van Rossumf4aac481995-03-02 12:37:55 +000076explained below). The optional \var{globals} and \var{locals}
77arguments specify the environment in which the code is executed; by
Fred Drakec8993aa1999-04-22 16:50:40 +000078default the dictionary of the module \module{__main__} is used. (See
79the explanation of the \keyword{exec} statement or the
80\function{eval()} built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000081\end{funcdesc}
82
Fred Drakecce10901998-03-17 06:33:25 +000083\begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000084Evaluate the \var{expression} (given as a a string) under debugger
Fred Drakec8993aa1999-04-22 16:50:40 +000085control. When \function{runeval()} returns, it returns the value of the
Guido van Rossumf4aac481995-03-02 12:37:55 +000086expression. Otherwise this function is similar to
Fred Drakec8993aa1999-04-22 16:50:40 +000087\function{run()}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000088\end{funcdesc}
89
Fred Drakecce10901998-03-17 06:33:25 +000090\begin{funcdesc}{runcall}{function\optional{, argument, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000091Call the \var{function} (a function or method object, not a string)
Fred Drakec8993aa1999-04-22 16:50:40 +000092with the given arguments. When \function{runcall()} returns, it returns
Guido van Rossumf4aac481995-03-02 12:37:55 +000093whatever the function call returned. The debugger prompt appears as
94soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000095\end{funcdesc}
96
97\begin{funcdesc}{set_trace}{}
98Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +000099hard-code a breakpoint at a given point in a program, even if the code
100is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000101\end{funcdesc}
102
103\begin{funcdesc}{post_mortem}{traceback}
104Enter post-mortem debugging of the given \var{traceback} object.
105\end{funcdesc}
106
107\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +0000108Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000109\code{sys.last_traceback}.
110\end{funcdesc}
111
Fred Drakec8993aa1999-04-22 16:50:40 +0000112
113\section{Debugger Commands \label{debugger-commands}}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000114
115The debugger recognizes the following commands. Most commands can be
Fred Drakec8993aa1999-04-22 16:50:40 +0000116abbreviated to one or two letters; e.g. \samp{h(elp)} means that
117either \samp{h} or \samp{help} can be used to enter the help
118command (but not \samp{he} or \samp{hel}, nor \samp{H} or
119\samp{Help} or \samp{HELP}). Arguments to commands must be
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000120separated by whitespace (spaces or tabs). Optional arguments are
Fred Drakec8993aa1999-04-22 16:50:40 +0000121enclosed in square brackets (\samp{[]}) in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000122square brackets must not be typed. Alternatives in the command syntax
Fred Drakec8993aa1999-04-22 16:50:40 +0000123are separated by a vertical bar (\samp{|}).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000124
125Entering a blank line repeats the last command entered. Exception: if
Fred Drakec8993aa1999-04-22 16:50:40 +0000126the last command was a \samp{list} command, the next 11 lines are
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000127listed.
128
129Commands that the debugger doesn't recognize are assumed to be Python
130statements and are executed in the context of the program being
131debugged. Python statements can also be prefixed with an exclamation
Fred Drakec8993aa1999-04-22 16:50:40 +0000132point (\samp{!}). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000133being debugged; it is even possible to change a variable or call a
134function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000135exception occurs in such a statement, the exception name is printed
136but the debugger's state is not changed.
137
Guido van Rossum64421161998-09-17 15:11:51 +0000138Multiple commands may be entered on a single line, separated by
Fred Drakec8993aa1999-04-22 16:50:40 +0000139\samp{;;}. (A single \samp{;} is not used as it is
Guido van Rossum64421161998-09-17 15:11:51 +0000140the separator for multiple commands in a line that is passed to
141the Python parser.)
142No intelligence is applied to separating the commands;
Fred Drakec8993aa1999-04-22 16:50:40 +0000143the input is split at the first \samp{;;} pair, even if it is in
Guido van Rossum64421161998-09-17 15:11:51 +0000144the middle of a quoted string.
145
146The debugger supports aliases. Aliases can have parameters which
147allows one a certain level of adaptability to the context under
148examination.
149
Fred Drakec8993aa1999-04-22 16:50:40 +0000150If a file \file{.pdbrc}
151\indexii{.pdbrc}{file}\indexiii{debugger}{configuration}{file}
152exists in the user's home directory or in the current directory, it is
153read in and executed as if it had been typed at the debugger prompt.
154This is particularly useful for aliases. If both files exist, the one
155in the home directory is read first and aliases defined there can be
156overriden by the local file.
Guido van Rossum64421161998-09-17 15:11:51 +0000157
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000158\begin{description}
159
Fred Drake74947ac1998-01-12 15:38:30 +0000160\item[h(elp) \optional{\var{command}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000161
Fred Drake74947ac1998-01-12 15:38:30 +0000162Without argument, print the list of available commands. With a
163\var{command} as argument, print help about that command. \samp{help
164pdb} displays the full documentation file; if the environment variable
Fred Drakec8993aa1999-04-22 16:50:40 +0000165\envvar{PAGER} is defined, the file is piped through that command
Fred Drake74947ac1998-01-12 15:38:30 +0000166instead. Since the \var{command} argument must be an identifier,
167\samp{help exec} must be entered to get help on the \samp{!} command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000168
Guido van Rossum470be141995-03-17 16:07:09 +0000169\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000170
Fred Drake74947ac1998-01-12 15:38:30 +0000171Print a stack trace, with the most recent frame at the bottom. An
172arrow indicates the current frame, which determines the context of
173most commands.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000174
Guido van Rossum470be141995-03-17 16:07:09 +0000175\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000176
177Move the current frame one level down in the stack trace
178(to an older frame).
179
Guido van Rossum470be141995-03-17 16:07:09 +0000180\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000181
182Move the current frame one level up in the stack trace
183(to a newer frame).
184
Fred Drake31ec33e1999-04-13 21:36:44 +0000185\item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000186
187With a \var{lineno} argument, set a break there in the current
Guido van Rossum64421161998-09-17 15:11:51 +0000188file. With a \var{function} argument, set a break at the first
189executable statement within that function.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000190The line number may be prefixed with a filename and a colon,
191to specify a breakpoint in another file (probably one that
192hasn't been loaded yet). The file is searched on \code{sys.path}.
Guido van Rossum64421161998-09-17 15:11:51 +0000193Note that each breakpoint is assigned a number to which all the other
194breakpoint commands refer.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000195
Guido van Rossum64421161998-09-17 15:11:51 +0000196If a second argument is present, it is an expression which must
197evaluate to true before the breakpoint is honored.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000198
Guido van Rossum64421161998-09-17 15:11:51 +0000199Without argument, list all breaks, including for each breakpoint,
200the number of times that breakpoint has been hit, the current
201ignore count, and the associated condition if any.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000202
Fred Drake31ec33e1999-04-13 21:36:44 +0000203\item[tbreak \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
Guido van Rossum64421161998-09-17 15:11:51 +0000204
205Temporary breakpoint, which is removed automatically when it is
206first hit. The arguments are the same as break.
207
208\item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
209
210With a space separated list of breakpoint numbers, clear those
211breakpoints. Without argument, clear all breaks (but first
212ask confirmation).
213
214\item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
215
216Disables the breakpoints given as a space separated list of
217breakpoint numbers. Disabling a breakpoint means it cannot cause
218the program to stop execution, but unlike clearing a breakpoint, it
219remains in the list of breakpoints and can be (re-)enabled.
220
221\item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
222
223Enables the breakpoints specified.
224
225\item[ignore \var{bpnumber} \optional{\var{count}}]
226
227Sets the ignore count for the given breakpoint number. If
228count is omitted, the ignore count is set to 0. A breakpoint
229becomes active when the ignore count is zero. When non-zero,
230the count is decremented each time the breakpoint is reached
231and the breakpoint is not disabled and any associated condition
232evaluates to true.
233
234\item[condition \var{bpnumber} \optional{\var{condition}}]
235
236Condition is an expression which must evaluate to true before
237the breakpoint is honored. If condition is absent, any existing
238condition is removed; i.e., the breakpoint is made unconditional.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000239
Guido van Rossum470be141995-03-17 16:07:09 +0000240\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000241
242Execute the current line, stop at the first possible occasion
243(either in a function that is called or on the next line in the
244current function).
245
Guido van Rossum470be141995-03-17 16:07:09 +0000246\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000247
248Continue execution until the next line in the current function
Fred Drakec8993aa1999-04-22 16:50:40 +0000249is reached or it returns. (The difference between \samp{next} and
250\samp{step} is that \samp{step} stops inside a called function, while
251\samp{next} executes called functions at (nearly) full speed, only
Guido van Rossumf4aac481995-03-02 12:37:55 +0000252stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000253
Guido van Rossum470be141995-03-17 16:07:09 +0000254\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000255
256Continue execution until the current function returns.
257
Guido van Rossum470be141995-03-17 16:07:09 +0000258\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000259
260Continue execution, only stop when a breakpoint is encountered.
261
Fred Drake74947ac1998-01-12 15:38:30 +0000262\item[l(ist) \optional{\var{first\optional{, last}}}]
Guido van Rossum43b655c1998-09-17 17:07:15 +0000263
Guido van Rossumf4aac481995-03-02 12:37:55 +0000264List source code for the current file. Without arguments, list 11
265lines around the current line or continue the previous listing. With
266one argument, list 11 lines around at that line. With two arguments,
267list the given range; if the second argument is less than the first,
268it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000269
Guido van Rossum470be141995-03-17 16:07:09 +0000270\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000271
272Print the argument list of the current function.
273
Guido van Rossum470be141995-03-17 16:07:09 +0000274\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000275
276Evaluate the \var{expression} in the current context and print its
Fred Drakec8993aa1999-04-22 16:50:40 +0000277value. (Note: \samp{print} can also be used, but is not a debugger
278command --- this executes the Python \keyword{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000279
Guido van Rossum64421161998-09-17 15:11:51 +0000280\item[alias \optional{\var{name} \optional{command}}]
281
282Creates an alias called \var{name} that executes \var{command}. The
283command must \emph{not} be enclosed in quotes. Replaceable parameters
284can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is
285replaced by all the parameters. If no command is given, the current
286alias for \var{name} is shown. If no arguments are given, all
287aliases are listed.
288
289Aliases may be nested and can contain anything that can be
290legally typed at the pdb prompt. Note that internal pdb commands
291\emph{can} be overridden by aliases. Such a command is
292then hidden until the alias is removed. Aliasing is recursively
293applied to the first word of the command line; all other words
294in the line are left alone.
295
296As an example, here are two useful aliases (especially when placed
297in the \file{.pdbrc} file):
298
299\begin{verbatim}
300#Print instance variables (usage "pi classInst")
301alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
302#Print instance variables in self
303alias ps pi self
304\end{verbatim}
305
306\item[unalias \var{name}]
307
308Deletes the specified alias.
309
Fred Drake74947ac1998-01-12 15:38:30 +0000310\item[\optional{!}\var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000311
312Execute the (one-line) \var{statement} in the context of
313the current stack frame.
314The exclamation point can be omitted unless the first word
315of the statement resembles a debugger command.
316To set a global variable, you can prefix the assignment
Fred Drakec8993aa1999-04-22 16:50:40 +0000317command with a \samp{global} command on the same line, e.g.:
Fred Drake82d493f1998-04-07 19:14:17 +0000318
Fred Drake19479911998-02-13 06:58:54 +0000319\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000320(Pdb) global list_options; list_options = ['-l']
321(Pdb)
Fred Drake19479911998-02-13 06:58:54 +0000322\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +0000323
Guido van Rossum470be141995-03-17 16:07:09 +0000324\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000325
326Quit from the debugger.
327The program being executed is aborted.
328
329\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000330
331\section{How It Works}
332
333Some changes were made to the interpreter:
334
335\begin{itemize}
Fred Drake74947ac1998-01-12 15:38:30 +0000336\item \code{sys.settrace(\var{func})} sets the global trace function
Guido van Rossum470be141995-03-17 16:07:09 +0000337\item there can also a local trace function (see later)
338\end{itemize}
339
Fred Drake82d493f1998-04-07 19:14:17 +0000340Trace functions have three arguments: \var{frame}, \var{event}, and
341\var{arg}. \var{frame} is the current stack frame. \var{event} is a
342string: \code{'call'}, \code{'line'}, \code{'return'} or
343\code{'exception'}. \var{arg} depends on the event type.
Guido van Rossum470be141995-03-17 16:07:09 +0000344
Guido van Rossum9d37a4d1997-10-27 19:57:20 +0000345The global trace function is invoked (with \var{event} set to
346\code{'call'}) whenever a new local scope is entered; it should return
347a reference to the local trace function to be used that scope, or
348\code{None} if the scope shouldn't be traced.
349
350The local trace function should return a reference to itself (or to
351another function for further tracing in that scope), or \code{None} to
352turn off tracing in that scope.
353
354Instance methods are accepted (and very useful!) as trace functions.
Guido van Rossum470be141995-03-17 16:07:09 +0000355
356The events have the following meaning:
357
358\begin{description}
359
360\item[\code{'call'}]
361A function is called (or some other code block entered). The global
362trace function is called; arg is the argument list to the function;
363the return value specifies the local trace function.
364
365\item[\code{'line'}]
366The interpreter is about to execute a new line of code (sometimes
367multiple line events on one line exist). The local trace function is
368called; arg in None; the return value specifies the new local trace
369function.
370
371\item[\code{'return'}]
372A function (or other code block) is about to return. The local trace
373function is called; arg is the value that will be returned. The trace
374function's return value is ignored.
375
376\item[\code{'exception'}]
377An exception has occurred. The local trace function is called; arg is
378a triple (exception, value, traceback); the return value specifies the
379new local trace function
380
381\end{description}
382
383Note that as an exception is propagated down the chain of callers, an
384\code{'exception'} event is generated at each level.
385
Fred Drakebc8ad5b1998-03-11 06:29:59 +0000386For more information on code and frame objects, refer to the
387\emph{Python Reference Manual}.