Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _debugger: |
| 2 | |
Georg Brandl | 546e2d6 | 2007-09-12 18:04:37 +0000 | [diff] [blame] | 3 | :mod:`pdb` --- The Python Debugger |
| 4 | ================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | |
| 6 | .. module:: pdb |
| 7 | :synopsis: The Python debugger for interactive interpreters. |
| 8 | |
| 9 | |
| 10 | .. index:: single: debugging |
| 11 | |
| 12 | The module :mod:`pdb` defines an interactive source code debugger for Python |
| 13 | programs. It supports setting (conditional) breakpoints and single stepping at |
| 14 | the source line level, inspection of stack frames, source code listing, and |
| 15 | evaluation of arbitrary Python code in the context of any stack frame. It also |
| 16 | supports post-mortem debugging and can be called under program control. |
| 17 | |
| 18 | .. index:: |
| 19 | single: Pdb (class in pdb) |
| 20 | module: bdb |
| 21 | module: cmd |
| 22 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 23 | The debugger is extensible -- it is actually defined as the class :class:`Pdb`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | This is currently undocumented but easily understood by reading the source. The |
Georg Brandl | 1f70cdf | 2010-03-21 09:04:24 +0000 | [diff] [blame] | 25 | extension interface uses the modules :mod:`bdb` and :mod:`cmd`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | The debugger's prompt is ``(Pdb)``. Typical usage to run a program under control |
| 28 | of the debugger is:: |
| 29 | |
| 30 | >>> import pdb |
| 31 | >>> import mymodule |
| 32 | >>> pdb.run('mymodule.test()') |
| 33 | > <string>(0)?() |
| 34 | (Pdb) continue |
| 35 | > <string>(1)?() |
| 36 | (Pdb) continue |
| 37 | NameError: 'spam' |
| 38 | > <string>(1)?() |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 39 | (Pdb) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
| 41 | :file:`pdb.py` can also be invoked as a script to debug other scripts. For |
| 42 | example:: |
| 43 | |
Georg Brandl | 45bb63f | 2009-09-16 09:42:19 +0000 | [diff] [blame] | 44 | python3 -m pdb myscript.py |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | When invoked as a script, pdb will automatically enter post-mortem debugging if |
| 47 | the program being debugged exits abnormally. After post-mortem debugging (or |
| 48 | after normal exit of the program), pdb will restart the program. Automatic |
| 49 | restarting preserves pdb's state (such as breakpoints) and in most cases is more |
| 50 | useful than quitting the debugger upon program's exit. |
| 51 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 52 | The typical usage to break into the debugger from a running program is to |
| 53 | insert :: |
| 54 | |
| 55 | import pdb; pdb.set_trace() |
| 56 | |
| 57 | at the location you want to break into the debugger. You can then step through |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 58 | the code following this statement, and continue running without the debugger |
| 59 | using the :pdbcmd:`continue` command. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 60 | |
| 61 | The typical usage to inspect a crashed program is:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
| 63 | >>> import pdb |
| 64 | >>> import mymodule |
| 65 | >>> mymodule.test() |
| 66 | Traceback (most recent call last): |
| 67 | File "<stdin>", line 1, in ? |
| 68 | File "./mymodule.py", line 4, in test |
| 69 | test2() |
| 70 | File "./mymodule.py", line 3, in test2 |
Georg Brandl | c987924 | 2007-09-04 07:07:56 +0000 | [diff] [blame] | 71 | print(spam) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | NameError: spam |
| 73 | >>> pdb.pm() |
| 74 | > ./mymodule.py(3)test2() |
Georg Brandl | c987924 | 2007-09-04 07:07:56 +0000 | [diff] [blame] | 75 | -> print(spam) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 76 | (Pdb) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | The module defines the following functions; each enters the debugger in a |
| 80 | slightly different way: |
| 81 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 82 | .. function:: run(statement, globals=None, locals=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | Execute the *statement* (given as a string) under debugger control. The |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 85 | debugger prompt appears before any code is executed; you can set breakpoints |
| 86 | and type :pdbcmd:`continue`, or you can step through the statement using |
| 87 | :pdbcmd:`step` or :pdbcmd:`next` (all these commands are explained below). |
| 88 | The optional *globals* and *locals* arguments specify the environment in |
| 89 | which the code is executed; by default the dictionary of the module |
| 90 | :mod:`__main__` is used. (See the explanation of the built-in :func:`exec` |
| 91 | or :func:`eval` functions.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 93 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 94 | .. function:: runeval(expression, globals=None, locals=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | Evaluate the *expression* (given as a string) under debugger control. When |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 97 | :func:`runeval` returns, it returns the value of the expression. Otherwise |
| 98 | this function is similar to :func:`run`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | |
| 100 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 101 | .. function:: runcall(function, *args, **kwds) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 103 | Call the *function* (a function or method object, not a string) with the |
| 104 | given arguments. When :func:`runcall` returns, it returns whatever the |
| 105 | function call returned. The debugger prompt appears as soon as the function |
| 106 | is entered. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | .. function:: set_trace() |
| 110 | |
| 111 | Enter the debugger at the calling stack frame. This is useful to hard-code a |
| 112 | breakpoint at a given point in a program, even if the code is not otherwise |
| 113 | being debugged (e.g. when an assertion fails). |
| 114 | |
| 115 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 116 | .. function:: post_mortem(traceback=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 118 | Enter post-mortem debugging of the given *traceback* object. If no |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 119 | *traceback* is given, it uses the one of the exception that is currently |
| 120 | being handled (an exception must be being handled if the default is to be |
| 121 | used). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | .. function:: pm() |
| 125 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 126 | Enter post-mortem debugging of the traceback found in |
| 127 | :data:`sys.last_traceback`. |
| 128 | |
| 129 | |
| 130 | The ``run_*`` functions and :func:`set_trace` are aliases for instantiating the |
| 131 | :class:`Pdb` class and calling the method of the same name. If you want to |
| 132 | access further features, you have to do this yourself: |
| 133 | |
| 134 | .. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None) |
| 135 | |
| 136 | :class:`Pdb` is the debugger class. |
| 137 | |
| 138 | The *completekey*, *stdin* and *stdout* arguments are passed to the |
| 139 | underlying :class:`cmd.Cmd` class; see the description there. |
| 140 | |
| 141 | The *skip* argument, if given, must be an iterable of glob-style module name |
| 142 | patterns. The debugger will not step into frames that originate in a module |
| 143 | that matches one of these patterns. [1]_ |
| 144 | |
| 145 | Example call to enable tracing with *skip*:: |
| 146 | |
| 147 | import pdb; pdb.Pdb(skip=['django.*']).set_trace() |
| 148 | |
Georg Brandl | 705d9d5 | 2009-05-05 09:29:50 +0000 | [diff] [blame] | 149 | .. versionadded:: 3.1 |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 150 | The *skip* argument. |
| 151 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 152 | .. method:: run(statement, globals=None, locals=None) |
| 153 | runeval(expression, globals=None, locals=None) |
| 154 | runcall(function, *args, **kwds) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 155 | set_trace() |
| 156 | |
| 157 | See the documentation for the functions explained above. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | .. _debugger-commands: |
| 161 | |
| 162 | Debugger Commands |
| 163 | ================= |
| 164 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 165 | The commands recognized by the debugger are listed below. Most commands can be |
| 166 | abbreviated to one or two letters as indicated; e.g. ``h(elp)`` means that |
| 167 | either ``h`` or ``help`` can be used to enter the help command (but not ``he`` |
| 168 | or ``hel``, nor ``H`` or ``Help`` or ``HELP``). Arguments to commands must be |
| 169 | separated by whitespace (spaces or tabs). Optional arguments are enclosed in |
| 170 | square brackets (``[]``) in the command syntax; the square brackets must not be |
| 171 | typed. Alternatives in the command syntax are separated by a vertical bar |
| 172 | (``|``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
| 174 | Entering a blank line repeats the last command entered. Exception: if the last |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 175 | command was a :pdbcmd:`list` command, the next 11 lines are listed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
| 177 | Commands that the debugger doesn't recognize are assumed to be Python statements |
| 178 | and are executed in the context of the program being debugged. Python |
| 179 | statements can also be prefixed with an exclamation point (``!``). This is a |
| 180 | powerful way to inspect the program being debugged; it is even possible to |
| 181 | change a variable or call a function. When an exception occurs in such a |
| 182 | statement, the exception name is printed but the debugger's state is not |
| 183 | changed. |
| 184 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 185 | The debugger supports :ref:`aliases <debugger-aliases>`. Aliases can have |
| 186 | parameters which allows one a certain level of adaptability to the context under |
| 187 | examination. |
| 188 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | Multiple commands may be entered on a single line, separated by ``;;``. (A |
| 190 | single ``;`` is not used as it is the separator for multiple commands in a line |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 191 | that is passed to the Python parser.) No intelligence is applied to separating |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | the commands; the input is split at the first ``;;`` pair, even if it is in the |
| 193 | middle of a quoted string. |
| 194 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 195 | .. index:: |
| 196 | pair: .pdbrc; file |
| 197 | triple: debugger; configuration; file |
| 198 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 199 | If a file :file:`.pdbrc` exists in the user's home directory or in the current |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | directory, it is read in and executed as if it had been typed at the debugger |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 201 | prompt. This is particularly useful for aliases. If both files exist, the one |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | in the home directory is read first and aliases defined there can be overridden |
| 203 | by the local file. |
| 204 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 205 | |
| 206 | .. pdbcommand:: h(elp) [command] |
| 207 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | Without argument, print the list of available commands. With a *command* as |
| 209 | argument, print help about that command. ``help pdb`` displays the full |
Georg Brandl | 55353ca | 2010-07-19 08:02:46 +0000 | [diff] [blame] | 210 | documentation (the docstring of the :mod:`pdb` module). Since the *command* |
| 211 | argument must be an identifier, ``help exec`` must be entered to get help on |
| 212 | the ``!`` command. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 214 | .. pdbcommand:: w(here) |
| 215 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 216 | Print a stack trace, with the most recent frame at the bottom. An arrow |
| 217 | indicates the current frame, which determines the context of most commands. |
| 218 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 219 | .. pdbcommand:: d(own) [count] |
| 220 | |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 221 | Move the current frame *count* (default one) levels down in the stack trace |
| 222 | (to a newer frame). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 224 | .. pdbcommand:: u(p) [count] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 226 | Move the current frame *count* (default one) levels up in the stack trace (to |
| 227 | an older frame). |
| 228 | |
| 229 | .. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]] |
| 230 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | With a *lineno* argument, set a break there in the current file. With a |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 232 | *function* argument, set a break at the first executable statement within |
| 233 | that function. The line number may be prefixed with a filename and a colon, |
| 234 | to specify a breakpoint in another file (probably one that hasn't been loaded |
| 235 | yet). The file is searched on :data:`sys.path`. Note that each breakpoint |
| 236 | is assigned a number to which all the other breakpoint commands refer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 238 | If a second argument is present, it is an expression which must evaluate to |
| 239 | true before the breakpoint is honored. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 241 | Without argument, list all breaks, including for each breakpoint, the number |
| 242 | of times that breakpoint has been hit, the current ignore count, and the |
| 243 | associated condition if any. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 245 | .. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 246 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 247 | Temporary breakpoint, which is removed automatically when it is first hit. |
| 248 | The arguments are the same as for :pdbcmd:`break`. |
| 249 | |
| 250 | .. pdbcommand:: cl(ear) [bpnumber [bpnumber ...]] |
| 251 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | With a space separated list of breakpoint numbers, clear those breakpoints. |
| 253 | Without argument, clear all breaks (but first ask confirmation). |
| 254 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 255 | .. pdbcommand:: disable [bpnumber [bpnumber ...]] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 257 | Disable the breakpoints given as a space separated list of breakpoint |
| 258 | numbers. Disabling a breakpoint means it cannot cause the program to stop |
| 259 | execution, but unlike clearing a breakpoint, it remains in the list of |
| 260 | breakpoints and can be (re-)enabled. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 261 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 262 | .. pdbcommand:: enable [bpnumber [bpnumber ...]] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 263 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 264 | Enable the breakpoints specified. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 266 | .. pdbcommand:: ignore bpnumber [count] |
| 267 | |
| 268 | Set the ignore count for the given breakpoint number. If count is omitted, |
| 269 | the ignore count is set to 0. A breakpoint becomes active when the ignore |
| 270 | count is zero. When non-zero, the count is decremented each time the |
| 271 | breakpoint is reached and the breakpoint is not disabled and any associated |
| 272 | condition evaluates to true. |
| 273 | |
| 274 | .. pdbcommand:: condition bpnumber [condition] |
| 275 | |
| 276 | Set a new *condition* for the breakpoint, an expression which must evaluate |
| 277 | to true before the breakpoint is honored. If *condition* is absent, any |
| 278 | existing condition is removed; i.e., the breakpoint is made unconditional. |
| 279 | |
| 280 | .. pdbcommand:: commands [bpnumber] |
| 281 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | Specify a list of commands for breakpoint number *bpnumber*. The commands |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 283 | themselves appear on the following lines. Type a line containing just |
| 284 | ``end`` to terminate the commands. An example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | |
| 286 | (Pdb) commands 1 |
| 287 | (com) print some_variable |
| 288 | (com) end |
| 289 | (Pdb) |
| 290 | |
| 291 | To remove all commands from a breakpoint, type commands and follow it |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 292 | immediately with ``end``; that is, give no commands. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | |
| 294 | With no *bpnumber* argument, commands refers to the last breakpoint set. |
| 295 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 296 | You can use breakpoint commands to start your program up again. Simply use |
| 297 | the continue command, or step, or any other command that resumes execution. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
| 299 | Specifying any command resuming execution (currently continue, step, next, |
| 300 | return, jump, quit and their abbreviations) terminates the command list (as if |
| 301 | that command was immediately followed by end). This is because any time you |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 302 | resume execution (even with a simple next or step), you may encounter another |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 303 | breakpoint--which could have its own command list, leading to ambiguities about |
| 304 | which list to execute. |
| 305 | |
| 306 | If you use the 'silent' command in the command list, the usual message about |
| 307 | stopping at a breakpoint is not printed. This may be desirable for breakpoints |
| 308 | that are to print a specific message and then continue. If none of the other |
| 309 | commands print anything, you see no sign that the breakpoint was reached. |
| 310 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 311 | .. pdbcommand:: s(tep) |
| 312 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | Execute the current line, stop at the first possible occasion (either in a |
| 314 | function that is called or on the next line in the current function). |
| 315 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 316 | .. pdbcommand:: n(ext) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 318 | Continue execution until the next line in the current function is reached or |
| 319 | it returns. (The difference between :pdbcmd:`next` and :pdbcmd:`step` is |
| 320 | that :pdbcmd:`step` stops inside a called function, while :pdbcmd:`next` |
| 321 | executes called functions at (nearly) full speed, only stopping at the next |
| 322 | line in the current function.) |
| 323 | |
| 324 | .. pdbcommand:: unt(il) |
| 325 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 326 | Continue execution until the line with the line number greater than the |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 327 | current one is reached or when returning from current frame. |
| 328 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 329 | .. pdbcommand:: r(eturn) |
| 330 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 331 | Continue execution until the current function returns. |
| 332 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 333 | .. pdbcommand:: c(ont(inue)) |
| 334 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 335 | Continue execution, only stop when a breakpoint is encountered. |
| 336 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 337 | .. pdbcommand:: j(ump) lineno |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 339 | Set the next line that will be executed. Only available in the bottom-most |
| 340 | frame. This lets you jump back and execute code again, or jump forward to |
| 341 | skip code that you don't want to run. |
| 342 | |
| 343 | It should be noted that not all jumps are allowed -- for instance it is not |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | possible to jump into the middle of a :keyword:`for` loop or out of a |
| 345 | :keyword:`finally` clause. |
| 346 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 347 | .. pdbcommand:: l(ist) [first[, last]] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 349 | List source code for the current file. Without arguments, list 11 lines |
Georg Brandl | a5eacee | 2010-07-23 16:55:26 +0000 | [diff] [blame^] | 350 | around the current line or continue the previous listing. With ``.`` as |
| 351 | argument, list 11 lines around the current line. With one argument, |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 352 | list 11 lines around at that line. With two arguments, list the given range; |
| 353 | if the second argument is less than the first, it is interpreted as a count. |
| 354 | |
| 355 | .. pdbcommand:: a(rgs) |
| 356 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | Print the argument list of the current function. |
| 358 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 359 | .. pdbcommand:: p(rint) expression |
| 360 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | Evaluate the *expression* in the current context and print its value. |
| 362 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 363 | .. pdbcommand:: pp expression |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 365 | Like the :pdbcmd:`print` command, except the value of the expression is |
| 366 | pretty-printed using the :mod:`pprint` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 368 | .. pdbcommand:: whatis expression |
| 369 | |
| 370 | Print the type of the *expression*. |
| 371 | |
| 372 | .. _debugger-aliases: |
| 373 | |
| 374 | .. pdbcommand:: alias [name [command]] |
| 375 | |
| 376 | Create an alias called *name* that executes *command*. The command must |
| 377 | *not* be enclosed in quotes. Replaceable parameters can be indicated by |
| 378 | ``%1``, ``%2``, and so on, while ``%*`` is replaced by all the parameters. |
| 379 | If no command is given, the current alias for *name* is shown. If no |
| 380 | arguments are given, all aliases are listed. |
| 381 | |
| 382 | Aliases may be nested and can contain anything that can be legally typed at |
| 383 | the pdb prompt. Note that internal pdb commands *can* be overridden by |
| 384 | aliases. Such a command is then hidden until the alias is removed. Aliasing |
| 385 | is recursively applied to the first word of the command line; all other words |
| 386 | in the line are left alone. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
| 388 | As an example, here are two useful aliases (especially when placed in the |
| 389 | :file:`.pdbrc` file):: |
| 390 | |
| 391 | #Print instance variables (usage "pi classInst") |
Georg Brandl | c987924 | 2007-09-04 07:07:56 +0000 | [diff] [blame] | 392 | alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | #Print instance variables in self |
| 394 | alias ps pi self |
| 395 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 396 | .. pdbcommand:: unalias name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 398 | Delete the specified alias. |
| 399 | |
| 400 | .. pdbcommand:: ! statement |
| 401 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 402 | Execute the (one-line) *statement* in the context of the current stack frame. |
| 403 | The exclamation point can be omitted unless the first word of the statement |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 404 | resembles a debugger command. To set a global variable, you can prefix the |
| 405 | assignment command with a :keyword:`global` statement on the same line, |
| 406 | e.g.:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
| 408 | (Pdb) global list_options; list_options = ['-l'] |
| 409 | (Pdb) |
| 410 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 411 | .. pdbcommand:: run [args ...] |
| 412 | restart [args ...] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 414 | Restart the debugged Python program. If an argument is supplied, it is split |
| 415 | with :mod:`shlex` and the result is used as the new :data:`sys.argv`. |
| 416 | History, breakpoints, actions and debugger options are preserved. |
| 417 | :pdbcmd:`restart` is an alias for :pdbcmd:`run`. |
| 418 | |
| 419 | .. pdbcommand:: q(uit) |
| 420 | |
| 421 | Quit from the debugger. The program being executed is aborted. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 422 | |
| 423 | |
| 424 | .. rubric:: Footnotes |
| 425 | |
| 426 | .. [1] Whether a frame is considered to originate in a certain module |
| 427 | is determined by the ``__name__`` in the frame globals. |