blob: 303cb1e07231766f82c99b48976dd360cd42fc64 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
16Command Line Usage
17------------------
18
19The :mod:`trace` module can be invoked from the command line. It can be as
20simple as ::
21
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000022 python -m trace --count -C . somefile.py ...
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000024The above will execute :file:`somefile.py` and generate annotated listings of all
25Python modules imported during the execution into the current directory.
Georg Brandl8ec7f652007-08-15 14:28:01 +000026
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000027Meta-options
28^^^^^^^^^^^^
Georg Brandl8ec7f652007-08-15 14:28:01 +000029
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000030``--help``
31
32 Display usage and exit.
33
34``--version``
35
36 Display the version of the module and exit.
37
38Main options
39^^^^^^^^^^^^
40
41The ``--listfuncs`` option is mutually exclusive with the ``--trace`` and
42``--count`` options . When ``--listfuncs`` is provided, neither ``--counts``
43nor ``--trace`` are accepted, and vice versa.
44
45``--count, -c``
46
47 Produce a set of annotated listing files upon program completion that shows
48 how many times each statement was executed.
49 See also ``--coverdir``, ``--file``, ``--no-report`` below.
50
51``--trace, -t``
52
Georg Brandl8ec7f652007-08-15 14:28:01 +000053 Display lines as they are executed.
54
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000055``--listfuncs, -l``
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000057 Display the functions executed by running the program.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000059``--report, -r``
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000061 Produce an annotated list from an earlier program run that used the ``--count``
62 and ``--file`` option. Do not execute any code.
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000064``--trackcalls, -T``
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000066 Display the calling relationships exposed by running the program.
Georg Brandl8ec7f652007-08-15 14:28:01 +000067
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000068Modifiers
69^^^^^^^^^
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000071``--file=<file>, -f``
72
73 Name of a file to accumulate counts over several tracing runs. Should be used
74 with the ``--count`` option.
75
76``--coverdir=<dir>, -C``
77
78 Directory where the report files go. The coverage report for
79 ``package.module`` is written to file ``dir/package/module.cover``.
80
81``--missing, -m``
82
Georg Brandl8ec7f652007-08-15 14:28:01 +000083 When generating annotated listings, mark lines which were not executed with
84 '``>>>>>>``'.
85
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000086``--summary, -s``
87
88 When using ``--count`` or ``--report``, write a brief summary to
Georg Brandl8ec7f652007-08-15 14:28:01 +000089 stdout for each file processed.
90
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +000091``--no-report, -R``
92
93 Do not generate annotated listings. This is useful if you intend to make
94 several runs with ``--count`` then produce a single set of annotated
95 listings at the end.
96
97``--timing, -g``
98
99 Prefix each line with the time since the program started. Only used while
100 tracing.
101
102Filters
103^^^^^^^
104
105These options may be repeated multiple times.
106
107``--ignore-module=<mod>``
108
Facundo Batista873c9852008-01-19 18:38:19 +0000109 Accepts comma separated list of module names. Ignore each of the named
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000110 modules and its submodules (if it is a package).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000112``--ignore-dir=<dir>``
113
Facundo Batista873c9852008-01-19 18:38:19 +0000114 Ignore all modules and packages in the named directory and subdirectories
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000115 (multiple directories can be joined by ``os.pathsep``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000116
117.. _trace-api:
118
119Programming Interface
120---------------------
121
122
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000123.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125 Create an object to trace execution of a single statement or expression. All
126 parameters are optional. *count* enables counting of line numbers. *trace*
127 enables line execution tracing. *countfuncs* enables listing of the functions
128 called during the run. *countcallers* enables call relationship tracking.
129 *ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list
130 of directories whose modules or packages should be ignored. *infile* is the
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000131 name of the file from which to read stored count information. *outfile* is
132 the name of the file in which to write updated count information. *timing*
133 enables a timestamp relative to when tracing was started to be displayed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
135
136.. method:: Trace.run(cmd)
137
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000138 Run *cmd* under control of the :class:`Trace` object with the current tracing parameters.
139 *cmd* must be a string or code object, suitable for passing into :func:`exec`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
141
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000142.. method:: Trace.runctx(cmd, globals=None, locals=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000144 Run *cmd* under control of the :class:`Trace` object with the current tracing parameters
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145 in the defined global and local environments. If not defined, *globals* and
146 *locals* default to empty dictionaries.
147
148
149.. method:: Trace.runfunc(func, *args, **kwds)
150
151 Call *func* with the given arguments under control of the :class:`Trace` object
152 with the current tracing parameters.
153
Alexander Belopolskyedabc7b2010-08-02 19:29:03 +0000154.. method:: Trace.results()
155
156 Return a :class:`CoverageResults` object that contains the cumulative results
157 of all previous calls to ``run``, ``runctx`` and ``runfunc`` for the given
158 :class:`Trace` instance. Does not reset the accumulated trace results.
159
160.. class:: CoverageResults
161
162 A container for coverage results, created by :meth:`Trace.results`. Should not
163 be created directly by the user.
164
165.. method:: CoverageResults.update(other)
166
167 Merge in data from another :class:`CoverageResults` object.
168
169.. method:: CoverageResults.write_results(show_missing=True, summary=False, coverdir=None)
170
171 Write coverage results. Set *show_missing* to show lines that had no hits.
172 Set *summary* to include in the output the coverage summary per module. *coverdir*
173 specifies the directory into which the coverage result files will be output.
174 If ``None``, the results for each source file are placed in its directory.
175
176..
177
178A simple example demonstrating the use of the programming interface::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000179
180 import sys
181 import trace
182
183 # create a Trace object, telling it what to ignore, and whether to
184 # do tracing or line-counting or both.
185 tracer = trace.Trace(
186 ignoredirs=[sys.prefix, sys.exec_prefix],
187 trace=0,
188 count=1)
189
190 # run the new command using the given tracer
191 tracer.run('main()')
192
193 # make a report, placing output in /tmp
194 r = tracer.results()
195 r.write_results(show_missing=True, coverdir="/tmp")
196