blob: c92b57229d7e693cba705a91bb0758517faa54af [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
64b(reak) [lineno]
65 With a line number argument, set a break there in the current file.
66 Without argument, list all breaks.
67
68cl(ear) [lineno]
69 With a line number argument, clear that break in the current file.
70 Without argument, clear all breaks (but first ask confirmation).
71
72s(tep)
73 Execute the current line, stop at the first possible occasion
74 (either in a function that is called or in the current function).
75
76n(ext)
77 Continue execution until the next line in the current function
78 is reached or it returns.
79
80r(eturn)
81 Continue execution until the current function returns.
82
83c(ont(inue))
84 Continue execution, only stop when a breakpoint is encountered.
85
86l(ist) [first [,last]]
87 List source code for the current file.
88 Without arguments, list 11 lines around the current line
89 or continue the previous listing.
90 With one argument, list 11 lines starting at that line.
91 With two arguments, list the given range;
92 if the second argument is less than the first, it is a count.
93
94a(rgs)
95 Print the argument list of the current function.
96
97p expression
98 Print the value of the expression.
99
100(!) statement
101 Execute the (one-line) statement in the context of
102 the current stack frame.
103 The exclamation point can be omitted unless the first word
104 of the statement resembles a debugger command.
105 To assign to a global variable you must always prefix the
106 command with a 'global' command, e.g.:
107 (Pdb) global list_options; list_options = ['-l']
108 (Pdb)
109
110q(uit)
111 Quit from the debugger.
112 The program being executed is aborted.
113
114
115How it works
116============
117
118Some changes were made to the interpreter:
Guido van Rossum35dcf451992-03-27 15:06:53 +0000119- sys.settrace(func) sets the global trace function
Guido van Rossum3bead091992-01-27 17:00:37 +0000120- there can also a local trace function (see later)
121
122Trace functions have three arguments: (frame, event, arg)
123 - frame is the current stack frame
124 - event is a string: 'call', 'line', 'return' or 'exception'
125 - arg is dependent on the event type
126A trace function should return a new trace function or None.
127Class methods are accepted (and most useful!) as trace methods.
128
129The events have the following meaning:
130
131 'call': A function is called (or some other code block entered).
132 The global trace function is called;
133 arg is the argument list to the function;
134 the return value specifies the local trace function.
135
136 'line': The interpreter is about to execute a new line of code
137 (sometimes multiple line events on one line exist).
138 The local trace function is called; arg in None;
139 the return value specifies the new local trace function.
140
141 'return': A function (or other code block) is about to return.
142 The local trace function is called;
143 arg is the value that will be returned.
144 The trace function's return value is ignored.
145
146 'exception': An exception has occurred.
147 The local trace function is called;
148 arg is a triple (exception, value, traceback);
149 the return value specifies the new local trace function
150
151Note that as an exception is propagated down the chain of callers, an
152'exception' event is generated at each level.
153
154Stack frame objects have the following read-only attributes:
155 f_code: the code object being executed
156 f_lineno: the current line number (-1 for 'call' events)
157 f_back: the stack frame of the caller, or None
158 f_locals: dictionary containing local name bindings
159 f_globals: dictionary containing global name bindings
160
161Code objects have the following read-only attributes:
162 co_code: the code string
163 co_names: the list of names used by the code
164 co_consts: the list of (literal) constants used by the code
165 co_filename: the filename from which the code was compiled