blob: 7ebad79130ae32e40c7e38d8048593a9b95e9b7b [file] [log] [blame]
Georg Brandlb98273f2010-10-29 05:24:24 +00001.. _debugger:
2
Georg Brandl546e2d62007-09-12 18:04:37 +00003:mod:`pdb` --- The Python Debugger
4==================================
Georg Brandl116aa622007-08-15 14:28:22 +00005
6.. module:: pdb
7 :synopsis: The Python debugger for interactive interpreters.
8
Benjamin Peterson45d929d2014-09-30 16:02:06 -04009**Source code:** :source:`Lib/pdb.py`
10
Georg Brandl116aa622007-08-15 14:28:22 +000011.. index:: single: debugging
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015The module :mod:`pdb` defines an interactive source code debugger for Python
16programs. It supports setting (conditional) breakpoints and single stepping at
17the source line level, inspection of stack frames, source code listing, and
18evaluation of arbitrary Python code in the context of any stack frame. It also
19supports post-mortem debugging and can be called under program control.
20
21.. index::
22 single: Pdb (class in pdb)
23 module: bdb
24 module: cmd
25
Georg Brandl02053ee2010-07-18 10:11:03 +000026The debugger is extensible -- it is actually defined as the class :class:`Pdb`.
Georg Brandl116aa622007-08-15 14:28:22 +000027This is currently undocumented but easily understood by reading the source. The
Georg Brandl1f70cdf2010-03-21 09:04:24 +000028extension interface uses the modules :mod:`bdb` and :mod:`cmd`.
Georg Brandl116aa622007-08-15 14:28:22 +000029
30The debugger's prompt is ``(Pdb)``. Typical usage to run a program under control
31of the debugger is::
32
33 >>> import pdb
34 >>> import mymodule
35 >>> pdb.run('mymodule.test()')
36 > <string>(0)?()
37 (Pdb) continue
38 > <string>(1)?()
39 (Pdb) continue
40 NameError: 'spam'
41 > <string>(1)?()
Georg Brandl48310cd2009-01-03 21:18:54 +000042 (Pdb)
Georg Brandl116aa622007-08-15 14:28:22 +000043
Georg Brandl4c7c3c52012-03-10 22:36:48 +010044.. versionchanged:: 3.3
45 Tab-completion via the :mod:`readline` module is available for commands and
46 command arguments, e.g. the current global and local names are offered as
R David Murray78d692f2013-10-10 17:23:26 -040047 arguments of the ``p`` command.
Georg Brandl4c7c3c52012-03-10 22:36:48 +010048
Georg Brandl116aa622007-08-15 14:28:22 +000049:file:`pdb.py` can also be invoked as a script to debug other scripts. For
50example::
51
Georg Brandl45bb63f2009-09-16 09:42:19 +000052 python3 -m pdb myscript.py
Georg Brandl116aa622007-08-15 14:28:22 +000053
54When invoked as a script, pdb will automatically enter post-mortem debugging if
Georg Brandle0230912010-07-30 08:29:39 +000055the program being debugged exits abnormally. After post-mortem debugging (or
56after normal exit of the program), pdb will restart the program. Automatic
Georg Brandl116aa622007-08-15 14:28:22 +000057restarting preserves pdb's state (such as breakpoints) and in most cases is more
58useful than quitting the debugger upon program's exit.
59
Georg Brandle0230912010-07-30 08:29:39 +000060.. versionadded:: 3.2
61 :file:`pdb.py` now accepts a ``-c`` option that executes commands as if given
62 in a :file:`.pdbrc` file, see :ref:`debugger-commands`.
63
Mario Corchero9f1e5f12018-01-06 07:53:05 +000064.. versionadded:: 3.7
65 :file:`pdb.py` now accepts a ``-m`` option that execute modules similar to the way
66 ``python3 -m`` does. As with a script, the debugger will pause execution just
67 before the first line of the module.
68
69
Georg Brandl243ad662009-05-05 09:00:19 +000070The typical usage to break into the debugger from a running program is to
71insert ::
72
73 import pdb; pdb.set_trace()
74
75at the location you want to break into the debugger. You can then step through
Georg Brandl02053ee2010-07-18 10:11:03 +000076the code following this statement, and continue running without the debugger
77using the :pdbcmd:`continue` command.
Georg Brandl243ad662009-05-05 09:00:19 +000078
João Matoscf991e62019-01-30 17:23:39 +000079.. versionadded:: 3.7
80 The built-in :func:`breakpoint()`, when called with defaults, can be used
81 instead of ``import pdb; pdb.set_trace()``.
82
Georg Brandl243ad662009-05-05 09:00:19 +000083The typical usage to inspect a crashed program is::
Georg Brandl116aa622007-08-15 14:28:22 +000084
85 >>> import pdb
86 >>> import mymodule
87 >>> mymodule.test()
88 Traceback (most recent call last):
UltimateCoder88569402017-05-03 22:16:45 +053089 File "<stdin>", line 1, in <module>
Georg Brandl116aa622007-08-15 14:28:22 +000090 File "./mymodule.py", line 4, in test
91 test2()
92 File "./mymodule.py", line 3, in test2
Georg Brandlc9879242007-09-04 07:07:56 +000093 print(spam)
Georg Brandl116aa622007-08-15 14:28:22 +000094 NameError: spam
95 >>> pdb.pm()
96 > ./mymodule.py(3)test2()
Georg Brandlc9879242007-09-04 07:07:56 +000097 -> print(spam)
Georg Brandl48310cd2009-01-03 21:18:54 +000098 (Pdb)
Georg Brandl116aa622007-08-15 14:28:22 +000099
Georg Brandl243ad662009-05-05 09:00:19 +0000100
Georg Brandl116aa622007-08-15 14:28:22 +0000101The module defines the following functions; each enters the debugger in a
102slightly different way:
103
Georg Brandl18244152009-09-02 20:34:52 +0000104.. function:: run(statement, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandl46b9afc2010-07-30 09:14:20 +0000106 Execute the *statement* (given as a string or a code object) under debugger
107 control. The debugger prompt appears before any code is executed; you can
108 set breakpoints and type :pdbcmd:`continue`, or you can step through the
109 statement using :pdbcmd:`step` or :pdbcmd:`next` (all these commands are
110 explained below). The optional *globals* and *locals* arguments specify the
111 environment in which the code is executed; by default the dictionary of the
112 module :mod:`__main__` is used. (See the explanation of the built-in
113 :func:`exec` or :func:`eval` functions.)
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115
Georg Brandl18244152009-09-02 20:34:52 +0000116.. function:: runeval(expression, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Georg Brandl46b9afc2010-07-30 09:14:20 +0000118 Evaluate the *expression* (given as a string or a code object) under debugger
119 control. When :func:`runeval` returns, it returns the value of the
120 expression. Otherwise this function is similar to :func:`run`.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
Georg Brandl18244152009-09-02 20:34:52 +0000123.. function:: runcall(function, *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Georg Brandl02053ee2010-07-18 10:11:03 +0000125 Call the *function* (a function or method object, not a string) with the
126 given arguments. When :func:`runcall` returns, it returns whatever the
127 function call returned. The debugger prompt appears as soon as the function
128 is entered.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
Barry Warsaw35425d62017-09-22 12:29:42 -0400131.. function:: set_trace(*, header=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Barry Warsaw35425d62017-09-22 12:29:42 -0400133 Enter the debugger at the calling stack frame. This is useful to hard-code
134 a breakpoint at a given point in a program, even if the code is not
135 otherwise being debugged (e.g. when an assertion fails). If given,
Berker Peksag12d60562017-11-04 15:17:56 +0300136 *header* is printed to the console just before debugging begins.
Barry Warsaw35425d62017-09-22 12:29:42 -0400137
Berker Peksag12d60562017-11-04 15:17:56 +0300138 .. versionchanged:: 3.7
139 The keyword-only argument *header*.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
Georg Brandl18244152009-09-02 20:34:52 +0000142.. function:: post_mortem(traceback=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Georg Brandl48310cd2009-01-03 21:18:54 +0000144 Enter post-mortem debugging of the given *traceback* object. If no
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000145 *traceback* is given, it uses the one of the exception that is currently
146 being handled (an exception must be being handled if the default is to be
147 used).
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
150.. function:: pm()
151
Georg Brandl243ad662009-05-05 09:00:19 +0000152 Enter post-mortem debugging of the traceback found in
153 :data:`sys.last_traceback`.
154
155
Eli Bendersky489f3922011-01-14 08:25:03 +0000156The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
Georg Brandl243ad662009-05-05 09:00:19 +0000157:class:`Pdb` class and calling the method of the same name. If you want to
158access further features, you have to do this yourself:
159
Georg Brandl44f2b642010-12-04 16:00:47 +0000160.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700161 nosigint=False, readrc=True)
Georg Brandl243ad662009-05-05 09:00:19 +0000162
163 :class:`Pdb` is the debugger class.
164
165 The *completekey*, *stdin* and *stdout* arguments are passed to the
166 underlying :class:`cmd.Cmd` class; see the description there.
167
168 The *skip* argument, if given, must be an iterable of glob-style module name
169 patterns. The debugger will not step into frames that originate in a module
170 that matches one of these patterns. [1]_
171
Georg Brandl44f2b642010-12-04 16:00:47 +0000172 By default, Pdb sets a handler for the SIGINT signal (which is sent when the
Serhiy Storchaka0424eaf2015-09-12 17:45:25 +0300173 user presses :kbd:`Ctrl-C` on the console) when you give a ``continue`` command.
174 This allows you to break into the debugger again by pressing :kbd:`Ctrl-C`. If you
Terry Jan Reedyde260a62016-08-24 17:52:57 -0400175 want Pdb not to touch the SIGINT handler, set *nosigint* to true.
Georg Brandl44f2b642010-12-04 16:00:47 +0000176
Berker Peksag2d7250b2016-09-10 09:28:03 +0300177 The *readrc* argument defaults to true and controls whether Pdb will load
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700178 .pdbrc files from the filesystem.
179
Georg Brandl243ad662009-05-05 09:00:19 +0000180 Example call to enable tracing with *skip*::
181
182 import pdb; pdb.Pdb(skip=['django.*']).set_trace()
183
Steve Dower60419a72019-06-24 08:42:54 -0700184 .. audit-event:: pdb.Pdb
185
Georg Brandl705d9d52009-05-05 09:29:50 +0000186 .. versionadded:: 3.1
Georg Brandl243ad662009-05-05 09:00:19 +0000187 The *skip* argument.
188
Georg Brandl44f2b642010-12-04 16:00:47 +0000189 .. versionadded:: 3.2
190 The *nosigint* argument. Previously, a SIGINT handler was never set by
191 Pdb.
192
Berker Peksag2d7250b2016-09-10 09:28:03 +0300193 .. versionchanged:: 3.6
Łukasz Langa2eb6eca2016-09-09 22:21:17 -0700194 The *readrc* argument.
195
Georg Brandl18244152009-09-02 20:34:52 +0000196 .. method:: run(statement, globals=None, locals=None)
197 runeval(expression, globals=None, locals=None)
198 runcall(function, *args, **kwds)
Georg Brandl243ad662009-05-05 09:00:19 +0000199 set_trace()
200
201 See the documentation for the functions explained above.
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203
204.. _debugger-commands:
205
206Debugger Commands
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +0000207-----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Georg Brandl02053ee2010-07-18 10:11:03 +0000209The commands recognized by the debugger are listed below. Most commands can be
210abbreviated to one or two letters as indicated; e.g. ``h(elp)`` means that
211either ``h`` or ``help`` can be used to enter the help command (but not ``he``
212or ``hel``, nor ``H`` or ``Help`` or ``HELP``). Arguments to commands must be
213separated by whitespace (spaces or tabs). Optional arguments are enclosed in
214square brackets (``[]``) in the command syntax; the square brackets must not be
215typed. Alternatives in the command syntax are separated by a vertical bar
216(``|``).
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218Entering a blank line repeats the last command entered. Exception: if the last
Georg Brandl02053ee2010-07-18 10:11:03 +0000219command was a :pdbcmd:`list` command, the next 11 lines are listed.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221Commands that the debugger doesn't recognize are assumed to be Python statements
222and are executed in the context of the program being debugged. Python
223statements can also be prefixed with an exclamation point (``!``). This is a
224powerful way to inspect the program being debugged; it is even possible to
225change a variable or call a function. When an exception occurs in such a
226statement, the exception name is printed but the debugger's state is not
227changed.
228
Georg Brandl02053ee2010-07-18 10:11:03 +0000229The debugger supports :ref:`aliases <debugger-aliases>`. Aliases can have
230parameters which allows one a certain level of adaptability to the context under
231examination.
232
Georg Brandl116aa622007-08-15 14:28:22 +0000233Multiple commands may be entered on a single line, separated by ``;;``. (A
234single ``;`` is not used as it is the separator for multiple commands in a line
Georg Brandl02053ee2010-07-18 10:11:03 +0000235that is passed to the Python parser.) No intelligence is applied to separating
Georg Brandl116aa622007-08-15 14:28:22 +0000236the commands; the input is split at the first ``;;`` pair, even if it is in the
237middle of a quoted string.
238
Georg Brandl116aa622007-08-15 14:28:22 +0000239.. index::
240 pair: .pdbrc; file
241 triple: debugger; configuration; file
242
Georg Brandl02053ee2010-07-18 10:11:03 +0000243If a file :file:`.pdbrc` exists in the user's home directory or in the current
Georg Brandl116aa622007-08-15 14:28:22 +0000244directory, it is read in and executed as if it had been typed at the debugger
Georg Brandl02053ee2010-07-18 10:11:03 +0000245prompt. This is particularly useful for aliases. If both files exist, the one
Georg Brandl116aa622007-08-15 14:28:22 +0000246in the home directory is read first and aliases defined there can be overridden
247by the local file.
248
Georg Brandle0230912010-07-30 08:29:39 +0000249.. versionchanged:: 3.2
250 :file:`.pdbrc` can now contain commands that continue debugging, such as
251 :pdbcmd:`continue` or :pdbcmd:`next`. Previously, these commands had no
252 effect.
253
Georg Brandl02053ee2010-07-18 10:11:03 +0000254
255.. pdbcommand:: h(elp) [command]
256
Georg Brandl116aa622007-08-15 14:28:22 +0000257 Without argument, print the list of available commands. With a *command* as
258 argument, print help about that command. ``help pdb`` displays the full
Georg Brandl55353ca2010-07-19 08:02:46 +0000259 documentation (the docstring of the :mod:`pdb` module). Since the *command*
260 argument must be an identifier, ``help exec`` must be entered to get help on
261 the ``!`` command.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Georg Brandl02053ee2010-07-18 10:11:03 +0000263.. pdbcommand:: w(here)
264
Georg Brandl116aa622007-08-15 14:28:22 +0000265 Print a stack trace, with the most recent frame at the bottom. An arrow
266 indicates the current frame, which determines the context of most commands.
267
Georg Brandl02053ee2010-07-18 10:11:03 +0000268.. pdbcommand:: d(own) [count]
269
Georg Brandleb1f4aa2010-06-27 10:37:48 +0000270 Move the current frame *count* (default one) levels down in the stack trace
271 (to a newer frame).
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Georg Brandl02053ee2010-07-18 10:11:03 +0000273.. pdbcommand:: u(p) [count]
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Georg Brandl02053ee2010-07-18 10:11:03 +0000275 Move the current frame *count* (default one) levels up in the stack trace (to
276 an older frame).
277
278.. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]]
279
Georg Brandl116aa622007-08-15 14:28:22 +0000280 With a *lineno* argument, set a break there in the current file. With a
Georg Brandl02053ee2010-07-18 10:11:03 +0000281 *function* argument, set a break at the first executable statement within
282 that function. The line number may be prefixed with a filename and a colon,
283 to specify a breakpoint in another file (probably one that hasn't been loaded
284 yet). The file is searched on :data:`sys.path`. Note that each breakpoint
285 is assigned a number to which all the other breakpoint commands refer.
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Georg Brandl02053ee2010-07-18 10:11:03 +0000287 If a second argument is present, it is an expression which must evaluate to
288 true before the breakpoint is honored.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
Georg Brandl02053ee2010-07-18 10:11:03 +0000290 Without argument, list all breaks, including for each breakpoint, the number
291 of times that breakpoint has been hit, the current ignore count, and the
292 associated condition if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000293
Georg Brandl02053ee2010-07-18 10:11:03 +0000294.. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]]
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Georg Brandl02053ee2010-07-18 10:11:03 +0000296 Temporary breakpoint, which is removed automatically when it is first hit.
297 The arguments are the same as for :pdbcmd:`break`.
298
Senthil Kumaran6f107042010-11-29 11:54:17 +0000299.. pdbcommand:: cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
Georg Brandl02053ee2010-07-18 10:11:03 +0000300
Senthil Kumaran6f107042010-11-29 11:54:17 +0000301 With a *filename:lineno* argument, clear all the breakpoints at this line.
Georg Brandl116aa622007-08-15 14:28:22 +0000302 With a space separated list of breakpoint numbers, clear those breakpoints.
303 Without argument, clear all breaks (but first ask confirmation).
304
Georg Brandl02053ee2010-07-18 10:11:03 +0000305.. pdbcommand:: disable [bpnumber [bpnumber ...]]
Georg Brandl116aa622007-08-15 14:28:22 +0000306
Georg Brandl02053ee2010-07-18 10:11:03 +0000307 Disable the breakpoints given as a space separated list of breakpoint
308 numbers. Disabling a breakpoint means it cannot cause the program to stop
309 execution, but unlike clearing a breakpoint, it remains in the list of
310 breakpoints and can be (re-)enabled.
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Georg Brandl02053ee2010-07-18 10:11:03 +0000312.. pdbcommand:: enable [bpnumber [bpnumber ...]]
Georg Brandl116aa622007-08-15 14:28:22 +0000313
Georg Brandl02053ee2010-07-18 10:11:03 +0000314 Enable the breakpoints specified.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Georg Brandl02053ee2010-07-18 10:11:03 +0000316.. pdbcommand:: ignore bpnumber [count]
317
318 Set the ignore count for the given breakpoint number. If count is omitted,
319 the ignore count is set to 0. A breakpoint becomes active when the ignore
320 count is zero. When non-zero, the count is decremented each time the
321 breakpoint is reached and the breakpoint is not disabled and any associated
322 condition evaluates to true.
323
324.. pdbcommand:: condition bpnumber [condition]
325
326 Set a new *condition* for the breakpoint, an expression which must evaluate
327 to true before the breakpoint is honored. If *condition* is absent, any
328 existing condition is removed; i.e., the breakpoint is made unconditional.
329
330.. pdbcommand:: commands [bpnumber]
331
Georg Brandl116aa622007-08-15 14:28:22 +0000332 Specify a list of commands for breakpoint number *bpnumber*. The commands
Georg Brandl02053ee2010-07-18 10:11:03 +0000333 themselves appear on the following lines. Type a line containing just
334 ``end`` to terminate the commands. An example::
Georg Brandl116aa622007-08-15 14:28:22 +0000335
336 (Pdb) commands 1
R David Murray78d692f2013-10-10 17:23:26 -0400337 (com) p some_variable
Georg Brandl116aa622007-08-15 14:28:22 +0000338 (com) end
339 (Pdb)
340
Stéphane Wirtel67adb312018-01-30 10:34:33 +0100341 To remove all commands from a breakpoint, type ``commands`` and follow it
Georg Brandl02053ee2010-07-18 10:11:03 +0000342 immediately with ``end``; that is, give no commands.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Stéphane Wirtel67adb312018-01-30 10:34:33 +0100344 With no *bpnumber* argument, ``commands`` refers to the last breakpoint set.
Georg Brandl116aa622007-08-15 14:28:22 +0000345
Georg Brandl02053ee2010-07-18 10:11:03 +0000346 You can use breakpoint commands to start your program up again. Simply use
Stéphane Wirtel67adb312018-01-30 10:34:33 +0100347 the :pdbcmd:`continue` command, or :pdbcmd:`step`,
348 or any other command that resumes execution.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Stéphane Wirtel67adb312018-01-30 10:34:33 +0100350 Specifying any command resuming execution
351 (currently :pdbcmd:`continue`, :pdbcmd:`step`, :pdbcmd:`next`,
352 :pdbcmd:`return`, :pdbcmd:`jump`, :pdbcmd:`quit` and their abbreviations)
Julien Palard7943c5e2018-07-06 11:15:13 +0200353 terminates the command list (as if
Georg Brandl116aa622007-08-15 14:28:22 +0000354 that command was immediately followed by end). This is because any time you
Georg Brandl9afde1c2007-11-01 20:32:30 +0000355 resume execution (even with a simple next or step), you may encounter another
Martin Panter357ed2e2016-11-21 00:15:20 +0000356 breakpoint—which could have its own command list, leading to ambiguities about
Georg Brandl116aa622007-08-15 14:28:22 +0000357 which list to execute.
358
359 If you use the 'silent' command in the command list, the usual message about
360 stopping at a breakpoint is not printed. This may be desirable for breakpoints
361 that are to print a specific message and then continue. If none of the other
362 commands print anything, you see no sign that the breakpoint was reached.
363
Georg Brandl02053ee2010-07-18 10:11:03 +0000364.. pdbcommand:: s(tep)
365
Georg Brandl116aa622007-08-15 14:28:22 +0000366 Execute the current line, stop at the first possible occasion (either in a
367 function that is called or on the next line in the current function).
368
Georg Brandl02053ee2010-07-18 10:11:03 +0000369.. pdbcommand:: n(ext)
Georg Brandl116aa622007-08-15 14:28:22 +0000370
Georg Brandl02053ee2010-07-18 10:11:03 +0000371 Continue execution until the next line in the current function is reached or
372 it returns. (The difference between :pdbcmd:`next` and :pdbcmd:`step` is
373 that :pdbcmd:`step` stops inside a called function, while :pdbcmd:`next`
374 executes called functions at (nearly) full speed, only stopping at the next
375 line in the current function.)
376
Georg Brandl2dfec552010-07-30 08:43:32 +0000377.. pdbcommand:: unt(il) [lineno]
Georg Brandl02053ee2010-07-18 10:11:03 +0000378
Georg Brandl2dfec552010-07-30 08:43:32 +0000379 Without argument, continue execution until the line with a number greater
380 than the current one is reached.
381
382 With a line number, continue execution until a line with a number greater or
383 equal to that is reached. In both cases, also stop when the current frame
384 returns.
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000385
Georg Brandl26a0f872010-07-30 08:45:26 +0000386 .. versionchanged:: 3.2
387 Allow giving an explicit line number.
388
Georg Brandl02053ee2010-07-18 10:11:03 +0000389.. pdbcommand:: r(eturn)
390
Georg Brandl116aa622007-08-15 14:28:22 +0000391 Continue execution until the current function returns.
392
Georg Brandl02053ee2010-07-18 10:11:03 +0000393.. pdbcommand:: c(ont(inue))
394
Georg Brandl116aa622007-08-15 14:28:22 +0000395 Continue execution, only stop when a breakpoint is encountered.
396
Georg Brandl02053ee2010-07-18 10:11:03 +0000397.. pdbcommand:: j(ump) lineno
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Georg Brandl02053ee2010-07-18 10:11:03 +0000399 Set the next line that will be executed. Only available in the bottom-most
400 frame. This lets you jump back and execute code again, or jump forward to
401 skip code that you don't want to run.
402
403 It should be noted that not all jumps are allowed -- for instance it is not
Georg Brandl116aa622007-08-15 14:28:22 +0000404 possible to jump into the middle of a :keyword:`for` loop or out of a
405 :keyword:`finally` clause.
406
Georg Brandl02053ee2010-07-18 10:11:03 +0000407.. pdbcommand:: l(ist) [first[, last]]
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Georg Brandl02053ee2010-07-18 10:11:03 +0000409 List source code for the current file. Without arguments, list 11 lines
Georg Brandla5eacee2010-07-23 16:55:26 +0000410 around the current line or continue the previous listing. With ``.`` as
411 argument, list 11 lines around the current line. With one argument,
Georg Brandl02053ee2010-07-18 10:11:03 +0000412 list 11 lines around at that line. With two arguments, list the given range;
413 if the second argument is less than the first, it is interpreted as a count.
414
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000415 The current line in the current frame is indicated by ``->``. If an
416 exception is being debugged, the line where the exception was originally
417 raised or propagated is indicated by ``>>``, if it differs from the current
418 line.
419
420 .. versionadded:: 3.2
421 The ``>>`` marker.
422
Georg Brandle59ca2a2010-07-30 17:04:28 +0000423.. pdbcommand:: ll | longlist
424
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000425 List all source code for the current function or frame. Interesting lines
426 are marked as for :pdbcmd:`list`.
Georg Brandle59ca2a2010-07-30 17:04:28 +0000427
428 .. versionadded:: 3.2
429
Georg Brandl02053ee2010-07-18 10:11:03 +0000430.. pdbcommand:: a(rgs)
431
Georg Brandl116aa622007-08-15 14:28:22 +0000432 Print the argument list of the current function.
433
R David Murrayd5793762013-10-10 17:33:43 -0400434.. pdbcommand:: p expression
Georg Brandl02053ee2010-07-18 10:11:03 +0000435
Georg Brandl116aa622007-08-15 14:28:22 +0000436 Evaluate the *expression* in the current context and print its value.
437
R David Murrayd5793762013-10-10 17:33:43 -0400438 .. note::
439
440 ``print()`` can also be used, but is not a debugger command --- this executes the
441 Python :func:`print` function.
442
443
Georg Brandl02053ee2010-07-18 10:11:03 +0000444.. pdbcommand:: pp expression
Georg Brandl116aa622007-08-15 14:28:22 +0000445
R David Murray78d692f2013-10-10 17:23:26 -0400446 Like the :pdbcmd:`p` command, except the value of the expression is
Georg Brandl02053ee2010-07-18 10:11:03 +0000447 pretty-printed using the :mod:`pprint` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
Georg Brandl02053ee2010-07-18 10:11:03 +0000449.. pdbcommand:: whatis expression
450
451 Print the type of the *expression*.
452
Georg Brandle59ca2a2010-07-30 17:04:28 +0000453.. pdbcommand:: source expression
454
455 Try to get source code for the given object and display it.
456
457 .. versionadded:: 3.2
458
Georg Brandlcbc79c72010-12-04 16:21:42 +0000459.. pdbcommand:: display [expression]
460
461 Display the value of the expression if it changed, each time execution stops
462 in the current frame.
463
464 Without expression, list all display expressions for the current frame.
465
466 .. versionadded:: 3.2
467
468.. pdbcommand:: undisplay [expression]
469
470 Do not display the expression any more in the current frame. Without
471 expression, clear all display expressions for the current frame.
472
473 .. versionadded:: 3.2
474
Georg Brandl1acb7462010-12-04 11:20:26 +0000475.. pdbcommand:: interact
476
Martin Panter69332c12016-08-04 13:07:31 +0000477 Start an interactive interpreter (using the :mod:`code` module) whose global
Georg Brandl1acb7462010-12-04 11:20:26 +0000478 namespace contains all the (global and local) names found in the current
479 scope.
480
481 .. versionadded:: 3.2
482
Georg Brandl02053ee2010-07-18 10:11:03 +0000483.. _debugger-aliases:
484
485.. pdbcommand:: alias [name [command]]
486
487 Create an alias called *name* that executes *command*. The command must
488 *not* be enclosed in quotes. Replaceable parameters can be indicated by
489 ``%1``, ``%2``, and so on, while ``%*`` is replaced by all the parameters.
490 If no command is given, the current alias for *name* is shown. If no
491 arguments are given, all aliases are listed.
492
493 Aliases may be nested and can contain anything that can be legally typed at
494 the pdb prompt. Note that internal pdb commands *can* be overridden by
495 aliases. Such a command is then hidden until the alias is removed. Aliasing
496 is recursively applied to the first word of the command line; all other words
497 in the line are left alone.
Georg Brandl116aa622007-08-15 14:28:22 +0000498
499 As an example, here are two useful aliases (especially when placed in the
500 :file:`.pdbrc` file)::
501
Georg Brandle0230912010-07-30 08:29:39 +0000502 # Print instance variables (usage "pi classInst")
Georg Brandlc9879242007-09-04 07:07:56 +0000503 alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
Georg Brandle0230912010-07-30 08:29:39 +0000504 # Print instance variables in self
Georg Brandl116aa622007-08-15 14:28:22 +0000505 alias ps pi self
506
Georg Brandl02053ee2010-07-18 10:11:03 +0000507.. pdbcommand:: unalias name
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Georg Brandl02053ee2010-07-18 10:11:03 +0000509 Delete the specified alias.
510
511.. pdbcommand:: ! statement
512
Georg Brandl116aa622007-08-15 14:28:22 +0000513 Execute the (one-line) *statement* in the context of the current stack frame.
514 The exclamation point can be omitted unless the first word of the statement
Georg Brandl02053ee2010-07-18 10:11:03 +0000515 resembles a debugger command. To set a global variable, you can prefix the
516 assignment command with a :keyword:`global` statement on the same line,
517 e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000518
519 (Pdb) global list_options; list_options = ['-l']
520 (Pdb)
521
Georg Brandl02053ee2010-07-18 10:11:03 +0000522.. pdbcommand:: run [args ...]
523 restart [args ...]
Georg Brandl116aa622007-08-15 14:28:22 +0000524
Georg Brandl02053ee2010-07-18 10:11:03 +0000525 Restart the debugged Python program. If an argument is supplied, it is split
526 with :mod:`shlex` and the result is used as the new :data:`sys.argv`.
527 History, breakpoints, actions and debugger options are preserved.
528 :pdbcmd:`restart` is an alias for :pdbcmd:`run`.
529
530.. pdbcommand:: q(uit)
531
532 Quit from the debugger. The program being executed is aborted.
Georg Brandl243ad662009-05-05 09:00:19 +0000533
534
535.. rubric:: Footnotes
536
537.. [1] Whether a frame is considered to originate in a certain module
538 is determined by the ``__name__`` in the frame globals.