Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 1 | \chapter{The Python Debugger} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{pdb} |
| 3 | |
| 4 | \modulesynopsis{None} |
| 5 | |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 6 | \index{debugging} |
| 7 | |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 8 | |
| 9 | The module \code{pdb} defines an interactive source code debugger for |
Guido van Rossum | 255d790 | 1997-07-11 13:51:17 +0000 | [diff] [blame] | 10 | Python programs. It supports setting |
| 11 | (conditional) breakpoints and single stepping |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 12 | at the source line level, inspection of stack frames, source code |
| 13 | listing, and evaluation of arbitrary Python code in the context of any |
| 14 | stack frame. It also supports post-mortem debugging and can be called |
| 15 | under program control. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 16 | |
| 17 | The debugger is extensible --- it is actually defined as a class |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 18 | \class{Pdb}. |
| 19 | \withsubitem{(class in pdb)}{\ttindex{Pdb}} |
| 20 | This is currently undocumented but easily understood by reading the |
| 21 | source. The extension interface uses the (also undocumented) modules |
| 22 | \module{bdb}\refstmodindex{bdb} and \module{cmd}\refstmodindex{cmd}. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 23 | |
| 24 | A primitive windowing version of the debugger also exists --- this is |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 25 | module \module{wdb}, which requires \module{stdwin} (see the chapter |
| 26 | on STDWIN specific modules). |
| 27 | \refbimodindex{stdwin} |
| 28 | \refstmodindex{wdb} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 29 | |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 30 | The debugger's prompt is \samp{(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} |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 45 | |
| 46 | \file{pdb.py} can also be invoked as |
Guido van Rossum | 809408e | 1997-06-02 17:28:16 +0000 | [diff] [blame] | 47 | a script to debug other scripts. For example: |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 48 | |
| 49 | \begin{verbatim} |
| 50 | python /usr/local/lib/python1.5/pdb.py myscript.py |
| 51 | \end{verbatim} |
Guido van Rossum | 809408e | 1997-06-02 17:28:16 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 53 | Typical usage to inspect a crashed program is: |
| 54 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 55 | \begin{verbatim} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 56 | >>> import pdb |
| 57 | >>> import mymodule |
| 58 | >>> mymodule.test() |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 59 | Traceback (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 |
| 65 | NameError: spam |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 66 | >>> pdb.pm() |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 67 | > ./mymodule.py(3)test2() |
| 68 | -> print spam |
| 69 | (Pdb) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 70 | \end{verbatim} |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 72 | The module defines the following functions; each enters the debugger |
| 73 | in a slightly different way: |
| 74 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 75 | \begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 76 | Execute the \var{statement} (given as a string) under debugger |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 77 | control. The debugger prompt appears before any code is executed; you |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 78 | can set breakpoints and type \code{continue}, or you can step through |
| 79 | the statement using \code{step} or \code{next} (all these commands are |
| 80 | explained below). The optional \var{globals} and \var{locals} |
| 81 | arguments specify the environment in which the code is executed; by |
| 82 | default the dictionary of the module \code{__main__} is used. (See |
| 83 | the explanation of the \code{exec} statement or the \code{eval()} |
| 84 | built-in function.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 85 | \end{funcdesc} |
| 86 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 87 | \begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 88 | Evaluate the \var{expression} (given as a a string) under debugger |
| 89 | control. When \code{runeval()} returns, it returns the value of the |
| 90 | expression. Otherwise this function is similar to |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 91 | \code{run()}. |
| 92 | \end{funcdesc} |
| 93 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 94 | \begin{funcdesc}{runcall}{function\optional{, argument, ...}} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 95 | Call the \var{function} (a function or method object, not a string) |
| 96 | with the given arguments. When \code{runcall()} returns, it returns |
| 97 | whatever the function call returned. The debugger prompt appears as |
| 98 | soon as the function is entered. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 99 | \end{funcdesc} |
| 100 | |
| 101 | \begin{funcdesc}{set_trace}{} |
| 102 | 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] | 103 | hard-code a breakpoint at a given point in a program, even if the code |
| 104 | is not otherwise being debugged (e.g. when an assertion fails). |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 105 | \end{funcdesc} |
| 106 | |
| 107 | \begin{funcdesc}{post_mortem}{traceback} |
| 108 | Enter post-mortem debugging of the given \var{traceback} object. |
| 109 | \end{funcdesc} |
| 110 | |
| 111 | \begin{funcdesc}{pm}{} |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 112 | Enter post-mortem debugging of the traceback found in |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 113 | \code{sys.last_traceback}. |
| 114 | \end{funcdesc} |
| 115 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 116 | \section{Debugger Commands} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 117 | |
| 118 | The debugger recognizes the following commands. Most commands can be |
| 119 | abbreviated to one or two letters; e.g. ``\code{h(elp)}'' means that |
| 120 | either ``\code{h}'' or ``\code{help}'' can be used to enter the help |
| 121 | command (but not ``\code{he}'' or ``\code{hel}'', nor ``\code{H}'' or |
| 122 | ``\code{Help} or ``\code{HELP}''). Arguments to commands must be |
| 123 | separated by whitespace (spaces or tabs). Optional arguments are |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 124 | enclosed in square brackets (``\code{[]}'') in the command syntax; the |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 125 | square brackets must not be typed. Alternatives in the command syntax |
| 126 | are separated by a vertical bar (``\code{|}''). |
| 127 | |
| 128 | Entering a blank line repeats the last command entered. Exception: if |
| 129 | the last command was a ``\code{list}'' command, the next 11 lines are |
| 130 | listed. |
| 131 | |
| 132 | Commands that the debugger doesn't recognize are assumed to be Python |
| 133 | statements and are executed in the context of the program being |
| 134 | debugged. Python statements can also be prefixed with an exclamation |
| 135 | point (``\code{!}''). This is a powerful way to inspect the program |
Guido van Rossum | 25f6fcc | 1995-04-04 12:28:53 +0000 | [diff] [blame] | 136 | being debugged; it is even possible to change a variable or call a |
| 137 | function. When an |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 138 | exception occurs in such a statement, the exception name is printed |
| 139 | but the debugger's state is not changed. |
| 140 | |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 141 | Multiple commands may be entered on a single line, separated by |
| 142 | ''\code{;;}''. (A single ''\code{;}'' is not used as it is |
| 143 | the separator for multiple commands in a line that is passed to |
| 144 | the Python parser.) |
| 145 | No intelligence is applied to separating the commands; |
| 146 | the input is split at the first ''\code{;;}'' pair, even if it is in |
| 147 | the middle of a quoted string. |
| 148 | |
| 149 | The debugger supports aliases. Aliases can have parameters which |
| 150 | allows one a certain level of adaptability to the context under |
| 151 | examination. |
| 152 | |
| 153 | If a file \file{.pdbrc} exists in the user's home directory or in the |
| 154 | current directory, it is read in and executed as if it had been typed |
| 155 | at the debugger prompt. This is particularly useful for aliases. If |
| 156 | both files exist, the one in the home directory is read first and |
| 157 | aliases defined there can be overriden by the local file. |
| 158 | |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 159 | \begin{description} |
| 160 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 161 | \item[h(elp) \optional{\var{command}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 162 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 163 | Without argument, print the list of available commands. With a |
| 164 | \var{command} as argument, print help about that command. \samp{help |
| 165 | pdb} displays the full documentation file; if the environment variable |
| 166 | \code{PAGER} is defined, the file is piped through that command |
| 167 | instead. 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 Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 170 | \item[w(here)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 171 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 172 | Print a stack trace, with the most recent frame at the bottom. An |
| 173 | arrow indicates the current frame, which determines the context of |
| 174 | most commands. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 176 | \item[d(own)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 177 | |
| 178 | Move the current frame one level down in the stack trace |
| 179 | (to an older frame). |
| 180 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 181 | \item[u(p)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 182 | |
| 183 | Move the current frame one level up in the stack trace |
| 184 | (to a newer frame). |
| 185 | |
Fred Drake | 31ec33e | 1999-04-13 21:36:44 +0000 | [diff] [blame^] | 186 | \item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 187 | |
| 188 | With a \var{lineno} argument, set a break there in the current |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 189 | file. With a \var{function} argument, set a break at the first |
| 190 | executable statement within that function. |
Guido van Rossum | 897b9f0 | 1998-07-20 23:29:10 +0000 | [diff] [blame] | 191 | The line number may be prefixed with a filename and a colon, |
| 192 | to specify a breakpoint in another file (probably one that |
| 193 | hasn't been loaded yet). The file is searched on \code{sys.path}. |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 194 | Note that each breakpoint is assigned a number to which all the other |
| 195 | breakpoint commands refer. |
Guido van Rossum | 897b9f0 | 1998-07-20 23:29:10 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 197 | If a second argument is present, it is an expression which must |
| 198 | evaluate to true before the breakpoint is honored. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 200 | Without argument, list all breaks, including for each breakpoint, |
| 201 | the number of times that breakpoint has been hit, the current |
| 202 | ignore count, and the associated condition if any. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 203 | |
Fred Drake | 31ec33e | 1999-04-13 21:36:44 +0000 | [diff] [blame^] | 204 | \item[tbreak \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}] |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 205 | |
| 206 | Temporary breakpoint, which is removed automatically when it is |
| 207 | first hit. The arguments are the same as break. |
| 208 | |
| 209 | \item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}] |
| 210 | |
| 211 | With a space separated list of breakpoint numbers, clear those |
| 212 | breakpoints. Without argument, clear all breaks (but first |
| 213 | ask confirmation). |
| 214 | |
| 215 | \item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}] |
| 216 | |
| 217 | Disables the breakpoints given as a space separated list of |
| 218 | breakpoint numbers. Disabling a breakpoint means it cannot cause |
| 219 | the program to stop execution, but unlike clearing a breakpoint, it |
| 220 | remains in the list of breakpoints and can be (re-)enabled. |
| 221 | |
| 222 | \item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}] |
| 223 | |
| 224 | Enables the breakpoints specified. |
| 225 | |
| 226 | \item[ignore \var{bpnumber} \optional{\var{count}}] |
| 227 | |
| 228 | Sets the ignore count for the given breakpoint number. If |
| 229 | count is omitted, the ignore count is set to 0. A breakpoint |
| 230 | becomes active when the ignore count is zero. When non-zero, |
| 231 | the count is decremented each time the breakpoint is reached |
| 232 | and the breakpoint is not disabled and any associated condition |
| 233 | evaluates to true. |
| 234 | |
| 235 | \item[condition \var{bpnumber} \optional{\var{condition}}] |
| 236 | |
| 237 | Condition is an expression which must evaluate to true before |
| 238 | the breakpoint is honored. If condition is absent, any existing |
| 239 | condition is removed; i.e., the breakpoint is made unconditional. |
Guido van Rossum | 897b9f0 | 1998-07-20 23:29:10 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 241 | \item[s(tep)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 242 | |
| 243 | Execute the current line, stop at the first possible occasion |
| 244 | (either in a function that is called or on the next line in the |
| 245 | current function). |
| 246 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 247 | \item[n(ext)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 248 | |
| 249 | Continue execution until the next line in the current function |
| 250 | is reached or it returns. (The difference between \code{next} and |
| 251 | \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] | 252 | \code{next} executes called functions at (nearly) full speed, only |
| 253 | stopping at the next line in the current function.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 255 | \item[r(eturn)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 256 | |
| 257 | Continue execution until the current function returns. |
| 258 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 259 | \item[c(ont(inue))] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 260 | |
| 261 | Continue execution, only stop when a breakpoint is encountered. |
| 262 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 263 | \item[l(ist) \optional{\var{first\optional{, last}}}] |
Guido van Rossum | 43b655c | 1998-09-17 17:07:15 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 265 | List source code for the current file. Without arguments, list 11 |
| 266 | lines around the current line or continue the previous listing. With |
| 267 | one argument, list 11 lines around at that line. With two arguments, |
| 268 | list the given range; if the second argument is less than the first, |
| 269 | it is interpreted as a count. |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 271 | \item[a(rgs)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 272 | |
| 273 | Print the argument list of the current function. |
| 274 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 275 | \item[p \var{expression}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 276 | |
| 277 | Evaluate the \var{expression} in the current context and print its |
Guido van Rossum | f4aac48 | 1995-03-02 12:37:55 +0000 | [diff] [blame] | 278 | value. (Note: \code{print} can also be used, but is not a debugger |
| 279 | command --- this executes the Python \code{print} statement.) |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | 6442116 | 1998-09-17 15:11:51 +0000 | [diff] [blame] | 281 | \item[alias \optional{\var{name} \optional{command}}] |
| 282 | |
| 283 | Creates an alias called \var{name} that executes \var{command}. The |
| 284 | command must \emph{not} be enclosed in quotes. Replaceable parameters |
| 285 | can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is |
| 286 | replaced by all the parameters. If no command is given, the current |
| 287 | alias for \var{name} is shown. If no arguments are given, all |
| 288 | aliases are listed. |
| 289 | |
| 290 | Aliases may be nested and can contain anything that can be |
| 291 | legally typed at the pdb prompt. Note that internal pdb commands |
| 292 | \emph{can} be overridden by aliases. Such a command is |
| 293 | then hidden until the alias is removed. Aliasing is recursively |
| 294 | applied to the first word of the command line; all other words |
| 295 | in the line are left alone. |
| 296 | |
| 297 | As an example, here are two useful aliases (especially when placed |
| 298 | in the \file{.pdbrc} file): |
| 299 | |
| 300 | \begin{verbatim} |
| 301 | #Print instance variables (usage "pi classInst") |
| 302 | alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k] |
| 303 | #Print instance variables in self |
| 304 | alias ps pi self |
| 305 | \end{verbatim} |
| 306 | |
| 307 | \item[unalias \var{name}] |
| 308 | |
| 309 | Deletes the specified alias. |
| 310 | |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 311 | \item[\optional{!}\var{statement}] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 312 | |
| 313 | Execute the (one-line) \var{statement} in the context of |
| 314 | the current stack frame. |
| 315 | The exclamation point can be omitted unless the first word |
| 316 | of the statement resembles a debugger command. |
| 317 | To set a global variable, you can prefix the assignment |
| 318 | command with a ``\code{global}'' command on the same line, e.g.: |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 319 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 320 | \begin{verbatim} |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 321 | (Pdb) global list_options; list_options = ['-l'] |
| 322 | (Pdb) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 323 | \end{verbatim} |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 325 | \item[q(uit)] |
Guido van Rossum | dc46c7f | 1995-03-01 15:38:16 +0000 | [diff] [blame] | 326 | |
| 327 | Quit from the debugger. |
| 328 | The program being executed is aborted. |
| 329 | |
| 330 | \end{description} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 331 | |
| 332 | \section{How It Works} |
| 333 | |
| 334 | Some changes were made to the interpreter: |
| 335 | |
| 336 | \begin{itemize} |
Fred Drake | 74947ac | 1998-01-12 15:38:30 +0000 | [diff] [blame] | 337 | \item \code{sys.settrace(\var{func})} sets the global trace function |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 338 | \item there can also a local trace function (see later) |
| 339 | \end{itemize} |
| 340 | |
Fred Drake | 82d493f | 1998-04-07 19:14:17 +0000 | [diff] [blame] | 341 | Trace functions have three arguments: \var{frame}, \var{event}, and |
| 342 | \var{arg}. \var{frame} is the current stack frame. \var{event} is a |
| 343 | string: \code{'call'}, \code{'line'}, \code{'return'} or |
| 344 | \code{'exception'}. \var{arg} depends on the event type. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | 9d37a4d | 1997-10-27 19:57:20 +0000 | [diff] [blame] | 346 | The global trace function is invoked (with \var{event} set to |
| 347 | \code{'call'}) whenever a new local scope is entered; it should return |
| 348 | a reference to the local trace function to be used that scope, or |
| 349 | \code{None} if the scope shouldn't be traced. |
| 350 | |
| 351 | The local trace function should return a reference to itself (or to |
| 352 | another function for further tracing in that scope), or \code{None} to |
| 353 | turn off tracing in that scope. |
| 354 | |
| 355 | Instance methods are accepted (and very useful!) as trace functions. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 356 | |
| 357 | The events have the following meaning: |
| 358 | |
| 359 | \begin{description} |
| 360 | |
| 361 | \item[\code{'call'}] |
| 362 | A function is called (or some other code block entered). The global |
| 363 | trace function is called; arg is the argument list to the function; |
| 364 | the return value specifies the local trace function. |
| 365 | |
| 366 | \item[\code{'line'}] |
| 367 | The interpreter is about to execute a new line of code (sometimes |
| 368 | multiple line events on one line exist). The local trace function is |
| 369 | called; arg in None; the return value specifies the new local trace |
| 370 | function. |
| 371 | |
| 372 | \item[\code{'return'}] |
| 373 | A function (or other code block) is about to return. The local trace |
| 374 | function is called; arg is the value that will be returned. The trace |
| 375 | function's return value is ignored. |
| 376 | |
| 377 | \item[\code{'exception'}] |
| 378 | An exception has occurred. The local trace function is called; arg is |
| 379 | a triple (exception, value, traceback); the return value specifies the |
| 380 | new local trace function |
| 381 | |
| 382 | \end{description} |
| 383 | |
| 384 | Note that as an exception is propagated down the chain of callers, an |
| 385 | \code{'exception'} event is generated at each level. |
| 386 | |
Fred Drake | bc8ad5b | 1998-03-11 06:29:59 +0000 | [diff] [blame] | 387 | For more information on code and frame objects, refer to the |
| 388 | \emph{Python Reference Manual}. |