blob: 878fbed7ea1ad0007edfcd1d7217d84183d52efa [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
20Command Line Usage
21------------------
22
23The :mod:`trace` module can be invoked from the command line. It can be as
24simple as ::
25
26 python -m trace --count somefile.py ...
27
28The above will generate annotated listings of all Python modules imported during
29the execution of :file:`somefile.py`.
30
31The following command-line arguments are supported:
32
33:option:`--trace`, :option:`-t`
34 Display lines as they are executed.
35
36:option:`--count`, :option:`-c`
37 Produce a set of annotated listing files upon program completion that shows how
38 many times each statement was executed.
39
40:option:`--report`, :option:`-r`
41 Produce an annotated list from an earlier program run that used the
42 :option:`--count` and :option:`--file` arguments.
43
44:option:`--no-report`, :option:`-R`
45 Do not generate annotated listings. This is useful if you intend to make
46 several runs with :option:`--count` then produce a single set of annotated
47 listings at the end.
48
49:option:`--listfuncs`, :option:`-l`
50 List the functions executed by running the program.
51
52:option:`--trackcalls`, :option:`-T`
53 Generate calling relationships exposed by running the program.
54
55:option:`--file`, :option:`-f`
56 Name a file containing (or to contain) counts.
57
58:option:`--coverdir`, :option:`-C`
59 Name a directory in which to save annotated listing files.
60
61:option:`--missing`, :option:`-m`
62 When generating annotated listings, mark lines which were not executed with
63 '``>>>>>>``'.
64
65:option:`--summary`, :option:`-s`
66 When using :option:`--count` or :option:`--report`, write a brief summary to
67 stdout for each file processed.
68
69:option:`--ignore-module`
Georg Brandlfceab5a2008-01-19 20:08:23 +000070 Accepts comma separated list of module names. Ignore each of the named
Georg Brandl48310cd2009-01-03 21:18:54 +000071 module and its submodules (if it is a package). May be given
Georg Brandl116aa622007-08-15 14:28:22 +000072 multiple times.
73
74:option:`--ignore-dir`
Georg Brandlfceab5a2008-01-19 20:08:23 +000075 Ignore all modules and packages in the named directory and subdirectories
76 (multiple directories can be joined by os.pathsep). May be given multiple
Georg Brandl48310cd2009-01-03 21:18:54 +000077 times.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
80.. _trace-api:
81
82Programming Interface
83---------------------
84
85
Georg Brandl7f01a132009-09-16 15:58:14 +000086.. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False)
Georg Brandl116aa622007-08-15 14:28:22 +000087
88 Create an object to trace execution of a single statement or expression. All
89 parameters are optional. *count* enables counting of line numbers. *trace*
90 enables line execution tracing. *countfuncs* enables listing of the functions
91 called during the run. *countcallers* enables call relationship tracking.
92 *ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list
93 of directories whose modules or packages should be ignored. *infile* is the
94 file from which to read stored count information. *outfile* is a file in which
Christian Heimes380f7f22008-02-28 11:19:05 +000095 to write updated count information. *timing* enables a timestamp relative
96 to when tracing was started to be displayed.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
99.. method:: Trace.run(cmd)
100
101 Run *cmd* under control of the Trace object with the current tracing parameters.
102
103
Georg Brandl7f01a132009-09-16 15:58:14 +0000104.. method:: Trace.runctx(cmd, globals=None, locals=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106 Run *cmd* under control of the Trace object with the current tracing parameters
107 in the defined global and local environments. If not defined, *globals* and
108 *locals* default to empty dictionaries.
109
110
111.. method:: Trace.runfunc(func, *args, **kwds)
112
113 Call *func* with the given arguments under control of the :class:`Trace` object
114 with the current tracing parameters.
115
116This is a simple example showing the use of this module::
117
118 import sys
119 import trace
120
121 # create a Trace object, telling it what to ignore, and whether to
122 # do tracing or line-counting or both.
123 tracer = trace.Trace(
124 ignoredirs=[sys.prefix, sys.exec_prefix],
125 trace=0,
126 count=1)
127
128 # run the new command using the given tracer
129 tracer.run('main()')
130
131 # make a report, placing output in /tmp
132 r = tracer.results()
133 r.write_results(show_missing=True, coverdir="/tmp")
134