blob: 65d13522b7c9de7e1a9618a7d29938b9abd02a6d [file] [log] [blame]
Skip Montanaro47767c32006-04-23 19:14:27 +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}
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
Skip Montanaro56a37062006-04-23 19:26:33 +000022during the execution of \code{somefile.py}.
Skip Montanaro47767c32006-04-23 19:14:27 +000023
24\subsection{Command Line Arguments}
25
26\begin{description}
27\item[--trace, -t]{Display lines as they are executed.}
28\item[--count, -c]{Produce a set of annotated listing files upon program
29completion that shows how many times each statement was executed.}
30\item[--report, -r]{Produce an annotated list from an earlier program run that
31used the \code{--count} and \code{--file} arguments.}
32\item[--no-report, -R]{Do not generate annotated listings. This is useful
33if you intend to make several runs with \code{--count} then produce a single
34set of annotated listings at the end.}
35\item[--listfuncs, -l]{List the functions executed by running the program.}
36\item[--trackcalls, -T]{Generate calling relationships exposed by running the
37program.}
38\item[--file, -f]{Name a file containing (or to contain) counts.}
39\item[--coverdir, -C]{Name a directory in which to save annotated listing
40files.}
41\item[--missing, -m]{When generating annotated listings, mark lines which
42were not executed with \code{>>>>>>}.}
43\item[--summary -s]{When using \code{--count} or \code{--report}, write a
44brief summary to stdout for each file processed.}
45\item[--ignore-module]{Ignore the named module and its submodules (if it is
46a package). May be given multiple times.}
47\item[--ignore-dir]{Ignore all modules and packages in the named directory
48and subdirectories. May be given multiple times.}
49\end{description}
50
51\subsection{Program Usage}
52
53\begin{classdesc}{Trace}{\optional{count=1\optional{,trace=1\optional{,countfuncs=0\optional{,countcallers=0\optional{,ignoremods=()\optional{,ignoredirs=()\optional{,infile=None\optional{,outfile=None}}}}}}}}}
54
55Create an object to trace execution of a single statement or expression.
56All parameters are optional. \var{count} enables counting of line numbers.
57\var{trace} enables line execution tracing. \var{countfuncs} enables
58listing of the functions called during the run. \var{countcallers} enables
59call relationship tracking. \var{ignoremods} is a list of modules or
60packages to ignore. \var{ignoredirs} is a list of directories whose modules
61or packages should be ignored. \var{infile} is the file from which to read
62stored count information. \var{outfile} is a file in which to write updated
63count information.
64
65\end{classdesc}
66
67\begin{methoddesc}[Trace]{run}{cmd}
68Run \code{cmd} under control of the Trace object with the current tracing
69parameters.
70\end{methoddesc}
71
Skip Montanaro56a37062006-04-23 19:26:33 +000072\begin{methoddesc}[Trace]{runctx}{cmd\optional{,globals=None\optional{,locals=None}}}
Skip Montanaro47767c32006-04-23 19:14:27 +000073Run \code{cmd} under control of the Trace object with the current tracing
74parameters in the defined global and local environments. If not defined,
75\code{globals} and \code{locals} default to empty dictionaries.
76\end{methoddesc}
77
78\begin{methoddesc}[Trace]{runfunc}{func, *args, **kwds}
79Call \code{function} with the given arguments under control of the Trace
80object with the current tracing parameters.
81\end{methoddesc}
82
83\subsubsection{Example}
84
85\begin{verbatim}
Skip Montanaro9ab2f452006-04-23 19:30:50 +000086import sys
87
Skip Montanaro47767c32006-04-23 19:14:27 +000088# create a Trace object, telling it what to ignore, and whether to
89# do tracing or line-counting or both.
Skip Montanaro9ab2f452006-04-23 19:30:50 +000090tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0,
Skip Montanaro47767c32006-04-23 19:14:27 +000091 count=1)
Skip Montanaro9ab2f452006-04-23 19:30:50 +000092# run the new command using the given tracer
93tracer.run('main()')
94# make a report, placing output in /tmp
95r = tracer.results()
96r.write_results(show_missing=True, coverdir="/tmp")
Skip Montanaro47767c32006-04-23 19:14:27 +000097\end{verbatim}