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