blob: ea03abf4e05dd1a3c477dba424604e62ec453cac [file] [log] [blame]
Guido van Rossumf4aac481995-03-02 12:37:55 +00001\chapter{The Python Debugger}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +00002\stmodindex{pdb}
3\index{debugging}
4
Guido van Rossumf4aac481995-03-02 12:37:55 +00005\renewcommand{\indexsubitem}{(in module pdb)}
6
7The module \code{pdb} defines an interactive source code debugger for
8Python programs. It supports setting breakpoints and single stepping
9at the source line level, inspection of stack frames, source code
10listing, and evaluation of arbitrary Python code in the context of any
11stack frame. It also supports post-mortem debugging and can be called
12under program control.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000013
14The debugger is extensible --- it is actually defined as a class
15\code{Pdb}. The extension interface uses the (also undocumented)
Guido van Rossumf4aac481995-03-02 12:37:55 +000016modules \code{bdb} and \code{cmd}; it is currently undocumented but
17easily understood by reading the source.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000018\ttindex{Pdb}
19\ttindex{bdb}
20\ttindex{cmd}
21
22A primitive windowing version of the debugger also exists --- this is
Guido van Rossumf4aac481995-03-02 12:37:55 +000023module \code{wdb}, which requires STDWIN (see the chapter on STDWIN
24specific modules).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000025\index{stdwin}
26\ttindex{wdb}
27
28Typical usage to run a program under control of the debugger is:
29
30\begin{verbatim}
31>>> import pdb
32>>> import mymodule
33>>> pdb.run('mymodule.test()')
34(Pdb)
35\end{verbatim}
36
37Typical usage to inspect a crashed program is:
38
39\begin{verbatim}
40>>> import pdb
41>>> import mymodule
42>>> mymodule.test()
43(crashes with a stack trace)
44>>> pdb.pm()
45(Pdb)
46\end{verbatim}
47
48The debugger's prompt is ``\code{(Pdb) }''.
49
50The module defines the following functions; each enters the debugger
51in a slightly different way:
52
53\begin{funcdesc}{run}{statement\optional{\, globals\optional{\, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000054Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000055control. The debugger prompt appears before any code is executed; you
Guido van Rossumf4aac481995-03-02 12:37:55 +000056can set breakpoints and type \code{continue}, or you can step through
57the statement using \code{step} or \code{next} (all these commands are
58explained below). The optional \var{globals} and \var{locals}
59arguments specify the environment in which the code is executed; by
60default the dictionary of the module \code{__main__} is used. (See
61the explanation of the \code{exec} statement or the \code{eval()}
62built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000063\end{funcdesc}
64
65\begin{funcdesc}{runeval}{expression\optional{\, globals\optional{\, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000066Evaluate the \var{expression} (given as a a string) under debugger
67control. When \code{runeval()} returns, it returns the value of the
68expression. Otherwise this function is similar to
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000069\code{run()}.
70\end{funcdesc}
71
72\begin{funcdesc}{runcall}{function\optional{\, argument\, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000073Call the \var{function} (a function or method object, not a string)
74with the given arguments. When \code{runcall()} returns, it returns
75whatever the function call returned. The debugger prompt appears as
76soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000077\end{funcdesc}
78
79\begin{funcdesc}{set_trace}{}
80Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +000081hard-code a breakpoint at a given point in a program, even if the code
82is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000083\end{funcdesc}
84
85\begin{funcdesc}{post_mortem}{traceback}
86Enter post-mortem debugging of the given \var{traceback} object.
87\end{funcdesc}
88
89\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +000090Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000091\code{sys.last_traceback}.
92\end{funcdesc}
93
94\subsection{Debugger Commands}
95
96The debugger recognizes the following commands. Most commands can be
97abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that
98either ``\code{h}'' or ``\code{help}'' can be used to enter the help
99command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or
100``\code{Help} or ``\code{HELP}''). Arguments to commands must be
101separated by whitespace (spaces or tabs). Optional arguments are
102enclosed in square brackets (``\code{[]}'')in the command syntax; the
103square brackets must not be typed. Alternatives in the command syntax
104are separated by a vertical bar (``\code{|}'').
105
106Entering a blank line repeats the last command entered. Exception: if
107the last command was a ``\code{list}'' command, the next 11 lines are
108listed.
109
110Commands that the debugger doesn't recognize are assumed to be Python
111statements and are executed in the context of the program being
112debugged. Python statements can also be prefixed with an exclamation
113point (``\code{!}''). This is a powerful way to inspect the program
114being debugged; it is even possible to change variables. When an
115exception occurs in such a statement, the exception name is printed
116but the debugger's state is not changed.
117
118\begin{description}
119
120\item[{h(elp) [\var{command}]}]
121
122Without argument, print the list of available commands.
123With a \var{command} as argument, print help about that command.
124``\code{help pdb}'' displays the full documentation file; if the
125environment variable \code{PAGER} is defined, the file is piped
Guido van Rossumf4aac481995-03-02 12:37:55 +0000126through that command instead. Since the \var{command} argument must be
127an identifier, ``\code{help exec}'' must be entered to get help on the
128``\code{!}'' command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000129
130\item[{w(here)}]
131
132Print a stack trace, with the most recent frame at the bottom.
133An arrow indicates the current frame, which determines the
134context of most commands.
135
136\item[{d(own)}]
137
138Move the current frame one level down in the stack trace
139(to an older frame).
140
141\item[{u(p)}]
142
143Move the current frame one level up in the stack trace
144(to a newer frame).
145
Guido van Rossumf4aac481995-03-02 12:37:55 +0000146\item[{b(reak) [\var{lineno}\code{|}\var{function}]}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000147
148With a \var{lineno} argument, set a break there in the current
149file. With a \var{function} argument, set a break at the entry of
150that function. Without argument, list all breaks.
151
Guido van Rossumf4aac481995-03-02 12:37:55 +0000152\item[{cl(ear) [\var{lineno}]}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000153
154With a \var{lineno} argument, clear that break in the current file.
155Without argument, clear all breaks (but first ask confirmation).
156
157\item[{s(tep)}]
158
159Execute the current line, stop at the first possible occasion
160(either in a function that is called or on the next line in the
161current function).
162
163\item[{n(ext)}]
164
165Continue execution until the next line in the current function
166is reached or it returns. (The difference between \code{next} and
167\code{step} is that \code{step} stops inside a called function, while
Guido van Rossumf4aac481995-03-02 12:37:55 +0000168\code{next} executes called functions at (nearly) full speed, only
169stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000170
171\item[{r(eturn)}]
172
173Continue execution until the current function returns.
174
175\item[{c(ont(inue))}]
176
177Continue execution, only stop when a breakpoint is encountered.
178
179\item[{l(ist) [\var{first} [, \var{last}]]}]
180
Guido van Rossumf4aac481995-03-02 12:37:55 +0000181List source code for the current file. Without arguments, list 11
182lines around the current line or continue the previous listing. With
183one argument, list 11 lines around at that line. With two arguments,
184list the given range; if the second argument is less than the first,
185it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000186
187\item[{a(rgs)}]
188
189Print the argument list of the current function.
190
191\item[{p \var{expression}}]
192
193Evaluate the \var{expression} in the current context and print its
Guido van Rossumf4aac481995-03-02 12:37:55 +0000194value. (Note: \code{print} can also be used, but is not a debugger
195command --- this executes the Python \code{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000196
197\item[{[!] \var{statement}}]
198
199Execute the (one-line) \var{statement} in the context of
200the current stack frame.
201The exclamation point can be omitted unless the first word
202of the statement resembles a debugger command.
203To set a global variable, you can prefix the assignment
204command with a ``\code{global}'' command on the same line, e.g.:
205\begin{verbatim}
206(Pdb) global list_options; list_options = ['-l']
207(Pdb)
208\end{verbatim}
209
210\item[{q(uit)}]
211
212Quit from the debugger.
213The program being executed is aborted.
214
215\end{description}