blob: 43a91c24b2b8ecfadda5b99ea81c1901f974e8f2 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001The Python Debugger
2===================
3
4To use the debugger in its simplest form:
5
6 >>> import pdb
7 >>> pdb.run('<a statement>')
8
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
16 >>> <a statement>
17 <exception traceback>
18 >>> import pdb
19 >>> pdb.pm()
20
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
27A blank line repeats the previous command literally. (Except for
28'list', where it lists the next 11 lines.)
29
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
38The debugger is not directly programmable; but it is implemented as a
39class from which you can derive your own debugger class, so you can
40make as fancy as you like.
41
42
43Debugger commands
44=================
45
46h(elp)
47 Without argument, print the list of available commands.
48 With a command name as argument, print help about that command
49 (this is currently not implemented).
50
51w(here)
52 Print a stack trace, with the most recent frame at the bottom.
53 An arrow indicates the "current frame", which determines the
54 context of most commands.
55
56d(own)
57 Move the current frame one level down in the stack trace
58 (to an older frame).
59
60u(p)
61 Move the current frame one level up in the stack trace
62 (to a newer frame).
63
Guido van Rossumb6775db1994-08-01 11:34:53 +000064b(reak) [lineno | function]
65 With a line number argument, set a break there in the current
66 file. With a function name, set a break at the entry of that
67 function. Without argument, list all breaks.
Guido van Rossum3bead091992-01-27 17:00:37 +000068
69cl(ear) [lineno]
70 With a line number argument, clear that break in the current file.
71 Without argument, clear all breaks (but first ask confirmation).
72
73s(tep)
74 Execute the current line, stop at the first possible occasion
75 (either in a function that is called or in the current function).
76
77n(ext)
78 Continue execution until the next line in the current function
79 is reached or it returns.
80
81r(eturn)
82 Continue execution until the current function returns.
83
84c(ont(inue))
85 Continue execution, only stop when a breakpoint is encountered.
86
87l(ist) [first [,last]]
88 List source code for the current file.
89 Without arguments, list 11 lines around the current line
90 or continue the previous listing.
91 With one argument, list 11 lines starting at that line.
92 With two arguments, list the given range;
93 if the second argument is less than the first, it is a count.
94
95a(rgs)
96 Print the argument list of the current function.
97
98p expression
99 Print the value of the expression.
100
101(!) statement
102 Execute the (one-line) statement in the context of
103 the current stack frame.
104 The exclamation point can be omitted unless the first word
105 of the statement resembles a debugger command.
106 To assign to a global variable you must always prefix the
107 command with a 'global' command, e.g.:
108 (Pdb) global list_options; list_options = ['-l']
109 (Pdb)
110
111q(uit)
112 Quit from the debugger.
113 The program being executed is aborted.
114
115
116How it works
117============
118
119Some changes were made to the interpreter:
Guido van Rossum35dcf451992-03-27 15:06:53 +0000120- sys.settrace(func) sets the global trace function
Guido van Rossum3bead091992-01-27 17:00:37 +0000121- there can also a local trace function (see later)
122
123Trace functions have three arguments: (frame, event, arg)
124 - frame is the current stack frame
125 - event is a string: 'call', 'line', 'return' or 'exception'
126 - arg is dependent on the event type
127A trace function should return a new trace function or None.
128Class methods are accepted (and most useful!) as trace methods.
129
130The events have the following meaning:
131
132 'call': A function is called (or some other code block entered).
133 The global trace function is called;
134 arg is the argument list to the function;
135 the return value specifies the local trace function.
136
137 'line': The interpreter is about to execute a new line of code
138 (sometimes multiple line events on one line exist).
139 The local trace function is called; arg in None;
140 the return value specifies the new local trace function.
141
142 'return': A function (or other code block) is about to return.
143 The local trace function is called;
144 arg is the value that will be returned.
145 The trace function's return value is ignored.
146
147 'exception': An exception has occurred.
148 The local trace function is called;
149 arg is a triple (exception, value, traceback);
150 the return value specifies the new local trace function
151
152Note that as an exception is propagated down the chain of callers, an
153'exception' event is generated at each level.
154
155Stack frame objects have the following read-only attributes:
156 f_code: the code object being executed
157 f_lineno: the current line number (-1 for 'call' events)
158 f_back: the stack frame of the caller, or None
159 f_locals: dictionary containing local name bindings
160 f_globals: dictionary containing global name bindings
161
162Code objects have the following read-only attributes:
163 co_code: the code string
164 co_names: the list of names used by the code
165 co_consts: the list of (literal) constants used by the code
166 co_filename: the filename from which the code was compiled