blob: db21d1f7c7c2233d369a4ddfa1cd46d0fc8f103f [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
9
10.. index:: single: debugging
11
12The module :mod:`pdb` defines an interactive source code debugger for Python
13programs. It supports setting (conditional) breakpoints and single stepping at
14the source line level, inspection of stack frames, source code listing, and
15evaluation of arbitrary Python code in the context of any stack frame. It also
16supports 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 Brandl02053ee2010-07-18 10:11:03 +000023The debugger is extensible -- it is actually defined as the class :class:`Pdb`.
Georg Brandl116aa622007-08-15 14:28:22 +000024This is currently undocumented but easily understood by reading the source. The
Georg Brandl1f70cdf2010-03-21 09:04:24 +000025extension interface uses the modules :mod:`bdb` and :mod:`cmd`.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27The debugger's prompt is ``(Pdb)``. Typical usage to run a program under control
28of 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 Brandl48310cd2009-01-03 21:18:54 +000039 (Pdb)
Georg Brandl116aa622007-08-15 14:28:22 +000040
41:file:`pdb.py` can also be invoked as a script to debug other scripts. For
42example::
43
Georg Brandl45bb63f2009-09-16 09:42:19 +000044 python3 -m pdb myscript.py
Georg Brandl116aa622007-08-15 14:28:22 +000045
46When invoked as a script, pdb will automatically enter post-mortem debugging if
Georg Brandle0230912010-07-30 08:29:39 +000047the program being debugged exits abnormally. After post-mortem debugging (or
48after normal exit of the program), pdb will restart the program. Automatic
Georg Brandl116aa622007-08-15 14:28:22 +000049restarting preserves pdb's state (such as breakpoints) and in most cases is more
50useful than quitting the debugger upon program's exit.
51
Georg Brandle0230912010-07-30 08:29:39 +000052.. versionadded:: 3.2
53 :file:`pdb.py` now accepts a ``-c`` option that executes commands as if given
54 in a :file:`.pdbrc` file, see :ref:`debugger-commands`.
55
Georg Brandl243ad662009-05-05 09:00:19 +000056The typical usage to break into the debugger from a running program is to
57insert ::
58
59 import pdb; pdb.set_trace()
60
61at the location you want to break into the debugger. You can then step through
Georg Brandl02053ee2010-07-18 10:11:03 +000062the code following this statement, and continue running without the debugger
63using the :pdbcmd:`continue` command.
Georg Brandl243ad662009-05-05 09:00:19 +000064
65The typical usage to inspect a crashed program is::
Georg Brandl116aa622007-08-15 14:28:22 +000066
67 >>> import pdb
68 >>> import mymodule
69 >>> mymodule.test()
70 Traceback (most recent call last):
71 File "<stdin>", line 1, in ?
72 File "./mymodule.py", line 4, in test
73 test2()
74 File "./mymodule.py", line 3, in test2
Georg Brandlc9879242007-09-04 07:07:56 +000075 print(spam)
Georg Brandl116aa622007-08-15 14:28:22 +000076 NameError: spam
77 >>> pdb.pm()
78 > ./mymodule.py(3)test2()
Georg Brandlc9879242007-09-04 07:07:56 +000079 -> print(spam)
Georg Brandl48310cd2009-01-03 21:18:54 +000080 (Pdb)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Georg Brandl243ad662009-05-05 09:00:19 +000082
Georg Brandl116aa622007-08-15 14:28:22 +000083The module defines the following functions; each enters the debugger in a
84slightly different way:
85
Georg Brandl18244152009-09-02 20:34:52 +000086.. function:: run(statement, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +000087
Georg Brandl46b9afc2010-07-30 09:14:20 +000088 Execute the *statement* (given as a string or a code object) under debugger
89 control. The debugger prompt appears before any code is executed; you can
90 set breakpoints and type :pdbcmd:`continue`, or you can step through the
91 statement using :pdbcmd:`step` or :pdbcmd:`next` (all these commands are
92 explained below). The optional *globals* and *locals* arguments specify the
93 environment in which the code is executed; by default the dictionary of the
94 module :mod:`__main__` is used. (See the explanation of the built-in
95 :func:`exec` or :func:`eval` functions.)
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
Georg Brandl18244152009-09-02 20:34:52 +000098.. function:: runeval(expression, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +000099
Georg Brandl46b9afc2010-07-30 09:14:20 +0000100 Evaluate the *expression* (given as a string or a code object) under debugger
101 control. When :func:`runeval` returns, it returns the value of the
102 expression. Otherwise this function is similar to :func:`run`.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
Georg Brandl18244152009-09-02 20:34:52 +0000105.. function:: runcall(function, *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Georg Brandl02053ee2010-07-18 10:11:03 +0000107 Call the *function* (a function or method object, not a string) with the
108 given arguments. When :func:`runcall` returns, it returns whatever the
109 function call returned. The debugger prompt appears as soon as the function
110 is entered.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
113.. function:: set_trace()
114
115 Enter the debugger at the calling stack frame. This is useful to hard-code a
116 breakpoint at a given point in a program, even if the code is not otherwise
117 being debugged (e.g. when an assertion fails).
118
119
Georg Brandl18244152009-09-02 20:34:52 +0000120.. function:: post_mortem(traceback=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000121
Georg Brandl48310cd2009-01-03 21:18:54 +0000122 Enter post-mortem debugging of the given *traceback* object. If no
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000123 *traceback* is given, it uses the one of the exception that is currently
124 being handled (an exception must be being handled if the default is to be
125 used).
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128.. function:: pm()
129
Georg Brandl243ad662009-05-05 09:00:19 +0000130 Enter post-mortem debugging of the traceback found in
131 :data:`sys.last_traceback`.
132
133
134The ``run_*`` functions and :func:`set_trace` are aliases for instantiating the
135:class:`Pdb` class and calling the method of the same name. If you want to
136access further features, you have to do this yourself:
137
138.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None)
139
140 :class:`Pdb` is the debugger class.
141
142 The *completekey*, *stdin* and *stdout* arguments are passed to the
143 underlying :class:`cmd.Cmd` class; see the description there.
144
145 The *skip* argument, if given, must be an iterable of glob-style module name
146 patterns. The debugger will not step into frames that originate in a module
147 that matches one of these patterns. [1]_
148
149 Example call to enable tracing with *skip*::
150
151 import pdb; pdb.Pdb(skip=['django.*']).set_trace()
152
Georg Brandl705d9d52009-05-05 09:29:50 +0000153 .. versionadded:: 3.1
Georg Brandl243ad662009-05-05 09:00:19 +0000154 The *skip* argument.
155
Georg Brandl18244152009-09-02 20:34:52 +0000156 .. method:: run(statement, globals=None, locals=None)
157 runeval(expression, globals=None, locals=None)
158 runcall(function, *args, **kwds)
Georg Brandl243ad662009-05-05 09:00:19 +0000159 set_trace()
160
161 See the documentation for the functions explained above.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
164.. _debugger-commands:
165
166Debugger Commands
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +0000167-----------------
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Georg Brandl02053ee2010-07-18 10:11:03 +0000169The commands recognized by the debugger are listed below. Most commands can be
170abbreviated to one or two letters as indicated; e.g. ``h(elp)`` means that
171either ``h`` or ``help`` can be used to enter the help command (but not ``he``
172or ``hel``, nor ``H`` or ``Help`` or ``HELP``). Arguments to commands must be
173separated by whitespace (spaces or tabs). Optional arguments are enclosed in
174square brackets (``[]``) in the command syntax; the square brackets must not be
175typed. Alternatives in the command syntax are separated by a vertical bar
176(``|``).
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178Entering a blank line repeats the last command entered. Exception: if the last
Georg Brandl02053ee2010-07-18 10:11:03 +0000179command was a :pdbcmd:`list` command, the next 11 lines are listed.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181Commands that the debugger doesn't recognize are assumed to be Python statements
182and are executed in the context of the program being debugged. Python
183statements can also be prefixed with an exclamation point (``!``). This is a
184powerful way to inspect the program being debugged; it is even possible to
185change a variable or call a function. When an exception occurs in such a
186statement, the exception name is printed but the debugger's state is not
187changed.
188
Georg Brandl02053ee2010-07-18 10:11:03 +0000189The debugger supports :ref:`aliases <debugger-aliases>`. Aliases can have
190parameters which allows one a certain level of adaptability to the context under
191examination.
192
Georg Brandl116aa622007-08-15 14:28:22 +0000193Multiple commands may be entered on a single line, separated by ``;;``. (A
194single ``;`` is not used as it is the separator for multiple commands in a line
Georg Brandl02053ee2010-07-18 10:11:03 +0000195that is passed to the Python parser.) No intelligence is applied to separating
Georg Brandl116aa622007-08-15 14:28:22 +0000196the commands; the input is split at the first ``;;`` pair, even if it is in the
197middle of a quoted string.
198
Georg Brandl116aa622007-08-15 14:28:22 +0000199.. index::
200 pair: .pdbrc; file
201 triple: debugger; configuration; file
202
Georg Brandl02053ee2010-07-18 10:11:03 +0000203If a file :file:`.pdbrc` exists in the user's home directory or in the current
Georg Brandl116aa622007-08-15 14:28:22 +0000204directory, it is read in and executed as if it had been typed at the debugger
Georg Brandl02053ee2010-07-18 10:11:03 +0000205prompt. This is particularly useful for aliases. If both files exist, the one
Georg Brandl116aa622007-08-15 14:28:22 +0000206in the home directory is read first and aliases defined there can be overridden
207by the local file.
208
Georg Brandle0230912010-07-30 08:29:39 +0000209.. versionchanged:: 3.2
210 :file:`.pdbrc` can now contain commands that continue debugging, such as
211 :pdbcmd:`continue` or :pdbcmd:`next`. Previously, these commands had no
212 effect.
213
Georg Brandl02053ee2010-07-18 10:11:03 +0000214
215.. pdbcommand:: h(elp) [command]
216
Georg Brandl116aa622007-08-15 14:28:22 +0000217 Without argument, print the list of available commands. With a *command* as
218 argument, print help about that command. ``help pdb`` displays the full
Georg Brandl55353ca2010-07-19 08:02:46 +0000219 documentation (the docstring of the :mod:`pdb` module). Since the *command*
220 argument must be an identifier, ``help exec`` must be entered to get help on
221 the ``!`` command.
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Georg Brandl02053ee2010-07-18 10:11:03 +0000223.. pdbcommand:: w(here)
224
Georg Brandl116aa622007-08-15 14:28:22 +0000225 Print a stack trace, with the most recent frame at the bottom. An arrow
226 indicates the current frame, which determines the context of most commands.
227
Georg Brandl02053ee2010-07-18 10:11:03 +0000228.. pdbcommand:: d(own) [count]
229
Georg Brandleb1f4aa2010-06-27 10:37:48 +0000230 Move the current frame *count* (default one) levels down in the stack trace
231 (to a newer frame).
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Georg Brandl02053ee2010-07-18 10:11:03 +0000233.. pdbcommand:: u(p) [count]
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Georg Brandl02053ee2010-07-18 10:11:03 +0000235 Move the current frame *count* (default one) levels up in the stack trace (to
236 an older frame).
237
238.. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]]
239
Georg Brandl116aa622007-08-15 14:28:22 +0000240 With a *lineno* argument, set a break there in the current file. With a
Georg Brandl02053ee2010-07-18 10:11:03 +0000241 *function* argument, set a break at the first executable statement within
242 that function. The line number may be prefixed with a filename and a colon,
243 to specify a breakpoint in another file (probably one that hasn't been loaded
244 yet). The file is searched on :data:`sys.path`. Note that each breakpoint
245 is assigned a number to which all the other breakpoint commands refer.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Georg Brandl02053ee2010-07-18 10:11:03 +0000247 If a second argument is present, it is an expression which must evaluate to
248 true before the breakpoint is honored.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Georg Brandl02053ee2010-07-18 10:11:03 +0000250 Without argument, list all breaks, including for each breakpoint, the number
251 of times that breakpoint has been hit, the current ignore count, and the
252 associated condition if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000253
Georg Brandl02053ee2010-07-18 10:11:03 +0000254.. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]]
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Georg Brandl02053ee2010-07-18 10:11:03 +0000256 Temporary breakpoint, which is removed automatically when it is first hit.
257 The arguments are the same as for :pdbcmd:`break`.
258
Senthil Kumaran6f107042010-11-29 11:54:17 +0000259.. pdbcommand:: cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
Georg Brandl02053ee2010-07-18 10:11:03 +0000260
Senthil Kumaran6f107042010-11-29 11:54:17 +0000261 With a *filename:lineno* argument, clear all the breakpoints at this line.
Georg Brandl116aa622007-08-15 14:28:22 +0000262 With a space separated list of breakpoint numbers, clear those breakpoints.
263 Without argument, clear all breaks (but first ask confirmation).
264
Georg Brandl02053ee2010-07-18 10:11:03 +0000265.. pdbcommand:: disable [bpnumber [bpnumber ...]]
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Georg Brandl02053ee2010-07-18 10:11:03 +0000267 Disable the breakpoints given as a space separated list of breakpoint
268 numbers. Disabling a breakpoint means it cannot cause the program to stop
269 execution, but unlike clearing a breakpoint, it remains in the list of
270 breakpoints and can be (re-)enabled.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Georg Brandl02053ee2010-07-18 10:11:03 +0000272.. pdbcommand:: enable [bpnumber [bpnumber ...]]
Georg Brandl116aa622007-08-15 14:28:22 +0000273
Georg Brandl02053ee2010-07-18 10:11:03 +0000274 Enable the breakpoints specified.
Georg Brandl116aa622007-08-15 14:28:22 +0000275
Georg Brandl02053ee2010-07-18 10:11:03 +0000276.. pdbcommand:: ignore bpnumber [count]
277
278 Set the ignore count for the given breakpoint number. If count is omitted,
279 the ignore count is set to 0. A breakpoint becomes active when the ignore
280 count is zero. When non-zero, the count is decremented each time the
281 breakpoint is reached and the breakpoint is not disabled and any associated
282 condition evaluates to true.
283
284.. pdbcommand:: condition bpnumber [condition]
285
286 Set a new *condition* for the breakpoint, an expression which must evaluate
287 to true before the breakpoint is honored. If *condition* is absent, any
288 existing condition is removed; i.e., the breakpoint is made unconditional.
289
290.. pdbcommand:: commands [bpnumber]
291
Georg Brandl116aa622007-08-15 14:28:22 +0000292 Specify a list of commands for breakpoint number *bpnumber*. The commands
Georg Brandl02053ee2010-07-18 10:11:03 +0000293 themselves appear on the following lines. Type a line containing just
294 ``end`` to terminate the commands. An example::
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296 (Pdb) commands 1
297 (com) print some_variable
298 (com) end
299 (Pdb)
300
301 To remove all commands from a breakpoint, type commands and follow it
Georg Brandl02053ee2010-07-18 10:11:03 +0000302 immediately with ``end``; that is, give no commands.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 With no *bpnumber* argument, commands refers to the last breakpoint set.
305
Georg Brandl02053ee2010-07-18 10:11:03 +0000306 You can use breakpoint commands to start your program up again. Simply use
307 the continue command, or step, or any other command that resumes execution.
Georg Brandl116aa622007-08-15 14:28:22 +0000308
309 Specifying any command resuming execution (currently continue, step, next,
310 return, jump, quit and their abbreviations) terminates the command list (as if
311 that command was immediately followed by end). This is because any time you
Georg Brandl9afde1c2007-11-01 20:32:30 +0000312 resume execution (even with a simple next or step), you may encounter another
Georg Brandl116aa622007-08-15 14:28:22 +0000313 breakpoint--which could have its own command list, leading to ambiguities about
314 which list to execute.
315
316 If you use the 'silent' command in the command list, the usual message about
317 stopping at a breakpoint is not printed. This may be desirable for breakpoints
318 that are to print a specific message and then continue. If none of the other
319 commands print anything, you see no sign that the breakpoint was reached.
320
Georg Brandl02053ee2010-07-18 10:11:03 +0000321.. pdbcommand:: s(tep)
322
Georg Brandl116aa622007-08-15 14:28:22 +0000323 Execute the current line, stop at the first possible occasion (either in a
324 function that is called or on the next line in the current function).
325
Georg Brandl02053ee2010-07-18 10:11:03 +0000326.. pdbcommand:: n(ext)
Georg Brandl116aa622007-08-15 14:28:22 +0000327
Georg Brandl02053ee2010-07-18 10:11:03 +0000328 Continue execution until the next line in the current function is reached or
329 it returns. (The difference between :pdbcmd:`next` and :pdbcmd:`step` is
330 that :pdbcmd:`step` stops inside a called function, while :pdbcmd:`next`
331 executes called functions at (nearly) full speed, only stopping at the next
332 line in the current function.)
333
Georg Brandl2dfec552010-07-30 08:43:32 +0000334.. pdbcommand:: unt(il) [lineno]
Georg Brandl02053ee2010-07-18 10:11:03 +0000335
Georg Brandl2dfec552010-07-30 08:43:32 +0000336 Without argument, continue execution until the line with a number greater
337 than the current one is reached.
338
339 With a line number, continue execution until a line with a number greater or
340 equal to that is reached. In both cases, also stop when the current frame
341 returns.
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000342
Georg Brandl26a0f872010-07-30 08:45:26 +0000343 .. versionchanged:: 3.2
344 Allow giving an explicit line number.
345
Georg Brandl02053ee2010-07-18 10:11:03 +0000346.. pdbcommand:: r(eturn)
347
Georg Brandl116aa622007-08-15 14:28:22 +0000348 Continue execution until the current function returns.
349
Georg Brandl02053ee2010-07-18 10:11:03 +0000350.. pdbcommand:: c(ont(inue))
351
Georg Brandl116aa622007-08-15 14:28:22 +0000352 Continue execution, only stop when a breakpoint is encountered.
353
Georg Brandl02053ee2010-07-18 10:11:03 +0000354.. pdbcommand:: j(ump) lineno
Georg Brandl116aa622007-08-15 14:28:22 +0000355
Georg Brandl02053ee2010-07-18 10:11:03 +0000356 Set the next line that will be executed. Only available in the bottom-most
357 frame. This lets you jump back and execute code again, or jump forward to
358 skip code that you don't want to run.
359
360 It should be noted that not all jumps are allowed -- for instance it is not
Georg Brandl116aa622007-08-15 14:28:22 +0000361 possible to jump into the middle of a :keyword:`for` loop or out of a
362 :keyword:`finally` clause.
363
Georg Brandl02053ee2010-07-18 10:11:03 +0000364.. pdbcommand:: l(ist) [first[, last]]
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Georg Brandl02053ee2010-07-18 10:11:03 +0000366 List source code for the current file. Without arguments, list 11 lines
Georg Brandla5eacee2010-07-23 16:55:26 +0000367 around the current line or continue the previous listing. With ``.`` as
368 argument, list 11 lines around the current line. With one argument,
Georg Brandl02053ee2010-07-18 10:11:03 +0000369 list 11 lines around at that line. With two arguments, list the given range;
370 if the second argument is less than the first, it is interpreted as a count.
371
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000372 The current line in the current frame is indicated by ``->``. If an
373 exception is being debugged, the line where the exception was originally
374 raised or propagated is indicated by ``>>``, if it differs from the current
375 line.
376
377 .. versionadded:: 3.2
378 The ``>>`` marker.
379
Georg Brandle59ca2a2010-07-30 17:04:28 +0000380.. pdbcommand:: ll | longlist
381
Georg Brandl0a9c3e92010-07-30 18:46:38 +0000382 List all source code for the current function or frame. Interesting lines
383 are marked as for :pdbcmd:`list`.
Georg Brandle59ca2a2010-07-30 17:04:28 +0000384
385 .. versionadded:: 3.2
386
Georg Brandl02053ee2010-07-18 10:11:03 +0000387.. pdbcommand:: a(rgs)
388
Georg Brandl116aa622007-08-15 14:28:22 +0000389 Print the argument list of the current function.
390
Georg Brandl02053ee2010-07-18 10:11:03 +0000391.. pdbcommand:: p(rint) expression
392
Georg Brandl116aa622007-08-15 14:28:22 +0000393 Evaluate the *expression* in the current context and print its value.
394
Georg Brandl02053ee2010-07-18 10:11:03 +0000395.. pdbcommand:: pp expression
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Georg Brandl02053ee2010-07-18 10:11:03 +0000397 Like the :pdbcmd:`print` command, except the value of the expression is
398 pretty-printed using the :mod:`pprint` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Georg Brandl02053ee2010-07-18 10:11:03 +0000400.. pdbcommand:: whatis expression
401
402 Print the type of the *expression*.
403
Georg Brandle59ca2a2010-07-30 17:04:28 +0000404.. pdbcommand:: source expression
405
406 Try to get source code for the given object and display it.
407
408 .. versionadded:: 3.2
409
Georg Brandl1acb7462010-12-04 11:20:26 +0000410.. pdbcommand:: interact
411
412 Start an interative interpreter (using the :mod:`code` module) whose global
413 namespace contains all the (global and local) names found in the current
414 scope.
415
416 .. versionadded:: 3.2
417
Georg Brandl02053ee2010-07-18 10:11:03 +0000418.. _debugger-aliases:
419
420.. pdbcommand:: alias [name [command]]
421
422 Create an alias called *name* that executes *command*. The command must
423 *not* be enclosed in quotes. Replaceable parameters can be indicated by
424 ``%1``, ``%2``, and so on, while ``%*`` is replaced by all the parameters.
425 If no command is given, the current alias for *name* is shown. If no
426 arguments are given, all aliases are listed.
427
428 Aliases may be nested and can contain anything that can be legally typed at
429 the pdb prompt. Note that internal pdb commands *can* be overridden by
430 aliases. Such a command is then hidden until the alias is removed. Aliasing
431 is recursively applied to the first word of the command line; all other words
432 in the line are left alone.
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434 As an example, here are two useful aliases (especially when placed in the
435 :file:`.pdbrc` file)::
436
Georg Brandle0230912010-07-30 08:29:39 +0000437 # Print instance variables (usage "pi classInst")
Georg Brandlc9879242007-09-04 07:07:56 +0000438 alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
Georg Brandle0230912010-07-30 08:29:39 +0000439 # Print instance variables in self
Georg Brandl116aa622007-08-15 14:28:22 +0000440 alias ps pi self
441
Georg Brandl02053ee2010-07-18 10:11:03 +0000442.. pdbcommand:: unalias name
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Georg Brandl02053ee2010-07-18 10:11:03 +0000444 Delete the specified alias.
445
446.. pdbcommand:: ! statement
447
Georg Brandl116aa622007-08-15 14:28:22 +0000448 Execute the (one-line) *statement* in the context of the current stack frame.
449 The exclamation point can be omitted unless the first word of the statement
Georg Brandl02053ee2010-07-18 10:11:03 +0000450 resembles a debugger command. To set a global variable, you can prefix the
451 assignment command with a :keyword:`global` statement on the same line,
452 e.g.::
Georg Brandl116aa622007-08-15 14:28:22 +0000453
454 (Pdb) global list_options; list_options = ['-l']
455 (Pdb)
456
Georg Brandl02053ee2010-07-18 10:11:03 +0000457.. pdbcommand:: run [args ...]
458 restart [args ...]
Georg Brandl116aa622007-08-15 14:28:22 +0000459
Georg Brandl02053ee2010-07-18 10:11:03 +0000460 Restart the debugged Python program. If an argument is supplied, it is split
461 with :mod:`shlex` and the result is used as the new :data:`sys.argv`.
462 History, breakpoints, actions and debugger options are preserved.
463 :pdbcmd:`restart` is an alias for :pdbcmd:`run`.
464
465.. pdbcommand:: q(uit)
466
467 Quit from the debugger. The program being executed is aborted.
Georg Brandl243ad662009-05-05 09:00:19 +0000468
469
470.. rubric:: Footnotes
471
472.. [1] Whether a frame is considered to originate in a certain module
473 is determined by the ``__name__`` in the frame globals.