blob: 7b49c8fd49aa3d2b81066020049a6f984e8ca5e3 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`trace` --- Trace or track Python statement execution
2==========================================================
3
4.. module:: trace
5 :synopsis: Trace or track Python statement execution.
6
7
8The :mod:`trace` module allows you to trace program execution, generate
9annotated statement coverage listings, print caller/callee relationships and
10list functions executed during a program run. It can be used in another program
11or from the command line.
12
13
14.. _trace-cli:
15
Éric Araujob344dd02011-02-02 21:38:37 +000016Command-Line Usage
Georg Brandl116aa622007-08-15 14:28:22 +000017------------------
18
19The :mod:`trace` module can be invoked from the command line. It can be as
20simple as ::
21
Éric Araujob344dd02011-02-02 21:38:37 +000022 python -m trace --count -C . somefile.py ...
Georg Brandl116aa622007-08-15 14:28:22 +000023
Éric Araujob344dd02011-02-02 21:38:37 +000024The above will execute :file:`somefile.py` and generate annotated listings of
25all Python modules imported during the execution into the current directory.
Georg Brandl116aa622007-08-15 14:28:22 +000026
Éric Araujob344dd02011-02-02 21:38:37 +000027.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000028
Éric Araujob344dd02011-02-02 21:38:37 +000029.. cmdoption:: --help
30
31 Display usage and exit.
32
33.. cmdoption:: --version
34
35 Display the version of the module and exit.
36
37Main options
38^^^^^^^^^^^^
39
40At least one of the following options must be specified when invoking
41:mod:`trace`. The :option:`--listfuncs <-l>` option is mutually exclusive with
42the :option:`--trace <-t>` and :option:`--counts <-c>` options . When
43:option:`--listfuncs <-l>` is provided, neither :option:`--counts <-c>` nor
44:option:`--trace <-t>` are accepted, and vice versa.
45
46.. program:: trace
47
48.. cmdoption:: -c, --count
49
50 Produce a set of annotated listing files upon program completion that shows
51 how many times each statement was executed. See also
52 :option:`--coverdir <-C>`, :option:`--file <-f>` and
53 :option:`--no-report <-R>` below.
54
55.. cmdoption:: -t, --trace
56
Georg Brandl116aa622007-08-15 14:28:22 +000057 Display lines as they are executed.
58
Éric Araujob344dd02011-02-02 21:38:37 +000059.. cmdoption:: -l, --listfuncs
Georg Brandl116aa622007-08-15 14:28:22 +000060
Éric Araujob344dd02011-02-02 21:38:37 +000061 Display the functions executed by running the program.
62
63.. cmdoption:: -r, --report
64
Georg Brandl116aa622007-08-15 14:28:22 +000065 Produce an annotated list from an earlier program run that used the
Éric Araujob344dd02011-02-02 21:38:37 +000066 :option:`--count <-c>` and :option:`--file <-f>` option. This does not
67 execute any code.
Georg Brandl116aa622007-08-15 14:28:22 +000068
Éric Araujob344dd02011-02-02 21:38:37 +000069.. cmdoption:: -T, --trackcalls
Georg Brandl116aa622007-08-15 14:28:22 +000070
Éric Araujob344dd02011-02-02 21:38:37 +000071 Display the calling relationships exposed by running the program.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Éric Araujob344dd02011-02-02 21:38:37 +000073Modifiers
74^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +000075
Éric Araujob344dd02011-02-02 21:38:37 +000076.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000077
Éric Araujob344dd02011-02-02 21:38:37 +000078.. cmdoption:: -f, --file=<file>
Georg Brandl116aa622007-08-15 14:28:22 +000079
Éric Araujob344dd02011-02-02 21:38:37 +000080 Name of a file to accumulate counts over several tracing runs. Should be
81 used with the :option:`--count <-c>` option.
82
83.. cmdoption:: -C, --coverdir=<dir>
84
85 Directory where the report files go. The coverage report for
86 ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`.
87
88.. cmdoption:: -m, --missing
89
Georg Brandl116aa622007-08-15 14:28:22 +000090 When generating annotated listings, mark lines which were not executed with
Éric Araujob344dd02011-02-02 21:38:37 +000091 ``>>>>>>``.
Georg Brandl116aa622007-08-15 14:28:22 +000092
Éric Araujob344dd02011-02-02 21:38:37 +000093.. cmdoption:: -s, --summary
Georg Brandl116aa622007-08-15 14:28:22 +000094
Éric Araujob344dd02011-02-02 21:38:37 +000095 When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief
96 summary to stdout for each file processed.
Georg Brandl116aa622007-08-15 14:28:22 +000097
Éric Araujob344dd02011-02-02 21:38:37 +000098.. cmdoption:: -R, --no-report
Georg Brandl116aa622007-08-15 14:28:22 +000099
Éric Araujob344dd02011-02-02 21:38:37 +0000100 Do not generate annotated listings. This is useful if you intend to make
101 several runs with :option:`--count <-c>`, and then produce a single set of
102 annotated listings at the end.
103
104.. cmdoption:: -g, --timing
105
106 Prefix each line with the time since the program started. Only used while
107 tracing.
108
109Filters
110^^^^^^^
111
112These options may be repeated multiple times.
113
114.. program:: trace
115
116.. cmdoption:: --ignore-module=<mod>
117
118 Ignore each of the given module names and its submodules (if it is a
119 package). The argument can be a list of names separated by a comma.
120
121.. cmdoption:: --ignore-dir=<dir>
122
123 Ignore all modules and packages in the named directory and subdirectories.
124 The argument can be a list of directories separated by :data:`os.pathsep`.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. _trace-api:
127
Éric Araujob344dd02011-02-02 21:38:37 +0000128Programmatic Interface
129----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Éric Araujob344dd02011-02-02 21:38:37 +0000131.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(),\
132 ignoredirs=(), infile=None, outfile=None, timing=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Éric Araujob344dd02011-02-02 21:38:37 +0000134 Create an object to trace execution of a single statement or expression. All
135 parameters are optional. *count* enables counting of line numbers. *trace*
136 enables line execution tracing. *countfuncs* enables listing of the
137 functions called during the run. *countcallers* enables call relationship
138 tracking. *ignoremods* is a list of modules or packages to ignore.
139 *ignoredirs* is a list of directories whose modules or packages should be
140 ignored. *infile* is the name of the file from which to read stored count
141 information. *outfile* is the name of the file in which to write updated
142 count information. *timing* enables a timestamp relative to when tracing was
143 started to be displayed.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Éric Araujob344dd02011-02-02 21:38:37 +0000145 .. method:: run(cmd)
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Éric Araujob344dd02011-02-02 21:38:37 +0000147 Execute the command and gather statistics from the execution with
148 the current tracing parameters. *cmd* must be a string or code object,
149 suitable for passing into :func:`exec`.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Éric Araujob344dd02011-02-02 21:38:37 +0000151 .. method:: runctx(cmd, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Éric Araujob344dd02011-02-02 21:38:37 +0000153 Execute the command and gather statistics from the execution with the
154 current tracing parameters, in the defined global and local
155 environments. If not defined, *globals* and *locals* default to empty
156 dictionaries.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Éric Araujob344dd02011-02-02 21:38:37 +0000158 .. method:: runfunc(func, *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Éric Araujob344dd02011-02-02 21:38:37 +0000160 Call *func* with the given arguments under control of the :class:`Trace`
161 object with the current tracing parameters.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Éric Araujob344dd02011-02-02 21:38:37 +0000163 .. method:: results()
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Éric Araujob344dd02011-02-02 21:38:37 +0000165 Return a :class:`CoverageResults` object that contains the cumulative
166 results of all previous calls to ``run``, ``runctx`` and ``runfunc``
167 for the given :class:`Trace` instance. Does not reset the accumulated
168 trace results.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Éric Araujob344dd02011-02-02 21:38:37 +0000170.. class:: CoverageResults
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Éric Araujob344dd02011-02-02 21:38:37 +0000172 A container for coverage results, created by :meth:`Trace.results`. Should
173 not be created directly by the user.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Éric Araujob344dd02011-02-02 21:38:37 +0000175 .. method:: update(other)
176
177 Merge in data from another :class:`CoverageResults` object.
178
179 .. method:: write_results(show_missing=True, summary=False, coverdir=None)
180
181 Write coverage results. Set *show_missing* to show lines that had no
182 hits. Set *summary* to include in the output the coverage summary per
183 module. *coverdir* specifies the directory into which the coverage
184 result files will be output. If ``None``, the results for each source
185 file are placed in its directory.
186
187A simple example demonstrating the use of the programmatic interface::
Georg Brandl116aa622007-08-15 14:28:22 +0000188
189 import sys
190 import trace
191
192 # create a Trace object, telling it what to ignore, and whether to
193 # do tracing or line-counting or both.
194 tracer = trace.Trace(
195 ignoredirs=[sys.prefix, sys.exec_prefix],
196 trace=0,
197 count=1)
198
199 # run the new command using the given tracer
200 tracer.run('main()')
201
202 # make a report, placing output in /tmp
203 r = tracer.results()
204 r.write_results(show_missing=True, coverdir="/tmp")
205