blob: 36932d8c40004912b7d87c27c438ad092e94014c [file] [log] [blame]
Guido van Rossumf4aac481995-03-02 12:37:55 +00001\chapter{The Python Debugger}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{pdb}
3
4\modulesynopsis{None}
5
Guido van Rossumdc46c7f1995-03-01 15:38:16 +00006\index{debugging}
7
Guido van Rossumf4aac481995-03-02 12:37:55 +00008
9The module \code{pdb} defines an interactive source code debugger for
Guido van Rossum255d7901997-07-11 13:51:17 +000010Python programs. It supports setting
11(conditional) breakpoints and single stepping
Guido van Rossumf4aac481995-03-02 12:37:55 +000012at the source line level, inspection of stack frames, source code
13listing, and evaluation of arbitrary Python code in the context of any
14stack frame. It also supports post-mortem debugging and can be called
15under program control.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000016
17The debugger is extensible --- it is actually defined as a class
Fred Drake82d493f1998-04-07 19:14:17 +000018\class{Pdb}.
19\withsubitem{(class in pdb)}{\ttindex{Pdb}}
20This is currently undocumented but easily understood by reading the
21source. The extension interface uses the (also undocumented) modules
22\module{bdb}\refstmodindex{bdb} and \module{cmd}\refstmodindex{cmd}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000023
24A primitive windowing version of the debugger also exists --- this is
Fred Drake82d493f1998-04-07 19:14:17 +000025module \module{wdb}, which requires \module{stdwin} (see the chapter
26on STDWIN specific modules).
27\refbimodindex{stdwin}
28\refstmodindex{wdb}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000029
Fred Drake82d493f1998-04-07 19:14:17 +000030The debugger's prompt is \samp{(Pdb) }.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000031Typical usage to run a program under control of the debugger is:
32
Fred Drake19479911998-02-13 06:58:54 +000033\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000034>>> import pdb
35>>> import mymodule
36>>> pdb.run('mymodule.test()')
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000037> <string>(0)?()
38(Pdb) continue
39> <string>(1)?()
40(Pdb) continue
41NameError: 'spam'
42> <string>(1)?()
43(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000044\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000045
46\file{pdb.py} can also be invoked as
Guido van Rossum809408e1997-06-02 17:28:16 +000047a script to debug other scripts. For example:
Fred Drake82d493f1998-04-07 19:14:17 +000048
49\begin{verbatim}
50python /usr/local/lib/python1.5/pdb.py myscript.py
51\end{verbatim}
Guido van Rossum809408e1997-06-02 17:28:16 +000052
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000053Typical usage to inspect a crashed program is:
54
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000056>>> import pdb
57>>> import mymodule
58>>> mymodule.test()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000059Traceback (innermost last):
60 File "<stdin>", line 1, in ?
61 File "./mymodule.py", line 4, in test
62 test2()
63 File "./mymodule.py", line 3, in test2
64 print spam
65NameError: spam
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000066>>> pdb.pm()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000067> ./mymodule.py(3)test2()
68-> print spam
69(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000070\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000071
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000072The module defines the following functions; each enters the debugger
73in a slightly different way:
74
Fred Drakecce10901998-03-17 06:33:25 +000075\begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000076Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000077control. The debugger prompt appears before any code is executed; you
Guido van Rossumf4aac481995-03-02 12:37:55 +000078can set breakpoints and type \code{continue}, or you can step through
79the statement using \code{step} or \code{next} (all these commands are
80explained below). The optional \var{globals} and \var{locals}
81arguments specify the environment in which the code is executed; by
82default the dictionary of the module \code{__main__} is used. (See
83the explanation of the \code{exec} statement or the \code{eval()}
84built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000085\end{funcdesc}
86
Fred Drakecce10901998-03-17 06:33:25 +000087\begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000088Evaluate the \var{expression} (given as a a string) under debugger
89control. When \code{runeval()} returns, it returns the value of the
90expression. Otherwise this function is similar to
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000091\code{run()}.
92\end{funcdesc}
93
Fred Drakecce10901998-03-17 06:33:25 +000094\begin{funcdesc}{runcall}{function\optional{, argument, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000095Call the \var{function} (a function or method object, not a string)
96with the given arguments. When \code{runcall()} returns, it returns
97whatever the function call returned. The debugger prompt appears as
98soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000099\end{funcdesc}
100
101\begin{funcdesc}{set_trace}{}
102Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +0000103hard-code a breakpoint at a given point in a program, even if the code
104is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000105\end{funcdesc}
106
107\begin{funcdesc}{post_mortem}{traceback}
108Enter post-mortem debugging of the given \var{traceback} object.
109\end{funcdesc}
110
111\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +0000112Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000113\code{sys.last_traceback}.
114\end{funcdesc}
115
Guido van Rossum470be141995-03-17 16:07:09 +0000116\section{Debugger Commands}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000117
118The debugger recognizes the following commands. Most commands can be
119abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that
120either ``\code{h}'' or ``\code{help}'' can be used to enter the help
121command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or
122``\code{Help} or ``\code{HELP}''). Arguments to commands must be
123separated by whitespace (spaces or tabs). Optional arguments are
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000124enclosed in square brackets (``\code{[]}'') in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000125square brackets must not be typed. Alternatives in the command syntax
126are separated by a vertical bar (``\code{|}'').
127
128Entering a blank line repeats the last command entered. Exception: if
129the last command was a ``\code{list}'' command, the next 11 lines are
130listed.
131
132Commands that the debugger doesn't recognize are assumed to be Python
133statements and are executed in the context of the program being
134debugged. Python statements can also be prefixed with an exclamation
135point (``\code{!}''). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000136being debugged; it is even possible to change a variable or call a
137function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000138exception occurs in such a statement, the exception name is printed
139but the debugger's state is not changed.
140
Guido van Rossum64421161998-09-17 15:11:51 +0000141Multiple commands may be entered on a single line, separated by
142''\code{;;}''. (A single ''\code{;}'' is not used as it is
143the separator for multiple commands in a line that is passed to
144the Python parser.)
145No intelligence is applied to separating the commands;
146the input is split at the first ''\code{;;}'' pair, even if it is in
147the middle of a quoted string.
148
149The debugger supports aliases. Aliases can have parameters which
150allows one a certain level of adaptability to the context under
151examination.
152
153If a file \file{.pdbrc} exists in the user's home directory or in the
154current directory, it is read in and executed as if it had been typed
155at the debugger prompt. This is particularly useful for aliases. If
156both files exist, the one in the home directory is read first and
157aliases defined there can be overriden by the local file.
158
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000159\begin{description}
160
Fred Drake74947ac1998-01-12 15:38:30 +0000161\item[h(elp) \optional{\var{command}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000162
Fred Drake74947ac1998-01-12 15:38:30 +0000163Without argument, print the list of available commands. With a
164\var{command} as argument, print help about that command. \samp{help
165pdb} displays the full documentation file; if the environment variable
166\code{PAGER} is defined, the file is piped through that command
167instead. Since the \var{command} argument must be an identifier,
168\samp{help exec} must be entered to get help on the \samp{!} command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000169
Guido van Rossum470be141995-03-17 16:07:09 +0000170\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000171
Fred Drake74947ac1998-01-12 15:38:30 +0000172Print a stack trace, with the most recent frame at the bottom. An
173arrow indicates the current frame, which determines the context of
174most commands.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000175
Guido van Rossum470be141995-03-17 16:07:09 +0000176\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000177
178Move the current frame one level down in the stack trace
179(to an older frame).
180
Guido van Rossum470be141995-03-17 16:07:09 +0000181\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000182
183Move the current frame one level up in the stack trace
184(to a newer frame).
185
Guido van Rossum897b9f01998-07-20 23:29:10 +0000186\item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}%
187 \code{\Large|}\var{function}%
Guido van Rossum64421161998-09-17 15:11:51 +0000188 \optional{, \var{condition}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000189
190With a \var{lineno} argument, set a break there in the current
Guido van Rossum64421161998-09-17 15:11:51 +0000191file. With a \var{function} argument, set a break at the first
192executable statement within that function.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000193The line number may be prefixed with a filename and a colon,
194to specify a breakpoint in another file (probably one that
195hasn't been loaded yet). The file is searched on \code{sys.path}.
Guido van Rossum64421161998-09-17 15:11:51 +0000196Note that each breakpoint is assigned a number to which all the other
197breakpoint commands refer.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000198
Guido van Rossum64421161998-09-17 15:11:51 +0000199If a second argument is present, it is an expression which must
200evaluate to true before the breakpoint is honored.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000201
Guido van Rossum64421161998-09-17 15:11:51 +0000202Without argument, list all breaks, including for each breakpoint,
203the number of times that breakpoint has been hit, the current
204ignore count, and the associated condition if any.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000205
Guido van Rossum64421161998-09-17 15:11:51 +0000206\item[tbreak \optional{\optional{\var{filename}:}\var{lineno}%
207 \code{\Large|}\var{function}%
208 \optional{, \var{condition}}}]
209
210Temporary breakpoint, which is removed automatically when it is
211first hit. The arguments are the same as break.
212
213\item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
214
215With a space separated list of breakpoint numbers, clear those
216breakpoints. Without argument, clear all breaks (but first
217ask confirmation).
218
219\item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
220
221Disables the breakpoints given as a space separated list of
222breakpoint numbers. Disabling a breakpoint means it cannot cause
223the program to stop execution, but unlike clearing a breakpoint, it
224remains in the list of breakpoints and can be (re-)enabled.
225
226\item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
227
228Enables the breakpoints specified.
229
230\item[ignore \var{bpnumber} \optional{\var{count}}]
231
232Sets the ignore count for the given breakpoint number. If
233count is omitted, the ignore count is set to 0. A breakpoint
234becomes active when the ignore count is zero. When non-zero,
235the count is decremented each time the breakpoint is reached
236and the breakpoint is not disabled and any associated condition
237evaluates to true.
238
239\item[condition \var{bpnumber} \optional{\var{condition}}]
240
241Condition is an expression which must evaluate to true before
242the breakpoint is honored. If condition is absent, any existing
243condition is removed; i.e., the breakpoint is made unconditional.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000244
Guido van Rossum470be141995-03-17 16:07:09 +0000245\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000246
247Execute the current line, stop at the first possible occasion
248(either in a function that is called or on the next line in the
249current function).
250
Guido van Rossum470be141995-03-17 16:07:09 +0000251\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000252
253Continue execution until the next line in the current function
254is reached or it returns. (The difference between \code{next} and
255\code{step} is that \code{step} stops inside a called function, while
Guido van Rossumf4aac481995-03-02 12:37:55 +0000256\code{next} executes called functions at (nearly) full speed, only
257stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000258
Guido van Rossum470be141995-03-17 16:07:09 +0000259\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000260
261Continue execution until the current function returns.
262
Guido van Rossum470be141995-03-17 16:07:09 +0000263\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000264
265Continue execution, only stop when a breakpoint is encountered.
266
Fred Drake74947ac1998-01-12 15:38:30 +0000267\item[l(ist) \optional{\var{first\optional{, last}}}]
Guido van Rossum43b655c1998-09-17 17:07:15 +0000268
Guido van Rossumf4aac481995-03-02 12:37:55 +0000269List source code for the current file. Without arguments, list 11
270lines around the current line or continue the previous listing. With
271one argument, list 11 lines around at that line. With two arguments,
272list the given range; if the second argument is less than the first,
273it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000274
Guido van Rossum470be141995-03-17 16:07:09 +0000275\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000276
277Print the argument list of the current function.
278
Guido van Rossum470be141995-03-17 16:07:09 +0000279\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000280
281Evaluate the \var{expression} in the current context and print its
Guido van Rossumf4aac481995-03-02 12:37:55 +0000282value. (Note: \code{print} can also be used, but is not a debugger
283command --- this executes the Python \code{print} statement.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000284
Guido van Rossum64421161998-09-17 15:11:51 +0000285\item[alias \optional{\var{name} \optional{command}}]
286
287Creates an alias called \var{name} that executes \var{command}. The
288command must \emph{not} be enclosed in quotes. Replaceable parameters
289can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is
290replaced by all the parameters. If no command is given, the current
291alias for \var{name} is shown. If no arguments are given, all
292aliases are listed.
293
294Aliases may be nested and can contain anything that can be
295legally typed at the pdb prompt. Note that internal pdb commands
296\emph{can} be overridden by aliases. Such a command is
297then hidden until the alias is removed. Aliasing is recursively
298applied to the first word of the command line; all other words
299in the line are left alone.
300
301As an example, here are two useful aliases (especially when placed
302in the \file{.pdbrc} file):
303
304\begin{verbatim}
305#Print instance variables (usage "pi classInst")
306alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
307#Print instance variables in self
308alias ps pi self
309\end{verbatim}
310
311\item[unalias \var{name}]
312
313Deletes the specified alias.
314
Fred Drake74947ac1998-01-12 15:38:30 +0000315\item[\optional{!}\var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000316
317Execute the (one-line) \var{statement} in the context of
318the current stack frame.
319The exclamation point can be omitted unless the first word
320of the statement resembles a debugger command.
321To set a global variable, you can prefix the assignment
322command with a ``\code{global}'' command on the same line, e.g.:
Fred Drake82d493f1998-04-07 19:14:17 +0000323
Fred Drake19479911998-02-13 06:58:54 +0000324\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000325(Pdb) global list_options; list_options = ['-l']
326(Pdb)
Fred Drake19479911998-02-13 06:58:54 +0000327\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +0000328
Guido van Rossum470be141995-03-17 16:07:09 +0000329\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000330
331Quit from the debugger.
332The program being executed is aborted.
333
334\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000335
336\section{How It Works}
337
338Some changes were made to the interpreter:
339
340\begin{itemize}
Fred Drake74947ac1998-01-12 15:38:30 +0000341\item \code{sys.settrace(\var{func})} sets the global trace function
Guido van Rossum470be141995-03-17 16:07:09 +0000342\item there can also a local trace function (see later)
343\end{itemize}
344
Fred Drake82d493f1998-04-07 19:14:17 +0000345Trace functions have three arguments: \var{frame}, \var{event}, and
346\var{arg}. \var{frame} is the current stack frame. \var{event} is a
347string: \code{'call'}, \code{'line'}, \code{'return'} or
348\code{'exception'}. \var{arg} depends on the event type.
Guido van Rossum470be141995-03-17 16:07:09 +0000349
Guido van Rossum9d37a4d1997-10-27 19:57:20 +0000350The global trace function is invoked (with \var{event} set to
351\code{'call'}) whenever a new local scope is entered; it should return
352a reference to the local trace function to be used that scope, or
353\code{None} if the scope shouldn't be traced.
354
355The local trace function should return a reference to itself (or to
356another function for further tracing in that scope), or \code{None} to
357turn off tracing in that scope.
358
359Instance methods are accepted (and very useful!) as trace functions.
Guido van Rossum470be141995-03-17 16:07:09 +0000360
361The events have the following meaning:
362
363\begin{description}
364
365\item[\code{'call'}]
366A function is called (or some other code block entered). The global
367trace function is called; arg is the argument list to the function;
368the return value specifies the local trace function.
369
370\item[\code{'line'}]
371The interpreter is about to execute a new line of code (sometimes
372multiple line events on one line exist). The local trace function is
373called; arg in None; the return value specifies the new local trace
374function.
375
376\item[\code{'return'}]
377A function (or other code block) is about to return. The local trace
378function is called; arg is the value that will be returned. The trace
379function's return value is ignored.
380
381\item[\code{'exception'}]
382An exception has occurred. The local trace function is called; arg is
383a triple (exception, value, traceback); the return value specifies the
384new local trace function
385
386\end{description}
387
388Note that as an exception is propagated down the chain of callers, an
389\code{'exception'} event is generated at each level.
390
Fred Drakebc8ad5b1998-03-11 06:29:59 +0000391For more information on code and frame objects, refer to the
392\emph{Python Reference Manual}.