blob: e8f0725b7a0870ae84e3e8b9553803d9ce1578d4 [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 Rossumf0a275d1998-09-12 14:42:23 +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 Rossumf0a275d1998-09-12 14:42:23 +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 Rossumf0a275d1998-09-12 14:42:23 +000038The debugger supports aliases, which can save typing. And aliases
39can have parameters (see the alias help entry) which allows one a
40certain level of adaptability to the context under examination.
41
42Multiple commands may be entered on a single line, separated by
43semi-colons. No intelligence is applied to separating the commands;
44the input is split at the first ';', even if it is in the middle of
45a quoted string.
46
47If a file ".pdbrc" exists in your home directory or in the current
48directory, it is read in and executed as if it had been typed at
49the debugger prompt. This is particularly useful for aliases.
50If both files exist, the one in the home directory is read first
51and aliases defined there can be overriden by the local file.
52
53Aside from aliases, the debugger is not directly programmable; but
54it is implemented as a class from which you can derive your own
55debugger class, 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 Rossumf0a275d1998-09-12 14:42:23 +000062 Without argument, print the list of available commands.
63 With 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 Rossumf0a275d1998-09-12 14:42:23 +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 Rossumf0a275d1998-09-12 14:42:23 +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 Rossumf0a275d1998-09-12 14:42:23 +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 Rossumf0a275d1998-09-12 14:42:23 +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 name,
82 set a break at the first executable line of that function.
83 Without argument, list all breaks. Each breakpoint is assigned
84 a number which is by all the other breakpoint commands refer to it.
Guido van Rossumf60e8e81998-07-20 23:21:21 +000085
Guido van Rossumf0a275d1998-09-12 14:42:23 +000086 The condition argument, if present, is a string which must
87 evaluate to true in order for the breakpoint to be honored.
Guido van Rossum3bead091992-01-27 17:00:37 +000088
Guido van Rossumf0a275d1998-09-12 14:42:23 +000089tbreak [ ([filename:]lineno | function) [, "condition"] ]
90 Temporary breakpoint, which is removed automatically when it is
91 first hit. The arguments are the same as break.
Guido van Rossum3bead091992-01-27 17:00:37 +000092
Guido van Rossumf0a275d1998-09-12 14:42:23 +000093cl(ear) [bpnumber [bpnumber ...] ]
94 With a space separated list of breakpoint numbers, clear those
95 breakpoints. Without argument, clear all breaks (but first
96 ask confirmation).
97
98disable bpnumber [bpnumber ...]
99 Disables the breakpoints given as a space separated list of
100 breakpoint numbers. Disabling a breakpoint means it cannot cause
101 the program to stop execution, but unlike clearing a breakpoint, it
102 remains in the list of breakpoints and can be (re-)enabled.
103
104enable bpnumber [bpnumber ...]
105 Enables the breakpoints specified.
106
107ignore bpnumber count
108 Sets the ignore count for the given breakpoint number. If
109 count is omitted, the ignore count is set to 0. A breakpoint
110 becomes active when the ignore count is zero. When non-zero,
111 the count is decremented each time the breakpoint is reached
112 and the breakpoint is not disabled and any associated condition
113 evaluates to true.
114
115condition bpnumber str_condition
116 str_condition is a string specifying an expression which
117 must evaluate to true before the breakpoint is honored.
118 If str_condition is absent, any existing condition is removed;
119 i.e., the breakpoint is made unconditional.
Guido van Rossumf60e8e81998-07-20 23:21:21 +0000120
Guido van Rossum3bead091992-01-27 17:00:37 +0000121s(tep)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000122 Execute the current line, stop at the first possible occasion
123 (either in a function that is called or in the current function).
Guido van Rossum3bead091992-01-27 17:00:37 +0000124
125n(ext)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000126 Continue execution until the next line in the current function
127 is reached or it returns.
Guido van Rossum3bead091992-01-27 17:00:37 +0000128
129r(eturn)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000130 Continue execution until the current function returns.
Guido van Rossum3bead091992-01-27 17:00:37 +0000131
132c(ont(inue))
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000133 Continue execution, only stop when a breakpoint is encountered.
Guido van Rossum3bead091992-01-27 17:00:37 +0000134
135l(ist) [first [,last]]
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000136 List source code for the current file.
137 Without arguments, list 11 lines around the current line
138 or continue the previous listing.
139 With one argument, list 11 lines starting at that line.
140 With two arguments, list the given range;
141 if the second argument is less than the first, it is a count.
Guido van Rossum3bead091992-01-27 17:00:37 +0000142
143a(rgs)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000144 Print the argument list of the current function.
Guido van Rossum3bead091992-01-27 17:00:37 +0000145
146p expression
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000147 Print the value of the expression.
Guido van Rossum3bead091992-01-27 17:00:37 +0000148
149(!) statement
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000150 Execute the (one-line) statement in the context of the current
151 stack frame. The exclamation point can be omitted unless the
152 first word of the statement resembles a debugger command.
153 To assign to a global variable you must always prefix the
154 command with a 'global' command, e.g.:
155 (Pdb) global list_options; list_options = ['-l']
156 (Pdb)
157
158
159whatis arg
160 Prints the type of the argument.
161
162alias [name [command]]
163 Creates an alias called 'name' that executes 'command'. The command
164 must *not* be enclosed in quotes. Replaceable parameters can be
165 indicated by %1, %2, and so on, while %* is replaced by all the
166 parameters. If no command is given, the current alias for name
167 is shown. If no name is given, all aliases are listed.
168
169 Aliases may be nested and can contain anything that can be
170 legally typed at the pdb prompt. Note! You *can* override
171 internal pdb commands with aliases! Those internal commands
172 are then hidden until the alias is removed. Aliasing is recursively
173 applied to the first word of the command line; all other words
174 in the line are left alone.
175
176 As an example, here are two useful aliases (especially when placed
177 in the .pdbrc file):
178
179 #Print instance variables (usage "pi classInst")
180 alias pi for k in %1.__dict__.keys(): print "%1." + k + "=" + %1.__dict__[k]
181 #Print instance variables in self
182 alias ps pi self
183
184unalias name
185 Deletes the specified alias.
Guido van Rossum3bead091992-01-27 17:00:37 +0000186
187q(uit)
Guido van Rossumf0a275d1998-09-12 14:42:23 +0000188 Quit from the debugger.
189 The program being executed is aborted.
190
191
192How it works
193============
194
195Some changes were made to the interpreter:
196- sys.settrace(func) sets the global trace function
197- there can also a local trace function (see later)
198
199Trace functions have three arguments: (frame, event, arg)
200 - frame is the current stack frame
201 - event is a string: 'call', 'line', 'return' or 'exception'
202 - arg is dependent on the event type
203A trace function should return a new trace function or None.
204Class methods are accepted (and most useful!) as trace methods.
205
206The events have the following meaning:
207
208 'call': A function is called (or some other code block entered).
209 The global trace function is called;
210 arg is the argument list to the function;
211 the return value specifies the local trace function.
212
213 'line': The interpreter is about to execute a new line of code
214 (sometimes multiple line events on one line exist).
215 The local trace function is called; arg in None;
216 the return value specifies the new local trace function.
217
218 'return': A function (or other code block) is about to return.
219 The local trace function is called;
220 arg is the value that will be returned.
221 The trace function's return value is ignored.
222
223 'exception': An exception has occurred.
224 The local trace function is called;
225 arg is a triple (exception, value, traceback);
226 the return value specifies the new local trace function
227
228Note that as an exception is propagated down the chain of callers, an
229'exception' event is generated at each level.
230
231Stack frame objects have the following read-only attributes:
232 f_code: the code object being executed
233 f_lineno: the current line number (-1 for 'call' events)
234 f_back: the stack frame of the caller, or None
235 f_locals: dictionary containing local name bindings
236 f_globals: dictionary containing global name bindings
237
238Code objects have the following read-only attributes:
239 co_code: the code string
240 co_names: the list of names used by the code
241 co_consts: the list of (literal) constants used by the code
242 co_filename: the filename from which the code was compiled