blob: 2e9d2b4d86137efdc645a21e0516b3cd8c9c1b0a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`trace` --- Trace or track Python statement execution
3==========================================================
4
5.. module:: trace
6 :synopsis: Trace or track Python statement execution.
7
8
9The :mod:`trace` module allows you to trace program execution, generate
10annotated statement coverage listings, print caller/callee relationships and
11list functions executed during a program run. It can be used in another program
12or from the command line.
13
Raymond Hettingere0e08222010-11-06 07:10:31 +000014.. seealso::
15
16 Latest version of the `trace module Python source code
17 <http://svn.python.org/view/python/branches/release27-maint/Lib/trace.py?view=markup>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
19.. _trace-cli:
20
Éric Araujo54274ad2011-02-03 00:12:18 +000021Command-Line Usage
Georg Brandl8ec7f652007-08-15 14:28:01 +000022------------------
23
24The :mod:`trace` module can be invoked from the command line. It can be as
25simple as ::
26
Éric Araujo54274ad2011-02-03 00:12:18 +000027 python -m trace --count -C . somefile.py ...
Georg Brandl8ec7f652007-08-15 14:28:01 +000028
Éric Araujo54274ad2011-02-03 00:12:18 +000029The above will execute :file:`somefile.py` and generate annotated listings of
30all Python modules imported during the execution into the current directory.
Georg Brandl8ec7f652007-08-15 14:28:01 +000031
Éric Araujo54274ad2011-02-03 00:12:18 +000032.. program:: trace
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
Éric Araujo54274ad2011-02-03 00:12:18 +000034.. cmdoption:: --help
35
36 Display usage and exit.
37
38.. cmdoption:: --version
39
40 Display the version of the module and exit.
41
42Main options
43^^^^^^^^^^^^
44
45At least one of the following options must be specified when invoking
46:mod:`trace`. The :option:`--listfuncs <-l>` option is mutually exclusive with
47the :option:`--trace <-t>` and :option:`--counts <-c>` options . When
48:option:`--listfuncs <-l>` is provided, neither :option:`--counts <-c>` nor
49:option:`--trace <-t>` are accepted, and vice versa.
50
51.. program:: trace
52
53.. cmdoption:: -c, --count
54
55 Produce a set of annotated listing files upon program completion that shows
56 how many times each statement was executed. See also
57 :option:`--coverdir <-C>`, :option:`--file <-f>` and
58 :option:`--no-report <-R>` below.
59
60.. cmdoption:: -t, --trace
61
Georg Brandl8ec7f652007-08-15 14:28:01 +000062 Display lines as they are executed.
63
Éric Araujo54274ad2011-02-03 00:12:18 +000064.. cmdoption:: -l, --listfuncs
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Éric Araujo54274ad2011-02-03 00:12:18 +000066 Display the functions executed by running the program.
67
68.. cmdoption:: -r, --report
69
Georg Brandl8ec7f652007-08-15 14:28:01 +000070 Produce an annotated list from an earlier program run that used the
Éric Araujo54274ad2011-02-03 00:12:18 +000071 :option:`--count <-c>` and :option:`--file <-f>` option. This does not
72 execute any code.
Georg Brandl8ec7f652007-08-15 14:28:01 +000073
Éric Araujo54274ad2011-02-03 00:12:18 +000074.. cmdoption:: -T, --trackcalls
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
Éric Araujo54274ad2011-02-03 00:12:18 +000076 Display the calling relationships exposed by running the program.
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
Éric Araujo54274ad2011-02-03 00:12:18 +000078Modifiers
79^^^^^^^^^
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Éric Araujo54274ad2011-02-03 00:12:18 +000081.. program:: trace
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
Éric Araujo54274ad2011-02-03 00:12:18 +000083.. cmdoption:: -f, --file=<file>
Georg Brandl8ec7f652007-08-15 14:28:01 +000084
Éric Araujo54274ad2011-02-03 00:12:18 +000085 Name of a file to accumulate counts over several tracing runs. Should be
86 used with the :option:`--count <-c>` option.
87
88.. cmdoption:: -C, --coverdir=<dir>
89
90 Directory where the report files go. The coverage report for
91 ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`.
92
93.. cmdoption:: -m, --missing
94
Georg Brandl8ec7f652007-08-15 14:28:01 +000095 When generating annotated listings, mark lines which were not executed with
Éric Araujo54274ad2011-02-03 00:12:18 +000096 ``>>>>>>``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
Éric Araujo54274ad2011-02-03 00:12:18 +000098.. cmdoption:: -s, --summary
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
Éric Araujo54274ad2011-02-03 00:12:18 +0000100 When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief
101 summary to stdout for each file processed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
Éric Araujo54274ad2011-02-03 00:12:18 +0000103.. cmdoption:: -R, --no-report
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Éric Araujo54274ad2011-02-03 00:12:18 +0000105 Do not generate annotated listings. This is useful if you intend to make
106 several runs with :option:`--count <-c>`, and then produce a single set of
107 annotated listings at the end.
108
109.. cmdoption:: -g, --timing
110
111 Prefix each line with the time since the program started. Only used while
112 tracing.
113
114Filters
115^^^^^^^
116
117These options may be repeated multiple times.
118
119.. program:: trace
120
121.. cmdoption:: --ignore-module=<mod>
122
123 Ignore each of the given module names and its submodules (if it is a
124 package). The argument can be a list of names separated by a comma.
125
126.. cmdoption:: --ignore-dir=<dir>
127
128 Ignore all modules and packages in the named directory and subdirectories.
129 The argument can be a list of directories separated by :data:`os.pathsep`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000130
131.. _trace-api:
132
Éric Araujo54274ad2011-02-03 00:12:18 +0000133Programmatic Interface
134----------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Neal Norwitzca376612008-02-26 08:21:28 +0000136.. 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 +0000137
Éric Araujo54274ad2011-02-03 00:12:18 +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 Brandl8ec7f652007-08-15 14:28:01 +0000148
Éric Araujo54274ad2011-02-03 00:12:18 +0000149 .. method:: run(cmd)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
Éric Araujo54274ad2011-02-03 00:12:18 +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 Brandl8ec7f652007-08-15 14:28:01 +0000154
Éric Araujo54274ad2011-02-03 00:12:18 +0000155 .. method:: runctx(cmd[, globals=None[, locals=None]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
Éric Araujo54274ad2011-02-03 00:12:18 +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 Brandl8ec7f652007-08-15 14:28:01 +0000161
Éric Araujo54274ad2011-02-03 00:12:18 +0000162 .. method:: runfunc(func, *args, **kwds)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
Éric Araujo54274ad2011-02-03 00:12:18 +0000164 Call *func* with the given arguments under control of the :class:`Trace`
165 object with the current tracing parameters.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166
Éric Araujo54274ad2011-02-03 00:12:18 +0000167 .. method:: results()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168
Éric Araujo54274ad2011-02-03 00:12:18 +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.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
Éric Araujo54274ad2011-02-03 00:12:18 +0000174.. class:: CoverageResults
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
Éric Araujo54274ad2011-02-03 00:12:18 +0000176 A container for coverage results, created by :meth:`Trace.results`. Should
177 not be created directly by the user.
178
179 .. method:: update(other)
180
181 Merge in data from another :class:`CoverageResults` object.
182
183 .. method:: write_results([show_missing=True[, summary=False[, coverdir=None]]])
184
185 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.
190
191A simple example demonstrating the use of the programmatic interface::
Georg Brandl8ec7f652007-08-15 14:28:01 +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