blob: 5a19ea56ea00eefab891e64246363ac71e624b36 [file] [log] [blame]
Guido van Rossumf4aac481995-03-02 12:37:55 +00001\chapter{The Python Debugger}
Fred Drake31ecd501998-02-18 15:40:11 +00002\label{module-pdb}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +00003\stmodindex{pdb}
4\index{debugging}
5
Guido van Rossumf4aac481995-03-02 12:37:55 +00006
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
Fred Drake82d493f1998-04-07 19:14:17 +000016\class{Pdb}.
17\withsubitem{(class in pdb)}{\ttindex{Pdb}}
18This is currently undocumented but easily understood by reading the
19source. The extension interface uses the (also undocumented) modules
20\module{bdb}\refstmodindex{bdb} and \module{cmd}\refstmodindex{cmd}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000021
22A primitive windowing version of the debugger also exists --- this is
Fred Drake82d493f1998-04-07 19:14:17 +000023module \module{wdb}, which requires \module{stdwin} (see the chapter
24on STDWIN specific modules).
25\refbimodindex{stdwin}
26\refstmodindex{wdb}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000027
Fred Drake82d493f1998-04-07 19:14:17 +000028The debugger's prompt is \samp{(Pdb) }.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000029Typical usage to run a program under control of the debugger is:
30
Fred Drake19479911998-02-13 06:58:54 +000031\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000032>>> 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)
Fred Drake19479911998-02-13 06:58:54 +000042\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000043
44\file{pdb.py} can also be invoked as
Guido van Rossum809408e1997-06-02 17:28:16 +000045a script to debug other scripts. For example:
Fred Drake82d493f1998-04-07 19:14:17 +000046
47\begin{verbatim}
48python /usr/local/lib/python1.5/pdb.py myscript.py
49\end{verbatim}
Guido van Rossum809408e1997-06-02 17:28:16 +000050
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000051Typical usage to inspect a crashed program is:
52
Fred Drake19479911998-02-13 06:58:54 +000053\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000054>>> import pdb
55>>> import mymodule
56>>> mymodule.test()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000057Traceback (innermost last):
58 File "<stdin>", line 1, in ?
59 File "./mymodule.py", line 4, in test
60 test2()
61 File "./mymodule.py", line 3, in test2
62 print spam
63NameError: spam
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000064>>> pdb.pm()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000065> ./mymodule.py(3)test2()
66-> print spam
67(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000068\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000069
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000070The module defines the following functions; each enters the debugger
71in a slightly different way:
72
Fred Drakecce10901998-03-17 06:33:25 +000073\begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000074Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000075control. The debugger prompt appears before any code is executed; you
Guido van Rossumf4aac481995-03-02 12:37:55 +000076can set breakpoints and type \code{continue}, or you can step through
77the statement using \code{step} or \code{next} (all these commands are
78explained below). The optional \var{globals} and \var{locals}
79arguments specify the environment in which the code is executed; by
80default the dictionary of the module \code{__main__} is used. (See
81the explanation of the \code{exec} statement or the \code{eval()}
82built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000083\end{funcdesc}
84
Fred Drakecce10901998-03-17 06:33:25 +000085\begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000086Evaluate the \var{expression} (given as a a string) under debugger
87control. When \code{runeval()} returns, it returns the value of the
88expression. Otherwise this function is similar to
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000089\code{run()}.
90\end{funcdesc}
91
Fred Drakecce10901998-03-17 06:33:25 +000092\begin{funcdesc}{runcall}{function\optional{, argument, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000093Call the \var{function} (a function or method object, not a string)
94with the given arguments. When \code{runcall()} returns, it returns
95whatever the function call returned. The debugger prompt appears as
96soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000097\end{funcdesc}
98
99\begin{funcdesc}{set_trace}{}
100Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +0000101hard-code a breakpoint at a given point in a program, even if the code
102is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000103\end{funcdesc}
104
105\begin{funcdesc}{post_mortem}{traceback}
106Enter post-mortem debugging of the given \var{traceback} object.
107\end{funcdesc}
108
109\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +0000110Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000111\code{sys.last_traceback}.
112\end{funcdesc}
113
Guido van Rossum470be141995-03-17 16:07:09 +0000114\section{Debugger Commands}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000115
116The debugger recognizes the following commands. Most commands can be
117abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that
118either ``\code{h}'' or ``\code{help}'' can be used to enter the help
119command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or
120``\code{Help} or ``\code{HELP}''). Arguments to commands must be
121separated by whitespace (spaces or tabs). Optional arguments are
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000122enclosed in square brackets (``\code{[]}'') in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000123square brackets must not be typed. Alternatives in the command syntax
124are separated by a vertical bar (``\code{|}'').
125
126Entering a blank line repeats the last command entered. Exception: if
127the last command was a ``\code{list}'' command, the next 11 lines are
128listed.
129
130Commands that the debugger doesn't recognize are assumed to be Python
131statements and are executed in the context of the program being
132debugged. Python statements can also be prefixed with an exclamation
133point (``\code{!}''). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000134being debugged; it is even possible to change a variable or call a
135function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000136exception occurs in such a statement, the exception name is printed
137but the debugger's state is not changed.
138
139\begin{description}
140
Fred Drake74947ac1998-01-12 15:38:30 +0000141\item[h(elp) \optional{\var{command}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000142
Fred Drake74947ac1998-01-12 15:38:30 +0000143Without argument, print the list of available commands. With a
144\var{command} as argument, print help about that command. \samp{help
145pdb} displays the full documentation file; if the environment variable
146\code{PAGER} is defined, the file is piped through that command
147instead. Since the \var{command} argument must be an identifier,
148\samp{help exec} must be entered to get help on the \samp{!} command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000149
Guido van Rossum470be141995-03-17 16:07:09 +0000150\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000151
Fred Drake74947ac1998-01-12 15:38:30 +0000152Print a stack trace, with the most recent frame at the bottom. An
153arrow indicates the current frame, which determines the context of
154most commands.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000155
Guido van Rossum470be141995-03-17 16:07:09 +0000156\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000157
158Move the current frame one level down in the stack trace
159(to an older frame).
160
Guido van Rossum470be141995-03-17 16:07:09 +0000161\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000162
163Move the current frame one level up in the stack trace
164(to a newer frame).
165
Guido van Rossum897b9f01998-07-20 23:29:10 +0000166\item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}%
167 \code{\Large|}\var{function}%
Fred Drake74947ac1998-01-12 15:38:30 +0000168 \optional{, \code{'}\var{condition}\code{'}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000169
170With a \var{lineno} argument, set a break there in the current
171file. With a \var{function} argument, set a break at the entry of
172that function. Without argument, list all breaks.
Guido van Rossum31cbc841997-07-11 13:57:28 +0000173If a second argument is present, it is a string (included in string
174quotes!) specifying an expression which must evaluate to true before
175the breakpoint is honored.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000176
Guido van Rossum897b9f01998-07-20 23:29:10 +0000177The line number may be prefixed with a filename and a colon,
178to specify a breakpoint in another file (probably one that
179hasn't been loaded yet). The file is searched on \code{sys.path}.
180
181\item[cl(ear) \optional{\optional{\var{filename}:}\var{lineno}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000182
183With a \var{lineno} argument, clear that break in the current file.
184Without argument, clear all breaks (but first ask confirmation).
185
Guido van Rossum897b9f01998-07-20 23:29:10 +0000186The line number may be prefixed with a filename and a colon,
187to specify a breakpoint in another file (probably one that
188hasn't been loaded yet). The file is searched on \code{sys.path}.
189
Guido van Rossum470be141995-03-17 16:07:09 +0000190\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000191
192Execute the current line, stop at the first possible occasion
193(either in a function that is called or on the next line in the
194current function).
195
Guido van Rossum470be141995-03-17 16:07:09 +0000196\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000197
198Continue execution until the next line in the current function
199is reached or it returns. (The difference between \code{next} and
200\code{step} is that \code{step} stops inside a called function, while
Guido van Rossumf4aac481995-03-02 12:37:55 +0000201\code{next} executes called functions at (nearly) full speed, only
202stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000203
Guido van Rossum470be141995-03-17 16:07:09 +0000204\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000205
206Continue execution until the current function returns.
207
Guido van Rossum470be141995-03-17 16:07:09 +0000208\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000209
210Continue execution, only stop when a breakpoint is encountered.
211
Fred Drake74947ac1998-01-12 15:38:30 +0000212\item[l(ist) \optional{\var{first\optional{, last}}}]
Guido van Rossum897b9f01998-07-20 23:29:10 +0000213ppp
Guido van Rossumf4aac481995-03-02 12:37:55 +0000214List source code for the current file. Without arguments, list 11
215lines around the current line or continue the previous listing. With
216one argument, list 11 lines around at that line. With two arguments,
217list the given range; if the second argument is less than the first,
218it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000219
Guido van Rossum470be141995-03-17 16:07:09 +0000220\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000221
222Print the argument list of the current function.
223
Guido van Rossum470be141995-03-17 16:07:09 +0000224\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000225
226Evaluate the \var{expression} in the current context and print its
Guido van Rossumf4aac481995-03-02 12:37:55 +0000227value. (Note: \code{print} can also be used, but is not a debugger
228command --- this executes the Python \code{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000229
Fred Drake74947ac1998-01-12 15:38:30 +0000230\item[\optional{!}\var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000231
232Execute the (one-line) \var{statement} in the context of
233the current stack frame.
234The exclamation point can be omitted unless the first word
235of the statement resembles a debugger command.
236To set a global variable, you can prefix the assignment
237command with a ``\code{global}'' command on the same line, e.g.:
Fred Drake82d493f1998-04-07 19:14:17 +0000238
Fred Drake19479911998-02-13 06:58:54 +0000239\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000240(Pdb) global list_options; list_options = ['-l']
241(Pdb)
Fred Drake19479911998-02-13 06:58:54 +0000242\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +0000243
Guido van Rossum470be141995-03-17 16:07:09 +0000244\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000245
246Quit from the debugger.
247The program being executed is aborted.
248
249\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000250
251\section{How It Works}
252
253Some changes were made to the interpreter:
254
255\begin{itemize}
Fred Drake74947ac1998-01-12 15:38:30 +0000256\item \code{sys.settrace(\var{func})} sets the global trace function
Guido van Rossum470be141995-03-17 16:07:09 +0000257\item there can also a local trace function (see later)
258\end{itemize}
259
Fred Drake82d493f1998-04-07 19:14:17 +0000260Trace functions have three arguments: \var{frame}, \var{event}, and
261\var{arg}. \var{frame} is the current stack frame. \var{event} is a
262string: \code{'call'}, \code{'line'}, \code{'return'} or
263\code{'exception'}. \var{arg} depends on the event type.
Guido van Rossum470be141995-03-17 16:07:09 +0000264
Guido van Rossum9d37a4d1997-10-27 19:57:20 +0000265The global trace function is invoked (with \var{event} set to
266\code{'call'}) whenever a new local scope is entered; it should return
267a reference to the local trace function to be used that scope, or
268\code{None} if the scope shouldn't be traced.
269
270The local trace function should return a reference to itself (or to
271another function for further tracing in that scope), or \code{None} to
272turn off tracing in that scope.
273
274Instance methods are accepted (and very useful!) as trace functions.
Guido van Rossum470be141995-03-17 16:07:09 +0000275
276The events have the following meaning:
277
278\begin{description}
279
280\item[\code{'call'}]
281A function is called (or some other code block entered). The global
282trace function is called; arg is the argument list to the function;
283the return value specifies the local trace function.
284
285\item[\code{'line'}]
286The interpreter is about to execute a new line of code (sometimes
287multiple line events on one line exist). The local trace function is
288called; arg in None; the return value specifies the new local trace
289function.
290
291\item[\code{'return'}]
292A function (or other code block) is about to return. The local trace
293function is called; arg is the value that will be returned. The trace
294function's return value is ignored.
295
296\item[\code{'exception'}]
297An exception has occurred. The local trace function is called; arg is
298a triple (exception, value, traceback); the return value specifies the
299new local trace function
300
301\end{description}
302
303Note that as an exception is propagated down the chain of callers, an
304\code{'exception'} event is generated at each level.
305
Fred Drakebc8ad5b1998-03-11 06:29:59 +0000306For more information on code and frame objects, refer to the
307\emph{Python Reference Manual}.