blob: 40cf198f1287d78f378d454e5f9560b382c33b4a [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
Raymond Hettinger10480942011-01-10 03:26:08 +00007**Source code:** :source:`Lib/trace.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
Raymond Hettinger4f707fd2011-01-10 19:54:11 +00009--------------
10
Georg Brandl116aa622007-08-15 14:28:22 +000011The :mod:`trace` module allows you to trace program execution, generate
12annotated statement coverage listings, print caller/callee relationships and
13list functions executed during a program run. It can be used in another program
14or from the command line.
15
Marco Buttu5dfccb02017-03-03 21:42:04 +010016.. seealso::
17
18 `Coverage.py <https://coverage.readthedocs.io/>`_
19 A popular third-party coverage tool that provides HTML
20 output along with advanced features such as branch coverage.
21
Georg Brandl116aa622007-08-15 14:28:22 +000022.. _trace-cli:
23
Éric Araujod00862a2010-12-15 19:09:58 +000024Command-Line Usage
Georg Brandl116aa622007-08-15 14:28:22 +000025------------------
26
27The :mod:`trace` module can be invoked from the command line. It can be as
28simple as ::
29
Éric Araujod00862a2010-12-15 19:09:58 +000030 python -m trace --count -C . somefile.py ...
Georg Brandl116aa622007-08-15 14:28:22 +000031
Éric Araujo67f0b6c2010-12-15 19:30:15 +000032The above will execute :file:`somefile.py` and generate annotated listings of
33all Python modules imported during the execution into the current directory.
Georg Brandl116aa622007-08-15 14:28:22 +000034
Éric Araujod00862a2010-12-15 19:09:58 +000035.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000036
Éric Araujod00862a2010-12-15 19:09:58 +000037.. cmdoption:: --help
38
39 Display usage and exit.
40
41.. cmdoption:: --version
42
43 Display the version of the module and exit.
44
Mario Corchero354227a2019-06-01 05:49:10 +010045.. versionadded:: 3.8
46 Added ``--module`` option that allows to run an executable module.
47
Éric Araujod00862a2010-12-15 19:09:58 +000048Main options
49^^^^^^^^^^^^
50
Éric Araujo67f0b6c2010-12-15 19:30:15 +000051At least one of the following options must be specified when invoking
52:mod:`trace`. The :option:`--listfuncs <-l>` option is mutually exclusive with
Senthil Kumaranf5c34052014-04-06 10:59:47 -070053the :option:`--trace <-t>` and :option:`--count <-c>` options. When
54:option:`--listfuncs <-l>` is provided, neither :option:`--count <-c>` nor
Éric Araujo67f0b6c2010-12-15 19:30:15 +000055:option:`--trace <-t>` are accepted, and vice versa.
Éric Araujod00862a2010-12-15 19:09:58 +000056
57.. program:: trace
58
59.. cmdoption:: -c, --count
60
61 Produce a set of annotated listing files upon program completion that shows
Éric Araujo67f0b6c2010-12-15 19:30:15 +000062 how many times each statement was executed. See also
63 :option:`--coverdir <-C>`, :option:`--file <-f>` and
Éric Araujod00862a2010-12-15 19:09:58 +000064 :option:`--no-report <-R>` below.
65
66.. cmdoption:: -t, --trace
67
Georg Brandl116aa622007-08-15 14:28:22 +000068 Display lines as they are executed.
69
Éric Araujod00862a2010-12-15 19:09:58 +000070.. cmdoption:: -l, --listfuncs
Georg Brandl116aa622007-08-15 14:28:22 +000071
Éric Araujod00862a2010-12-15 19:09:58 +000072 Display the functions executed by running the program.
73
74.. cmdoption:: -r, --report
75
Georg Brandl116aa622007-08-15 14:28:22 +000076 Produce an annotated list from an earlier program run that used the
Éric Araujo67f0b6c2010-12-15 19:30:15 +000077 :option:`--count <-c>` and :option:`--file <-f>` option. This does not
78 execute any code.
Georg Brandl116aa622007-08-15 14:28:22 +000079
Éric Araujod00862a2010-12-15 19:09:58 +000080.. cmdoption:: -T, --trackcalls
Georg Brandl116aa622007-08-15 14:28:22 +000081
Éric Araujod00862a2010-12-15 19:09:58 +000082 Display the calling relationships exposed by running the program.
Georg Brandl116aa622007-08-15 14:28:22 +000083
Éric Araujod00862a2010-12-15 19:09:58 +000084Modifiers
85^^^^^^^^^
Georg Brandl116aa622007-08-15 14:28:22 +000086
Éric Araujod00862a2010-12-15 19:09:58 +000087.. program:: trace
Georg Brandl116aa622007-08-15 14:28:22 +000088
Éric Araujod00862a2010-12-15 19:09:58 +000089.. cmdoption:: -f, --file=<file>
Georg Brandl116aa622007-08-15 14:28:22 +000090
Éric Araujo67f0b6c2010-12-15 19:30:15 +000091 Name of a file to accumulate counts over several tracing runs. Should be
92 used with the :option:`--count <-c>` option.
Éric Araujod00862a2010-12-15 19:09:58 +000093
94.. cmdoption:: -C, --coverdir=<dir>
95
Éric Araujo67f0b6c2010-12-15 19:30:15 +000096 Directory where the report files go. The coverage report for
Éric Araujod00862a2010-12-15 19:09:58 +000097 ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`.
98
99.. cmdoption:: -m, --missing
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101 When generating annotated listings, mark lines which were not executed with
Éric Araujod00862a2010-12-15 19:09:58 +0000102 ``>>>>>>``.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Éric Araujod00862a2010-12-15 19:09:58 +0000104.. cmdoption:: -s, --summary
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Éric Araujod00862a2010-12-15 19:09:58 +0000106 When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief
107 summary to stdout for each file processed.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Éric Araujod00862a2010-12-15 19:09:58 +0000109.. cmdoption:: -R, --no-report
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Éric Araujod00862a2010-12-15 19:09:58 +0000111 Do not generate annotated listings. This is useful if you intend to make
112 several runs with :option:`--count <-c>`, and then produce a single set of
113 annotated listings at the end.
114
115.. cmdoption:: -g, --timing
116
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000117 Prefix each line with the time since the program started. Only used while
Éric Araujod00862a2010-12-15 19:09:58 +0000118 tracing.
119
120Filters
121^^^^^^^
122
123These options may be repeated multiple times.
124
125.. program:: trace
126
127.. cmdoption:: --ignore-module=<mod>
128
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000129 Ignore each of the given module names and its submodules (if it is a
130 package). The argument can be a list of names separated by a comma.
Éric Araujod00862a2010-12-15 19:09:58 +0000131
132.. cmdoption:: --ignore-dir=<dir>
133
134 Ignore all modules and packages in the named directory and subdirectories.
135 The argument can be a list of directories separated by :data:`os.pathsep`.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137.. _trace-api:
138
Éric Araujod00862a2010-12-15 19:09:58 +0000139Programmatic Interface
140----------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000142.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(),\
143 ignoredirs=(), infile=None, outfile=None, timing=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000145 Create an object to trace execution of a single statement or expression. All
146 parameters are optional. *count* enables counting of line numbers. *trace*
147 enables line execution tracing. *countfuncs* enables listing of the
148 functions called during the run. *countcallers* enables call relationship
149 tracking. *ignoremods* is a list of modules or packages to ignore.
150 *ignoredirs* is a list of directories whose modules or packages should be
151 ignored. *infile* is the name of the file from which to read stored count
152 information. *outfile* is the name of the file in which to write updated
153 count information. *timing* enables a timestamp relative to when tracing was
154 started to be displayed.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Andre Delfino96a09df2020-12-17 14:25:55 -0300156 .. method:: run(cmd)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Andre Delfino96a09df2020-12-17 14:25:55 -0300158 Execute the command and gather statistics from the execution with
159 the current tracing parameters. *cmd* must be a string or code object,
160 suitable for passing into :func:`exec`.
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Andre Delfino96a09df2020-12-17 14:25:55 -0300162 .. method:: runctx(cmd, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Andre Delfino96a09df2020-12-17 14:25:55 -0300164 Execute the command and gather statistics from the execution with the
165 current tracing parameters, in the defined global and local
166 environments. If not defined, *globals* and *locals* default to empty
167 dictionaries.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Andre Delfino96a09df2020-12-17 14:25:55 -0300169 .. method:: runfunc(func, /, *args, **kwds)
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Andre Delfino96a09df2020-12-17 14:25:55 -0300171 Call *func* with the given arguments under control of the :class:`Trace`
172 object with the current tracing parameters.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Andre Delfino96a09df2020-12-17 14:25:55 -0300174 .. method:: results()
Éric Araujod00862a2010-12-15 19:09:58 +0000175
Andre Delfino96a09df2020-12-17 14:25:55 -0300176 Return a :class:`CoverageResults` object that contains the cumulative
177 results of all previous calls to ``run``, ``runctx`` and ``runfunc``
178 for the given :class:`Trace` instance. Does not reset the accumulated
179 trace results.
Éric Araujod00862a2010-12-15 19:09:58 +0000180
181.. class:: CoverageResults
182
Éric Araujo67f0b6c2010-12-15 19:30:15 +0000183 A container for coverage results, created by :meth:`Trace.results`. Should
184 not be created directly by the user.
Éric Araujod00862a2010-12-15 19:09:58 +0000185
Andre Delfino96a09df2020-12-17 14:25:55 -0300186 .. method:: update(other)
Éric Araujod00862a2010-12-15 19:09:58 +0000187
Andre Delfino96a09df2020-12-17 14:25:55 -0300188 Merge in data from another :class:`CoverageResults` object.
Éric Araujod00862a2010-12-15 19:09:58 +0000189
Andre Delfino96a09df2020-12-17 14:25:55 -0300190 .. method:: write_results(show_missing=True, summary=False, coverdir=None)
Éric Araujod00862a2010-12-15 19:09:58 +0000191
Andre Delfino96a09df2020-12-17 14:25:55 -0300192 Write coverage results. Set *show_missing* to show lines that had no
193 hits. Set *summary* to include in the output the coverage summary per
194 module. *coverdir* specifies the directory into which the coverage
195 result files will be output. If ``None``, the results for each source
196 file are placed in its directory.
Éric Araujod00862a2010-12-15 19:09:58 +0000197
198A simple example demonstrating the use of the programmatic interface::
Georg Brandl116aa622007-08-15 14:28:22 +0000199
200 import sys
201 import trace
202
203 # create a Trace object, telling it what to ignore, and whether to
204 # do tracing or line-counting or both.
205 tracer = trace.Trace(
206 ignoredirs=[sys.prefix, sys.exec_prefix],
207 trace=0,
208 count=1)
209
210 # run the new command using the given tracer
211 tracer.run('main()')
212
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100213 # make a report, placing output in the current directory
Georg Brandl116aa622007-08-15 14:28:22 +0000214 r = tracer.results()
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +0100215 r.write_results(show_missing=True, coverdir=".")
Georg Brandl116aa622007-08-15 14:28:22 +0000216