blob: b2db2a2f61cb3ddb44816df702d9785b69d05a5f [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
Guido van Rossum255d7901997-07-11 13:51:17 +00008Python programs. It supports setting
9(conditional) breakpoints and single stepping
Guido van Rossumf4aac481995-03-02 12:37:55 +000010at the source line level, inspection of stack frames, source code
11listing, and evaluation of arbitrary Python code in the context of any
12stack frame. It also supports post-mortem debugging and can be called
13under program control.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000014
15The debugger is extensible --- it is actually defined as a class
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000016\code{Pdb}. This is currently undocumented but easily understood by
17reading the source. The extension interface uses the (also
18undocumented) modules \code{bdb} and \code{cmd}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000019\ttindex{Pdb}
20\ttindex{bdb}
21\ttindex{cmd}
22
23A primitive windowing version of the debugger also exists --- this is
Guido van Rossumf4aac481995-03-02 12:37:55 +000024module \code{wdb}, which requires STDWIN (see the chapter on STDWIN
25specific modules).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000026\index{stdwin}
27\ttindex{wdb}
28
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000029The debugger's prompt is ``\code{(Pdb) }''.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000030Typical usage to run a program under control of the debugger is:
31
Guido van Rossume47da0a1997-07-17 16:34:52 +000032\bcode\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000033>>> import pdb
34>>> import mymodule
35>>> pdb.run('mymodule.test()')
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000036> <string>(0)?()
37(Pdb) continue
38> <string>(1)?()
39(Pdb) continue
40NameError: 'spam'
41> <string>(1)?()
42(Pdb)
Guido van Rossume47da0a1997-07-17 16:34:52 +000043\end{verbatim}\ecode
44%
Guido van Rossum809408e1997-06-02 17:28:16 +000045\code{pdb.py} can also be invoked as
46a script to debug other scripts. For example:
47\code{python /usr/local/lib/python1.4/pdb.py myscript.py}
48
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000049Typical usage to inspect a crashed program is:
50
Guido van Rossume47da0a1997-07-17 16:34:52 +000051\bcode\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)
Guido van Rossume47da0a1997-07-17 16:34:52 +000066\end{verbatim}\ecode
67%
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000068The module defines the following functions; each enters the debugger
69in a slightly different way:
70
71\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
Guido van Rossumf4aac481995-03-02 12:37:55 +000074can set breakpoints and type \code{continue}, or you can step through
75the statement using \code{step} or \code{next} (all these commands are
76explained below). The optional \var{globals} and \var{locals}
77arguments specify the environment in which the code is executed; by
78default the dictionary of the module \code{__main__} is used. (See
79the explanation of the \code{exec} statement or the \code{eval()}
80built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000081\end{funcdesc}
82
83\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
85control. When \code{runeval()} returns, it returns the value of the
86expression. Otherwise this function is similar to
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000087\code{run()}.
88\end{funcdesc}
89
90\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)
92with the given arguments. When \code{runcall()} returns, it returns
93whatever 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
Guido van Rossum470be141995-03-17 16:07:09 +0000112\section{Debugger Commands}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000113
114The debugger recognizes the following commands. Most commands can be
115abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that
116either ``\code{h}'' or ``\code{help}'' can be used to enter the help
117command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or
118``\code{Help} or ``\code{HELP}''). Arguments to commands must be
119separated by whitespace (spaces or tabs). Optional arguments are
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000120enclosed in square brackets (``\code{[]}'') in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000121square brackets must not be typed. Alternatives in the command syntax
122are separated by a vertical bar (``\code{|}'').
123
124Entering a blank line repeats the last command entered. Exception: if
125the last command was a ``\code{list}'' command, the next 11 lines are
126listed.
127
128Commands that the debugger doesn't recognize are assumed to be Python
129statements and are executed in the context of the program being
130debugged. Python statements can also be prefixed with an exclamation
131point (``\code{!}''). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000132being debugged; it is even possible to change a variable or call a
133function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000134exception occurs in such a statement, the exception name is printed
135but the debugger's state is not changed.
136
137\begin{description}
138
Fred Drake74947ac1998-01-12 15:38:30 +0000139\item[h(elp) \optional{\var{command}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000140
Fred Drake74947ac1998-01-12 15:38:30 +0000141Without argument, print the list of available commands. With a
142\var{command} as argument, print help about that command. \samp{help
143pdb} displays the full documentation file; if the environment variable
144\code{PAGER} is defined, the file is piped through that command
145instead. Since the \var{command} argument must be an identifier,
146\samp{help exec} must be entered to get help on the \samp{!} command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000147
Guido van Rossum470be141995-03-17 16:07:09 +0000148\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000149
Fred Drake74947ac1998-01-12 15:38:30 +0000150Print a stack trace, with the most recent frame at the bottom. An
151arrow indicates the current frame, which determines the context of
152most commands.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000153
Guido van Rossum470be141995-03-17 16:07:09 +0000154\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000155
156Move the current frame one level down in the stack trace
157(to an older frame).
158
Guido van Rossum470be141995-03-17 16:07:09 +0000159\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000160
161Move the current frame one level up in the stack trace
162(to a newer frame).
163
Fred Drake74947ac1998-01-12 15:38:30 +0000164\item[b(reak) \optional{\var{lineno}{\Large\code{|}}\var{function}%
165 \optional{, \code{'}\var{condition}\code{'}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000166
167With a \var{lineno} argument, set a break there in the current
168file. With a \var{function} argument, set a break at the entry of
169that function. Without argument, list all breaks.
Guido van Rossum31cbc841997-07-11 13:57:28 +0000170If a second argument is present, it is a string (included in string
171quotes!) specifying an expression which must evaluate to true before
172the breakpoint is honored.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000173
Fred Drake74947ac1998-01-12 15:38:30 +0000174\item[cl(ear) \optional{\var{lineno}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000175
176With a \var{lineno} argument, clear that break in the current file.
177Without argument, clear all breaks (but first ask confirmation).
178
Guido van Rossum470be141995-03-17 16:07:09 +0000179\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000180
181Execute the current line, stop at the first possible occasion
182(either in a function that is called or on the next line in the
183current function).
184
Guido van Rossum470be141995-03-17 16:07:09 +0000185\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000186
187Continue execution until the next line in the current function
188is reached or it returns. (The difference between \code{next} and
189\code{step} is that \code{step} stops inside a called function, while
Guido van Rossumf4aac481995-03-02 12:37:55 +0000190\code{next} executes called functions at (nearly) full speed, only
191stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000192
Guido van Rossum470be141995-03-17 16:07:09 +0000193\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000194
195Continue execution until the current function returns.
196
Guido van Rossum470be141995-03-17 16:07:09 +0000197\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000198
199Continue execution, only stop when a breakpoint is encountered.
200
Fred Drake74947ac1998-01-12 15:38:30 +0000201\item[l(ist) \optional{\var{first\optional{, last}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000202
Guido van Rossumf4aac481995-03-02 12:37:55 +0000203List source code for the current file. Without arguments, list 11
204lines around the current line or continue the previous listing. With
205one argument, list 11 lines around at that line. With two arguments,
206list the given range; if the second argument is less than the first,
207it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000208
Guido van Rossum470be141995-03-17 16:07:09 +0000209\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000210
211Print the argument list of the current function.
212
Guido van Rossum470be141995-03-17 16:07:09 +0000213\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000214
215Evaluate the \var{expression} in the current context and print its
Guido van Rossumf4aac481995-03-02 12:37:55 +0000216value. (Note: \code{print} can also be used, but is not a debugger
217command --- this executes the Python \code{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000218
Fred Drake74947ac1998-01-12 15:38:30 +0000219\item[\optional{!}\var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000220
221Execute the (one-line) \var{statement} in the context of
222the current stack frame.
223The exclamation point can be omitted unless the first word
224of the statement resembles a debugger command.
225To set a global variable, you can prefix the assignment
226command with a ``\code{global}'' command on the same line, e.g.:
Guido van Rossume47da0a1997-07-17 16:34:52 +0000227\bcode\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000228(Pdb) global list_options; list_options = ['-l']
229(Pdb)
Guido van Rossume47da0a1997-07-17 16:34:52 +0000230\end{verbatim}\ecode
231%
Guido van Rossum470be141995-03-17 16:07:09 +0000232\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000233
234Quit from the debugger.
235The program being executed is aborted.
236
237\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000238
239\section{How It Works}
240
241Some changes were made to the interpreter:
242
243\begin{itemize}
Fred Drake74947ac1998-01-12 15:38:30 +0000244\item \code{sys.settrace(\var{func})} sets the global trace function
Guido van Rossum470be141995-03-17 16:07:09 +0000245\item there can also a local trace function (see later)
246\end{itemize}
247
248Trace functions have three arguments: (\var{frame}, \var{event}, \var{arg})
249
250\begin{description}
251
252\item[\var{frame}] is the current stack frame
253
254\item[\var{event}] is a string: \code{'call'}, \code{'line'}, \code{'return'}
255or \code{'exception'}
256
257\item[\var{arg}] is dependent on the event type
258
259\end{description}
260
Guido van Rossum9d37a4d1997-10-27 19:57:20 +0000261The global trace function is invoked (with \var{event} set to
262\code{'call'}) whenever a new local scope is entered; it should return
263a reference to the local trace function to be used that scope, or
264\code{None} if the scope shouldn't be traced.
265
266The local trace function should return a reference to itself (or to
267another function for further tracing in that scope), or \code{None} to
268turn off tracing in that scope.
269
270Instance methods are accepted (and very useful!) as trace functions.
Guido van Rossum470be141995-03-17 16:07:09 +0000271
272The events have the following meaning:
273
274\begin{description}
275
276\item[\code{'call'}]
277A function is called (or some other code block entered). The global
278trace function is called; arg is the argument list to the function;
279the return value specifies the local trace function.
280
281\item[\code{'line'}]
282The interpreter is about to execute a new line of code (sometimes
283multiple line events on one line exist). The local trace function is
284called; arg in None; the return value specifies the new local trace
285function.
286
287\item[\code{'return'}]
288A function (or other code block) is about to return. The local trace
289function is called; arg is the value that will be returned. The trace
290function's return value is ignored.
291
292\item[\code{'exception'}]
293An exception has occurred. The local trace function is called; arg is
294a triple (exception, value, traceback); the return value specifies the
295new local trace function
296
297\end{description}
298
299Note that as an exception is propagated down the chain of callers, an
300\code{'exception'} event is generated at each level.
301
302Stack frame objects have the following read-only attributes:
303
304\begin{description}
305\item[f_code] the code object being executed
306\item[f_lineno] the current line number (\code{-1} for \code{'call'} events)
307\item[f_back] the stack frame of the caller, or None
308\item[f_locals] dictionary containing local name bindings
309\item[f_globals] dictionary containing global name bindings
310\end{description}
311
312Code objects have the following read-only attributes:
313
314\begin{description}
315\item[co_code] the code string
316\item[co_names] the list of names used by the code
317\item[co_consts] the list of (literal) constants used by the code
318\item[co_filename] the filename from which the code was compiled
319\end{description}