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