blob: b252aeb42f1561e7f942897f3977297f28e12e26 [file] [log] [blame]
Fred Drake1a2302b2001-07-18 17:40:19 +00001\chapter{The Python Debugger \label{debugger}}
Fred Drakec8993aa1999-04-22 16:50:40 +00002
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{pdb}
Fred Drakec8993aa1999-04-22 16:50:40 +00004\modulesynopsis{The Python debugger for interactive interpreters.}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +00005
Guido van Rossumf4aac481995-03-02 12:37:55 +00006
Fred Drakec8993aa1999-04-22 16:50:40 +00007The module \module{pdb} defines an interactive source code
8debugger\index{debugging} for Python programs. It supports setting
9(conditional) breakpoints and single stepping at the source line
10level, inspection of stack frames, source code listing, and evaluation
11of arbitrary Python code in the context of any stack frame. It also
12supports post-mortem debugging and can be called under program
13control.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000014
Fred Drakec8993aa1999-04-22 16:50:40 +000015The debugger is extensible --- it is actually defined as the class
16\class{Pdb}\withsubitem{(class in pdb)}{\ttindex{Pdb}}.
Fred Drake82d493f1998-04-07 19:14:17 +000017This is currently undocumented but easily understood by reading the
Fred Drakec8993aa1999-04-22 16:50:40 +000018source. The extension interface uses the modules
19\module{bdb}\refstmodindex{bdb} (undocumented) and
20\refmodule{cmd}\refstmodindex{cmd}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000021
Fred Drake82d493f1998-04-07 19:14:17 +000022The debugger's prompt is \samp{(Pdb) }.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000023Typical usage to run a program under control of the debugger is:
24
Fred Drake19479911998-02-13 06:58:54 +000025\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000026>>> import pdb
27>>> import mymodule
28>>> pdb.run('mymodule.test()')
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000029> <string>(0)?()
30(Pdb) continue
31> <string>(1)?()
32(Pdb) continue
33NameError: 'spam'
34> <string>(1)?()
35(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000037
38\file{pdb.py} can also be invoked as
Guido van Rossum809408e1997-06-02 17:28:16 +000039a script to debug other scripts. For example:
Fred Drake82d493f1998-04-07 19:14:17 +000040
41\begin{verbatim}
Raymond Hettinger700d9b92004-11-07 06:18:37 +000042python -m pdb myscript.py
Fred Drake82d493f1998-04-07 19:14:17 +000043\end{verbatim}
Guido van Rossum809408e1997-06-02 17:28:16 +000044
Raymond Hettingera2325f62004-11-18 08:39:33 +000045When invoked as a script, pdb will automatically enter post-mortem debugging
46if the program being debugged exits abnormally. After post-mortem debugging
47(or after normal exit of the program), pdb will restart the program.
48Automatic restarting preserves pdb's state (such as breakpoints) and in most
49cases is more useful than quitting the debugger upon program's exit.
50\versionadded[Restarting post-mortem behavior added]{2.4}
51
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000052Typical usage to inspect a crashed program is:
53
Fred Drake19479911998-02-13 06:58:54 +000054\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000055>>> import pdb
56>>> import mymodule
57>>> mymodule.test()
Fred Drake162c6a62001-02-14 03:20:18 +000058Traceback (most recent call last):
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000059 File "<stdin>", line 1, in ?
60 File "./mymodule.py", line 4, in test
61 test2()
62 File "./mymodule.py", line 3, in test2
63 print spam
64NameError: spam
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000065>>> pdb.pm()
Guido van Rossum25f6fcc1995-04-04 12:28:53 +000066> ./mymodule.py(3)test2()
67-> print spam
68(Pdb)
Fred Drake19479911998-02-13 06:58:54 +000069\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +000070
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000071The module defines the following functions; each enters the debugger
72in a slightly different way:
73
Fred Drakecce10901998-03-17 06:33:25 +000074\begin{funcdesc}{run}{statement\optional{, globals\optional{, locals}}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000075Execute the \var{statement} (given as a string) under debugger
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000076control. The debugger prompt appears before any code is executed; you
Fred Drakec8993aa1999-04-22 16:50:40 +000077can set breakpoints and type \samp{continue}, or you can step through
78the statement using \samp{step} or \samp{next} (all these commands are
Guido van Rossumf4aac481995-03-02 12:37:55 +000079explained below). The optional \var{globals} and \var{locals}
80arguments specify the environment in which the code is executed; by
Fred Drake7a666b82000-09-14 20:32:17 +000081default the dictionary of the module \refmodule[main]{__main__} is
82used. (See the explanation of the \keyword{exec} statement or the
Fred Drakec8993aa1999-04-22 16:50:40 +000083\function{eval()} built-in function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000084\end{funcdesc}
85
Fred Drakecce10901998-03-17 06:33:25 +000086\begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}
Raymond Hettinger999b57c2003-08-25 04:28:05 +000087Evaluate the \var{expression} (given as a string) under debugger
Fred Drakec8993aa1999-04-22 16:50:40 +000088control. When \function{runeval()} returns, it returns the value of the
Guido van Rossumf4aac481995-03-02 12:37:55 +000089expression. Otherwise this function is similar to
Fred Drakec8993aa1999-04-22 16:50:40 +000090\function{run()}.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000091\end{funcdesc}
92
Fred Drakecce10901998-03-17 06:33:25 +000093\begin{funcdesc}{runcall}{function\optional{, argument, ...}}
Guido van Rossumf4aac481995-03-02 12:37:55 +000094Call the \var{function} (a function or method object, not a string)
Fred Drakec8993aa1999-04-22 16:50:40 +000095with the given arguments. When \function{runcall()} returns, it returns
Guido van Rossumf4aac481995-03-02 12:37:55 +000096whatever the function call returned. The debugger prompt appears as
97soon as the function is entered.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +000098\end{funcdesc}
99
100\begin{funcdesc}{set_trace}{}
101Enter the debugger at the calling stack frame. This is useful to
Guido van Rossumf4aac481995-03-02 12:37:55 +0000102hard-code a breakpoint at a given point in a program, even if the code
103is not otherwise being debugged (e.g. when an assertion fails).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000104\end{funcdesc}
105
106\begin{funcdesc}{post_mortem}{traceback}
107Enter post-mortem debugging of the given \var{traceback} object.
108\end{funcdesc}
109
110\begin{funcdesc}{pm}{}
Guido van Rossumf4aac481995-03-02 12:37:55 +0000111Enter post-mortem debugging of the traceback found in
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000112\code{sys.last_traceback}.
113\end{funcdesc}
114
Fred Drakec8993aa1999-04-22 16:50:40 +0000115
116\section{Debugger Commands \label{debugger-commands}}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000117
118The debugger recognizes the following commands. Most commands can be
Fred Drakec8993aa1999-04-22 16:50:40 +0000119abbreviated to one or two letters; e.g. \samp{h(elp)} means that
120either \samp{h} or \samp{help} can be used to enter the help
121command (but not \samp{he} or \samp{hel}, nor \samp{H} or
122\samp{Help} or \samp{HELP}). Arguments to commands must be
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000123separated by whitespace (spaces or tabs). Optional arguments are
Fred Drakec8993aa1999-04-22 16:50:40 +0000124enclosed in square brackets (\samp{[]}) in the command syntax; the
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000125square brackets must not be typed. Alternatives in the command syntax
Fred Drakec8993aa1999-04-22 16:50:40 +0000126are separated by a vertical bar (\samp{|}).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000127
128Entering a blank line repeats the last command entered. Exception: if
Fred Drakec8993aa1999-04-22 16:50:40 +0000129the last command was a \samp{list} command, the next 11 lines are
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000130listed.
131
132Commands that the debugger doesn't recognize are assumed to be Python
133statements and are executed in the context of the program being
134debugged. Python statements can also be prefixed with an exclamation
Fred Drakec8993aa1999-04-22 16:50:40 +0000135point (\samp{!}). This is a powerful way to inspect the program
Guido van Rossum25f6fcc1995-04-04 12:28:53 +0000136being debugged; it is even possible to change a variable or call a
137function. When an
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000138exception occurs in such a statement, the exception name is printed
139but the debugger's state is not changed.
140
Guido van Rossum64421161998-09-17 15:11:51 +0000141Multiple commands may be entered on a single line, separated by
Fred Drakec8993aa1999-04-22 16:50:40 +0000142\samp{;;}. (A single \samp{;} is not used as it is
Guido van Rossum64421161998-09-17 15:11:51 +0000143the separator for multiple commands in a line that is passed to
144the Python parser.)
145No intelligence is applied to separating the commands;
Fred Drakec8993aa1999-04-22 16:50:40 +0000146the input is split at the first \samp{;;} pair, even if it is in
Guido van Rossum64421161998-09-17 15:11:51 +0000147the middle of a quoted string.
148
149The debugger supports aliases. Aliases can have parameters which
150allows one a certain level of adaptability to the context under
151examination.
152
Fred Drakec8993aa1999-04-22 16:50:40 +0000153If a file \file{.pdbrc}
154\indexii{.pdbrc}{file}\indexiii{debugger}{configuration}{file}
155exists in the user's home directory or in the current directory, it is
156read in and executed as if it had been typed at the debugger prompt.
157This is particularly useful for aliases. If both files exist, the one
158in the home directory is read first and aliases defined there can be
Thomas Woutersf8316632000-07-16 19:01:10 +0000159overridden by the local file.
Guido van Rossum64421161998-09-17 15:11:51 +0000160
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000161\begin{description}
162
Fred Drake74947ac1998-01-12 15:38:30 +0000163\item[h(elp) \optional{\var{command}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000164
Fred Drake74947ac1998-01-12 15:38:30 +0000165Without argument, print the list of available commands. With a
166\var{command} as argument, print help about that command. \samp{help
167pdb} displays the full documentation file; if the environment variable
Fred Drakec8993aa1999-04-22 16:50:40 +0000168\envvar{PAGER} is defined, the file is piped through that command
Fred Drake74947ac1998-01-12 15:38:30 +0000169instead. Since the \var{command} argument must be an identifier,
170\samp{help exec} must be entered to get help on the \samp{!} command.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000171
Guido van Rossum470be141995-03-17 16:07:09 +0000172\item[w(here)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000173
Fred Drake74947ac1998-01-12 15:38:30 +0000174Print a stack trace, with the most recent frame at the bottom. An
175arrow indicates the current frame, which determines the context of
176most commands.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000177
Guido van Rossum470be141995-03-17 16:07:09 +0000178\item[d(own)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000179
180Move the current frame one level down in the stack trace
George Yoshida4917c342006-05-11 15:53:27 +0000181(to a newer frame).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000182
Guido van Rossum470be141995-03-17 16:07:09 +0000183\item[u(p)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000184
185Move the current frame one level up in the stack trace
George Yoshida4917c342006-05-11 15:53:27 +0000186(to an older frame).
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000187
Fred Drake31ec33e1999-04-13 21:36:44 +0000188\item[b(reak) \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000189
190With a \var{lineno} argument, set a break there in the current
Guido van Rossum64421161998-09-17 15:11:51 +0000191file. With a \var{function} argument, set a break at the first
192executable statement within that function.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000193The line number may be prefixed with a filename and a colon,
194to specify a breakpoint in another file (probably one that
195hasn't been loaded yet). The file is searched on \code{sys.path}.
Guido van Rossum64421161998-09-17 15:11:51 +0000196Note that each breakpoint is assigned a number to which all the other
197breakpoint commands refer.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000198
Guido van Rossum64421161998-09-17 15:11:51 +0000199If a second argument is present, it is an expression which must
200evaluate to true before the breakpoint is honored.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000201
Guido van Rossum64421161998-09-17 15:11:51 +0000202Without argument, list all breaks, including for each breakpoint,
203the number of times that breakpoint has been hit, the current
204ignore count, and the associated condition if any.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000205
Fred Drake31ec33e1999-04-13 21:36:44 +0000206\item[tbreak \optional{\optional{\var{filename}:}\var{lineno}\code{\Large{|}}\var{function}\optional{, \var{condition}}}]
Guido van Rossum64421161998-09-17 15:11:51 +0000207
208Temporary breakpoint, which is removed automatically when it is
209first hit. The arguments are the same as break.
210
211\item[cl(ear) \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
212
213With a space separated list of breakpoint numbers, clear those
214breakpoints. Without argument, clear all breaks (but first
215ask confirmation).
216
217\item[disable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
218
219Disables the breakpoints given as a space separated list of
220breakpoint numbers. Disabling a breakpoint means it cannot cause
221the program to stop execution, but unlike clearing a breakpoint, it
222remains in the list of breakpoints and can be (re-)enabled.
223
224\item[enable \optional{\var{bpnumber} \optional{\var{bpnumber ...}}}]
225
226Enables the breakpoints specified.
227
228\item[ignore \var{bpnumber} \optional{\var{count}}]
229
230Sets the ignore count for the given breakpoint number. If
231count is omitted, the ignore count is set to 0. A breakpoint
232becomes active when the ignore count is zero. When non-zero,
233the count is decremented each time the breakpoint is reached
234and the breakpoint is not disabled and any associated condition
235evaluates to true.
236
237\item[condition \var{bpnumber} \optional{\var{condition}}]
238
239Condition is an expression which must evaluate to true before
240the breakpoint is honored. If condition is absent, any existing
241condition is removed; i.e., the breakpoint is made unconditional.
Guido van Rossum897b9f01998-07-20 23:29:10 +0000242
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000243\item[commands \optional{\var{bpnumber}}]
244
245Specify a list of commands for breakpoint number \var{bpnumber}. The
246commands themselves appear on the following lines. Type a line
247containing just 'end' to terminate the commands. An example:
248
249\begin{verbatim}
250(Pdb) commands 1
251(com) print some_variable
252(com) end
253(Pdb)
254\end{verbatim}
255
256To remove all commands from a breakpoint, type commands and
257follow it immediately with end; that is, give no commands.
258
259With no \var{bpnumber} argument, commands refers to the last
260breakpoint set.
261
262You can use breakpoint commands to start your program up again.
263Simply use the continue command, or step, or any other
264command that resumes execution.
265
266Specifying any command resuming execution (currently continue,
267step, next, return, jump, quit and their abbreviations) terminates
268the command list (as if that command was immediately followed by end).
269This is because any time you resume execution
270(even with a simple next or step), you may encounter·
271another breakpoint--which could have its own command list, leading to
272ambiguities about which list to execute.
273
274 If you use the 'silent' command in the command list, the
275usual message about stopping at a breakpoint is not printed. This may
276be desirable for breakpoints that are to print a specific message and
277then continue. If none of the other commands print anything, you
278see no sign that the breakpoint was reached.
279
280\versionadded{2.5}
281
Guido van Rossum470be141995-03-17 16:07:09 +0000282\item[s(tep)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000283
284Execute the current line, stop at the first possible occasion
285(either in a function that is called or on the next line in the
286current function).
287
Guido van Rossum470be141995-03-17 16:07:09 +0000288\item[n(ext)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000289
290Continue execution until the next line in the current function
Fred Drakec8993aa1999-04-22 16:50:40 +0000291is reached or it returns. (The difference between \samp{next} and
292\samp{step} is that \samp{step} stops inside a called function, while
293\samp{next} executes called functions at (nearly) full speed, only
Guido van Rossumf4aac481995-03-02 12:37:55 +0000294stopping at the next line in the current function.)
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000295
Guido van Rossum470be141995-03-17 16:07:09 +0000296\item[r(eturn)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000297
298Continue execution until the current function returns.
299
Guido van Rossum470be141995-03-17 16:07:09 +0000300\item[c(ont(inue))]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000301
302Continue execution, only stop when a breakpoint is encountered.
303
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000304\item[j(ump) \var{lineno}]
305
306Set the next line that will be executed. Only available in the
307bottom-most frame. This lets you jump back and execute code
308again, or jump forward to skip code that you don't want to run.
309
Fred Drakee0f02f02002-12-18 02:07:14 +0000310It should be noted that not all jumps are allowed --- for instance it
311is not possible to jump into the middle of a \keyword{for} loop or out
312of a \keyword{finally} clause.
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000313
Fred Drake853276e2003-07-16 17:58:38 +0000314\item[l(ist) \optional{\var{first}\optional{, \var{last}}}]
Guido van Rossum43b655c1998-09-17 17:07:15 +0000315
Guido van Rossumf4aac481995-03-02 12:37:55 +0000316List source code for the current file. Without arguments, list 11
317lines around the current line or continue the previous listing. With
318one argument, list 11 lines around at that line. With two arguments,
319list the given range; if the second argument is less than the first,
320it is interpreted as a count.
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000321
Guido van Rossum470be141995-03-17 16:07:09 +0000322\item[a(rgs)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000323
324Print the argument list of the current function.
325
Guido van Rossum470be141995-03-17 16:07:09 +0000326\item[p \var{expression}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000327
328Evaluate the \var{expression} in the current context and print its
Fred Drakee0f02f02002-12-18 02:07:14 +0000329value. \note{\samp{print} can also be used, but is not a debugger
330command --- this executes the Python \keyword{print} statement.}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000331
Barry Warsaw75f36b72002-11-05 22:41:16 +0000332\item[pp \var{expression}]
333
Neal Norwitza5c64592004-10-17 19:55:47 +0000334Like the \samp{p} command, except the value of the expression is
Barry Warsaw75f36b72002-11-05 22:41:16 +0000335pretty-printed using the \module{pprint} module.
336
Guido van Rossum64421161998-09-17 15:11:51 +0000337\item[alias \optional{\var{name} \optional{command}}]
338
339Creates an alias called \var{name} that executes \var{command}. The
340command must \emph{not} be enclosed in quotes. Replaceable parameters
341can be indicated by \samp{\%1}, \samp{\%2}, and so on, while \samp{\%*} is
342replaced by all the parameters. If no command is given, the current
343alias for \var{name} is shown. If no arguments are given, all
344aliases are listed.
345
346Aliases may be nested and can contain anything that can be
347legally typed at the pdb prompt. Note that internal pdb commands
348\emph{can} be overridden by aliases. Such a command is
349then hidden until the alias is removed. Aliasing is recursively
350applied to the first word of the command line; all other words
351in the line are left alone.
352
353As an example, here are two useful aliases (especially when placed
354in the \file{.pdbrc} file):
355
356\begin{verbatim}
357#Print instance variables (usage "pi classInst")
358alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
359#Print instance variables in self
360alias ps pi self
361\end{verbatim}
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000362
Guido van Rossum64421161998-09-17 15:11:51 +0000363\item[unalias \var{name}]
364
365Deletes the specified alias.
366
Fred Drake74947ac1998-01-12 15:38:30 +0000367\item[\optional{!}\var{statement}]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000368
369Execute the (one-line) \var{statement} in the context of
370the current stack frame.
371The exclamation point can be omitted unless the first word
372of the statement resembles a debugger command.
373To set a global variable, you can prefix the assignment
Fred Drakec8993aa1999-04-22 16:50:40 +0000374command with a \samp{global} command on the same line, e.g.:
Fred Drake82d493f1998-04-07 19:14:17 +0000375
Fred Drake19479911998-02-13 06:58:54 +0000376\begin{verbatim}
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000377(Pdb) global list_options; list_options = ['-l']
378(Pdb)
Fred Drake19479911998-02-13 06:58:54 +0000379\end{verbatim}
Fred Drake82d493f1998-04-07 19:14:17 +0000380
Guido van Rossum470be141995-03-17 16:07:09 +0000381\item[q(uit)]
Guido van Rossumdc46c7f1995-03-01 15:38:16 +0000382
383Quit from the debugger.
384The program being executed is aborted.
385
386\end{description}
Guido van Rossum470be141995-03-17 16:07:09 +0000387
Fred Drake1a2302b2001-07-18 17:40:19 +0000388\section{How It Works \label{debugger-hooks}}
Guido van Rossum470be141995-03-17 16:07:09 +0000389
390Some changes were made to the interpreter:
391
392\begin{itemize}
Fred Drake74947ac1998-01-12 15:38:30 +0000393\item \code{sys.settrace(\var{func})} sets the global trace function
Guido van Rossum470be141995-03-17 16:07:09 +0000394\item there can also a local trace function (see later)
395\end{itemize}
396
Fred Drake82d493f1998-04-07 19:14:17 +0000397Trace functions have three arguments: \var{frame}, \var{event}, and
398\var{arg}. \var{frame} is the current stack frame. \var{event} is a
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000399string: \code{'call'}, \code{'line'}, \code{'return'}, \code{'exception'},
400 \code{'c_call'}, \code{'c_return'}, or \code{'c_exception'}. \var{arg}
401 depends on the event type.
Guido van Rossum470be141995-03-17 16:07:09 +0000402
Guido van Rossum9d37a4d1997-10-27 19:57:20 +0000403The global trace function is invoked (with \var{event} set to
404\code{'call'}) whenever a new local scope is entered; it should return
405a reference to the local trace function to be used that scope, or
406\code{None} if the scope shouldn't be traced.
407
408The local trace function should return a reference to itself (or to
409another function for further tracing in that scope), or \code{None} to
410turn off tracing in that scope.
411
412Instance methods are accepted (and very useful!) as trace functions.
Guido van Rossum470be141995-03-17 16:07:09 +0000413
414The events have the following meaning:
415
416\begin{description}
417
418\item[\code{'call'}]
419A function is called (or some other code block entered). The global
Fred Drakeb9a96282001-09-13 16:56:43 +0000420trace function is called; \var{arg} is \code{None};
Guido van Rossum470be141995-03-17 16:07:09 +0000421the return value specifies the local trace function.
422
423\item[\code{'line'}]
424The interpreter is about to execute a new line of code (sometimes
425multiple line events on one line exist). The local trace function is
Fred Drakeb9a96282001-09-13 16:56:43 +0000426called; \var{arg} is \code{None}; the return value specifies the new
427local trace function.
Guido van Rossum470be141995-03-17 16:07:09 +0000428
429\item[\code{'return'}]
430A function (or other code block) is about to return. The local trace
Fred Drakeb9a96282001-09-13 16:56:43 +0000431function is called; \var{arg} is the value that will be returned. The
432trace function's return value is ignored.
Guido van Rossum470be141995-03-17 16:07:09 +0000433
434\item[\code{'exception'}]
Fred Drakeb9a96282001-09-13 16:56:43 +0000435An exception has occurred. The local trace function is called;
436\var{arg} is a triple \code{(\var{exception}, \var{value},
437\var{traceback})}; the return value specifies the new local trace
438function.
Guido van Rossum470be141995-03-17 16:07:09 +0000439
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000440\item[\code{'c_call'}]
441A C function is about to be called. This may be an extension function
Nicholas Bastin068979c2004-07-04 04:47:40 +0000442or a builtin. \var{arg} is the C function object.
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000443
444\item[\code{'c_return'}]
445A C function has returned. \var{arg} is \code{None}.
446
447\item[\code{'c_exception'}]
448A C function has thrown an exception. \var{arg} is \code{None}.
449
Guido van Rossum470be141995-03-17 16:07:09 +0000450\end{description}
451
452Note that as an exception is propagated down the chain of callers, an
453\code{'exception'} event is generated at each level.
454
Fred Drakebc8ad5b1998-03-11 06:29:59 +0000455For more information on code and frame objects, refer to the
Fred Drake356d0ce1999-11-09 20:10:01 +0000456\citetitle[../ref/ref.html]{Python Reference Manual}.