blob: 3beb0c2b7e518ae5ac1782928900ba6ab0759230 [file] [log] [blame]
Guido van Rossumf0a275d1998-09-12 14:42:23 +00001The Python Debugger Pdb
2=======================
Guido van Rossum3bead091992-01-27 17:00:37 +00003
4To use the debugger in its simplest form:
5
Guido van Rossumfc076d41998-09-17 15:01:38 +00006 >>> import pdb
7 >>> pdb.run('<a statement>')
Guido van Rossum3bead091992-01-27 17:00:37 +00008
9The debugger's prompt is '(Pdb) '. This will stop in the first
10function call in <a statement>.
11
Guido van Rossum35771131992-09-08 11:59:04 +000012Alternatively, if a statement terminated with an unhandled exception,
13you can use pdb's post-mortem facility to inspect the contents of the
14traceback:
15
Guido van Rossumfc076d41998-09-17 15:01:38 +000016 >>> <a statement>
17 <exception traceback>
18 >>> import pdb
19 >>> pdb.pm()
Guido van Rossum35771131992-09-08 11:59:04 +000020
Guido van Rossum3bead091992-01-27 17:00:37 +000021The commands recognized by the debugger are listed in the next
22section. Most can be abbreviated as indicated; e.g., h(elp) means
23that 'help' can be typed as 'h' or 'help' (but not as 'he' or 'hel',
24nor as 'H' or 'Help' or 'HELP'). Optional arguments are enclosed in
25square brackets.
26
Guido van Rossumf0a275d1998-09-12 14:42:23 +000027A blank line repeats the previous command literally, except for
28'list', where it lists the next 11 lines.
Guido van Rossum3bead091992-01-27 17:00:37 +000029
30Commands that the debugger doesn't recognize are assumed to be Python
31statements and are executed in the context of the program being
32debugged. Python statements can also be prefixed with an exclamation
33point ('!'). This is a powerful way to inspect the program being
34debugged; it is even possible to change variables. When an exception
35occurs in such a statement, the exception name is printed but the
36debugger's state is not changed.
37
Guido van Rossumfc076d41998-09-17 15:01:38 +000038The debugger supports aliases, which can save typing. And aliases can
39have parameters (see the alias help entry) which allows one a certain
40level of adaptability to the context under examination.
Guido van Rossumf0a275d1998-09-12 14:42:23 +000041
Guido van Rossumfc076d41998-09-17 15:01:38 +000042Multiple commands may be entered on a single line, separated by the
43pair ';;'. No intelligence is applied to separating the commands; the
44input is split at the first ';;', even if it is in the middle of a
45quoted string.
Guido van Rossumf0a275d1998-09-12 14:42:23 +000046
47If a file ".pdbrc" exists in your home directory or in the current
Guido van Rossumfc076d41998-09-17 15:01:38 +000048directory, it is read in and executed as if it had been typed at the
49debugger prompt. This is particularly useful for aliases. If both
50files exist, the one in the home directory is read first and aliases
51defined there can be overriden by the local file.
Guido van Rossumf0a275d1998-09-12 14:42:23 +000052
Guido van Rossumfc076d41998-09-17 15:01:38 +000053Aside from aliases, the debugger is not directly programmable; but it
54is implemented as a class from which you can derive your own debugger
55class, which you can make as fancy as you like.
Guido van Rossum3bead091992-01-27 17:00:37 +000056
57
58Debugger commands
59=================
60
61h(elp)
Guido van Rossumfc076d41998-09-17 15:01:38 +000062 Without argument, print the list of available commands. With
63 a command name as argument, print help about that command
64 (this is currently not implemented).
Guido van Rossum3bead091992-01-27 17:00:37 +000065
66w(here)
Guido van Rossumfc076d41998-09-17 15:01:38 +000067 Print a stack trace, with the most recent frame at the bottom.
68 An arrow indicates the "current frame", which determines the
69 context of most commands.
Guido van Rossum3bead091992-01-27 17:00:37 +000070
71d(own)
Guido van Rossumfc076d41998-09-17 15:01:38 +000072 Move the current frame one level down in the stack trace
73 (to an older frame).
Guido van Rossum3bead091992-01-27 17:00:37 +000074
75u(p)
Guido van Rossumfc076d41998-09-17 15:01:38 +000076 Move the current frame one level up in the stack trace
77 (to a newer frame).
Guido van Rossum3bead091992-01-27 17:00:37 +000078
Guido van Rossumfc076d41998-09-17 15:01:38 +000079b(reak) [ ([filename:]lineno | function) [, condition] ]
80 With a filename:line number argument, set a break there. If
81 filename is omitted, use the current file. With a function
82 name, set a break at the first executable line of that
83 function. Without argument, list all breaks. Each breakpoint
84 is assigned a number to which all the other breakpoint
85 commands refer.
Guido van Rossumf60e8e81998-07-20 23:21:21 +000086
Guido van Rossumfc076d41998-09-17 15:01:38 +000087 The condition argument, if present, is a string which must
88 evaluate to true in order for the breakpoint to be honored.
Guido van Rossum3bead091992-01-27 17:00:37 +000089
Guido van Rossumfc076d41998-09-17 15:01:38 +000090tbreak [ ([filename:]lineno | function) [, condition] ]
91 Temporary breakpoint, which is removed automatically when it
92 is first hit. The arguments are the same as break.
Guido van Rossum3bead091992-01-27 17:00:37 +000093
Guido van Rossumf0a275d1998-09-12 14:42:23 +000094cl(ear) [bpnumber [bpnumber ...] ]
Guido van Rossumfc076d41998-09-17 15:01:38 +000095 With a space separated list of breakpoint numbers, clear those
96 breakpoints. Without argument, clear all breaks (but first
97 ask confirmation).
Guido van Rossumf0a275d1998-09-12 14:42:23 +000098
99disable bpnumber [bpnumber ...]
Guido van Rossumfc076d41998-09-17 15:01:38 +0000100 Disables the breakpoints given as a space separated list of
101 breakpoint numbers. Disabling a breakpoint means it cannot
102 cause the program to stop execution, but unlike clearing a
103 breakpoint, it remains in the list of breakpoints and can be
104 (re-)enabled.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000105
106enable bpnumber [bpnumber ...]
Guido van Rossumfc076d41998-09-17 15:01:38 +0000107 Enables the breakpoints specified.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000108
109ignore bpnumber count
Guido van Rossumfc076d41998-09-17 15:01:38 +0000110 Sets the ignore count for the given breakpoint number. If
111 count is omitted, the ignore count is set to 0. A breakpoint
112 becomes active when the ignore count is zero. When non-zero,
113 the count is decremented each time the breakpoint is reached
114 and the breakpoint is not disabled and any associated
115 condition evaluates to true.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000116
Guido van Rossumfc076d41998-09-17 15:01:38 +0000117condition bpnumber condition
118 condition is an expression which must evaluate to true before
119 the breakpoint is honored. If condition is absent, any
120 existing condition is removed; i.e., the breakpoint is made
121 unconditional.
Guido van Rossumf60e8e81998-07-20 23:21:21 +0000122
Guido van Rossum3bead091992-01-27 17:00:37 +0000123s(tep)
Guido van Rossumfc076d41998-09-17 15:01:38 +0000124 Execute the current line, stop at the first possible occasion
125 (either in a function that is called or in the current function).
Guido van Rossum3bead091992-01-27 17:00:37 +0000126
127n(ext)
Guido van Rossumfc076d41998-09-17 15:01:38 +0000128 Continue execution until the next line in the current function
129 is reached or it returns.
Guido van Rossum3bead091992-01-27 17:00:37 +0000130
131r(eturn)
Guido van Rossumfc076d41998-09-17 15:01:38 +0000132 Continue execution until the current function returns.
Guido van Rossum3bead091992-01-27 17:00:37 +0000133
134c(ont(inue))
Guido van Rossumfc076d41998-09-17 15:01:38 +0000135 Continue execution, only stop when a breakpoint is encountered.
Guido van Rossum3bead091992-01-27 17:00:37 +0000136
137l(ist) [first [,last]]
Guido van Rossumfc076d41998-09-17 15:01:38 +0000138 List source code for the current file.
139 Without arguments, list 11 lines around the current line
140 or continue the previous listing.
141 With one argument, list 11 lines starting at that line.
142 With two arguments, list the given range;
143 if the second argument is less than the first, it is a count.
Guido van Rossum3bead091992-01-27 17:00:37 +0000144
145a(rgs)
Guido van Rossumfc076d41998-09-17 15:01:38 +0000146 Print the argument list of the current function.
Guido van Rossum3bead091992-01-27 17:00:37 +0000147
148p expression
Guido van Rossumfc076d41998-09-17 15:01:38 +0000149 Print the value of the expression.
Guido van Rossum3bead091992-01-27 17:00:37 +0000150
151(!) statement
Guido van Rossumfc076d41998-09-17 15:01:38 +0000152 Execute the (one-line) statement in the context of the current
153 stack frame. The exclamation point can be omitted unless the
154 first word of the statement resembles a debugger command. To
155 assign to a global variable you must always prefix the command
156 with a 'global' command, e.g.:
157 (Pdb) global list_options; list_options = ['-l']
158 (Pdb)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000159
160
161whatis arg
Guido van Rossumfc076d41998-09-17 15:01:38 +0000162 Prints the type of the argument.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000163
164alias [name [command]]
Guido van Rossumfc076d41998-09-17 15:01:38 +0000165 Creates an alias called 'name' that executes 'command'. The
166 command must *not* be enclosed in quotes. Replaceable
167 parameters can be indicated by %1, %2, and so on, while %* is
168 replaced by all the parameters. If no command is given, the
169 current alias for name is shown. If no name is given, all
170 aliases are listed.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000171
Guido van Rossumfc076d41998-09-17 15:01:38 +0000172 Aliases may be nested and can contain anything that can be
173 legally typed at the pdb prompt. Note! You *can* override
174 internal pdb commands with aliases! Those internal commands
175 are then hidden until the alias is removed. Aliasing is
176 recursively applied to the first word of the command line; all
177 other words in the line are left alone.
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000178
Guido van Rossumfc076d41998-09-17 15:01:38 +0000179 As an example, here are two useful aliases (especially when
180 placed in the .pdbrc file):
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000181
Guido van Rossumfc076d41998-09-17 15:01:38 +0000182 #Print instance variables (usage "pi classInst")
183 alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
184 #Print instance variables in self
185 alias ps pi self
186
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000187unalias name
Guido van Rossumfc076d41998-09-17 15:01:38 +0000188 Deletes the specified alias.
Guido van Rossum3bead091992-01-27 17:00:37 +0000189
190q(uit)
Guido van Rossumfc076d41998-09-17 15:01:38 +0000191 Quit from the debugger.
192 The program being executed is aborted.