blob: 54a3f58d06e8de7ddf91509b5e89ef5316c68c46 [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
Guido van Rossum470be141995-03-17 16:07:09 +000094\section{Debugger Commands}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000095
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
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000102enclosed in square brackets (``\code{[]}'') in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000103square 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
Guido van Rossum470be141995-03-17 16:07:09 +0000120\item[h(elp) [\var{command}]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000121
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
Guido van Rossum470be141995-03-17 16:07:09 +0000130\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000131
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
Guido van Rossum470be141995-03-17 16:07:09 +0000136\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000137
138Move the current frame one level down in the stack trace
139(to an older frame).
140
Guido van Rossum470be141995-03-17 16:07:09 +0000141\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000142
143Move the current frame one level up in the stack trace
144(to a newer frame).
145
Guido van Rossum470be141995-03-17 16:07:09 +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 Rossum470be141995-03-17 16:07:09 +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
Guido van Rossum470be141995-03-17 16:07:09 +0000157\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000158
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
Guido van Rossum470be141995-03-17 16:07:09 +0000163\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000164
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
Guido van Rossum470be141995-03-17 16:07:09 +0000171\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000172
173Continue execution until the current function returns.
174
Guido van Rossum470be141995-03-17 16:07:09 +0000175\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000176
177Continue execution, only stop when a breakpoint is encountered.
178
Guido van Rossum470be141995-03-17 16:07:09 +0000179\item[l(ist) [\var{first} [, \var{last}]]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000180
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
Guido van Rossum470be141995-03-17 16:07:09 +0000187\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000188
189Print the argument list of the current function.
190
Guido van Rossum470be141995-03-17 16:07:09 +0000191\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000192
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
Guido van Rossum470be141995-03-17 16:07:09 +0000197\item[[!] \var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000198
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
Guido van Rossum470be141995-03-17 16:07:09 +0000210\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000211
212Quit from the debugger.
213The program being executed is aborted.
214
215\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000216
217\section{How It Works}
218
219Some changes were made to the interpreter:
220
221\begin{itemize}
222\item sys.settrace(func) sets the global trace function
223\item there can also a local trace function (see later)
224\end{itemize}
225
226Trace functions have three arguments: (\var{frame}, \var{event}, \var{arg})
227
228\begin{description}
229
230\item[\var{frame}] is the current stack frame
231
232\item[\var{event}] is a string: \code{'call'}, \code{'line'}, \code{'return'}
233or \code{'exception'}
234
235\item[\var{arg}] is dependent on the event type
236
237\end{description}
238
239A trace function should return a new trace function or None.
240Class methods are accepted (and most useful!) as trace methods.
241
242The events have the following meaning:
243
244\begin{description}
245
246\item[\code{'call'}]
247A function is called (or some other code block entered). The global
248trace function is called; arg is the argument list to the function;
249the return value specifies the local trace function.
250
251\item[\code{'line'}]
252The interpreter is about to execute a new line of code (sometimes
253multiple line events on one line exist). The local trace function is
254called; arg in None; the return value specifies the new local trace
255function.
256
257\item[\code{'return'}]
258A function (or other code block) is about to return. The local trace
259function is called; arg is the value that will be returned. The trace
260function's return value is ignored.
261
262\item[\code{'exception'}]
263An exception has occurred. The local trace function is called; arg is
264a triple (exception, value, traceback); the return value specifies the
265new local trace function
266
267\end{description}
268
269Note that as an exception is propagated down the chain of callers, an
270\code{'exception'} event is generated at each level.
271
272Stack frame objects have the following read-only attributes:
273
274\begin{description}
275\item[f_code] the code object being executed
276\item[f_lineno] the current line number (\code{-1} for \code{'call'} events)
277\item[f_back] the stack frame of the caller, or None
278\item[f_locals] dictionary containing local name bindings
279\item[f_globals] dictionary containing global name bindings
280\end{description}
281
282Code objects have the following read-only attributes:
283
284\begin{description}
285\item[co_code] the code string
286\item[co_names] the list of names used by the code
287\item[co_consts] the list of (literal) constants used by the code
288\item[co_filename] the filename from which the code was compiled
289\end{description}