blob: a439363734dbecac7f7efce1abd89f2365d3ddbb [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
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000015\code{Pdb}. This is currently undocumented but easily understood by
16reading the source. The extension interface uses the (also
17undocumented) modules \code{bdb} and \code{cmd}.
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
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000028The debugger's prompt is ``\code{(Pdb) }''.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000029Typical usage to run a program under control of the debugger is:
30
31\begin{verbatim}
32>>> import pdb
33>>> import mymodule
34>>> pdb.run('mymodule.test()')
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000035> <string>(0)?()
36(Pdb) continue
37> <string>(1)?()
38(Pdb) continue
39NameError: 'spam'
40> <string>(1)?()
41(Pdb)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000042\end{verbatim}
43
44Typical usage to inspect a crashed program is:
45
46\begin{verbatim}
47>>> import pdb
48>>> import mymodule
49>>> mymodule.test()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000050Traceback (innermost last):
51 File "<stdin>", line 1, in ?
52 File "./mymodule.py", line 4, in test
53 test2()
54 File "./mymodule.py", line 3, in test2
55 print spam
56NameError: spam
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000057>>> pdb.pm()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000058> ./mymodule.py(3)test2()
59-> print spam
60(Pdb)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000061\end{verbatim}
62
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000063The module defines the following functions; each enters the debugger
64in a slightly different way:
65
66\begin{funcdesc}{run}{statement\optional{\, globals\optional{\, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000067Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000068control. The debugger prompt appears before any code is executed; you
Guido van Rossumf4aac481995-03-02 12:37:55 +000069can set breakpoints and type \code{continue}, or you can step through
70the statement using \code{step} or \code{next} (all these commands are
71explained below). The optional \var{globals} and \var{locals}
72arguments specify the environment in which the code is executed; by
73default the dictionary of the module \code{__main__} is used. (See
74the explanation of the \code{exec} statement or the \code{eval()}
75built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000076\end{funcdesc}
77
78\begin{funcdesc}{runeval}{expression\optional{\, globals\optional{\, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000079Evaluate the \var{expression} (given as a a string) under debugger
80control. When \code{runeval()} returns, it returns the value of the
81expression. Otherwise this function is similar to
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000082\code{run()}.
83\end{funcdesc}
84
85\begin{funcdesc}{runcall}{function\optional{\, argument\, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000086Call the \var{function} (a function or method object, not a string)
87with the given arguments. When \code{runcall()} returns, it returns
88whatever the function call returned. The debugger prompt appears as
89soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000090\end{funcdesc}
91
92\begin{funcdesc}{set_trace}{}
93Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +000094hard-code a breakpoint at a given point in a program, even if the code
95is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000096\end{funcdesc}
97
98\begin{funcdesc}{post_mortem}{traceback}
99Enter post-mortem debugging of the given \var{traceback} object.
100\end{funcdesc}
101
102\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +0000103Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000104\code{sys.last_traceback}.
105\end{funcdesc}
106
Guido van Rossum470be141995-03-17 16:07:09 +0000107\section{Debugger Commands}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000108
109The debugger recognizes the following commands. Most commands can be
110abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that
111either ``\code{h}'' or ``\code{help}'' can be used to enter the help
112command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or
113``\code{Help} or ``\code{HELP}''). Arguments to commands must be
114separated by whitespace (spaces or tabs). Optional arguments are
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000115enclosed in square brackets (``\code{[]}'') in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000116square brackets must not be typed. Alternatives in the command syntax
117are separated by a vertical bar (``\code{|}'').
118
119Entering a blank line repeats the last command entered. Exception: if
120the last command was a ``\code{list}'' command, the next 11 lines are
121listed.
122
123Commands that the debugger doesn't recognize are assumed to be Python
124statements and are executed in the context of the program being
125debugged. Python statements can also be prefixed with an exclamation
126point (``\code{!}''). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000127being debugged; it is even possible to change a variable or call a
128function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000129exception occurs in such a statement, the exception name is printed
130but the debugger's state is not changed.
131
132\begin{description}
133
Guido van Rossum470be141995-03-17 16:07:09 +0000134\item[h(elp) [\var{command}]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000135
136Without argument, print the list of available commands.
137With a \var{command} as argument, print help about that command.
138``\code{help pdb}'' displays the full documentation file; if the
139environment variable \code{PAGER} is defined, the file is piped
Guido van Rossumf4aac481995-03-02 12:37:55 +0000140through that command instead. Since the \var{command} argument must be
141an identifier, ``\code{help exec}'' must be entered to get help on the
142``\code{!}'' command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000143
Guido van Rossum470be141995-03-17 16:07:09 +0000144\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000145
146Print a stack trace, with the most recent frame at the bottom.
147An arrow indicates the current frame, which determines the
148context of most commands.
149
Guido van Rossum470be141995-03-17 16:07:09 +0000150\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000151
152Move the current frame one level down in the stack trace
153(to an older frame).
154
Guido van Rossum470be141995-03-17 16:07:09 +0000155\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000156
157Move the current frame one level up in the stack trace
158(to a newer frame).
159
Guido van Rossum470be141995-03-17 16:07:09 +0000160\item[b(reak) [\var{lineno}\code{|}\var{function}]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000161
162With a \var{lineno} argument, set a break there in the current
163file. With a \var{function} argument, set a break at the entry of
164that function. Without argument, list all breaks.
165
Guido van Rossum470be141995-03-17 16:07:09 +0000166\item[cl(ear) [\var{lineno}]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000167
168With a \var{lineno} argument, clear that break in the current file.
169Without argument, clear all breaks (but first ask confirmation).
170
Guido van Rossum470be141995-03-17 16:07:09 +0000171\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000172
173Execute the current line, stop at the first possible occasion
174(either in a function that is called or on the next line in the
175current function).
176
Guido van Rossum470be141995-03-17 16:07:09 +0000177\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000178
179Continue execution until the next line in the current function
180is reached or it returns. (The difference between \code{next} and
181\code{step} is that \code{step} stops inside a called function, while
Guido van Rossumf4aac481995-03-02 12:37:55 +0000182\code{next} executes called functions at (nearly) full speed, only
183stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000184
Guido van Rossum470be141995-03-17 16:07:09 +0000185\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000186
187Continue execution until the current function returns.
188
Guido van Rossum470be141995-03-17 16:07:09 +0000189\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000190
191Continue execution, only stop when a breakpoint is encountered.
192
Guido van Rossum470be141995-03-17 16:07:09 +0000193\item[l(ist) [\var{first} [, \var{last}]]]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000194
Guido van Rossumf4aac481995-03-02 12:37:55 +0000195List source code for the current file. Without arguments, list 11
196lines around the current line or continue the previous listing. With
197one argument, list 11 lines around at that line. With two arguments,
198list the given range; if the second argument is less than the first,
199it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000200
Guido van Rossum470be141995-03-17 16:07:09 +0000201\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000202
203Print the argument list of the current function.
204
Guido van Rossum470be141995-03-17 16:07:09 +0000205\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000206
207Evaluate the \var{expression} in the current context and print its
Guido van Rossumf4aac481995-03-02 12:37:55 +0000208value. (Note: \code{print} can also be used, but is not a debugger
209command --- this executes the Python \code{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000210
Guido van Rossum470be141995-03-17 16:07:09 +0000211\item[[!] \var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000212
213Execute the (one-line) \var{statement} in the context of
214the current stack frame.
215The exclamation point can be omitted unless the first word
216of the statement resembles a debugger command.
217To set a global variable, you can prefix the assignment
218command with a ``\code{global}'' command on the same line, e.g.:
219\begin{verbatim}
220(Pdb) global list_options; list_options = ['-l']
221(Pdb)
222\end{verbatim}
223
Guido van Rossum470be141995-03-17 16:07:09 +0000224\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000225
226Quit from the debugger.
227The program being executed is aborted.
228
229\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000230
231\section{How It Works}
232
233Some changes were made to the interpreter:
234
235\begin{itemize}
236\item sys.settrace(func) sets the global trace function
237\item there can also a local trace function (see later)
238\end{itemize}
239
240Trace functions have three arguments: (\var{frame}, \var{event}, \var{arg})
241
242\begin{description}
243
244\item[\var{frame}] is the current stack frame
245
246\item[\var{event}] is a string: \code{'call'}, \code{'line'}, \code{'return'}
247or \code{'exception'}
248
249\item[\var{arg}] is dependent on the event type
250
251\end{description}
252
253A trace function should return a new trace function or None.
254Class methods are accepted (and most useful!) as trace methods.
255
256The events have the following meaning:
257
258\begin{description}
259
260\item[\code{'call'}]
261A function is called (or some other code block entered). The global
262trace function is called; arg is the argument list to the function;
263the return value specifies the local trace function.
264
265\item[\code{'line'}]
266The interpreter is about to execute a new line of code (sometimes
267multiple line events on one line exist). The local trace function is
268called; arg in None; the return value specifies the new local trace
269function.
270
271\item[\code{'return'}]
272A function (or other code block) is about to return. The local trace
273function is called; arg is the value that will be returned. The trace
274function's return value is ignored.
275
276\item[\code{'exception'}]
277An exception has occurred. The local trace function is called; arg is
278a triple (exception, value, traceback); the return value specifies the
279new local trace function
280
281\end{description}
282
283Note that as an exception is propagated down the chain of callers, an
284\code{'exception'} event is generated at each level.
285
286Stack frame objects have the following read-only attributes:
287
288\begin{description}
289\item[f_code] the code object being executed
290\item[f_lineno] the current line number (\code{-1} for \code{'call'} events)
291\item[f_back] the stack frame of the caller, or None
292\item[f_locals] dictionary containing local name bindings
293\item[f_globals] dictionary containing global name bindings
294\end{description}
295
296Code objects have the following read-only attributes:
297
298\begin{description}
299\item[co_code] the code string
300\item[co_names] the list of names used by the code
301\item[co_consts] the list of (literal) constants used by the code
302\item[co_filename] the filename from which the code was compiled
303\end{description}