blob: 2465aacafe4df8dbd8f1d9fc7224e7d5d9d681b7 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001\section{\module{trace} ---
2 Trace or track Python statement execution}
3
4\declaremodule{standard}{trace}
5\modulesynopsis{Trace or track Python statement execution.}
6
7The \module{trace} module allows you to trace program execution, generate
8annotated statement coverage listings, print caller/callee relationships and
9list functions executed during a program run. It can be used in another
10program or from the command line.
11
12\subsection{Command Line Usage\label{trace-cli}}
13
14The \module{trace} module can be invoked from the command line. It can be
15as simple as
16
17\begin{verbatim}
18python -m trace --count somefile.py ...
19\end{verbatim}
20
21The above will generate annotated listings of all Python modules imported
22during the execution of \file{somefile.py}.
23
24The following command-line arguments are supported:
25
26\begin{description}
27\item[\longprogramopt{trace}, \programopt{-t}]
28Display lines as they are executed.
29
30\item[\longprogramopt{count}, \programopt{-c}]
31Produce a set of annotated listing files upon program
32completion that shows how many times each statement was executed.
33
34\item[\longprogramopt{report}, \programopt{-r}]
35Produce an annotated list from an earlier program run that
36used the \longprogramopt{count} and \longprogramopt{file} arguments.
37
38\item[\longprogramopt{no-report}, \programopt{-R}]
39Do not generate annotated listings. This is useful if you intend to make
40several runs with \longprogramopt{count} then produce a single set
41of annotated listings at the end.
42
43\item[\longprogramopt{listfuncs}, \programopt{-l}]
44List the functions executed by running the program.
45
46\item[\longprogramopt{trackcalls}, \programopt{-T}]
47Generate calling relationships exposed by running the program.
48
49\item[\longprogramopt{file}, \programopt{-f}]
50Name a file containing (or to contain) counts.
51
52\item[\longprogramopt{coverdir}, \programopt{-C}]
53Name a directory in which to save annotated listing files.
54
55\item[\longprogramopt{missing}, \programopt{-m}]
56When generating annotated listings, mark lines which
57were not executed with `\code{>>>>>>}'.
58
59\item[\longprogramopt{summary}, \programopt{-s}]
60When using \longprogramopt{count} or \longprogramopt{report}, write a
61brief summary to stdout for each file processed.
62
63\item[\longprogramopt{ignore-module}]
64Ignore the named module and its submodules (if it is
65a package). May be given multiple times.
66
67\item[\longprogramopt{ignore-dir}]
68Ignore all modules and packages in the named directory
69and subdirectories. May be given multiple times.
70\end{description}
71
72\subsection{Programming Interface\label{trace-api}}
73
74\begin{classdesc}{Trace}{\optional{count=1\optional{, trace=1\optional{,
75 countfuncs=0\optional{, countcallers=0\optional{,
76 ignoremods=()\optional{, ignoredirs=()\optional{,
77 infile=None\optional{, outfile=None}}}}}}}}}
78Create an object to trace execution of a single statement or expression.
79All parameters are optional. \var{count} enables counting of line numbers.
80\var{trace} enables line execution tracing. \var{countfuncs} enables
81listing of the functions called during the run. \var{countcallers} enables
82call relationship tracking. \var{ignoremods} is a list of modules or
83packages to ignore. \var{ignoredirs} is a list of directories whose modules
84or packages should be ignored. \var{infile} is the file from which to read
85stored count information. \var{outfile} is a file in which to write updated
86count information.
87\end{classdesc}
88
89\begin{methoddesc}[Trace]{run}{cmd}
90Run \var{cmd} under control of the Trace object with the current tracing
91parameters.
92\end{methoddesc}
93
94\begin{methoddesc}[Trace]{runctx}{cmd\optional{, globals=None\optional{,
95 locals=None}}}
96Run \var{cmd} under control of the Trace object with the current tracing
97parameters in the defined global and local environments. If not defined,
98\var{globals} and \var{locals} default to empty dictionaries.
99\end{methoddesc}
100
101\begin{methoddesc}[Trace]{runfunc}{func, *args, **kwds}
102Call \var{func} with the given arguments under control of the
103\class{Trace} object with the current tracing parameters.
104\end{methoddesc}
105
106This is a simple example showing the use of this module:
107
108\begin{verbatim}
109import sys
110import trace
111
112# create a Trace object, telling it what to ignore, and whether to
113# do tracing or line-counting or both.
114tracer = trace.Trace(
115 ignoredirs=[sys.prefix, sys.exec_prefix],
116 trace=0,
117 count=1)
118
119# run the new command using the given tracer
120tracer.run('main()')
121
122# make a report, placing output in /tmp
123r = tracer.results()
124r.write_results(show_missing=True, coverdir="/tmp")
125\end{verbatim}