blob: 96f31e97192ac1d86c0820a4b40e91c37589e665 [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
Éric Araujo6e6cb8e2010-11-16 19:13:50 +000013.. seealso::
14
15 Latest version of the :source:`trace module Python source code
16 <Lib/trace.py>`
Georg Brandl116aa622007-08-15 14:28:22 +000017
18.. _trace-cli:
19
Éric Araujod00862a2010-12-15 19:09:58 +000020Command-Line Usage
Georg Brandl116aa622007-08-15 14:28:22 +000021------------------
22
23The :mod:`trace` module can be invoked from the command line. It can be as
24simple as ::
25
Éric Araujod00862a2010-12-15 19:09:58 +000026 python -m trace --count -C . somefile.py ...
Georg Brandl116aa622007-08-15 14:28:22 +000027
Éric Araujo67f0b6c2010-12-15 19:30:15 +000028The above will execute :file:`somefile.py` and generate annotated listings of
29all Python modules imported during the execution into the current directory.
Georg Brandl116aa622007-08-15 14:28:22 +000030
Éric Araujod00862a2010-12-15 19:09:58 +000031.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000032
Éric Araujod00862a2010-12-15 19:09:58 +000033.. cmdoption:: --help
34
35 Display usage and exit.
36
37.. cmdoption:: --version
38
39 Display the version of the module and exit.
40
41Main options
42^^^^^^^^^^^^
43
Éric Araujo67f0b6c2010-12-15 19:30:15 +000044At least one of the following options must be specified when invoking
45:mod:`trace`. The :option:`--listfuncs <-l>` option is mutually exclusive with
46the :option:`--trace <-t>` and :option:`--counts <-c>` options . When
47:option:`--listfuncs <-l>` is provided, neither :option:`--counts <-c>` nor
48:option:`--trace <-t>` are accepted, and vice versa.
Éric Araujod00862a2010-12-15 19:09:58 +000049
50.. program:: trace
51
52.. cmdoption:: -c, --count
53
54 Produce a set of annotated listing files upon program completion that shows
Éric Araujo67f0b6c2010-12-15 19:30:15 +000055 how many times each statement was executed. See also
56 :option:`--coverdir <-C>`, :option:`--file <-f>` and
Éric Araujod00862a2010-12-15 19:09:58 +000057 :option:`--no-report <-R>` below.
58
59.. cmdoption:: -t, --trace
60
Georg Brandl116aa622007-08-15 14:28:22 +000061 Display lines as they are executed.
62
Éric Araujod00862a2010-12-15 19:09:58 +000063.. cmdoption:: -l, --listfuncs
Georg Brandl116aa622007-08-15 14:28:22 +000064
Éric Araujod00862a2010-12-15 19:09:58 +000065 Display the functions executed by running the program.
66
67.. cmdoption:: -r, --report
68
Georg Brandl116aa622007-08-15 14:28:22 +000069 Produce an annotated list from an earlier program run that used the
Éric Araujo67f0b6c2010-12-15 19:30:15 +000070 :option:`--count <-c>` and :option:`--file <-f>` option. This does not
71 execute any code.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Éric Araujod00862a2010-12-15 19:09:58 +000073.. cmdoption:: -T, --trackcalls
Georg Brandl116aa622007-08-15 14:28:22 +000074
Éric Araujod00862a2010-12-15 19:09:58 +000075 Display the calling relationships exposed by running the program.
Georg Brandl116aa622007-08-15 14:28:22 +000076
Éric Araujod00862a2010-12-15 19:09:58 +000077Modifiers
78^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +000079
Éric Araujod00862a2010-12-15 19:09:58 +000080.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000081
Éric Araujod00862a2010-12-15 19:09:58 +000082.. cmdoption:: -f, --file=<file>
Georg Brandl116aa622007-08-15 14:28:22 +000083
Éric Araujo67f0b6c2010-12-15 19:30:15 +000084 Name of a file to accumulate counts over several tracing runs. Should be
85 used with the :option:`--count <-c>` option.
Éric Araujod00862a2010-12-15 19:09:58 +000086
87.. cmdoption:: -C, --coverdir=<dir>
88
Éric Araujo67f0b6c2010-12-15 19:30:15 +000089 Directory where the report files go. The coverage report for
Éric Araujod00862a2010-12-15 19:09:58 +000090 ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`.
91
92.. cmdoption:: -m, --missing
93
Georg Brandl116aa622007-08-15 14:28:22 +000094 When generating annotated listings, mark lines which were not executed with
Éric Araujod00862a2010-12-15 19:09:58 +000095 ``>>>>>>``.
Georg Brandl116aa622007-08-15 14:28:22 +000096
Éric Araujod00862a2010-12-15 19:09:58 +000097.. cmdoption:: -s, --summary
Georg Brandl116aa622007-08-15 14:28:22 +000098
Éric Araujod00862a2010-12-15 19:09:58 +000099 When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief
100 summary to stdout for each file processed.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Éric Araujod00862a2010-12-15 19:09:58 +0000102.. cmdoption:: -R, --no-report
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Éric Araujod00862a2010-12-15 19:09:58 +0000104 Do not generate annotated listings. This is useful if you intend to make
105 several runs with :option:`--count <-c>`, and then produce a single set of
106 annotated listings at the end.
107
108.. cmdoption:: -g, --timing
109
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000110 Prefix each line with the time since the program started. Only used while
Éric Araujod00862a2010-12-15 19:09:58 +0000111 tracing.
112
113Filters
114^^^^^^^
115
116These options may be repeated multiple times.
117
118.. program:: trace
119
120.. cmdoption:: --ignore-module=<mod>
121
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000122 Ignore each of the given module names and its submodules (if it is a
123 package). The argument can be a list of names separated by a comma.
Éric Araujod00862a2010-12-15 19:09:58 +0000124
125.. cmdoption:: --ignore-dir=<dir>
126
127 Ignore all modules and packages in the named directory and subdirectories.
128 The argument can be a list of directories separated by :data:`os.pathsep`.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130.. _trace-api:
131
Éric Araujod00862a2010-12-15 19:09:58 +0000132Programmatic Interface
133----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000135.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(),\
136 ignoredirs=(), infile=None, outfile=None, timing=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000138 Create an object to trace execution of a single statement or expression. All
139 parameters are optional. *count* enables counting of line numbers. *trace*
140 enables line execution tracing. *countfuncs* enables listing of the
141 functions called during the run. *countcallers* enables call relationship
142 tracking. *ignoremods* is a list of modules or packages to ignore.
143 *ignoredirs* is a list of directories whose modules or packages should be
144 ignored. *infile* is the name of the file from which to read stored count
145 information. *outfile* is the name of the file in which to write updated
146 count information. *timing* enables a timestamp relative to when tracing was
147 started to be displayed.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000149 .. method:: run(cmd)
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000151 Execute the command and gather statistics from the execution with
152 the current tracing parameters. *cmd* must be a string or code object,
153 suitable for passing into :func:`exec`.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000155 .. method:: runctx(cmd, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000157 Execute the command and gather statistics from the execution with the
158 current tracing parameters, in the defined global and local
159 environments. If not defined, *globals* and *locals* default to empty
160 dictionaries.
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000162 .. method:: runfunc(func, *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000164 Call *func* with the given arguments under control of the :class:`Trace`
165 object with the current tracing parameters.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000167 .. method:: results()
Éric Araujod00862a2010-12-15 19:09:58 +0000168
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000169 Return a :class:`CoverageResults` object that contains the cumulative
170 results of all previous calls to ``run``, ``runctx`` and ``runfunc``
171 for the given :class:`Trace` instance. Does not reset the accumulated
172 trace results.
Éric Araujod00862a2010-12-15 19:09:58 +0000173
174.. class:: CoverageResults
175
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000176 A container for coverage results, created by :meth:`Trace.results`. Should
177 not be created directly by the user.
Éric Araujod00862a2010-12-15 19:09:58 +0000178
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000179 .. method:: update(other)
Éric Araujod00862a2010-12-15 19:09:58 +0000180
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000181 Merge in data from another :class:`CoverageResults` object.
Éric Araujod00862a2010-12-15 19:09:58 +0000182
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000183 .. method:: write_results(show_missing=True, summary=False, coverdir=None)
Éric Araujod00862a2010-12-15 19:09:58 +0000184
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000185 Write coverage results. Set *show_missing* to show lines that had no
186 hits. Set *summary* to include in the output the coverage summary per
187 module. *coverdir* specifies the directory into which the coverage
188 result files will be output. If ``None``, the results for each source
189 file are placed in its directory.
Éric Araujod00862a2010-12-15 19:09:58 +0000190
191A simple example demonstrating the use of the programmatic interface::
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193 import sys
194 import trace
195
196 # create a Trace object, telling it what to ignore, and whether to
197 # do tracing or line-counting or both.
198 tracer = trace.Trace(
199 ignoredirs=[sys.prefix, sys.exec_prefix],
200 trace=0,
201 count=1)
202
203 # run the new command using the given tracer
204 tracer.run('main()')
205
206 # make a report, placing output in /tmp
207 r = tracer.results()
208 r.write_results(show_missing=True, coverdir="/tmp")
209