1 \chapter{The Python Debugger
}
7 The module
\code{pdb
} defines an interactive source code debugger for
8 Python programs. It supports setting
9 (conditional) breakpoints and single stepping
10 at the source line level, inspection of stack frames, source code
11 listing, and evaluation of arbitrary Python code in the context of any
12 stack frame. It also supports post-mortem debugging and can be called
13 under program control.
15 The debugger is extensible --- it is actually defined as a class
17 \withsubitem{(class in pdb)
}{\ttindex{Pdb
}}
18 This is currently undocumented but easily understood by reading the
19 source. The extension interface uses the (also undocumented) modules
20 \module{bdb
}\refstmodindex{bdb
} and
\module{cmd
}\refstmodindex{cmd
}.
22 A primitive windowing version of the debugger also exists --- this is
23 module
\module{wdb
}, which requires
\module{stdwin
} (see the chapter
24 on STDWIN specific modules).
25 \refbimodindex{stdwin
}
28 The debugger's prompt is
\samp{(Pdb)
}.
29 Typical usage to run a program under control of the debugger is:
34 >>> pdb.run('mymodule.test()')
44 \file{pdb.py
} can also be invoked as
45 a script to debug other scripts. For example:
48 python /usr/local/lib/python1.5/pdb.py myscript.py
51 Typical usage to inspect a crashed program is:
57 Traceback (innermost last):
58 File "<stdin>", line
1, in ?
59 File "./mymodule.py", line
4, in test
61 File "./mymodule.py", line
3, in test2
65 > ./mymodule.py(
3)test2()
70 The module defines the following functions; each enters the debugger
71 in a slightly different way:
73 \begin{funcdesc
}{run
}{statement
\optional{, globals
\optional{, locals
}}}
74 Execute the
\var{statement
} (given as a string) under debugger
75 control. The debugger prompt appears before any code is executed; you
76 can set breakpoints and type
\code{continue
}, or you can step through
77 the statement using
\code{step
} or
\code{next
} (all these commands are
78 explained below). The optional
\var{globals
} and
\var{locals
}
79 arguments specify the environment in which the code is executed; by
80 default the dictionary of the module
\code{__main__
} is used. (See
81 the explanation of the
\code{exec
} statement or the
\code{eval()
}
85 \begin{funcdesc
}{runeval
}{expression
\optional{, globals
\optional{, locals
}}}
86 Evaluate the
\var{expression
} (given as a a string) under debugger
87 control. When
\code{runeval()
} returns, it returns the value of the
88 expression. Otherwise this function is similar to
92 \begin{funcdesc
}{runcall
}{function
\optional{, argument, ...
}}
93 Call the
\var{function
} (a function or method object, not a string)
94 with the given arguments. When
\code{runcall()
} returns, it returns
95 whatever the function call returned. The debugger prompt appears as
96 soon as the function is entered.
99 \begin{funcdesc
}{set_trace
}{}
100 Enter the debugger at the calling stack frame. This is useful to
101 hard-code a breakpoint at a given point in a program, even if the code
102 is not otherwise being debugged (e.g. when an assertion fails).
105 \begin{funcdesc
}{post_mortem
}{traceback
}
106 Enter post-mortem debugging of the given
\var{traceback
} object.
109 \begin{funcdesc
}{pm
}{}
110 Enter post-mortem debugging of the traceback found in
111 \code{sys.last_traceback
}.
114 \section{Debugger Commands
}
116 The debugger recognizes the following commands. Most commands can be
117 abbreviated to one or two letters; e.g. ``
\code{h(elp)
}'' means that
118 either ``
\code{h
}'' or ``
\code{help
}'' can be used to enter the help
119 command (but not ``
\code{he
}'' or ``
\code{hel
}'', nor ``
\code{H
}'' or
120 ``
\code{Help
} or ``
\code{HELP
}''). Arguments to commands must be
121 separated by whitespace (spaces or tabs). Optional arguments are
122 enclosed in square brackets (``
\code{[]}'') in the command syntax; the
123 square brackets must not be typed. Alternatives in the command syntax
124 are separated by a vertical bar (``
\code{|
}'').
126 Entering a blank line repeats the last command entered. Exception: if
127 the last command was a ``
\code{list
}'' command, the next
11 lines are
130 Commands that the debugger doesn't recognize are assumed to be Python
131 statements and are executed in the context of the program being
132 debugged. Python statements can also be prefixed with an exclamation
133 point (``
\code{!
}''). This is a powerful way to inspect the program
134 being debugged; it is even possible to change a variable or call a
136 exception occurs in such a statement, the exception name is printed
137 but the debugger's state is not changed.
141 \item[h(elp)
\optional{\var{command
}}]
143 Without argument, print the list of available commands. With a
144 \var{command
} as argument, print help about that command.
\samp{help
145 pdb
} displays the full documentation file; if the environment variable
146 \code{PAGER
} is defined, the file is piped through that command
147 instead. Since the
\var{command
} argument must be an identifier,
148 \samp{help exec
} must be entered to get help on the
\samp{!
} command.
152 Print a stack trace, with the most recent frame at the bottom. An
153 arrow indicates the current frame, which determines the context of
158 Move the current frame one level down in the stack trace
163 Move the current frame one level up in the stack trace
166 \item[b(reak)
\optional{\var{lineno
}\code{\Large|
}\var{function
}%
167 \optional{,
\code{'
}\var{condition
}\code{'
}}}]
169 With a
\var{lineno
} argument, set a break there in the current
170 file. With a
\var{function
} argument, set a break at the entry of
171 that function. Without argument, list all breaks.
172 If a second argument is present, it is a string (included in string
173 quotes!) specifying an expression which must evaluate to true before
174 the breakpoint is honored.
176 \item[cl(ear)
\optional{\var{lineno
}}]
178 With a
\var{lineno
} argument, clear that break in the current file.
179 Without argument, clear all breaks (but first ask confirmation).
183 Execute the current line, stop at the first possible occasion
184 (either in a function that is called or on the next line in the
189 Continue execution until the next line in the current function
190 is reached or it returns. (The difference between
\code{next
} and
191 \code{step
} is that
\code{step
} stops inside a called function, while
192 \code{next
} executes called functions at (nearly) full speed, only
193 stopping at the next line in the current function.)
197 Continue execution until the current function returns.
201 Continue execution, only stop when a breakpoint is encountered.
203 \item[l(ist)
\optional{\var{first
\optional{, last
}}}]
205 List source code for the current file. Without arguments, list
11
206 lines around the current line or continue the previous listing. With
207 one argument, list
11 lines around at that line. With two arguments,
208 list the given range; if the second argument is less than the first,
209 it is interpreted as a count.
213 Print the argument list of the current function.
215 \item[p
\var{expression
}]
217 Evaluate the
\var{expression
} in the current context and print its
218 value. (Note:
\code{print
} can also be used, but is not a debugger
219 command --- this executes the Python
\code{print
} statement.)
221 \item[\optional{!
}\var{statement
}]
223 Execute the (one-line)
\var{statement
} in the context of
224 the current stack frame.
225 The exclamation point can be omitted unless the first word
226 of the statement resembles a debugger command.
227 To set a global variable, you can prefix the assignment
228 command with a ``
\code{global
}'' command on the same line, e.g.:
231 (Pdb) global list_options; list_options =
['-l'
]
237 Quit from the debugger.
238 The program being executed is aborted.
242 \section{How It Works
}
244 Some changes were made to the interpreter:
247 \item \code{sys.settrace(
\var{func
})
} sets the global trace function
248 \item there can also a local trace function (see later)
251 Trace functions have three arguments:
\var{frame
},
\var{event
}, and
252 \var{arg
}.
\var{frame
} is the current stack frame.
\var{event
} is a
253 string:
\code{'call'
},
\code{'line'
},
\code{'return'
} or
254 \code{'exception'
}.
\var{arg
} depends on the event type.
256 The global trace function is invoked (with
\var{event
} set to
257 \code{'call'
}) whenever a new local scope is entered; it should return
258 a reference to the local trace function to be used that scope, or
259 \code{None
} if the scope shouldn't be traced.
261 The local trace function should return a reference to itself (or to
262 another function for further tracing in that scope), or
\code{None
} to
263 turn off tracing in that scope.
265 Instance methods are accepted (and very useful!) as trace functions.
267 The events have the following meaning:
272 A function is called (or some other code block entered). The global
273 trace function is called; arg is the argument list to the function;
274 the return value specifies the local trace function.
277 The interpreter is about to execute a new line of code (sometimes
278 multiple line events on one line exist). The local trace function is
279 called; arg in None; the return value specifies the new local trace
282 \item[\code{'return'
}]
283 A function (or other code block) is about to return. The local trace
284 function is called; arg is the value that will be returned. The trace
285 function's return value is ignored.
287 \item[\code{'exception'
}]
288 An exception has occurred. The local trace function is called; arg is
289 a triple (exception, value, traceback); the return value specifies the
290 new local trace function
294 Note that as an exception is propagated down the chain of callers, an
295 \code{'exception'
} event is generated at each level.
297 For more information on code and frame objects, refer to the
298 \emph{Python Reference Manual
}.