Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`trace` --- Trace or track Python statement execution |
| 2 | ========================================================== |
| 3 | |
| 4 | .. module:: trace |
| 5 | :synopsis: Trace or track Python statement execution. |
| 6 | |
| 7 | |
| 8 | The :mod:`trace` module allows you to trace program execution, generate |
| 9 | annotated statement coverage listings, print caller/callee relationships and |
| 10 | list functions executed during a program run. It can be used in another program |
| 11 | or from the command line. |
| 12 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 13 | .. seealso:: |
| 14 | |
| 15 | Latest version of the :source:`trace module Python source code |
| 16 | <Lib/trace.py>` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
| 18 | .. _trace-cli: |
| 19 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 20 | Command-Line Usage |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | ------------------ |
| 22 | |
| 23 | The :mod:`trace` module can be invoked from the command line. It can be as |
| 24 | simple as :: |
| 25 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 26 | python -m trace --count -C . somefile.py ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 28 | The above will execute :file:`somefile.py` and generate annotated listings of all |
| 29 | Python modules imported during the execution into the current directory. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 31 | .. program:: trace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 33 | .. cmdoption:: --help |
| 34 | |
| 35 | Display usage and exit. |
| 36 | |
| 37 | .. cmdoption:: --version |
| 38 | |
| 39 | Display the version of the module and exit. |
| 40 | |
| 41 | Main options |
| 42 | ^^^^^^^^^^^^ |
| 43 | |
| 44 | At least one of the following options must be specified when invoking :mod:`trace`. |
| 45 | The :option:`--listfuncs <-l>` option is mutually exclusive with the |
| 46 | :option:`--trace <-t>` and :option:`--counts <-c>` options . When :option:`--listfuncs <-l>` |
| 47 | is provided, neither :option:`--counts <-c>` nor :option:`--trace <-t>` are accepted, |
| 48 | and vice versa. |
| 49 | |
| 50 | .. program:: trace |
| 51 | |
| 52 | .. cmdoption:: -c, --count |
| 53 | |
| 54 | Produce a set of annotated listing files upon program completion that shows |
| 55 | how many times each statement was executed. |
| 56 | See also :option:`--coverdir <-C>`, :option:`--file <-f>`, |
| 57 | :option:`--no-report <-R>` below. |
| 58 | |
| 59 | .. cmdoption:: -t, --trace |
| 60 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | Display lines as they are executed. |
| 62 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 63 | .. cmdoption:: -l, --listfuncs |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 65 | Display the functions executed by running the program. |
| 66 | |
| 67 | .. cmdoption:: -r, --report |
| 68 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | Produce an annotated list from an earlier program run that used the |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 70 | :option:`--count <-c>` and :option:`--file <-f>` option. Do not execute any code. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 72 | .. cmdoption:: -T, --trackcalls |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 74 | Display the calling relationships exposed by running the program. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 76 | Modifiers |
| 77 | ^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 79 | .. program:: trace |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 81 | .. cmdoption:: -f, --file=<file> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 83 | Name of a file to accumulate counts over several tracing runs. Should be used |
| 84 | with the :option:`--count <-c>` option. |
| 85 | |
| 86 | .. cmdoption:: -C, --coverdir=<dir> |
| 87 | |
| 88 | Directory where the report files go. The coverage report for |
| 89 | ``package.module`` is written to file :file:`{dir}/{package}/{module}.cover`. |
| 90 | |
| 91 | .. cmdoption:: -m, --missing |
| 92 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | When generating annotated listings, mark lines which were not executed with |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 94 | ``>>>>>>``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 96 | .. cmdoption:: -s, --summary |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 98 | When using :option:`--count <-c>` or :option:`--report <-r>`, write a brief |
| 99 | summary to stdout for each file processed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 101 | .. cmdoption:: -R, --no-report |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 103 | Do not generate annotated listings. This is useful if you intend to make |
| 104 | several runs with :option:`--count <-c>`, and then produce a single set of |
| 105 | annotated listings at the end. |
| 106 | |
| 107 | .. cmdoption:: -g, --timing |
| 108 | |
| 109 | Prefix each line with the time since the program started. Only used while |
| 110 | tracing. |
| 111 | |
| 112 | Filters |
| 113 | ^^^^^^^ |
| 114 | |
| 115 | These options may be repeated multiple times. |
| 116 | |
| 117 | .. program:: trace |
| 118 | |
| 119 | .. cmdoption:: --ignore-module=<mod> |
| 120 | |
| 121 | Ignore each of the given module names and its submodules (if it is a package). |
| 122 | The argument can be a list of names separated by a comma. |
| 123 | |
| 124 | .. cmdoption:: --ignore-dir=<dir> |
| 125 | |
| 126 | Ignore all modules and packages in the named directory and subdirectories. |
| 127 | The argument can be a list of directories separated by :data:`os.pathsep`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | .. _trace-api: |
| 130 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 131 | Programmatic Interface |
| 132 | ---------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 134 | .. class:: Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 136 | Create an object to trace execution of a single statement or expression. All |
| 137 | parameters are optional. *count* enables counting of line numbers. *trace* |
| 138 | enables line execution tracing. *countfuncs* enables listing of the functions |
| 139 | called during the run. *countcallers* enables call relationship tracking. |
| 140 | *ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list |
| 141 | of directories whose modules or packages should be ignored. *infile* is the |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 142 | name of the file from which to read stored count information. *outfile* is |
| 143 | the name of the file in which to write updated count information. *timing* |
| 144 | enables a timestamp relative to when tracing was started to be displayed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
| 146 | .. method:: Trace.run(cmd) |
| 147 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 148 | Execute the command and gather statistics from the execution with |
| 149 | the current tracing parameters. |
| 150 | *cmd* must be a string or code object, suitable for passing into :func:`exec`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 152 | .. method:: Trace.runctx(cmd, globals=None, locals=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 154 | Execute the command and gather statistics from the execution with |
| 155 | the current tracing parameters, in the defined global and local environments. |
| 156 | If not defined, *globals* and *locals* default to empty dictionaries. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | .. method:: Trace.runfunc(func, *args, **kwds) |
| 159 | |
| 160 | Call *func* with the given arguments under control of the :class:`Trace` object |
| 161 | with the current tracing parameters. |
| 162 | |
Éric Araujo | d00862a | 2010-12-15 19:09:58 +0000 | [diff] [blame^] | 163 | .. method:: Trace.results() |
| 164 | |
| 165 | Return a :class:`CoverageResults` object that contains the cumulative results |
| 166 | of all previous calls to ``run``, ``runctx`` and ``runfunc`` for the given |
| 167 | :class:`Trace` instance. Does not reset the accumulated trace results. |
| 168 | |
| 169 | .. class:: CoverageResults |
| 170 | |
| 171 | A container for coverage results, created by :meth:`Trace.results`. Should not |
| 172 | be created directly by the user. |
| 173 | |
| 174 | .. method:: CoverageResults.update(other) |
| 175 | |
| 176 | Merge in data from another :class:`CoverageResults` object. |
| 177 | |
| 178 | .. method:: CoverageResults.write_results(show_missing=True, summary=False, coverdir=None) |
| 179 | |
| 180 | Write coverage results. Set *show_missing* to show lines that had no hits. |
| 181 | Set *summary* to include in the output the coverage summary per module. *coverdir* |
| 182 | specifies the directory into which the coverage result files will be output. |
| 183 | If ``None``, the results for each source file are placed in its directory. |
| 184 | |
| 185 | A simple example demonstrating the use of the programmatic interface:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
| 187 | import sys |
| 188 | import trace |
| 189 | |
| 190 | # create a Trace object, telling it what to ignore, and whether to |
| 191 | # do tracing or line-counting or both. |
| 192 | tracer = trace.Trace( |
| 193 | ignoredirs=[sys.prefix, sys.exec_prefix], |
| 194 | trace=0, |
| 195 | count=1) |
| 196 | |
| 197 | # run the new command using the given tracer |
| 198 | tracer.run('main()') |
| 199 | |
| 200 | # make a report, placing output in /tmp |
| 201 | r = tracer.results() |
| 202 | r.write_results(show_missing=True, coverdir="/tmp") |
| 203 | |