blob: bafee61a60dec8b3ee0718b8a8e6c69bffa41d72 [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
Fred Drake2afbf962006-04-26 05:15:41 +000012\subsection{Command Line Usage\label{trace-cli}}
Skip Montanaro47767c32006-04-23 19:14:27 +000013
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
Fred Drake2afbf962006-04-26 05:15:41 +000022during the execution of \file{somefile.py}.
Skip Montanaro47767c32006-04-23 19:14:27 +000023
Fred Drake2afbf962006-04-26 05:15:41 +000024The following command-line arguments are supported:
Skip Montanaro47767c32006-04-23 19:14:27 +000025
26\begin{description}
Fred Drake2afbf962006-04-26 05:15:41 +000027\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{>}\code{>}\code{>}\code{>}\code{>}\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.
Skip Montanaro47767c32006-04-23 19:14:27 +000070\end{description}
71
Fred Drake2afbf962006-04-26 05:15:41 +000072\subsection{Programming Interface\label{trace-api}}
Skip Montanaro47767c32006-04-23 19:14:27 +000073
Fred Drake2afbf962006-04-26 05:15:41 +000074\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}}}}}}}}}
Skip Montanaro47767c32006-04-23 19:14:27 +000078Create 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.
Skip Montanaro47767c32006-04-23 19:14:27 +000087\end{classdesc}
88
89\begin{methoddesc}[Trace]{run}{cmd}
Fred Drake2afbf962006-04-26 05:15:41 +000090Run \var{cmd} under control of the Trace object with the current tracing
Skip Montanaro47767c32006-04-23 19:14:27 +000091parameters.
92\end{methoddesc}
93
Fred Drake2afbf962006-04-26 05:15:41 +000094\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
Skip Montanaro47767c32006-04-23 19:14:27 +000097parameters in the defined global and local environments. If not defined,
Fred Drake2afbf962006-04-26 05:15:41 +000098\var{globals} and \var{locals} default to empty dictionaries.
Skip Montanaro47767c32006-04-23 19:14:27 +000099\end{methoddesc}
100
101\begin{methoddesc}[Trace]{runfunc}{func, *args, **kwds}
Fred Drake2afbf962006-04-26 05:15:41 +0000102Call \var{func} with the given arguments under control of the
103\class{Trace} object with the current tracing parameters.
Skip Montanaro47767c32006-04-23 19:14:27 +0000104\end{methoddesc}
105
Fred Drake2afbf962006-04-26 05:15:41 +0000106This is a simple example showing the use of this module:
Skip Montanaro47767c32006-04-23 19:14:27 +0000107
108\begin{verbatim}
Skip Montanaro9ab2f452006-04-23 19:30:50 +0000109import sys
Fred Drake2afbf962006-04-26 05:15:41 +0000110import trace
Skip Montanaro9ab2f452006-04-23 19:30:50 +0000111
Skip Montanaro47767c32006-04-23 19:14:27 +0000112# create a Trace object, telling it what to ignore, and whether to
113# do tracing or line-counting or both.
Fred Drake2afbf962006-04-26 05:15:41 +0000114tracer = trace.Trace(
115 ignoredirs=[sys.prefix, sys.exec_prefix],
116 trace=0,
117 count=1)
118
Skip Montanaro9ab2f452006-04-23 19:30:50 +0000119# run the new command using the given tracer
120tracer.run('main()')
Fred Drake2afbf962006-04-26 05:15:41 +0000121
Skip Montanaro9ab2f452006-04-23 19:30:50 +0000122# make a report, placing output in /tmp
123r = tracer.results()
124r.write_results(show_missing=True, coverdir="/tmp")
Skip Montanaro47767c32006-04-23 19:14:27 +0000125\end{verbatim}