Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 1 | \section{\module{trace} --- |
| 2 | Trace or track Python statement execution} |
| 3 | |
| 4 | \declaremodule{standard}{trace} |
| 5 | \modulesynopsis{Trace or track Python statement execution.} |
| 6 | |
| 7 | The \module{trace} module allows you to trace program execution, generate |
| 8 | annotated statement coverage listings, print caller/callee relationships and |
| 9 | list functions executed during a program run. It can be used in another |
| 10 | program or from the command line. |
| 11 | |
| 12 | \subsection{Command Line Usage} |
| 13 | |
| 14 | The \module{trace} module can be invoked from the command line. It can be |
| 15 | as simple as |
| 16 | |
| 17 | \begin{verbatim} |
| 18 | python -m trace --count somefile.py ... |
| 19 | \end{verbatim} |
| 20 | |
| 21 | The above will generate annotated listings of all Python modules imported |
Skip Montanaro | 56a3706 | 2006-04-23 19:26:33 +0000 | [diff] [blame] | 22 | during the execution of \code{somefile.py}. |
Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 23 | |
| 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 |
| 29 | completion that shows how many times each statement was executed.} |
| 30 | \item[--report, -r]{Produce an annotated list from an earlier program run that |
| 31 | used the \code{--count} and \code{--file} arguments.} |
| 32 | \item[--no-report, -R]{Do not generate annotated listings. This is useful |
| 33 | if you intend to make several runs with \code{--count} then produce a single |
| 34 | set 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 |
| 37 | program.} |
| 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 |
| 40 | files.} |
| 41 | \item[--missing, -m]{When generating annotated listings, mark lines which |
| 42 | were not executed with \code{>>>>>>}.} |
| 43 | \item[--summary -s]{When using \code{--count} or \code{--report}, write a |
| 44 | brief summary to stdout for each file processed.} |
| 45 | \item[--ignore-module]{Ignore the named module and its submodules (if it is |
| 46 | a package). May be given multiple times.} |
| 47 | \item[--ignore-dir]{Ignore all modules and packages in the named directory |
| 48 | and 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 | |
| 55 | Create an object to trace execution of a single statement or expression. |
| 56 | All parameters are optional. \var{count} enables counting of line numbers. |
| 57 | \var{trace} enables line execution tracing. \var{countfuncs} enables |
| 58 | listing of the functions called during the run. \var{countcallers} enables |
| 59 | call relationship tracking. \var{ignoremods} is a list of modules or |
| 60 | packages to ignore. \var{ignoredirs} is a list of directories whose modules |
| 61 | or packages should be ignored. \var{infile} is the file from which to read |
| 62 | stored count information. \var{outfile} is a file in which to write updated |
| 63 | count information. |
| 64 | |
| 65 | \end{classdesc} |
| 66 | |
| 67 | \begin{methoddesc}[Trace]{run}{cmd} |
| 68 | Run \code{cmd} under control of the Trace object with the current tracing |
| 69 | parameters. |
| 70 | \end{methoddesc} |
| 71 | |
Skip Montanaro | 56a3706 | 2006-04-23 19:26:33 +0000 | [diff] [blame] | 72 | \begin{methoddesc}[Trace]{runctx}{cmd\optional{,globals=None\optional{,locals=None}}} |
Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 73 | Run \code{cmd} under control of the Trace object with the current tracing |
| 74 | parameters 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} |
| 79 | Call \code{function} with the given arguments under control of the Trace |
| 80 | object with the current tracing parameters. |
| 81 | \end{methoddesc} |
| 82 | |
| 83 | \subsubsection{Example} |
| 84 | |
| 85 | \begin{verbatim} |
Skip Montanaro | 9ab2f45 | 2006-04-23 19:30:50 +0000 | [diff] [blame] | 86 | import sys |
| 87 | |
Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 88 | # create a Trace object, telling it what to ignore, and whether to |
| 89 | # do tracing or line-counting or both. |
Skip Montanaro | 9ab2f45 | 2006-04-23 19:30:50 +0000 | [diff] [blame] | 90 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, |
Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 91 | count=1) |
Skip Montanaro | 9ab2f45 | 2006-04-23 19:30:50 +0000 | [diff] [blame] | 92 | # run the new command using the given tracer |
| 93 | tracer.run('main()') |
| 94 | # make a report, placing output in /tmp |
| 95 | r = tracer.results() |
| 96 | r.write_results(show_missing=True, coverdir="/tmp") |
Skip Montanaro | 47767c3 | 2006-04-23 19:14:27 +0000 | [diff] [blame] | 97 | \end{verbatim} |