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