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