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