Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 1 | \chapter{The Python Debugger} |
Fred Drake | 31ecd50 | 1998-02-18 15:40:11 +0000 | [diff] [blame] | 2 | \label{module-pdb} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 3 | \stmodindex{pdb} |
| 4 | \index{debugging} |
| 5 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 6 | \setindexsubitem{(in module pdb)} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 7 | |
| 8 | The module \code{pdb} defines an interactive source code debugger for |
Guido van Rossum | 255d790 | 1997-07-11 13:51:17 +0000 | [diff] [blame] | 9 | Python programs. It supports setting |
| 10 | (conditional) breakpoints and single stepping |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 11 | at the source line level, inspection of stack frames, source code |
| 12 | listing, and evaluation of arbitrary Python code in the context of any |
| 13 | stack frame. It also supports post-mortem debugging and can be called |
| 14 | under program control. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 15 | |
| 16 | The debugger is extensible --- it is actually defined as a class |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 17 | \code{Pdb}. This is currently undocumented but easily understood by |
| 18 | reading the source. The extension interface uses the (also |
| 19 | undocumented) modules \code{bdb} and \code{cmd}. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 20 | \ttindex{Pdb} |
| 21 | \ttindex{bdb} |
| 22 | \ttindex{cmd} |
| 23 | |
| 24 | A primitive windowing version of the debugger also exists --- this is |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 25 | module \code{wdb}, which requires STDWIN (see the chapter on STDWIN |
| 26 | specific modules). |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 27 | \index{stdwin} |
| 28 | \ttindex{wdb} |
| 29 | |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 30 | The debugger's prompt is ``\code{(Pdb) }''. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 31 | Typical usage to run a program under control of the debugger is: |
| 32 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 33 | \begin{verbatim} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 34 | >>> import pdb |
| 35 | >>> import mymodule |
| 36 | >>> pdb.run('mymodule.test()') |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 37 | > <string>(0)?() |
| 38 | (Pdb) continue |
| 39 | > <string>(1)?() |
| 40 | (Pdb) continue |
| 41 | NameError: 'spam' |
| 42 | > <string>(1)?() |
| 43 | (Pdb) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 44 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 45 | % |
Guido van Rossum | 809408e | 1997-06-02 17:28:16 +0000 | [diff] [blame] | 46 | \code{pdb.py} can also be invoked as |
| 47 | a script to debug other scripts. For example: |
| 48 | \code{python /usr/local/lib/python1.4/pdb.py myscript.py} |
| 49 | |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 50 | Typical usage to inspect a crashed program is: |
| 51 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 52 | \begin{verbatim} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 53 | >>> import pdb |
| 54 | >>> import mymodule |
| 55 | >>> mymodule.test() |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 56 | Traceback (innermost last): |
| 57 | File "<stdin>", line 1, in ? |
| 58 | File "./mymodule.py", line 4, in test |
| 59 | test2() |
| 60 | File "./mymodule.py", line 3, in test2 |
| 61 | print spam |
| 62 | NameError: spam |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 63 | >>> pdb.pm() |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 64 | > ./mymodule.py(3)test2() |
| 65 | -> print spam |
| 66 | (Pdb) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 67 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 68 | % |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 69 | The module defines the following functions; each enters the debugger |
| 70 | in a slightly different way: |
| 71 | |
| 72 | \begin{funcdesc}{run}{statement\optional{\, globals\optional{\, locals}}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 73 | Execute the \var{statement} (given as a string) under debugger |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 74 | control. The debugger prompt appears before any code is executed; you |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 75 | can set breakpoints and type \code{continue}, or you can step through |
| 76 | the statement using \code{step} or \code{next} (all these commands are |
| 77 | explained below). The optional \var{globals} and \var{locals} |
| 78 | arguments specify the environment in which the code is executed; by |
| 79 | default the dictionary of the module \code{__main__} is used. (See |
| 80 | the explanation of the \code{exec} statement or the \code{eval()} |
| 81 | built-in function.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 82 | \end{funcdesc} |
| 83 | |
| 84 | \begin{funcdesc}{runeval}{expression\optional{\, globals\optional{\, locals}}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 85 | Evaluate the \var{expression} (given as a a string) under debugger |
| 86 | control. When \code{runeval()} returns, it returns the value of the |
| 87 | expression. Otherwise this function is similar to |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 88 | \code{run()}. |
| 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{runcall}{function\optional{\, argument\, ...}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 92 | Call the \var{function} (a function or method object, not a string) |
| 93 | with the given arguments. When \code{runcall()} returns, it returns |
| 94 | whatever the function call returned. The debugger prompt appears as |
| 95 | soon as the function is entered. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 96 | \end{funcdesc} |
| 97 | |
| 98 | \begin{funcdesc}{set_trace}{} |
| 99 | Enter the debugger at the calling stack frame. This is useful to |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 100 | hard-code a breakpoint at a given point in a program, even if the code |
| 101 | is not otherwise being debugged (e.g. when an assertion fails). |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{post_mortem}{traceback} |
| 105 | Enter post-mortem debugging of the given \var{traceback} object. |
| 106 | \end{funcdesc} |
| 107 | |
| 108 | \begin{funcdesc}{pm}{} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 109 | Enter post-mortem debugging of the traceback found in |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 110 | \code{sys.last_traceback}. |
| 111 | \end{funcdesc} |
| 112 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 113 | \section{Debugger Commands} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 114 | |
| 115 | The debugger recognizes the following commands. Most commands can be |
| 116 | abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that |
| 117 | either ``\code{h}'' or ``\code{help}'' can be used to enter the help |
| 118 | command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or |
| 119 | ``\code{Help} or ``\code{HELP}''). Arguments to commands must be |
| 120 | separated by whitespace (spaces or tabs). Optional arguments are |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 121 | enclosed in square brackets (``\code{[]}'') in the command syntax; the |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 122 | square brackets must not be typed. Alternatives in the command syntax |
| 123 | are separated by a vertical bar (``\code{|}''). |
| 124 | |
| 125 | Entering a blank line repeats the last command entered. Exception: if |
| 126 | the last command was a ``\code{list}'' command, the next 11 lines are |
| 127 | listed. |
| 128 | |
| 129 | Commands that the debugger doesn't recognize are assumed to be Python |
| 130 | statements and are executed in the context of the program being |
| 131 | debugged. Python statements can also be prefixed with an exclamation |
| 132 | point (``\code{!}''). This is a powerful way to inspect the program |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 133 | being debugged; it is even possible to change a variable or call a |
| 134 | function. When an |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 135 | exception occurs in such a statement, the exception name is printed |
| 136 | but the debugger's state is not changed. |
| 137 | |
| 138 | \begin{description} |
| 139 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 140 | \item[h(elp) \optional{\var{command}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 141 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 142 | Without argument, print the list of available commands. With a |
| 143 | \var{command} as argument, print help about that command. \samp{help |
| 144 | pdb} displays the full documentation file; if the environment variable |
| 145 | \code{PAGER} is defined, the file is piped through that command |
| 146 | instead. Since the \var{command} argument must be an identifier, |
| 147 | \samp{help exec} must be entered to get help on the \samp{!} command. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 149 | \item[w(here)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 150 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 151 | Print a stack trace, with the most recent frame at the bottom. An |
| 152 | arrow indicates the current frame, which determines the context of |
| 153 | most commands. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 155 | \item[d(own)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 156 | |
| 157 | Move the current frame one level down in the stack trace |
| 158 | (to an older frame). |
| 159 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 160 | \item[u(p)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 161 | |
| 162 | Move the current frame one level up in the stack trace |
| 163 | (to a newer frame). |
| 164 | |
Fred Drake | c927407 | 1998-02-19 15:18:02 +0000 | [diff] [blame] | 165 | \item[b(reak) \optional{\var{lineno}\code{\Large|}\var{function}% |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 166 | \optional{, \code{'}\var{condition}\code{'}}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 167 | |
| 168 | With a \var{lineno} argument, set a break there in the current |
| 169 | file. With a \var{function} argument, set a break at the entry of |
| 170 | that function. Without argument, list all breaks. |
Guido van Rossum | 31cbc84 | 1997-07-11 13:57:28 +0000 | [diff] [blame] | 171 | If a second argument is present, it is a string (included in string |
| 172 | quotes!) specifying an expression which must evaluate to true before |
| 173 | the breakpoint is honored. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 174 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 175 | \item[cl(ear) \optional{\var{lineno}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 176 | |
| 177 | With a \var{lineno} argument, clear that break in the current file. |
| 178 | Without argument, clear all breaks (but first ask confirmation). |
| 179 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 180 | \item[s(tep)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 181 | |
| 182 | Execute the current line, stop at the first possible occasion |
| 183 | (either in a function that is called or on the next line in the |
| 184 | current function). |
| 185 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 186 | \item[n(ext)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 187 | |
| 188 | Continue execution until the next line in the current function |
| 189 | is reached or it returns. (The difference between \code{next} and |
| 190 | \code{step} is that \code{step} stops inside a called function, while |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 191 | \code{next} executes called functions at (nearly) full speed, only |
| 192 | stopping at the next line in the current function.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 194 | \item[r(eturn)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 195 | |
| 196 | Continue execution until the current function returns. |
| 197 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 198 | \item[c(ont(inue))] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 199 | |
| 200 | Continue execution, only stop when a breakpoint is encountered. |
| 201 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 202 | \item[l(ist) \optional{\var{first\optional{, last}}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 204 | List source code for the current file. Without arguments, list 11 |
| 205 | lines around the current line or continue the previous listing. With |
| 206 | one argument, list 11 lines around at that line. With two arguments, |
| 207 | list the given range; if the second argument is less than the first, |
| 208 | it is interpreted as a count. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 210 | \item[a(rgs)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 211 | |
| 212 | Print the argument list of the current function. |
| 213 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 214 | \item[p \var{expression}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 215 | |
| 216 | Evaluate the \var{expression} in the current context and print its |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 217 | value. (Note: \code{print} can also be used, but is not a debugger |
| 218 | command --- this executes the Python \code{print} statement.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 219 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 220 | \item[\optional{!}\var{statement}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 221 | |
| 222 | Execute the (one-line) \var{statement} in the context of |
| 223 | the current stack frame. |
| 224 | The exclamation point can be omitted unless the first word |
| 225 | of the statement resembles a debugger command. |
| 226 | To set a global variable, you can prefix the assignment |
| 227 | command with a ``\code{global}'' command on the same line, e.g.: |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 228 | \begin{verbatim} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 229 | (Pdb) global list_options; list_options = ['-l'] |
| 230 | (Pdb) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 231 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 232 | % |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 233 | \item[q(uit)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 234 | |
| 235 | Quit from the debugger. |
| 236 | The program being executed is aborted. |
| 237 | |
| 238 | \end{description} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 239 | |
| 240 | \section{How It Works} |
| 241 | |
| 242 | Some changes were made to the interpreter: |
| 243 | |
| 244 | \begin{itemize} |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 245 | \item \code{sys.settrace(\var{func})} sets the global trace function |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 246 | \item there can also a local trace function (see later) |
| 247 | \end{itemize} |
| 248 | |
| 249 | Trace functions have three arguments: (\var{frame}, \var{event}, \var{arg}) |
| 250 | |
| 251 | \begin{description} |
| 252 | |
| 253 | \item[\var{frame}] is the current stack frame |
| 254 | |
| 255 | \item[\var{event}] is a string: \code{'call'}, \code{'line'}, \code{'return'} |
| 256 | or \code{'exception'} |
| 257 | |
| 258 | \item[\var{arg}] is dependent on the event type |
| 259 | |
| 260 | \end{description} |
| 261 | |
Guido van Rossum | 9d37a4d | 1997-10-27 19:57:20 +0000 | [diff] [blame] | 262 | The global trace function is invoked (with \var{event} set to |
| 263 | \code{'call'}) whenever a new local scope is entered; it should return |
| 264 | a reference to the local trace function to be used that scope, or |
| 265 | \code{None} if the scope shouldn't be traced. |
| 266 | |
| 267 | The local trace function should return a reference to itself (or to |
| 268 | another function for further tracing in that scope), or \code{None} to |
| 269 | turn off tracing in that scope. |
| 270 | |
| 271 | Instance methods are accepted (and very useful!) as trace functions. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 272 | |
| 273 | The events have the following meaning: |
| 274 | |
| 275 | \begin{description} |
| 276 | |
| 277 | \item[\code{'call'}] |
| 278 | A function is called (or some other code block entered). The global |
| 279 | trace function is called; arg is the argument list to the function; |
| 280 | the return value specifies the local trace function. |
| 281 | |
| 282 | \item[\code{'line'}] |
| 283 | The interpreter is about to execute a new line of code (sometimes |
| 284 | multiple line events on one line exist). The local trace function is |
| 285 | called; arg in None; the return value specifies the new local trace |
| 286 | function. |
| 287 | |
| 288 | \item[\code{'return'}] |
| 289 | A function (or other code block) is about to return. The local trace |
| 290 | function is called; arg is the value that will be returned. The trace |
| 291 | function's return value is ignored. |
| 292 | |
| 293 | \item[\code{'exception'}] |
| 294 | An exception has occurred. The local trace function is called; arg is |
| 295 | a triple (exception, value, traceback); the return value specifies the |
| 296 | new local trace function |
| 297 | |
| 298 | \end{description} |
| 299 | |
| 300 | Note that as an exception is propagated down the chain of callers, an |
| 301 | \code{'exception'} event is generated at each level. |
| 302 | |
Fred Drake | bc8ad5b | 1998-03-11 06:29:59 +0000 | [diff] [blame^] | 303 | For more information on code and frame objects, refer to the |
| 304 | \emph{Python Reference Manual}. |